diff --git a/NetMonitorServer/NetMonitorServer.csproj b/NetMonitorServer/NetMonitorServer.csproj index 9c09b61..efe4c71 100644 --- a/NetMonitorServer/NetMonitorServer.csproj +++ b/NetMonitorServer/NetMonitorServer.csproj @@ -28,7 +28,7 @@ x86 pdbonly true - bin\Release\ + E:\Programme\NetMonitor\ TRACE prompt 4 diff --git a/NetMonitorServer/NetMonitorServer.csproj.user b/NetMonitorServer/NetMonitorServer.csproj.user index 63df725..f239690 100644 --- a/NetMonitorServer/NetMonitorServer.csproj.user +++ b/NetMonitorServer/NetMonitorServer.csproj.user @@ -3,4 +3,7 @@ -r + + -r + \ No newline at end of file diff --git a/NetMonitorServer/SetService.cs b/NetMonitorServer/SetService.cs index 7c3bf48..4392499 100644 --- a/NetMonitorServer/SetService.cs +++ b/NetMonitorServer/SetService.cs @@ -31,15 +31,19 @@ namespace NetMonitorServer if (par.Length != 3) return; this.serviceController.ServiceName = par[2]; - this.serviceController.Start(); - this.serviceController.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running,new TimeSpan(0,0,10)); - String ret; + try + { + this.serviceController.Start(); + this.serviceController.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running, new TimeSpan(0, 0, 30)); + } + catch (Exception) + { + this.sendStr("false"); + } if (this.serviceController.Status == System.ServiceProcess.ServiceControllerStatus.Running) - ret = "true"; + this.sendStr("true"); else - ret = "false"; - byte[] answ = Encoding.UTF8.GetBytes(ret); - net_send.Send(answ, answ.Length, net_send_port); + this.sendStr("false"); } private void setServiceStop(string data) @@ -48,15 +52,19 @@ namespace NetMonitorServer if (par.Length != 3) return; this.serviceController.ServiceName = par[2]; - this.serviceController.Stop(); - this.serviceController.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 10)); - String ret; + try + { + this.serviceController.Stop(); + this.serviceController.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 30)); + } + catch (Exception) + { + this.sendStr("false"); + } if (this.serviceController.Status == System.ServiceProcess.ServiceControllerStatus.Stopped) - ret = "true"; + this.sendStr("true"); else - ret = "false"; - byte[] answ = Encoding.UTF8.GetBytes(ret); - net_send.Send(answ, answ.Length, net_send_port); + this.sendStr("false"); } private void getServiceStatus(string data) @@ -64,13 +72,23 @@ namespace NetMonitorServer String[] par = data.Split(' '); if (par.Length != 3) return; - this.serviceController.ServiceName = par[2]; - String ret; - if (this.serviceController.Status == System.ServiceProcess.ServiceControllerStatus.Running) - ret = "true"; - else - ret = "false"; - byte[] answ = Encoding.UTF8.GetBytes(ret); + try + { + this.serviceController.ServiceName = par[2]; + if (this.serviceController.Status == System.ServiceProcess.ServiceControllerStatus.Running) + this.sendStr("true"); + else + this.sendStr("false"); + } + catch (Exception) + { + this.sendStr("false"); + } + } + + private void sendStr(string p) + { + byte[] answ = Encoding.UTF8.GetBytes(p); net_send.Send(answ, answ.Length, net_send_port); } } diff --git a/NetOpenVPNGUI/Form1.Designer.cs b/NetOpenVPNGUI/Form1.Designer.cs new file mode 100644 index 0000000..5136dc4 --- /dev/null +++ b/NetOpenVPNGUI/Form1.Designer.cs @@ -0,0 +1,175 @@ +namespace NetOpenVpnGui +{ + partial class Form1 + { + /// + /// Erforderliche Designervariable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Verwendete Ressourcen bereinigen. + /// + /// True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Vom Windows Form-Designer generierter Code + + /// + /// Erforderliche Methode für die Designerunterstützung. + /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden. + /// + private void InitializeComponent() + { + this.components = new System.ComponentModel.Container(); + this.trayIcon = new System.Windows.Forms.NotifyIcon(this.components); + this.trayMenu = new System.Windows.Forms.ContextMenuStrip(this.components); + this.trayMenu_open = new System.Windows.Forms.ToolStripMenuItem(); + this.trayMenu_start = new System.Windows.Forms.ToolStripMenuItem(); + this.trayMenu_stop = new System.Windows.Forms.ToolStripMenuItem(); + this.trayMenu_close = new System.Windows.Forms.ToolStripMenuItem(); + this.MainMenu = new System.Windows.Forms.MenuStrip(); + this.MainMenu_file = new System.Windows.Forms.ToolStripMenuItem(); + this.Statuslog = new System.Windows.Forms.ListBox(); + this.servicename = new System.Windows.Forms.TextBox(); + this.label1 = new System.Windows.Forms.Label(); + this.trayMenu.SuspendLayout(); + this.MainMenu.SuspendLayout(); + this.SuspendLayout(); + // + // trayIcon + // + this.trayIcon.ContextMenuStrip = this.trayMenu; + this.trayIcon.Text = "OpenVPN-GUI"; + this.trayIcon.Visible = true; + this.trayIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.trayIcon_MouseDoubleClick); + // + // trayMenu + // + this.trayMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.trayMenu_open, + this.trayMenu_start, + this.trayMenu_stop, + this.trayMenu_close}); + this.trayMenu.Name = "trayMenu"; + this.trayMenu.Size = new System.Drawing.Size(126, 92); + // + // trayMenu_open + // + this.trayMenu_open.Name = "trayMenu_open"; + this.trayMenu_open.Size = new System.Drawing.Size(125, 22); + this.trayMenu_open.Text = "Öffnen"; + this.trayMenu_open.Click += new System.EventHandler(this.trayMenu_open_Click); + // + // trayMenu_start + // + this.trayMenu_start.Name = "trayMenu_start"; + this.trayMenu_start.Size = new System.Drawing.Size(125, 22); + this.trayMenu_start.Text = "Start"; + this.trayMenu_start.Click += new System.EventHandler(this.trayMenu_start_Click); + // + // trayMenu_stop + // + this.trayMenu_stop.Name = "trayMenu_stop"; + this.trayMenu_stop.Size = new System.Drawing.Size(125, 22); + this.trayMenu_stop.Text = "Stop"; + this.trayMenu_stop.Click += new System.EventHandler(this.trayMenu_stop_Click); + // + // trayMenu_close + // + this.trayMenu_close.Name = "trayMenu_close"; + this.trayMenu_close.Size = new System.Drawing.Size(125, 22); + this.trayMenu_close.Text = "Schließen"; + this.trayMenu_close.Click += new System.EventHandler(this.trayMenu_close_Click); + // + // MainMenu + // + this.MainMenu.ContextMenuStrip = this.trayMenu; + this.MainMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.MainMenu_file}); + this.MainMenu.Location = new System.Drawing.Point(0, 0); + this.MainMenu.Name = "MainMenu"; + this.MainMenu.Size = new System.Drawing.Size(284, 24); + this.MainMenu.TabIndex = 1; + this.MainMenu.Text = "MainMenu"; + // + // MainMenu_file + // + this.MainMenu_file.DropDown = this.trayMenu; + this.MainMenu_file.Name = "MainMenu_file"; + this.MainMenu_file.Size = new System.Drawing.Size(46, 20); + this.MainMenu_file.Text = "Datei"; + // + // Statuslog + // + this.Statuslog.FormattingEnabled = true; + this.Statuslog.Location = new System.Drawing.Point(13, 28); + this.Statuslog.Name = "Statuslog"; + this.Statuslog.Size = new System.Drawing.Size(259, 199); + this.Statuslog.TabIndex = 2; + // + // servicename + // + this.servicename.Location = new System.Drawing.Point(90, 234); + this.servicename.Name = "servicename"; + this.servicename.Size = new System.Drawing.Size(182, 20); + this.servicename.TabIndex = 3; + this.servicename.Text = "OpenVPNService"; + this.servicename.TextChanged += new System.EventHandler(servicename_TextChanged); + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(13, 237); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(71, 13); + this.label1.TabIndex = 4; + this.label1.Text = "Dienst Name:"; + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(284, 262); + this.Controls.Add(this.label1); + this.Controls.Add(this.servicename); + this.Controls.Add(this.Statuslog); + this.Controls.Add(this.MainMenu); + this.MaximizeBox = false; + this.Name = "Form1"; + this.ShowInTaskbar = false; + this.Text = "OpenVPN-GUI"; + this.WindowState = System.Windows.Forms.FormWindowState.Minimized; + this.Resize += new System.EventHandler(this.EventHandler_Resize); + this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Application_Closed); + this.trayMenu.ResumeLayout(false); + this.MainMenu.ResumeLayout(false); + this.MainMenu.PerformLayout(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.NotifyIcon trayIcon; + private System.Windows.Forms.ContextMenuStrip trayMenu; + private System.Windows.Forms.ToolStripMenuItem trayMenu_open; + private System.Windows.Forms.ToolStripMenuItem trayMenu_start; + private System.Windows.Forms.ToolStripMenuItem trayMenu_stop; + private System.Windows.Forms.ToolStripMenuItem trayMenu_close; + private System.Windows.Forms.MenuStrip MainMenu; + private System.Windows.Forms.ToolStripMenuItem MainMenu_file; + private System.Windows.Forms.ListBox Statuslog; + private System.Windows.Forms.TextBox servicename; + private System.Windows.Forms.Label label1; + } +} + diff --git a/NetOpenVPNGUI/Form1.cs b/NetOpenVPNGUI/Form1.cs new file mode 100644 index 0000000..052a4f9 --- /dev/null +++ b/NetOpenVPNGUI/Form1.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Windows.Forms; + +namespace NetOpenVpnGui +{ + public partial class Form1 : Form + { + private Runner sr; + public Form1() + { + InitializeComponent(); + sr = new Runner(this.servicename.Text); + this.setIcon(); + } + private void EventHandler_Resize(object sender, System.EventArgs e) + { + if (this.WindowState == FormWindowState.Minimized) + { + this.ShowInTaskbar = false; + this.WindowState = FormWindowState.Minimized; + } + } + private void setIcon() + { + if (sr.check_service()) + { + this.trayMenu_isRunning(true); + this.trayIcon.Icon = Resources.icon_ok; + this.Icon = Resources.icon_ok; + } + else + { + this.trayMenu_isRunning(false); + this.trayIcon.Icon = Resources.icon_no; + this.Icon = Resources.icon_no; + } + } + + private void trayMenu_open_Click(object sender, EventArgs e) + { + this.WindowState = FormWindowState.Normal; + this.ShowInTaskbar = true; + } + + private void trayMenu_start_Click(object sender, EventArgs e) + { + if (sr.run_service()) + { + this.showPopup("OpenVPN-GUI", "Service succesful started", ToolTipIcon.Info); + this.setIcon(); + } + else + { + this.showPopup("OpenVPN-GUI", "Error while starting Service", ToolTipIcon.Error); + this.setIcon(); + } + this.trayIcon.ShowBalloonTip(100); + } + + private void trayMenu_isRunning(bool p) + { + this.trayMenu_start.Enabled = !p; + this.trayMenu_stop.Enabled = p; + } + + private void trayMenu_stop_Click(object sender, EventArgs e) + { + if (sr.stop_service()) + { + this.showPopup("OpenVPN-GUI", "Service succesful stopped", ToolTipIcon.Info); + this.setIcon(); + } + else + { + this.showPopup("OpenVPN-GUI", "Error while stopping Service", ToolTipIcon.Error); + this.setIcon(); + } + this.trayIcon.ShowBalloonTip(100); + } + + private void trayMenu_close_Click(object sender, EventArgs e) + { + sr.stop_service(); + Application.Exit(); + } + private void Application_Closed(object sender, FormClosedEventArgs e) + { + this.trayMenu_close_Click(sender, (EventArgs)e); + } + + private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e) + { + if (this.trayMenu_start.Enabled) + this.trayMenu_start_Click(sender, e); + else if (this.trayMenu_stop.Enabled) + this.trayMenu_stop_Click(sender, e); + } + + private void servicename_TextChanged(object sender, EventArgs e) + { + this.sr.service = this.servicename.Text; + this.setIcon(); + } + private void showPopup(string title, string text, ToolTipIcon toolTipIcon) + { + this.Statuslog.Items.Add(text); + this.trayIcon.BalloonTipIcon = toolTipIcon; + this.trayIcon.BalloonTipText = text; + this.trayIcon.BalloonTipTitle = title; + } + } +} diff --git a/NetOpenVPNGUI/Form1.resx b/NetOpenVPNGUI/Form1.resx new file mode 100644 index 0000000..07e02cd --- /dev/null +++ b/NetOpenVPNGUI/Form1.resx @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + 17, 17 + + + 112, 17 + + + 216, 17 + + \ No newline at end of file diff --git a/NetOpenVPNGUI/NetOpenVPNGUI.csproj b/NetOpenVPNGUI/NetOpenVPNGUI.csproj index cb86d42..0647749 100644 --- a/NetOpenVPNGUI/NetOpenVPNGUI.csproj +++ b/NetOpenVPNGUI/NetOpenVPNGUI.csproj @@ -28,7 +28,7 @@ x86 pdbonly true - bin\Release\ + E:\Programme\NetMonitor\ TRACE prompt 4 @@ -46,6 +46,12 @@ + + Form + + + Form1.cs + Resources.resx True @@ -54,6 +60,9 @@ + + Form1.cs + ResXFileCodeGenerator Resources.Designer.cs diff --git a/NetOpenVPNGUI/Program.cs b/NetOpenVPNGUI/Program.cs index af5947d..ac2264e 100644 --- a/NetOpenVPNGUI/Program.cs +++ b/NetOpenVPNGUI/Program.cs @@ -1,10 +1,21 @@ -namespace NetOpenVPNGUI +using System; +using System.Collections.Generic; +using System.Linq; +using System.Windows.Forms; + +namespace NetOpenVpnGui { static class Program { + /// + /// Der Haupteinstiegspunkt für die Anwendung. + /// + [STAThread] static void Main() { - new Runner(); + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); } } } diff --git a/NetOpenVPNGUI/Resources.Designer.cs b/NetOpenVPNGUI/Resources.Designer.cs index 0fca8df..1d7d07a 100644 --- a/NetOpenVPNGUI/Resources.Designer.cs +++ b/NetOpenVPNGUI/Resources.Designer.cs @@ -8,7 +8,7 @@ // //------------------------------------------------------------------------------ -namespace NetOpenVPNGUI { +namespace NetOpenVpnGui { using System; diff --git a/NetOpenVPNGUI/Runner.cs b/NetOpenVPNGUI/Runner.cs index b0b8ecc..716c4d1 100644 --- a/NetOpenVPNGUI/Runner.cs +++ b/NetOpenVPNGUI/Runner.cs @@ -6,81 +6,53 @@ using System.Net.Sockets; using System.Net; using System.Windows.Forms; -namespace NetOpenVPNGUI +namespace NetOpenVpnGui { class Runner { - private NotifyIcon trayIcon; private UdpClient net_send; private IPEndPoint net_send_port; private UdpClient net_gets; private IPEndPoint net_gets_port; - public Runner() - { - this.trayIcon = new System.Windows.Forms.NotifyIcon(); - this.start(); - this.draw_icon(false); - if (!this.check_service("Apache2.2")) - this.run_service("Apache2.2"); - else - this.draw_icon(true); - this.wait(); - } - - private void wait() - { - while (true) - { - System.Threading.Thread.Sleep(100); - } - } - - private void draw_icon(bool p) - { - this.trayIcon.Visible = true; - if(p) - this.trayIcon.Icon = Resources.icon_ok; - else - this.trayIcon.Icon = Resources.icon_no; - } - - private void run_service(string p) - { - byte[] data = Encoding.UTF8.GetBytes("service start " + p); - this.net_send.Send(data, data.Length, this.net_send_port); - String ret = Encoding.UTF8.GetString(net_gets.Receive(ref net_gets_port)); - this.trayIcon.BalloonTipIcon = ToolTipIcon.Info; - if (Boolean.Parse(ret)) - { - this.trayIcon.BalloonTipText = "Service " + p + " Started"; - this.draw_icon(true); - } - else - { - this.trayIcon.BalloonTipText = "Service " + p + " NOT Started"; - this.draw_icon(false); - } - - this.trayIcon.ShowBalloonTip(200); - - } - - private bool check_service(string p) - { - byte[] data = Encoding.UTF8.GetBytes("service status " + p); - this.net_send.Send(data, data.Length, this.net_send_port); - int ret = int.Parse(Encoding.UTF8.GetString(net_gets.Receive(ref net_gets_port))); - if (ret == 1) - return true; - return false; - } - - private void start() + public Runner(String p) { + this.service = p; 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); } + + public bool run_service() + { + byte[] data = Encoding.UTF8.GetBytes("service start " + this.service); + this.net_send.Send(data, data.Length, this.net_send_port); + bool ret = Boolean.Parse(Encoding.UTF8.GetString(net_gets.Receive(ref net_gets_port))); + if (ret) + return true; + return false; + } + + public bool stop_service() + { + byte[] data = Encoding.UTF8.GetBytes("service stop " + this.service); + this.net_send.Send(data, data.Length, this.net_send_port); + bool ret = Boolean.Parse(Encoding.UTF8.GetString(net_gets.Receive(ref net_gets_port))); + if (ret) + return true; + return false; + } + + public bool check_service() + { + byte[] data = Encoding.UTF8.GetBytes("service status " + this.service); + this.net_send.Send(data, data.Length, this.net_send_port); + bool ret = Boolean.Parse(Encoding.UTF8.GetString(net_gets.Receive(ref net_gets_port))); + if (ret) + return true; + return false; + } + + public string service { get; set; } } }