using System; using System.Net.NetworkInformation; using System.Threading; using Gtk; using MelonVPNCore; public partial class MainWindow : Window { private Label statusLabel; private Button startBtn; private Button stopBtn; private Button refreshBtn; private Button viewClientsBtn; private Thread startupCheckThread; private ThreadWrapper wrapper; public MainWindow() : base(WindowType.Toplevel) { Build(); Title = "Melon VPN"; SetSizeRequest(300, 300); SetDefaultSize(300, 300); SetPosition(WindowPosition.Center); Resizable = false; TypeHint = Gdk.WindowTypeHint.Normal; Present(); statusLabel = new Label { Visible = true, Text = "Loading..." }; fixed1.Put(statusLabel, 10, 10); startBtn = new Button { Visible = true, Label = "Start" }; startBtn.SetSizeRequest(80, 30); startBtn.Clicked += OnStartClicked; fixed1.Put(startBtn, 10, 40); stopBtn = new Button { Visible = true, Label = "Stop" }; stopBtn.SetSizeRequest(80, 30); stopBtn.Clicked += OnStopClicked; fixed1.Put(stopBtn, 100, 40); refreshBtn = new Button { Visible = true, Label = "Refresh" }; refreshBtn.SetSizeRequest(80, 30); refreshBtn.Clicked += OnRefreshClicked; fixed1.Put(refreshBtn, 190, 40); viewClientsBtn = new Button { Visible = true, Label = "View Clients" }; viewClientsBtn.SetSizeRequest(100, 30); viewClientsBtn.Clicked += OnViewClientsClicked; fixed1.Put(viewClientsBtn, 10, 80); GUISocketServer.Receive += delegate (object sender, ClientResponseState s) { Console.WriteLine(s); Application.Invoke(delegate { UpdateStatus(s); }); }; GUISocketServer.ClientListUpdate += delegate (object sender, ConnectedClient[] clients) { Console.WriteLine(clients.Length); foreach (ConnectedClient c in clients) { Console.WriteLine(c.name + "--" + c.ip); } }; wrapper = new ThreadWrapper(GUISocketServer.StartServer); startupCheckThread = new Thread(StartupCheckMethod) { IsBackground = true }; startupCheckThread.Start(); } string GetVpnInternalIp() { if (NetworkInterface.GetIsNetworkAvailable()) { foreach (NetworkInterface f in NetworkInterface.GetAllNetworkInterfaces()) { IPInterfaceProperties p = f.GetIPProperties(); IPAddressInformationCollection addressesColl = p.AnycastAddresses; foreach (IPAddressInformation ip in addressesColl) { if (ip.Address.ToString().StartsWith("10.137.248.", StringComparison.CurrentCulture)) { return ip.Address.ToString(); } } } } return ""; } void StartupCheckMethod() { ClientResponseState s = Client.SendDataMessage(DataMessage.Status); Console.WriteLine(s); Application.Invoke(delegate { UpdateStatus(s); }); } void Refresh() { ClientResponseState s = Client.SendDataMessage(DataMessage.Status); switch (s) { case ClientResponseState.Error: case ClientResponseState.ServerError: UpdateStatus(s); MessageDialog md = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Ok, "Error stopping VPN. Is the daemon running?"); md.Run(); md.Destroy(); break; case ClientResponseState.Online: case ClientResponseState.Offline: UpdateStatus(s); break; } } void UpdateStatus(ClientResponseState s) { switch (s) { case ClientResponseState.Error: statusLabel.Text = "Client Error"; break; case ClientResponseState.ServerError: statusLabel.Text = "Server Error"; break; case ClientResponseState.Blank: statusLabel.Text = "No reply"; break; case ClientResponseState.Online: statusLabel.Text = "Online - " + GetVpnInternalIp(); break; case ClientResponseState.Offline: statusLabel.Text = "Offline"; break; default: statusLabel.Text = "Unknown"; break; } } protected void OnDeleteEvent(object sender, DeleteEventArgs a) { if (wrapper != null) { wrapper.Kill(); wrapper.Join(); } Application.Quit(); a.RetVal = true; } void OnStartClicked(object sender, EventArgs e) { ClientResponseState s = Client.SendDataMessage(DataMessage.Start); switch (s) { case ClientResponseState.Error: case ClientResponseState.ServerError: case ClientResponseState.Offline: UpdateStatus(s); MessageDialog md = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Ok, "Error starting VPN. Is the daemon running?"); md.Run(); md.Destroy(); break; } } void OnStopClicked(object sender, EventArgs e) { ClientResponseState s = Client.SendDataMessage(DataMessage.Stop); switch (s) { case ClientResponseState.Error: case ClientResponseState.ServerError: case ClientResponseState.Online: UpdateStatus(s); MessageDialog md = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Ok, "Error stopping VPN. Is the daemon running?"); md.Run(); md.Destroy(); break; } } void OnRefreshClicked(object sender, EventArgs e) { Refresh(); } void OnViewClientsClicked(object sender, EventArgs e) { Refresh(); } }