using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using NetMonitorUtils; using BlubbFish.Utils; namespace NetMonitorTray.Controller { public class Tray : OwnController { private static View.Tray tray; private static OpenVpnSetter vpn; private static NetworkSetter network; private static Controller.Window window; /// /// Tray Controller /// public Tray() { vpn = new OpenVpnSetter("vpn.ini", "config.ini"); network = new NetworkSetter("network.ini", "config.ini"); } /// /// Init! /// override protected void init() { tray = new View.Tray(); window = new Controller.Window(); tray.setWorkingIcon(); try { tray.Model.connectedStatus = false; List vpns = tray.Model.getVpnList(); tray.Model.VpnSelected = vpn.getStatus(vpns); List adapter = tray.Model.getAdapterList(); tray.Model.NetworkSelected = network.getNetwork(adapter); } catch(Exception e) { tray.showError(e.Message); tray.Model.connectedStatus = false; } } /// /// User klickt auf den Eintrag Beenden /// /// /// internal static void Click_Quit(object sender, EventArgs e) { tray.Dispose(); //Application.ExitThread(); Application.Exit(); } /// /// User klick auf einen Eintrag Service Starten/Stoppen /// /// /// internal static void Click_Vpn(object sender, EventArgs e) { tray.setWorkingIcon(); string[] conf = ((ToolStripMenuItem)sender).Name.Split('|'); switch(tray.Model.VpnSelected[conf[1]]) { case "disconnected": case "unknown": try { vpn.setStart(conf[1]); for(int i = 0; i < 10; i++) { string status = vpn.getStatus(conf[1]); if(status.StartsWith("[connected|")) { tray.Model.VpnSelected[conf[1]] = "connected"; status = status.Substring(0, status.Length - 1).Substring(1); string[] ip = status.Split('|'); tray.setVpn(true, "Verbunden am: " + ip[5] + "\nmit IP: " + ip[1] + "\nConfig: " + conf[1]); break; } System.Threading.Thread.Sleep(3000); } } catch(Exception ex) { tray.Model.connectedStatus = false; tray.showError("Fehler beim starten von Openvpn mit " + conf[1] + ": " + ex.Message); } break; case "connected": try { vpn.setStop(conf[1]); for(int i = 0; i < 10; i++) { string status = vpn.getStatus(conf[1]); if(status == "[disconnected]") { tray.Model.VpnSelected[conf[1]] = "disconnected"; tray.setVpn(false, "Config: " + conf[1]); break; } System.Threading.Thread.Sleep(1000); } } catch(Exception ex) { tray.Model.connectedStatus = false; tray.showError("Fehler beim beenden von Openvpn mit " + conf + ": " + ex.Message); } break; } } /// /// User klickt Doppelt auf das TrayIcon /// /// /// internal static void Click_Tray(object sender, EventArgs e) { Click_Open(sender, e); } /// /// User klickt auf den Tray-Ballon /// /// /// internal static void Click_Ballon(object sender, EventArgs e) { throw new NotImplementedException(); } /// /// User klickt auf einen Eintrag Netzwerk /// /// /// internal static void Click_Network(object sender, EventArgs e) { tray.setWorkingIcon(); try { network.setNetwork(((ToolStripMenuItem)sender).Name); string n = ((ToolStripMenuItem)sender).Name; tray.Model.NetworkSelected[n.Split('|')[0]] = n.Split('|')[1]; } catch(Exception ex) { tray.showError("Fehler beim setzen des Netzwerks: " + ex.Message); } } /// /// User klickt auf den Eintrag Öffnen /// /// /// internal static void Click_Open(object sender, EventArgs e) { window.execute(); } } }