diff --git a/NetMonitor.sln b/NetMonitor.sln index 6a9fdaf..bd3d5f6 100644 --- a/NetMonitor.sln +++ b/NetMonitor.sln @@ -1,6 +1,6 @@  -Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetMonitorClient", "NetMonitorClient\NetMonitorClient.csproj", "{718693A0-06A3-4765-801D-33842861AA61}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetMonitorServer", "NetMonitorServer\NetMonitorServer.csproj", "{7813196F-D729-44A2-81C7-82BB83E207F5}" @@ -9,6 +9,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetMonitorConsole", "NetMon EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetOpenVPNGUI", "NetOpenVPNGUI\NetOpenVPNGUI.csproj", "{23485575-888C-4990-A4C8-6C74563F6A85}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils", "..\Utils\Utils\Utils.csproj", "{FAC8CE64-BF13-4ECE-8097-AEB5DD060098}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetMonitorUtils", "NetMonitorUtils\NetMonitorUtils.csproj", "{45EE003D-5572-48FE-B90E-6BFE18D9EEC8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetMonitorTray", "NetMonitorTray\NetMonitorTray.csproj", "{0B85C838-3295-4FDA-9B07-4F5B3681BE2F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -59,6 +65,36 @@ Global {23485575-888C-4990-A4C8-6C74563F6A85}.Release|Mixed Platforms.Build.0 = Release|x86 {23485575-888C-4990-A4C8-6C74563F6A85}.Release|x86.ActiveCfg = Release|x86 {23485575-888C-4990-A4C8-6C74563F6A85}.Release|x86.Build.0 = Release|x86 + {FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Debug|x86.ActiveCfg = Debug|Any CPU + {FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Release|Any CPU.Build.0 = Release|Any CPU + {FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Release|x86.ActiveCfg = Release|Any CPU + {45EE003D-5572-48FE-B90E-6BFE18D9EEC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {45EE003D-5572-48FE-B90E-6BFE18D9EEC8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {45EE003D-5572-48FE-B90E-6BFE18D9EEC8}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {45EE003D-5572-48FE-B90E-6BFE18D9EEC8}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {45EE003D-5572-48FE-B90E-6BFE18D9EEC8}.Debug|x86.ActiveCfg = Debug|Any CPU + {45EE003D-5572-48FE-B90E-6BFE18D9EEC8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {45EE003D-5572-48FE-B90E-6BFE18D9EEC8}.Release|Any CPU.Build.0 = Release|Any CPU + {45EE003D-5572-48FE-B90E-6BFE18D9EEC8}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {45EE003D-5572-48FE-B90E-6BFE18D9EEC8}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {45EE003D-5572-48FE-B90E-6BFE18D9EEC8}.Release|x86.ActiveCfg = Release|Any CPU + {0B85C838-3295-4FDA-9B07-4F5B3681BE2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0B85C838-3295-4FDA-9B07-4F5B3681BE2F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0B85C838-3295-4FDA-9B07-4F5B3681BE2F}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {0B85C838-3295-4FDA-9B07-4F5B3681BE2F}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {0B85C838-3295-4FDA-9B07-4F5B3681BE2F}.Debug|x86.ActiveCfg = Debug|Any CPU + {0B85C838-3295-4FDA-9B07-4F5B3681BE2F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0B85C838-3295-4FDA-9B07-4F5B3681BE2F}.Release|Any CPU.Build.0 = Release|Any CPU + {0B85C838-3295-4FDA-9B07-4F5B3681BE2F}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {0B85C838-3295-4FDA-9B07-4F5B3681BE2F}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {0B85C838-3295-4FDA-9B07-4F5B3681BE2F}.Release|x86.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/NetMonitorClient/NetMonitorClient.csproj b/NetMonitorClient/NetMonitorClient.csproj index 6d40a03..b2e4ae1 100644 --- a/NetMonitorClient/NetMonitorClient.csproj +++ b/NetMonitorClient/NetMonitorClient.csproj @@ -115,7 +115,6 @@ Settings.settings True - diff --git a/NetMonitorClient/ServiceControl.cs b/NetMonitorClient/ServiceControl.cs deleted file mode 100644 index ba02207..0000000 --- a/NetMonitorClient/ServiceControl.cs +++ /dev/null @@ -1,141 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.ServiceProcess; -using System.Net.Sockets; -using System.Net; - -namespace NetMonitorClient -{ - public class ServiceControl - { - private UdpClient net_send; - private IPEndPoint net_send_port; - private UdpClient net_gets; - private IPEndPoint net_gets_port; - public ServiceControl(string name) - { - ServiceController sc = new ServiceController(); - sc.ServiceName = name; - //if (sc.Status != ServiceControllerStatus.Running) - //{ - // sc.Start(); - //} - this.net_send = new UdpClient(); - this.net_send_port = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 34523); - this.net_gets = new UdpClient(34524); - this.net_gets_port = new IPEndPoint(IPAddress.Loopback, 0); - } - - internal bool setNetworkAuto(string adapter) - { - if (adapter == null) - { - System.Windows.Forms.MessageBox.Show("Adapter ist not Set!"); - return false; - } - byte[] data = Encoding.UTF8.GetBytes("NET AUTO "+adapter); - this.net_send.Send(data, data.Length, this.net_send_port); - return Boolean.Parse(Encoding.UTF8.GetString(net_gets.Receive(ref net_gets_port))); - } - - internal bool setNetworkIp(string ip, string subnet, string gateway, string adapter) - { - if (ip == null) - { - System.Windows.Forms.MessageBox.Show("IP address not Set!"); - return false; - } - if (subnet == null) - { - System.Windows.Forms.MessageBox.Show("Subnet address not Set!"); - return false; - } - if (gateway == null) - { - System.Windows.Forms.MessageBox.Show("Gateway address not Set!"); - return false; - } - if (adapter == null) - { - System.Windows.Forms.MessageBox.Show("Adapter ist not Set!"); - return false; - } - byte[] data = Encoding.UTF8.GetBytes("NET IP " + ip + " " + subnet + " " + gateway + " " + adapter); - this.net_send.Send(data, data.Length, this.net_send_port); - return Boolean.Parse(Encoding.UTF8.GetString(net_gets.Receive(ref net_gets_port))); - } - - internal bool setNetworkDNS(string dns, string adapter) - { - if (dns == null) - { - System.Windows.Forms.MessageBox.Show("DNS address not Set!"); - return false; - } - if (adapter == null) - { - System.Windows.Forms.MessageBox.Show("Adapter ist not Set!"); - return false; - } - byte[] data = Encoding.UTF8.GetBytes("NET DNS " + dns + " " + adapter); - this.net_send.Send(data, data.Length, this.net_send_port); - return Boolean.Parse(Encoding.UTF8.GetString(net_gets.Receive(ref net_gets_port))); - } - - internal bool setNetworkWINS(string wins, string adapter) - { - if (wins == null) - { - System.Windows.Forms.MessageBox.Show("WINS address not Set!"); - return false; - } - if (adapter == null) - { - System.Windows.Forms.MessageBox.Show("Adapter ist not Set!"); - return false; - } - byte[] data = Encoding.UTF8.GetBytes("NET WINS " + wins + " " + adapter); - this.net_send.Send(data, data.Length, this.net_send_port); - return Boolean.Parse(Encoding.UTF8.GetString(net_gets.Receive(ref net_gets_port))); - } - - internal bool setNetworkIpAuto(string adapter) - { - if (adapter == null) - { - System.Windows.Forms.MessageBox.Show("Adapter ist not Set!"); - return false; - } - byte[] data = Encoding.UTF8.GetBytes("NET IP AUTO "+adapter); - this.net_send.Send(data, data.Length, this.net_send_port); - return Boolean.Parse(Encoding.UTF8.GetString(net_gets.Receive(ref net_gets_port))); - } - - - internal bool setNetworkDnsAuto(string adapter) - { - if (adapter == null) - { - System.Windows.Forms.MessageBox.Show("Adapter ist not Set!"); - return false; - } - byte[] data = Encoding.UTF8.GetBytes("NET DNS AUTO " + adapter); - this.net_send.Send(data, data.Length, this.net_send_port); - return Boolean.Parse(Encoding.UTF8.GetString(net_gets.Receive(ref net_gets_port))); - } - - internal bool setNetworkWinsAuto(string adapter) - { - if (adapter == null) - { - System.Windows.Forms.MessageBox.Show("Adapter ist not Set!"); - return false; - } - byte[] data = Encoding.UTF8.GetBytes("NET WINS AUTO " + adapter); - this.net_send.Send(data, data.Length, this.net_send_port); - return Boolean.Parse(Encoding.UTF8.GetString(net_gets.Receive(ref net_gets_port))); - } - } -} diff --git a/NetMonitorConsole/CMDArgs.cs b/NetMonitorConsole/CMDArgs.cs deleted file mode 100644 index 8446df5..0000000 --- a/NetMonitorConsole/CMDArgs.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace NetMonitorConsole -{ - class CMDArgs - { - private string[] args; - public enum Worker - { - Network, - Nothing - } - private Worker work; - public CMDArgs(string[] arg) - { - this.args = arg; - work = Worker.Nothing; - setWorker(); - } - - private void setWorker() - { - if (args.Length >= 1 && args[0].ToLower() == "-n") - work = Worker.Network; - } - - - internal Worker getWorker() - { - return work; - } - - internal string getNetworkName() - { - if (work == Worker.Network) - return args[1]; - return null; - } - } -} diff --git a/NetMonitorConsole/Factory.cs b/NetMonitorConsole/Factory.cs new file mode 100644 index 0000000..878bd59 --- /dev/null +++ b/NetMonitorConsole/Factory.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using BlubbFish.Utils; +using NetMonitorUtils; + +namespace NetMonitorConsole +{ + class Factory + { + private static CmdArgs instanceArguments; + private static FileLogger instanceLogger; + public static CmdArgs getArguments(string[] args) + { + if (instanceArguments == null) + { + instanceArguments = CmdArgs.getInstance(String.Join(" ", args, 0, args.Length)); + Dictionary argp = new Dictionary(); + argp.Add("-n", CmdArgs.ArgLength.Touple); + instanceArguments.setArguments(argp); + } + return instanceArguments; + } + public static FileLogger getLogger() + { + if (instanceLogger == null) + { + instanceLogger = FileLogger.getInstance("logs/console.log", true); + } + return instanceLogger; + } + } +} diff --git a/NetMonitorConsole/NetMonitorConsole.csproj b/NetMonitorConsole/NetMonitorConsole.csproj index 2a1a732..22a6105 100644 --- a/NetMonitorConsole/NetMonitorConsole.csproj +++ b/NetMonitorConsole/NetMonitorConsole.csproj @@ -6,36 +6,48 @@ 8.0.30703 2.0 {148AA47C-FA4D-4807-9EB0-5AEAC36C1524} - Exe + WinExe Properties NetMonitorConsole NetMonitorConsole - v4.0 - Client + v4.5 + + 512 - x86 + AnyCPU true full false - E:\Programme\NetMonitor\ + ..\bin\Debug\ DEBUG;TRACE prompt 4 + false - x86 + AnyCPU pdbonly true - bin\Release\ - TRACE + ..\bin\Release\ + + prompt 4 + false + + + icon.ico + + + + + @@ -43,15 +55,38 @@ - + + + True + True + Resources.resx + - - {718693A0-06A3-4765-801D-33842861AA61} - NetMonitorClient + + {fac8ce64-bf13-4ece-8097-aeb5dd060098} + Utils + + {45ee003d-5572-48fe-b90e-6bfe18d9eec8} + NetMonitorUtils + + + + + PreserveNewest + + + + + + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + ..\icon.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + \ No newline at end of file diff --git a/NetMonitorConsole/icon.ico b/NetMonitorConsole/icon.ico new file mode 100644 index 0000000..179d0a4 Binary files /dev/null and b/NetMonitorConsole/icon.ico differ diff --git a/NetMonitorConsole/icon.png b/NetMonitorConsole/icon.png new file mode 100644 index 0000000..ca8cc23 Binary files /dev/null and b/NetMonitorConsole/icon.png differ diff --git a/NetMonitorConsole/network.ini b/NetMonitorConsole/network.ini new file mode 100644 index 0000000..fc415b3 --- /dev/null +++ b/NetMonitorConsole/network.ini @@ -0,0 +1,6 @@ +[network1] +Name=Home +Ip=auto +Dns=auto +Wins=auto +Adapter=LAN-Verbindung \ No newline at end of file diff --git a/NetMonitorServer/ActionClass.cs b/NetMonitorServer/ActionClass.cs new file mode 100644 index 0000000..cd5c93c --- /dev/null +++ b/NetMonitorServer/ActionClass.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Net.Sockets; +using System.Net; + +namespace NetMonitorServer +{ + abstract class ActionClass + { + protected UdpClient outputNetworkStream; + protected IPEndPoint outputNetworkPort; + internal void SetAnswerStream(UdpClient udpClient, IPEndPoint iPEndPoint) + { + this.outputNetworkStream = udpClient; + this.outputNetworkPort = iPEndPoint; + } + + abstract internal bool Run(Queue arguments); + + protected void sendMessage(string message) + { + Console.WriteLine(message); + byte[] answ = Encoding.UTF8.GetBytes(message); + outputNetworkStream.Send(answ, answ.Length, outputNetworkPort); + } + } +} diff --git a/NetMonitorServer/Actions/SetNetworks.cs b/NetMonitorServer/Actions/SetNetworks.cs new file mode 100644 index 0000000..fdbf398 --- /dev/null +++ b/NetMonitorServer/Actions/SetNetworks.cs @@ -0,0 +1,151 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Diagnostics; +using System.Net; +using System.Net.Sockets; + +namespace NetMonitorServer +{ + class SetNetworks : ActionClass + { + private bool setNetworkWins(Queue data) + { + if (data.Count != 2) + { + this.sendMessage("To less arguments for SetNetworks.setNetworkWins class"); + return false; + } + string ip = data.Dequeue(); + string name = data.Dequeue(); + string netsh = "interface ipv4 set winsservers name=\"" + name + (ip == "auto" ? "\" source=dhcp" : "\" static " + ip); + try + { + this.runProgramNetsh(netsh); + } + catch (Exception e) + { + this.sendMessage(e.Message); + return false; + } + this.sendMessage("true"); + return true; + } + + private bool setNetworkDns(Queue data) + { + if (data.Count != 2) + { + this.sendMessage("To less arguments for SetNetworks.setNetworkDns class"); + return false; + } + string ip = data.Dequeue(); + string name = data.Dequeue(); + string netsh = "interface ipv4 set dnsservers name=\"" + name + (ip == "auto" ? "\" source=dhcp" : "\" static " + ip + "primary"); + try + { + this.runProgramNetsh(netsh); + } + catch (Exception e) + { + this.sendMessage(e.Message); + return false; + } + this.sendMessage("true"); + return true; + } + + private bool setNetworkIp(Queue data) + { + if (data.Count != 2 && data.Count != 4) + { + this.sendMessage("To less arguments for SetNetworks.setNetworkIp class"); + return false; + } + string ip = data.Dequeue(); + string netsh = ""; + if (ip == "auto") + { + string name = data.Dequeue(); + netsh = "interface ipv4 set address name=\"" + name + "\" source=dhcp"; + } + else + { + string subnet = data.Dequeue(); + string gw = data.Dequeue(); + string name = data.Dequeue(); + netsh = "interface ipv4 set address \"" + name + "\" static " + ip + " " + subnet + " " + gw; + } + try + { + this.runProgramNetsh(netsh); + } + catch (Exception e) + { + this.sendMessage(e.Message); + return false; + } + this.sendMessage("true"); + return true; + } + + private bool setNetworkAuto(Queue data) + { + if (data.Count != 1) + { + this.sendMessage("To less arguments for SetNetworks.setNetworkDns class"); + return false; + } + string name = data.Dequeue(); + try + { + this.runProgramNetsh("interface ipv4 set address name=\"" + name + "\" source=dhcp"); + this.runProgramNetsh("interface ipv4 set dnsservers name=\"" + name + "\" source=dhcp"); + this.runProgramNetsh("interface ipv4 set winsservers name=\"" + name + "\" source=dhcp"); + } + catch (Exception e) + { + this.sendMessage(e.Message); + return false; + } + this.sendMessage("true"); + return true; + } + + private bool runProgramNetsh(string args) + { + Process p = new Process(); + p.StartInfo.Arguments = args; + p.StartInfo.FileName = "netsh"; + p.StartInfo.CreateNoWindow = true; + p.StartInfo.RedirectStandardOutput = true; + p.StartInfo.UseShellExecute = false; + p.Start(); + string output = p.StandardOutput.ReadToEnd(); + p.WaitForExit(); + Console.WriteLine(output.Trim()); + if (p.ExitCode == 0) + { + return true; + } + throw new Exception(output.Trim()); + } + internal override bool Run(Queue arguments) + { + if (arguments.Count < 2) + { + this.sendMessage("To less arguments for SetNetwork class"); + return false; + } + switch (arguments.Dequeue().ToLower()) + { + case "auto": return this.setNetworkAuto(arguments); + case "ip": return this.setNetworkIp(arguments); + case "dns": return this.setNetworkDns(arguments); + case "wins": return this.setNetworkWins(arguments); + default: this.sendMessage("Wrong arguments for service on SetNetwork class"); return false; + } + } + } +} diff --git a/NetMonitorServer/Actions/SetOpenVPNService.cs b/NetMonitorServer/Actions/SetOpenVPNService.cs new file mode 100644 index 0000000..9123730 --- /dev/null +++ b/NetMonitorServer/Actions/SetOpenVPNService.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NetMonitorServer +{ + class SetOpenVPNService : ActionClass + { + internal override bool Run(Queue arguments) + { + throw new NotImplementedException(); + } + } +} diff --git a/NetMonitorServer/Actions/SetService.cs b/NetMonitorServer/Actions/SetService.cs new file mode 100644 index 0000000..d381b93 --- /dev/null +++ b/NetMonitorServer/Actions/SetService.cs @@ -0,0 +1,115 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Diagnostics; +using System.Net; +using System.Net.Sockets; +using System.ServiceProcess; + +namespace NetMonitorServer +{ + class SetService : ActionClass + { + ServiceController serviceController; + + private bool setServiceStart(Queue data) + { + if (data.Count != 1) + { + this.sendMessage("To less arguments for SetService.setServiceStart class"); + return false; + } + this.serviceController.ServiceName = data.Dequeue(); + try + { + this.serviceController.Start(); + this.serviceController.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30)); + } + catch (Exception e) + { + this.sendMessage(e.Message); + return false; + } + if (this.serviceController.Status == ServiceControllerStatus.Running) + { + this.sendMessage("true"); + } + else + { + this.sendMessage("The service isn't started"); + return false; + } + return true; + } + + private bool setServiceStop(Queue data) + { + if (data.Count != 1) + { + this.sendMessage("To less arguments for SetService.setServiceStop class"); + return false; + } + this.serviceController.ServiceName = data.Dequeue(); + try + { + this.serviceController.Stop(); + this.serviceController.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 30)); + } + catch (Exception e) + { + this.sendMessage(e.Message); + return false; + } + if (this.serviceController.Status == ServiceControllerStatus.Stopped) + { + this.sendMessage("true"); + } + else + { + this.sendMessage("The service isn't stopped"); + return false; + } + return true; + } + + private bool getServiceStatus(Queue data) + { + if (data.Count != 1) + { + this.sendMessage("To less arguments for SetService.setServiceStatus class"); + return false; + } + try + { + this.serviceController.ServiceName = data.Dequeue(); + if (this.serviceController.Status == ServiceControllerStatus.Running) + this.sendMessage("running"); + else + this.sendMessage("stopped"); + } + catch (Exception e) + { + this.sendMessage(e.Message); + return false; + } + return true; + } + internal override bool Run(Queue arguments) + { + if (arguments.Count != 2) + { + this.sendMessage("To less arguments for SetService class"); + return false; + } + this.serviceController = new ServiceController(); + switch (arguments.Dequeue().ToLower()) + { + case "start": return this.setServiceStart(arguments); + case "stop": return this.setServiceStop(arguments); + case "status": return this.getServiceStatus(arguments); + default: this.sendMessage("Wrong arguments for service on SetService class"); return false; + } + } + } +} diff --git a/NetMonitorServer/Factory.cs b/NetMonitorServer/Factory.cs new file mode 100644 index 0000000..88654c7 --- /dev/null +++ b/NetMonitorServer/Factory.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using BlubbFish.Utils; + +namespace NetMonitorServer +{ + class Factory + { + internal static ActionClass getAction(string className) + { + if (className.Substring(0, 3).ToLower() == "net") + return new SetNetworks(); + if (className.Substring(0, 7).ToLower() == "service") + return new SetService(); + if (className.Substring(0, 7).ToLower() == "openvpn") + return new SetOpenVPNService(); + return null; + } + internal static InIReader getSettings() { + return InIReader.getInstance("config.ini"); + } + } +} diff --git a/NetMonitorServer/Service1.Designer.cs b/NetMonitorServer/MainService.Designer.cs similarity index 77% rename from NetMonitorServer/Service1.Designer.cs rename to NetMonitorServer/MainService.Designer.cs index 1e794f9..a10e8df 100644 --- a/NetMonitorServer/Service1.Designer.cs +++ b/NetMonitorServer/MainService.Designer.cs @@ -1,6 +1,6 @@ namespace NetMonitorServer { - partial class Service1 + partial class MainService { /// /// Required designer variable. @@ -28,10 +28,8 @@ /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); - this.timer1 = new System.Windows.Forms.Timer(this.components); // - // Service1 + // MainService // this.ServiceName = "NetMonitorServer"; @@ -39,6 +37,5 @@ #endregion - private System.Windows.Forms.Timer timer1; } } diff --git a/NetMonitorServer/MainService.cs b/NetMonitorServer/MainService.cs new file mode 100644 index 0000000..3f8cdcc --- /dev/null +++ b/NetMonitorServer/MainService.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Diagnostics; +using System.Linq; +using System.ServiceProcess; +using System.Text; +using System.Net; +using System.Net.Sockets; +using System.Threading; +using BlubbFish.Utils; + +namespace NetMonitorServer +{ + public partial class MainService : ServiceBase + { + private Thread serverThread = new Thread(new ThreadStart(overwatch)); + private static UdpClient receiveStream; + public MainService() + { + InitializeComponent(); + this.serverThread.IsBackground = true; + } + + private static void overwatch() + { + IPEndPoint networkStreamAddress = new IPEndPoint(IPAddress.Loopback, 0); + while (true) + { + if (Thread.CurrentThread.ThreadState == System.Threading.ThreadState.AbortRequested) + { + receiveStream.Close(); + break; + } + byte[] data = receiveStream.Receive(ref networkStreamAddress); + Thread queryThread = new Thread(new ParameterizedThreadStart(ServerCommandProcessor)); + queryThread.Start(data); + } + } + + private static void ServerCommandProcessor(object threadParam) + { + string data = Encoding.UTF8.GetString((byte[])threadParam); + Console.WriteLine(data); + Queue arguments = new Queue(data.Split(' ')); + if (arguments.Count < 2) + { + Console.WriteLine("Zu wenig Argumente"); + return; + } + ActionClass a = Factory.getAction(arguments.Dequeue()); + if (a == null) + { + Console.WriteLine("Falsche Angabe der ActionClass"); + return; + } + a.SetAnswerStream(new UdpClient(), new IPEndPoint(IPAddress.Loopback, Int32.Parse(arguments.Dequeue()))); + a.Run(arguments); + } + + protected override void OnStart(string[] args) + { + this.initServerThread(); + this.serverThread.Start(); + } + + private void initServerThread() + { + receiveStream = new UdpClient(Int32.Parse(Factory.getSettings().getValue("ports", "server"))); + } + + protected override void OnStop() + { + this.serverThread.Abort(); + try { receiveStream.Close(); } catch { }; + } + + public void StartServiceConsole(string[] args) + { + this.OnStart(args); + } + } +} diff --git a/NetMonitorServer/Service1.resx b/NetMonitorServer/MainService.resx similarity index 91% rename from NetMonitorServer/Service1.resx rename to NetMonitorServer/MainService.resx index 72995a7..34987b2 100644 --- a/NetMonitorServer/Service1.resx +++ b/NetMonitorServer/MainService.resx @@ -117,12 +117,6 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - 143, 71 - - - 230, 71 - False diff --git a/NetMonitorServer/NetMonitorServer.csproj b/NetMonitorServer/NetMonitorServer.csproj index efe4c71..d76ab9c 100644 --- a/NetMonitorServer/NetMonitorServer.csproj +++ b/NetMonitorServer/NetMonitorServer.csproj @@ -6,32 +6,42 @@ 8.0.30703 2.0 {7813196F-D729-44A2-81C7-82BB83E207F5} - WinExe + Exe Properties NetMonitorServer NetMonitorServer - v4.0 - Client + v4.5 + + 512 - x86 + AnyCPU true full false - E:\Programme\NetMonitor\ + ..\bin\Debug\ DEBUG;TRACE prompt 4 + false - x86 - pdbonly + AnyCPU + none true - E:\Programme\NetMonitor\ - TRACE + ..\bin\Release\ + + prompt 4 + false + + + icon.ico + + + @@ -46,24 +56,43 @@ - - + + + + + Component - - Service1.cs + + MainService.cs Component - + - - Service1.cs + + MainService.cs + + + + + PreserveNewest + + + + + + + + {fac8ce64-bf13-4ece-8097-aeb5dd060098} + Utils + + + \ No newline at end of file diff --git a/NetMonitorTray/Program.cs b/NetMonitorTray/Program.cs new file mode 100644 index 0000000..18875e0 --- /dev/null +++ b/NetMonitorTray/Program.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + + +namespace NetMonitorTray +{ + class Program + { + static void Main(string[] args) + { + Controller.Tray t = new Controller.Tray(); + t.view(); + } + } +} diff --git a/NetMonitorTray/Properties/AssemblyInfo.cs b/NetMonitorTray/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..d7489dc --- /dev/null +++ b/NetMonitorTray/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die mit einer Assembly verknüpft sind. +[assembly: AssemblyTitle("NetMonitorTray")] +[assembly: AssemblyDescription("NetMonitor TrayLauncher")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("BlubbFish")] +[assembly: AssemblyProduct("NetMonitorTray")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar +// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von +// COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("9069ec73-a0b6-4414-b9f0-e3794324b8bd")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern +// übernehmen, indem Sie "*" eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/NetMonitorTray/Properties/Resources.Designer.cs b/NetMonitorTray/Properties/Resources.Designer.cs new file mode 100644 index 0000000..4cfbdce --- /dev/null +++ b/NetMonitorTray/Properties/Resources.Designer.cs @@ -0,0 +1,123 @@ +//------------------------------------------------------------------------------ +// +// Dieser Code wurde von einem Tool generiert. +// Laufzeitversion:4.0.30319.18444 +// +// Änderungen an dieser Datei können falsches Verhalten verursachen und gehen verloren, wenn +// der Code erneut generiert wird. +// +//------------------------------------------------------------------------------ + +namespace NetMonitorTray.Properties { + using System; + + + /// + /// Eine stark typisierte Ressourcenklasse zum Suchen von lokalisierten Zeichenfolgen usw. + /// + // Diese Klasse wurde von der StronglyTypedResourceBuilder automatisch generiert + // -Klasse über ein Tool wie ResGen oder Visual Studio automatisch generiert. + // Um einen Member hinzuzufügen oder zu entfernen, bearbeiten Sie die .ResX-Datei und führen dann ResGen + // mit der /str-Option erneut aus, oder Sie erstellen Ihr VS-Projekt neu. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Gibt die zwischengespeicherte ResourceManager-Instanz zurück, die von dieser Klasse verwendet wird. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("NetMonitorTray.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Überschreibt die CurrentUICulture-Eigenschaft des aktuellen Threads für alle + /// Ressourcenzuordnungen, die diese stark typisierte Ressourcenklasse verwenden. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Sucht eine lokalisierte Ressource vom Typ System.Drawing.Icon ähnlich wie (Symbol). + /// + internal static System.Drawing.Icon IconConnected { + get { + object obj = ResourceManager.GetObject("IconConnected", resourceCulture); + return ((System.Drawing.Icon)(obj)); + } + } + + /// + /// Sucht eine lokalisierte Ressource vom Typ System.Drawing.Icon ähnlich wie (Symbol). + /// + internal static System.Drawing.Icon IconNothing { + get { + object obj = ResourceManager.GetObject("IconNothing", resourceCulture); + return ((System.Drawing.Icon)(obj)); + } + } + + /// + /// Sucht eine lokalisierte Ressource vom Typ System.Drawing.Icon ähnlich wie (Symbol). + /// + internal static System.Drawing.Icon IconWorking { + get { + object obj = ResourceManager.GetObject("IconWorking", resourceCulture); + return ((System.Drawing.Icon)(obj)); + } + } + + /// + /// Sucht eine lokalisierte Ressource vom Typ System.Drawing.Bitmap. + /// + internal static System.Drawing.Bitmap MenuImagesQuit { + get { + object obj = ResourceManager.GetObject("MenuImagesQuit", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + + /// + /// Sucht eine lokalisierte Ressource vom Typ System.Drawing.Bitmap. + /// + internal static System.Drawing.Bitmap MenuImagesServerConnect { + get { + object obj = ResourceManager.GetObject("MenuImagesServerConnect", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + + /// + /// Sucht eine lokalisierte Ressource vom Typ System.Drawing.Bitmap. + /// + internal static System.Drawing.Bitmap MenuImagesServerDisconnect { + get { + object obj = ResourceManager.GetObject("MenuImagesServerDisconnect", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + } +} diff --git a/NetMonitorTray/Properties/Resources.resx b/NetMonitorTray/Properties/Resources.resx new file mode 100644 index 0000000..f441025 --- /dev/null +++ b/NetMonitorTray/Properties/Resources.resx @@ -0,0 +1,139 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + ..\Resources\connected.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + + ..\Resources\nothing.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + + ..\Resources\working.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + + ..\Resources\Icons\door_open.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + + ..\Resources\Icons\server_connect.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + + ..\Resources\Icons\server_delete.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + \ No newline at end of file diff --git a/NetMonitorTray/Resources/Icons/door_open.png b/NetMonitorTray/Resources/Icons/door_open.png new file mode 100644 index 0000000..64bab57 Binary files /dev/null and b/NetMonitorTray/Resources/Icons/door_open.png differ diff --git a/NetMonitorTray/Resources/Icons/server_connect.png b/NetMonitorTray/Resources/Icons/server_connect.png new file mode 100644 index 0000000..49b2691 Binary files /dev/null and b/NetMonitorTray/Resources/Icons/server_connect.png differ diff --git a/NetMonitorTray/Resources/Icons/server_delete.png b/NetMonitorTray/Resources/Icons/server_delete.png new file mode 100644 index 0000000..61e740f Binary files /dev/null and b/NetMonitorTray/Resources/Icons/server_delete.png differ diff --git a/NetMonitorTray/Resources/connected.ico b/NetMonitorTray/Resources/connected.ico new file mode 100644 index 0000000..89d6848 Binary files /dev/null and b/NetMonitorTray/Resources/connected.ico differ diff --git a/NetMonitorTray/Resources/nothing.ico b/NetMonitorTray/Resources/nothing.ico new file mode 100644 index 0000000..b839200 Binary files /dev/null and b/NetMonitorTray/Resources/nothing.ico differ diff --git a/NetMonitorTray/Resources/working.ico b/NetMonitorTray/Resources/working.ico new file mode 100644 index 0000000..d6cbcd2 Binary files /dev/null and b/NetMonitorTray/Resources/working.ico differ diff --git a/NetMonitorTray/Views/Tray.cs b/NetMonitorTray/Views/Tray.cs new file mode 100644 index 0000000..96e5a3e --- /dev/null +++ b/NetMonitorTray/Views/Tray.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using System.Drawing; +using NetMonitorTray.Properties; +using BlubbFish.Utils; + +namespace NetMonitorTray.View +{ + public class Tray : OwnView + { + private NotifyIcon trayi; + private Dictionary> menuOpenVpnService; + private Dictionary> menuNetwork; + + public Tray() + { + this.initIcon(); + Model.Tray.Instance.setObserver(this); + } + + public override void update() + { + this.menuOpenVpnService = Model.Tray.Instance.getMenuOpenVpnService(); + this.menuNetwork = Model.Tray.Instance.getMenuNetwork(); + this.trayi.ContextMenuStrip = this.genMenu(); + } + + private void initIcon() + { + this.trayi = new NotifyIcon(); + this.trayi.Visible = true; + this.trayi.Icon = new Icon(Resources.IconNothing, 40, 40); + this.trayi.Text = "NetMonitorTray"; + this.trayi.DoubleClick += Controller.Tray.Click_Tray; + this.trayi.BalloonTipClicked += Controller.Tray.Click_Ballon; + } + + private ContextMenuStrip genMenu() + { + ContextMenuStrip menu = new ContextMenuStrip(); + ToolStripMenuItem m1 = new ToolStripMenuItem("Öffnen"); + m1.Image = Resources.MenuImagesQuit; + m1.Click += Controller.Tray.Click_Open; + m1.Name = "Open"; + menu.Items.Add(m1); + menu.Items.Add(new ToolStripSeparator()); + menu.Items.AddRange(this.genMenuIt(this.menuNetwork, Controller.Tray.Click_Network)); + menu.Items.Add(new ToolStripSeparator()); + menu.Items.AddRange(this.genMenuIt(this.menuOpenVpnService, Controller.Tray.Click_Service)); + menu.Items.Add(new ToolStripSeparator()); + ToolStripMenuItem m2 = new ToolStripMenuItem("Beenden"); + m2.Image = Resources.MenuImagesQuit; + m2.Click += Controller.Tray.Click_Quit; + m2.Name = "Quit"; + menu.Items.Add(m2); + return menu; + } + + private ToolStripMenuItem[] genMenuIt(Dictionary> dictionary, EventHandler ev) + { + List l = new List(); + foreach (String key in dictionary.Keys) + { + ToolStripMenuItem m = new ToolStripMenuItem(dictionary[key].Item1); + if (dictionary[key].Item2 != null) + { + m.Image = dictionary[key].Item2; + } + m.Click += ev; + m.Name = key; + m.Enabled = dictionary[key].Item3; + if (dictionary[key].Item4) + { + m.Font = new System.Drawing.Font(m.Font, m.Font.Style | FontStyle.Bold); + } + l.Add(m); + } + return l.ToArray(); + } + + private void showBallonTooltip(string text, ToolTipIcon toolTipIcon, string title = "NetMonitor Tray") + { + this.trayi.BalloonTipIcon = toolTipIcon; + this.trayi.BalloonTipText = text; + this.trayi.BalloonTipTitle = title; + this.trayi.ShowBalloonTip(100); + } + + internal void Dispose() + { + this.trayi.Visible = false; + Application.ExitThread(); + } + + internal void setService(bool run) + { + this.showBallonTooltip((run ? "Service erfolgreich gestartet." : "Service erfolgreich beendet"), ToolTipIcon.Info); + Model.Tray.Instance.setService(run); + } + + internal void showError(string text) + { + this.showBallonTooltip(text, ToolTipIcon.Error); + } + } +} diff --git a/NetMonitorTray/icon.ico b/NetMonitorTray/icon.ico new file mode 100644 index 0000000..e8dfc7c Binary files /dev/null and b/NetMonitorTray/icon.ico differ diff --git a/NetMonitorTray/icon.png b/NetMonitorTray/icon.png new file mode 100644 index 0000000..a86fb78 Binary files /dev/null and b/NetMonitorTray/icon.png differ diff --git a/NetMonitorUtils/Execptions/ServiceControlArgumentException.cs b/NetMonitorUtils/Execptions/ServiceControlArgumentException.cs new file mode 100644 index 0000000..98ba086 --- /dev/null +++ b/NetMonitorUtils/Execptions/ServiceControlArgumentException.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NetMonitorUtils.Execptions +{ + [Serializable()] + class ServiceControlArgumentException : System.Exception + { + public ServiceControlArgumentException() : base() { } + public ServiceControlArgumentException(string message) : base(message) { } + public ServiceControlArgumentException(string message, System.Exception inner) : base(message, inner) { } + protected ServiceControlArgumentException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } + } +} diff --git a/NetMonitorUtils/Execptions/ServiceControlException.cs b/NetMonitorUtils/Execptions/ServiceControlException.cs new file mode 100644 index 0000000..fb85881 --- /dev/null +++ b/NetMonitorUtils/Execptions/ServiceControlException.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NetMonitorUtils.Execptions +{ + [Serializable()] + class ServiceControlException : System.Exception + { + public ServiceControlException() : base() { } + public ServiceControlException(string message) : base(message) { } + public ServiceControlException(string message, System.Exception inner) : base(message, inner) { } + protected ServiceControlException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } + } +} diff --git a/NetMonitorUtils/Execptions/ServiceControlNetworkException.cs b/NetMonitorUtils/Execptions/ServiceControlNetworkException.cs new file mode 100644 index 0000000..dbcc12b --- /dev/null +++ b/NetMonitorUtils/Execptions/ServiceControlNetworkException.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NetMonitorUtils.Execptions +{ + [Serializable()] + class ServiceControlNetworkException : System.Exception + { + public ServiceControlNetworkException() : base() { } + public ServiceControlNetworkException(string message) : base(message) { } + public ServiceControlNetworkException(string message, System.Exception inner) : base(message, inner) { } + protected ServiceControlNetworkException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } + } +} diff --git a/NetMonitorUtils/NetMonitorUtils.csproj b/NetMonitorUtils/NetMonitorUtils.csproj new file mode 100644 index 0000000..ec2bade --- /dev/null +++ b/NetMonitorUtils/NetMonitorUtils.csproj @@ -0,0 +1,64 @@ + + + + + Debug + AnyCPU + {45EE003D-5572-48FE-B90E-6BFE18D9EEC8} + Library + Properties + NetMonitorUtils + NetMonitorUtils + v4.5 + 512 + + + true + full + false + ..\bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + ..\bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + {fac8ce64-bf13-4ece-8097-aeb5dd060098} + Utils + + + + + \ No newline at end of file diff --git a/NetMonitorUtils/NetworkSetter.cs b/NetMonitorUtils/NetworkSetter.cs new file mode 100644 index 0000000..8cbe756 --- /dev/null +++ b/NetMonitorUtils/NetworkSetter.cs @@ -0,0 +1,182 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using BlubbFish.Utils; + +namespace NetMonitorUtils +{ + public class NetworkSetter : OwnObject + { + private ServiceControl serviceController; + private InIReader networkini; + + private enum netsetterType + { + Auto, + IpAuto, + Ip, + DnsAuto, + Dns, + WinsAuto, + Wins + } + public NetworkSetter(string networkfile, string configfile) + { + this.addLog("NetworkSetter", "networkfile: " + networkfile + "; configfile: " + configfile + ";", LogLevel.Debug); + this.serviceController = ServiceControl.getInstance(configfile); + this.networkini = InIReader.getInstance(networkfile); + } + public bool setNetwork(string networkName) + { + this.addLog("setNetwork", "Setze Netzwerkeinstellungen: " + networkName, LogLevel.Info); + this.addLog("setNetwork", "networkName: " + networkName + ";", LogLevel.Debug); + String name = networkini.getValue(networkName, "Name"); + String ip = networkini.getValue(networkName, "Ip"); + String subnet = networkini.getValue(networkName, "Subnet"); + String gateway = networkini.getValue(networkName, "Gateway"); + String dns = networkini.getValue(networkName, "Dns"); + String wins = networkini.getValue(networkName, "Wins"); + String adapter = networkini.getValue(networkName, "Adapter"); + this.addLog("setNetwork", "name: " + name, LogLevel.Debug); + this.addLog("setNetwork", "ip: " + ip, LogLevel.Debug); + this.addLog("setNetwork", "subnet: " + subnet, LogLevel.Debug); + this.addLog("setNetwork", "gateway: " + gateway, LogLevel.Debug); + this.addLog("setNetwork", "dns: " + dns, LogLevel.Debug); + this.addLog("setNetwork", "wins: " + wins, LogLevel.Debug); + this.addLog("setNetwork", "adapter: " + adapter, LogLevel.Debug); + bool ret = false; + if (ip.ToLower() == "auto" && dns.ToLower() == "auto" && wins.ToLower() == "auto") + { + ret = setNetworkAuto(adapter, name); + this.addLog("setNetwork.setNetworkAuto", "return " + ret, LogLevel.Debug); + } + else + { + if (ip.ToLower() == "auto") + { + ret = setNetworkIpAuto(adapter, name); + this.addLog("setNetwork.setNetworkIpAuto", "return " + ret, LogLevel.Debug); + } + else + { + ret = setNetworkIp(ip, subnet, gateway, adapter, name); + this.addLog("setNetwork.setNetworkIp", "return " + ret, LogLevel.Debug); + } + if (dns.ToLower() == "auto") + { + ret = setNetworkDnsAuto(adapter, name); + this.addLog("setNetwork.setNetworkDnsAuto", "return " + ret, LogLevel.Debug); + } + else + { + ret = setNetworkDns(dns, adapter, name); + this.addLog("setNetwork.setNetworkDns", "return " + ret, LogLevel.Debug); + } + if (wins.ToLower() == "auto") + { + ret = setNetworkWinsAuto(adapter, name); + this.addLog("setNetwork.setNetworkWinsAuto", "return " + ret, LogLevel.Debug); + } + else + { + ret = setNetworkWins(wins, adapter, name); + this.addLog("setNetwork.setNetworkWins", "return " + ret, LogLevel.Debug); + } + } + this.addLog("setNetwork", "return " + ret, LogLevel.Debug); + return ret; + } + public bool setNetworkAuto(string adapter, string name) + { + this.addLog("setNetworkAuto", "Setze Netzwerkeinstellungen von " + adapter + " auf Automatisch", LogLevel.Info); + this.addLog("setNetworkAuto", "datapter: " + adapter + "; name: " + name + ";", LogLevel.Debug); + return setNetwork(netsetterType.Auto, null, null, null, adapter, name, null, null); + } + public bool setNetworkIpAuto(string adapter, string name) + { + this.addLog("setNetworkIpAuto", "Setze Netzwerkeinstellungen von " + adapter + " auf IP Automatisch", LogLevel.Info); + this.addLog("setNetworkIpAuto", "adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug); + return setNetwork(netsetterType.IpAuto, null, null, null, adapter, name, null, null); + } + public bool setNetworkIp(string ip, string subnet, string gateway, string adapter, string name) + { + this.addLog("setNetworkIp", "Setze Netzwerkeinstellungen von " + adapter + " auf IP " + ip + "/" + subnet + " GW " + gateway, LogLevel.Info); + this.addLog("setNetworkIp", "ip: " + ip + "; subnet: " + subnet + "; gateway: " + gateway + "; adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug); + return setNetwork(netsetterType.Ip, ip, subnet, gateway, adapter, name, null, null); + } + public bool setNetworkDnsAuto(string adapter, string name) + { + this.addLog("setNetworkDnsAuto", "Setze Netzwerkeinstellungen von " + adapter + " auf Dns Automatisch", LogLevel.Info); + this.addLog("setNetworkDnsAuto", "adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug); + return setNetwork(netsetterType.DnsAuto, null, null, null, adapter, name, null, null); + } + public bool setNetworkDns(string dns, string adapter, string name) + { + this.addLog("setNetworkDns", "Setze Netzwerkeinstellungen von " + adapter + " auf Dns " + dns, LogLevel.Info); + this.addLog("setNetworkDns", "dns: " + dns + "; adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug); + return setNetwork(netsetterType.Dns, null, null, null, adapter, name, dns, null); + } + public bool setNetworkWinsAuto(string adapter, string name) + { + this.addLog("setNetworkWinsAuto", "Setze Netzwerkeinstellungen von " + adapter + " auf Wins Automatisch", LogLevel.Info); + this.addLog("setNetworkWinsAuto", "adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug); + return setNetwork(netsetterType.WinsAuto, null, null, null, adapter, name, null, null); + } + public bool setNetworkWins(string wins, string adapter, string name) + { + this.addLog("setNetworkWins", "Setze Netzwerkeinstellungen von " + adapter + " auf Wins " + wins, LogLevel.Info); + this.addLog("setNetworkWins", "wins: " + wins + "; adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug); + return setNetwork(netsetterType.Wins, null, null, null, adapter, name, null, wins); + } + private bool setNetwork(netsetterType option, string ip, string subnet, string gateway, string adapter, string name, string dns, string wins) + { + this.addLog("setNetwork", "option: " + option + "; ip: " + ip + "; subnet: " + subnet + "; gateway: " + gateway + "; adapter: " + adapter + "; name: " + name + "; dns: " + dns + "; wins: " + wins + ";", LogLevel.Debug); + bool status = false; + try + { + if (option == netsetterType.Ip) + { + this.addLog("ServiceController.NetworkSetIp", "ip: "+ip+"; subnet: "+subnet+"; gateway: "+gateway+"; adapter: "+adapter+";", LogLevel.Debug); + status = this.serviceController.NetworkSetIp(ip, subnet, gateway, adapter); + } + if (option == netsetterType.Auto) + { + this.addLog("ServiceController.NetworkSetAuto", "adapter: "+adapter+";", LogLevel.Debug); + status = this.serviceController.NetworkSetAuto(adapter); + } + if (option == netsetterType.IpAuto) + { + this.addLog("ServiceController.NetworkSetIpAuto", "adapter: "+adapter+";", LogLevel.Debug); + status = this.serviceController.NetworkSetIpAuto(adapter); + } + if (option == netsetterType.DnsAuto) + { + this.addLog("ServiceController.NetworkSetDnsAuto", "adapter: "+adapter+";", LogLevel.Debug); + status = this.serviceController.NetworkSetDnsAuto(adapter); + } + if (option == netsetterType.WinsAuto) + { + this.addLog("ServiceController.NetworkSetWinsAuto", "adapter: "+adapter+";", LogLevel.Debug); + status = this.serviceController.NetworkSetWinsAuto(adapter); + } + if (option == netsetterType.Dns) + { + this.addLog("ServiceController.NetworkSetDNS", "dns: "+dns+"; adapter: "+adapter+";", LogLevel.Debug); + status = this.serviceController.NetworkSetDNS(dns, adapter); + } + if (option == netsetterType.Wins) + { + this.addLog("ServiceController.NetworkSetWINS", "wins: "+wins+"; adapter: "+adapter+";", LogLevel.Debug); + status = this.serviceController.NetworkSetWINS(wins, adapter); + } + } + catch (Exception e) + { + this.addLog("setNetwork", "Fehler beim Ausführen des Befehls: " + e.Message, LogLevel.Error); + throw e; + } + return status; + } + } +} diff --git a/NetMonitorUtils/Properties/AssemblyInfo.cs b/NetMonitorUtils/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..67cf7dd --- /dev/null +++ b/NetMonitorUtils/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die mit einer Assembly verknüpft sind. +[assembly: AssemblyTitle("NetMonitorUtils")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("NetMonitorUtils")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar +// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von +// COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("2c86a911-78de-4ced-a0fe-d63cd945c0ae")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern +// übernehmen, indem Sie "*" eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/NetMonitorUtils/ServiceControl.cs b/NetMonitorUtils/ServiceControl.cs new file mode 100644 index 0000000..f2586f4 --- /dev/null +++ b/NetMonitorUtils/ServiceControl.cs @@ -0,0 +1,227 @@ +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!"); + } + return this.sendMessage("SERVICE", "STATUS " + name); + } + } +} diff --git a/NetOpenVPNGUI/Resources/icon_con.ico b/NetOpenVPNGUI/Resources/icon_con.ico new file mode 100644 index 0000000..d6cbcd2 Binary files /dev/null and b/NetOpenVPNGUI/Resources/icon_con.ico differ diff --git a/bin/Debug/NetMonitorConsole.exe b/bin/Debug/NetMonitorConsole.exe new file mode 100644 index 0000000..f5653ff Binary files /dev/null and b/bin/Debug/NetMonitorConsole.exe differ diff --git a/bin/Debug/NetMonitorServer.exe b/bin/Debug/NetMonitorServer.exe new file mode 100644 index 0000000..a5a9794 Binary files /dev/null and b/bin/Debug/NetMonitorServer.exe differ diff --git a/bin/Debug/NetMonitorTray.exe b/bin/Debug/NetMonitorTray.exe new file mode 100644 index 0000000..baed8bb Binary files /dev/null and b/bin/Debug/NetMonitorTray.exe differ diff --git a/bin/Debug/NetMonitorUtils.dll b/bin/Debug/NetMonitorUtils.dll new file mode 100644 index 0000000..9256284 Binary files /dev/null and b/bin/Debug/NetMonitorUtils.dll differ diff --git a/bin/Debug/Utils.dll b/bin/Debug/Utils.dll new file mode 100644 index 0000000..c0c3583 Binary files /dev/null and b/bin/Debug/Utils.dll differ diff --git a/bin/Debug/config.ini b/bin/Debug/config.ini new file mode 100644 index 0000000..598f128 --- /dev/null +++ b/bin/Debug/config.ini @@ -0,0 +1,4 @@ +[ports] +server=54323 +client_from=54330 +client_to=54340 diff --git a/bin/Debug/network.ini b/bin/Debug/network.ini new file mode 100644 index 0000000..fc415b3 --- /dev/null +++ b/bin/Debug/network.ini @@ -0,0 +1,6 @@ +[network1] +Name=Home +Ip=auto +Dns=auto +Wins=auto +Adapter=LAN-Verbindung \ No newline at end of file