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; 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); } public static ServiceControl getInstance(string configfile) { if(instance == null) { instance = new ServiceControl(configfile); } return instance; } 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; } public bool ServiceStart() { return this.ServiceSetStatus(ServiceControllerStatus.Running); } public bool ServicePause() { return this.ServiceSetStatus(ServiceControllerStatus.Paused); } public bool ServiceStop() { return this.ServiceSetStatus(ServiceControllerStatus.Stopped); } public bool NetworkSetAuto(string adapter) { if(adapter == null) { throw new ServiceControlArgumentException("Adapter ist not Set!"); } return this.sendMessage("NET", "AUTO " + adapter); } 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); } } 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); } 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); } 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); } public bool NetworkSetIpAuto(string adapter) { if(adapter == null) { throw new ServiceControlArgumentException("Adapter ist not Set!"); } return this.sendMessage("NET", "IP AUTO " + adapter); } public bool NetworkSetDnsAuto(string adapter) { if(adapter == null) { throw new ServiceControlArgumentException("Adapter ist not Set!"); } return this.sendMessage("NET", "DNS AUTO " + adapter); } public bool NetworkSetWinsAuto(string adapter) { if(adapter == null) { throw new ServiceControlArgumentException("Adapter ist not Set!"); } return this.sendMessage("NET", "WINS AUTO " + adapter); } public bool ServiceStart(string name) { if(name == null) { throw new ServiceControlArgumentException("Service name ist not set!"); } return this.sendMessage("SERVICE", "START " + name); } 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; } } 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; } 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); } internal List NetworkGetAdapters() { String adapters = ""; try { this.sendMessage("NET", "ADAPTERS"); } catch(ServiceControlException e) { if(e.Message.ToLower().Substring(0, 1) == "[") { adapters = e.Message.Substring(0, e.Message.Length - 1).Substring(1); } } return adapters.Split('|').ToList(); } } }