melonvpn-original-cs/MelonVPNClient/MainWindow.cs

275 lines
8.5 KiB
C#
Raw Normal View History

2020-09-22 23:10:16 +01:00
using System;
2020-09-23 20:22:51 +01:00
using System.IO;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Threading;
using Gtk;
using MelonVPNCore;
public partial class MainWindow : Window
2020-09-22 23:10:16 +01:00
{
private Label statusLabel;
private Button startBtn;
private Button stopBtn;
private Button refreshBtn;
2020-09-23 20:22:51 +01:00
private ScrolledWindow clientsListScroller;
private TextView clientsListText;
2020-09-22 23:10:16 +01:00
private Thread startupCheckThread;
private ThreadWrapper wrapper;
2020-09-23 20:22:51 +01:00
private StatusIcon statusIcon;
public static readonly string MelonIconImg = "MiniMelonVPNIcon.png";
public static readonly string MelonOnlineImg = "MiniMelonVPNOnline.png";
public MainWindow() : base(WindowType.Toplevel)
{
2020-09-22 23:10:16 +01:00
Build();
Title = "Melon VPN";
2020-09-23 20:22:51 +01:00
UpdateIcon(MelonIconImg);
2020-09-22 23:10:16 +01:00
SetSizeRequest(300, 300);
SetDefaultSize(300, 300);
2020-09-23 20:22:51 +01:00
SetPosition(WindowPosition.CenterAlways);
2020-09-22 23:10:16 +01:00
Resizable = false;
TypeHint = Gdk.WindowTypeHint.Normal;
Present();
2020-09-23 20:22:51 +01:00
statusLabel = new Label
{
2020-09-22 23:10:16 +01:00
Visible = true,
2020-09-23 20:22:51 +01:00
Text = "Loading..."
2020-09-22 23:10:16 +01:00
};
2020-09-23 20:22:51 +01:00
fixed1.Put(statusLabel, 10, 10);
startBtn = new Button
{
Visible = true,
Label = "Start"
2020-09-22 23:10:16 +01:00
};
startBtn.SetSizeRequest(80, 30);
startBtn.Clicked += OnStartClicked;
fixed1.Put(startBtn, 10, 40);
2020-09-23 20:22:51 +01:00
stopBtn = new Button
{
Visible = true,
Label = "Stop"
2020-09-22 23:10:16 +01:00
};
stopBtn.SetSizeRequest(80, 30);
stopBtn.Clicked += OnStopClicked;
fixed1.Put(stopBtn, 100, 40);
2020-09-23 20:22:51 +01:00
refreshBtn = new Button
{
2020-09-22 23:10:16 +01:00
Visible = true,
2020-09-23 20:22:51 +01:00
Label = "Refresh"
2020-09-22 23:10:16 +01:00
};
refreshBtn.SetSizeRequest(80, 30);
refreshBtn.Clicked += OnRefreshClicked;
fixed1.Put(refreshBtn, 190, 40);
2020-09-23 20:22:51 +01:00
clientsListText = new TextView
2020-09-22 23:10:16 +01:00
{
Visible = true,
2020-09-23 20:22:51 +01:00
Editable = false
};
clientsListScroller = new ScrolledWindow
{
Visible = true
2020-09-22 23:10:16 +01:00
};
2020-09-23 20:22:51 +01:00
clientsListScroller.SetSizeRequest(280, 200);
clientsListScroller.Add(clientsListText);
fixed1.Put(clientsListScroller, 10, 80);
2020-09-22 23:10:16 +01:00
2020-09-23 20:22:51 +01:00
GUISocketServer.Receive += delegate (object sender, ClientResponseState s)
2020-09-22 23:10:16 +01:00
{
Console.WriteLine(s);
2020-09-23 20:22:51 +01:00
Application.Invoke(delegate
{
UpdateStatus(s);
2020-09-22 23:10:16 +01:00
});
};
2020-09-23 20:22:51 +01:00
GUISocketServer.ClientListUpdate += delegate (object sender, ConnectedClient[] clients)
{
2020-09-22 23:10:16 +01:00
Console.WriteLine(clients.Length);
2020-09-23 20:22:51 +01:00
string[][] a = new string[clients.Length][];
for (int i = 0; i < clients.Length; i++) a[i] = new string[] { clients[i].name, clients[i].ip };
clientsListText.Buffer.Text = GenerateTable(a);
};
2020-09-22 23:10:16 +01:00
wrapper = new ThreadWrapper(GUISocketServer.StartServer);
2020-09-23 20:22:51 +01:00
startupCheckThread = new Thread(StartupCheckMethod)
{
2020-09-22 23:10:16 +01:00
IsBackground = true
};
2020-09-23 20:22:51 +01:00
startupCheckThread.Start();
statusIcon = new StatusIcon
{
Visible = true
};
}
void UpdateIcon(string file)
{
DirectoryInfo ExeLocation = Directory.GetParent(Assembly.GetEntryAssembly().Location);
string IconPath = System.IO.Path.Combine(ExeLocation.FullName, file);
Console.WriteLine("Trying to update icon to: " + IconPath);
if (File.Exists(IconPath))
{
Icon = new Gdk.Pixbuf(IconPath);
if (statusIcon != null) statusIcon.Icon = new Gdk.Pixbuf(IconPath);
}
}
string GenerateTable(string[][] a)
{
if (a.Length == 0) return "";
int maxlength = a[0][0].Length;
foreach (string b in a[0])
{
if (b.Length > maxlength) maxlength = b.Length;
}
string[] c = new string[a.Length];
for (int i = 0; i < a.Length; i++) c[i] = a[i][0].PadRight(maxlength, ' ') + a[i][1];
return string.Join("\n", c);
2020-09-22 23:10:16 +01:00
}
2020-09-23 20:22:51 +01:00
string GetVpnInternalIp()
{
if (NetworkInterface.GetIsNetworkAvailable())
2020-09-22 23:10:16 +01:00
{
2020-09-23 20:22:51 +01:00
foreach (NetworkInterface f in NetworkInterface.GetAllNetworkInterfaces())
{
2020-09-22 23:10:16 +01:00
IPInterfaceProperties p = f.GetIPProperties();
IPAddressInformationCollection addressesColl = p.AnycastAddresses;
2020-09-23 20:22:51 +01:00
foreach (IPAddressInformation ip in addressesColl)
2020-09-22 23:10:16 +01:00
{
2020-09-23 20:22:51 +01:00
if (ip.Address.ToString().StartsWith("10.137.248.", StringComparison.CurrentCulture))
{
return ip.Address.ToString();
}
}
}
2020-09-22 23:10:16 +01:00
}
2020-09-23 20:22:51 +01:00
return "";
2020-09-22 23:10:16 +01:00
}
2020-09-23 20:22:51 +01:00
void StartupCheckMethod()
{
2020-09-22 23:10:16 +01:00
ClientResponseState s = Client.SendDataMessage(DataMessage.Status);
Console.WriteLine(s);
2020-09-23 20:22:51 +01:00
Application.Invoke(delegate
{
UpdateStatus(s);
});
2020-09-22 23:10:16 +01:00
}
2020-09-23 20:22:51 +01:00
void Refresh()
{
2020-09-22 23:10:16 +01:00
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;
2020-09-23 20:22:51 +01:00
}
2020-09-22 23:10:16 +01:00
}
2020-09-23 20:22:51 +01:00
void UpdateStatus(ClientResponseState s)
{
2020-09-22 23:10:16 +01:00
switch (s)
{
2020-09-23 20:22:51 +01:00
case ClientResponseState.Error:
2020-09-22 23:10:16 +01:00
statusLabel.Text = "Client Error";
2020-09-23 20:22:51 +01:00
UpdateIcon(MelonIconImg);
break;
2020-09-22 23:10:16 +01:00
case ClientResponseState.ServerError:
statusLabel.Text = "Server Error";
2020-09-23 20:22:51 +01:00
UpdateIcon(MelonIconImg);
2020-09-22 23:10:16 +01:00
break;
case ClientResponseState.Blank:
statusLabel.Text = "No reply";
2020-09-23 20:22:51 +01:00
UpdateIcon(MelonIconImg);
2020-09-22 23:10:16 +01:00
break;
case ClientResponseState.Online:
statusLabel.Text = "Online - " + GetVpnInternalIp();
2020-09-23 20:22:51 +01:00
UpdateIcon(MelonOnlineImg);
2020-09-22 23:10:16 +01:00
break;
case ClientResponseState.Offline:
statusLabel.Text = "Offline";
2020-09-23 20:22:51 +01:00
UpdateIcon(MelonIconImg);
2020-09-22 23:10:16 +01:00
break;
2020-09-23 20:22:51 +01:00
}
}
protected void OnDeleteEvent(object sender, DeleteEventArgs a)
2020-09-22 23:10:16 +01:00
{
2020-09-23 20:22:51 +01:00
if (wrapper != null)
{
2020-09-22 23:10:16 +01:00
wrapper.Kill();
2020-09-23 20:22:51 +01:00
wrapper.Join();
}
if (statusIcon != null) statusIcon.Dispose();
Application.Quit();
a.RetVal = true;
}
void OnStartClicked(object sender, EventArgs e)
2020-09-22 23:10:16 +01:00
{
ClientResponseState s = Client.SendDataMessage(DataMessage.Start);
2020-09-23 20:22:51 +01:00
switch (s)
{
case ClientResponseState.Error:
case ClientResponseState.ServerError:
2020-09-22 23:10:16 +01:00
case ClientResponseState.Offline:
UpdateStatus(s);
MessageDialog md = new MessageDialog(this,
DialogFlags.DestroyWithParent, MessageType.Error,
2020-09-23 20:22:51 +01:00
ButtonsType.Ok, "Error starting VPN. Is the daemon running?");
md.Run();
2020-09-22 23:10:16 +01:00
md.Destroy();
2020-09-23 20:22:51 +01:00
break;
}
}
void OnStopClicked(object sender, EventArgs e)
2020-09-22 23:10:16 +01:00
{
ClientResponseState s = Client.SendDataMessage(DataMessage.Stop);
2020-09-23 20:22:51 +01:00
switch (s)
{
case ClientResponseState.Error:
case ClientResponseState.ServerError:
2020-09-22 23:10:16 +01:00
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();
2020-09-23 20:22:51 +01:00
break;
}
2020-09-22 23:10:16 +01:00
}
void OnRefreshClicked(object sender, EventArgs e)
{
Refresh();
}
2020-09-23 20:22:51 +01:00
void OnViewClientsClicked(object sender, EventArgs e)
2020-09-22 23:10:16 +01:00
{
2020-09-23 20:22:51 +01:00
Refresh();
}
}