2014-07-11 16:38:10 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.ServiceProcess;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using BlubbFish.Utils;
|
|
|
|
|
using NetMonitorUtils.Execptions;
|
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
namespace NetMonitorUtils {
|
|
|
|
|
public class ServiceControl {
|
|
|
|
|
private ServiceController sc = new ServiceController();
|
|
|
|
|
private static ServiceControl instance;
|
|
|
|
|
private InIReader config;
|
|
|
|
|
private ServiceControl(string configfile) {
|
|
|
|
|
this.sc.ServiceName = "NetMonitorServer";
|
|
|
|
|
this.config = InIReader.getInstance(configfile);
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
public static ServiceControl getInstance(string configfile) {
|
|
|
|
|
if(instance == null) {
|
|
|
|
|
instance = new ServiceControl(configfile);
|
|
|
|
|
}
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
private bool ServiceSetStatus(ServiceControllerStatus status) {
|
|
|
|
|
if(this.sc.Status == status) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
this.sc.Start();
|
|
|
|
|
try {
|
|
|
|
|
this.sc.WaitForStatus(status, new TimeSpan(0, 0, 30));
|
|
|
|
|
} catch(System.ServiceProcess.TimeoutException) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
public bool ServiceStart() {
|
|
|
|
|
return this.ServiceSetStatus(ServiceControllerStatus.Running);
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
public bool ServicePause() {
|
|
|
|
|
return this.ServiceSetStatus(ServiceControllerStatus.Paused);
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
public bool ServiceStop() {
|
|
|
|
|
return this.ServiceSetStatus(ServiceControllerStatus.Stopped);
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
public bool NetworkSetAuto(string adapter) {
|
|
|
|
|
if(adapter == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
|
|
|
|
}
|
|
|
|
|
return this.sendMessage("NET", "AUTO " + adapter);
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
private bool sendMessage(string method, string message) {
|
|
|
|
|
IPEndPoint netRecieverPoint = new IPEndPoint(IPAddress.Loopback, 0);
|
|
|
|
|
int startPort = Int32.Parse(this.config.getValue("ports", "client_from"));
|
|
|
|
|
int rangePort = Int32.Parse(this.config.getValue("ports", "client_to"));
|
|
|
|
|
UdpClient netReciver = null;
|
|
|
|
|
int selectedPort = 0;
|
|
|
|
|
for(int i = startPort; i < rangePort; i++) {
|
|
|
|
|
try {
|
|
|
|
|
netReciver = new UdpClient(i);
|
|
|
|
|
} catch(System.Net.Sockets.SocketException) { }
|
|
|
|
|
if(netReciver != null) {
|
|
|
|
|
selectedPort = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(netReciver == null) {
|
|
|
|
|
throw new ServiceControlException("No Free Port for Comunication");
|
|
|
|
|
}
|
|
|
|
|
byte[] data = Encoding.UTF8.GetBytes(method + " " + selectedPort + " " + message);
|
|
|
|
|
|
|
|
|
|
UdpClient netSender = new UdpClient();
|
|
|
|
|
IPEndPoint netSenderPoint = new IPEndPoint(IPAddress.Loopback, Int32.Parse(this.config.getValue("ports", "server")));
|
|
|
|
|
netSender.Send(data, data.Length, netSenderPoint);
|
|
|
|
|
netReciver.Client.ReceiveTimeout = 60000;
|
|
|
|
|
byte[] recivedData;
|
|
|
|
|
try {
|
|
|
|
|
recivedData = netReciver.Receive(ref netRecieverPoint);
|
|
|
|
|
} catch(Exception e) {
|
|
|
|
|
throw new ServiceControlNetworkException(e.Message);
|
|
|
|
|
}
|
|
|
|
|
string recieveText = Encoding.UTF8.GetString(recivedData);
|
|
|
|
|
if(recieveText.ToLower() == "true") {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
throw new ServiceControlException(recieveText);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
public bool NetworkSetIp(string ip, string subnet, string gateway, string adapter) {
|
|
|
|
|
if(ip == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("IP address not Set!");
|
|
|
|
|
}
|
|
|
|
|
if(subnet == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("Subnet address not Set!");
|
|
|
|
|
}
|
|
|
|
|
if(gateway == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("Gateway address not Set!");
|
|
|
|
|
}
|
|
|
|
|
if(adapter == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
|
|
|
|
}
|
|
|
|
|
return this.sendMessage("NET", "IP " + ip + " " + subnet + " " + gateway + " " + adapter);
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
public bool NetworkSetDNS(string dns, string adapter) {
|
|
|
|
|
if(dns == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("DNS address not Set!");
|
|
|
|
|
}
|
|
|
|
|
if(adapter == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
|
|
|
|
}
|
|
|
|
|
return this.sendMessage("NET", "DNS " + dns + " " + adapter);
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
public bool NetworkSetWINS(string wins, string adapter) {
|
|
|
|
|
if(wins == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("WINS address not Set!");
|
|
|
|
|
}
|
|
|
|
|
if(adapter == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
|
|
|
|
}
|
|
|
|
|
return this.sendMessage("NET", "WINS " + wins + " " + adapter);
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
public bool NetworkSetIpAuto(string adapter) {
|
|
|
|
|
if(adapter == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
|
|
|
|
}
|
|
|
|
|
return this.sendMessage("NET", "IP AUTO " + adapter);
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
public bool NetworkSetDnsAuto(string adapter) {
|
|
|
|
|
if(adapter == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
|
|
|
|
}
|
|
|
|
|
return this.sendMessage("NET", "DNS AUTO " + adapter);
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
public bool NetworkSetWinsAuto(string adapter) {
|
|
|
|
|
if(adapter == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
|
|
|
|
}
|
|
|
|
|
return this.sendMessage("NET", "WINS AUTO " + adapter);
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
public bool ServiceStart(string name) {
|
|
|
|
|
if(name == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("Service name ist not set!");
|
|
|
|
|
}
|
|
|
|
|
return this.sendMessage("SERVICE", "START " + name);
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
public bool ServiceStop(string name) {
|
|
|
|
|
if(name == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("Service name ist not set!");
|
|
|
|
|
}
|
|
|
|
|
return this.sendMessage("SERVICE", "STOP " + name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool ServiceStatus(string name) {
|
|
|
|
|
if(name == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("Service name ist not set!");
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
return this.sendMessage("SERVICE", "STATUS " + name);
|
|
|
|
|
} catch(ServiceControlException e) {
|
|
|
|
|
if(e.Message.ToLower() == "stopped") {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if(e.Message.ToLower() == "running") {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
public string NetworkGetName(string adapter) {
|
|
|
|
|
if(adapter == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("Adaptername not set!");
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
return this.sendMessage("NET", "NETWORK " + adapter).ToString();
|
|
|
|
|
} catch(ServiceControlException e) {
|
|
|
|
|
if(e.Message.ToLower() == "unknown") {
|
|
|
|
|
return "unknown";
|
|
|
|
|
}
|
|
|
|
|
if(e.Message.ToLower() == "disconnected") {
|
|
|
|
|
return "disconnected";
|
|
|
|
|
}
|
|
|
|
|
if(e.Message.ToLower().Substring(0, 1) == "[") {
|
|
|
|
|
return e.Message;
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|
2015-03-04 23:21:34 +01:00
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public string VpnGetStatus(string vpn) {
|
|
|
|
|
if(vpn == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("Configname not set!");
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
return this.sendMessage("OPENVPN", "STATUS " + vpn).ToString();
|
|
|
|
|
} catch(ServiceControlException e) {
|
|
|
|
|
if(e.Message.ToLower() == "unknown") {
|
|
|
|
|
return "unknown";
|
|
|
|
|
}
|
|
|
|
|
if(e.Message.ToLower() == "disconnected") {
|
|
|
|
|
return "disconnected";
|
|
|
|
|
}
|
|
|
|
|
if(e.Message.ToLower().Substring(0, 1) == "[") {
|
|
|
|
|
return e.Message;
|
|
|
|
|
}
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool VpnSetStart(string name) {
|
|
|
|
|
if(name == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("OpenVPN Config name ist not set!");
|
|
|
|
|
}
|
|
|
|
|
return this.sendMessage("OPENVPN", "START " + name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool VpnSetStop(string name) {
|
|
|
|
|
if(name == null) {
|
|
|
|
|
throw new ServiceControlArgumentException("OpenVPN Config name ist not set!");
|
|
|
|
|
}
|
|
|
|
|
return this.sendMessage("OPENVPN", "STOP " + name);
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|
2015-03-04 23:21:34 +01:00
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|