Compare commits
2 Commits
master
...
old-wip-da
Author | SHA1 | Date | |
---|---|---|---|
|
12bcb4fe57 | ||
|
7701457ac0 |
@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net.NetworkInformation;
|
using System.Net.NetworkInformation;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
@ -25,8 +24,7 @@ public partial class MainWindow : Window
|
|||||||
private Button restartToggleBtn;
|
private Button restartToggleBtn;
|
||||||
private bool ConnectedToVPN;
|
private bool ConnectedToVPN;
|
||||||
private bool IsHidden;
|
private bool IsHidden;
|
||||||
private bool RestartMode;
|
private bool RestartMode = false;
|
||||||
private List<NotificationThreadMap> CurrentNotifications;
|
|
||||||
public static readonly string MelonIconImg = "MiniMelonVPNIcon.png";
|
public static readonly string MelonIconImg = "MiniMelonVPNIcon.png";
|
||||||
public static readonly string MelonOnlineImg = "MiniMelonVPNOnline.png";
|
public static readonly string MelonOnlineImg = "MiniMelonVPNOnline.png";
|
||||||
|
|
||||||
@ -35,8 +33,6 @@ public partial class MainWindow : Window
|
|||||||
|
|
||||||
Build();
|
Build();
|
||||||
|
|
||||||
CurrentNotifications = new List<NotificationThreadMap>();
|
|
||||||
|
|
||||||
Title = "Melon VPN";
|
Title = "Melon VPN";
|
||||||
UpdateIcon(MelonIconImg);
|
UpdateIcon(MelonIconImg);
|
||||||
SetSizeRequest(300, 300);
|
SetSizeRequest(300, 300);
|
||||||
@ -201,7 +197,7 @@ public partial class MainWindow : Window
|
|||||||
|
|
||||||
void StartupCheckMethod()
|
void StartupCheckMethod()
|
||||||
{
|
{
|
||||||
ClientResponseState s = Client.SendDataMessage(DataMessage.Status);
|
ClientResponseState s = GUISocketServer.SendDataMessage(DataMessage.Status);
|
||||||
Console.WriteLine(s);
|
Console.WriteLine(s);
|
||||||
Application.Invoke(delegate
|
Application.Invoke(delegate
|
||||||
{
|
{
|
||||||
@ -211,7 +207,7 @@ public partial class MainWindow : Window
|
|||||||
|
|
||||||
void Refresh()
|
void Refresh()
|
||||||
{
|
{
|
||||||
ClientResponseState s = Client.SendDataMessage(DataMessage.Status);
|
ClientResponseState s = GUISocketServer.SendDataMessage(DataMessage.Status);
|
||||||
switch (s)
|
switch (s)
|
||||||
{
|
{
|
||||||
case ClientResponseState.Error:
|
case ClientResponseState.Error:
|
||||||
@ -284,26 +280,25 @@ public partial class MainWindow : Window
|
|||||||
{
|
{
|
||||||
string msg = (current ? "Connected to" : "Disconnected from") + " the VPN";
|
string msg = (current ? "Connected to" : "Disconnected from") + " the VPN";
|
||||||
string icon = "/usr/lib/melon-vpn/MiniMelonVPN" + (current ? "Online" : "Icon") + ".png";
|
string icon = "/usr/lib/melon-vpn/MiniMelonVPN" + (current ? "Online" : "Icon") + ".png";
|
||||||
Notification notification = new Notification("Melon VPN", msg, icon);
|
Notification pop = new Notification("Melon VPN", msg, icon);
|
||||||
notification.Show();
|
pop.Show();
|
||||||
CloseNotificationAfterWait(notification, 1000);
|
CloseNotificationAfterWait(pop, 1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CloseNotificationAfterWait(Notification notification, int timeout)
|
void CloseNotificationAfterWait(Notification pop, int timeout)
|
||||||
{
|
{
|
||||||
// Close the notification after a wait
|
// Close the notification after a wait
|
||||||
// so it doesn't hang around
|
// so it doesn't hang around
|
||||||
Thread closeTimer = new Thread(() =>
|
Thread wait = new Thread(() =>
|
||||||
{
|
{
|
||||||
Thread.Sleep(timeout);
|
Thread.Sleep(timeout);
|
||||||
notification.Close();
|
pop.Close();
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
IsBackground = true
|
IsBackground = true
|
||||||
};
|
};
|
||||||
CurrentNotifications.Add(new NotificationThreadMap(notification, closeTimer));
|
wait.Start();
|
||||||
closeTimer.Start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SendToTray()
|
void SendToTray()
|
||||||
@ -314,7 +309,18 @@ public partial class MainWindow : Window
|
|||||||
Iconify();
|
Iconify();
|
||||||
SkipPagerHint = true;
|
SkipPagerHint = true;
|
||||||
SkipTaskbarHint = true;
|
SkipTaskbarHint = true;
|
||||||
Visible = false;
|
|
||||||
|
// Changing the visible property needs to be
|
||||||
|
// delayed for the window to start minimizing
|
||||||
|
Thread thread = new Thread(() =>
|
||||||
|
{
|
||||||
|
Thread.Sleep(100);
|
||||||
|
Visible = false;
|
||||||
|
})
|
||||||
|
{
|
||||||
|
IsBackground = true
|
||||||
|
};
|
||||||
|
thread.Start();
|
||||||
|
|
||||||
UpdateTrayMenu();
|
UpdateTrayMenu();
|
||||||
}
|
}
|
||||||
@ -351,9 +357,6 @@ public partial class MainWindow : Window
|
|||||||
wrapper.Join();
|
wrapper.Join();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destroy of all temporary notifications and the close timers
|
|
||||||
foreach(NotificationThreadMap map in CurrentNotifications) map.Dispose();
|
|
||||||
|
|
||||||
// Destroy the tray icon first
|
// Destroy the tray icon first
|
||||||
if (trayIcon != null) trayIcon.Dispose();
|
if (trayIcon != null) trayIcon.Dispose();
|
||||||
Application.Quit();
|
Application.Quit();
|
||||||
@ -361,7 +364,7 @@ public partial class MainWindow : Window
|
|||||||
|
|
||||||
void OnStartClicked(object sender, EventArgs e)
|
void OnStartClicked(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
ClientResponseState s = Client.SendDataMessage(DataMessage.Start);
|
ClientResponseState s = GUISocketServer.SendDataMessage(DataMessage.Start);
|
||||||
switch (s)
|
switch (s)
|
||||||
{
|
{
|
||||||
case ClientResponseState.Error:
|
case ClientResponseState.Error:
|
||||||
@ -379,7 +382,7 @@ public partial class MainWindow : Window
|
|||||||
|
|
||||||
void OnStopClicked(object sender, EventArgs e)
|
void OnStopClicked(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
ClientResponseState s = Client.SendDataMessage(DataMessage.Stop);
|
ClientResponseState s = GUISocketServer.SendDataMessage(DataMessage.Stop);
|
||||||
switch (s)
|
switch (s)
|
||||||
{
|
{
|
||||||
case ClientResponseState.Error:
|
case ClientResponseState.Error:
|
||||||
@ -414,7 +417,7 @@ public partial class MainWindow : Window
|
|||||||
void RestartToggleBtn_Clicked(object sender, EventArgs e)
|
void RestartToggleBtn_Clicked(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
|
|
||||||
ClientResponseState s = (RestartMode) ? Client.SendDataMessage(DataMessage.RestartOff) : Client.SendDataMessage(DataMessage.RestartOn);
|
ClientResponseState s = (RestartMode) ? GUISocketServer.SendDataMessage(DataMessage.RestartOff) : GUISocketServer.SendDataMessage(DataMessage.RestartOn);
|
||||||
switch (s)
|
switch (s)
|
||||||
{
|
{
|
||||||
case ClientResponseState.Error:
|
case ClientResponseState.Error:
|
||||||
|
@ -26,6 +26,15 @@
|
|||||||
<ErrorReport>prompt</ErrorReport>
|
<ErrorReport>prompt</ErrorReport>
|
||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
<PlatformTarget>x86</PlatformTarget>
|
<PlatformTarget>x86</PlatformTarget>
|
||||||
|
<CustomCommands>
|
||||||
|
<CustomCommands>
|
||||||
|
<Command>
|
||||||
|
<type>AfterBuild</type>
|
||||||
|
<command>rm appindicator3-sharp.dll</command>
|
||||||
|
<workingdir>${TargetDir}</workingdir>
|
||||||
|
</Command>
|
||||||
|
</CustomCommands>
|
||||||
|
</CustomCommands>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
@ -79,7 +88,6 @@
|
|||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="PopupMenu.cs" />
|
<Compile Include="PopupMenu.cs" />
|
||||||
<Compile Include="NotificationThreadMap.cs" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\MelonVPNCore\MelonVPNCore.csproj">
|
<ProjectReference Include="..\MelonVPNCore\MelonVPNCore.csproj">
|
||||||
@ -103,14 +111,6 @@
|
|||||||
<None Include="MelonVPNDesktopIcon.png">
|
<None Include="MelonVPNDesktopIcon.png">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
<None Include="net-libs\libnotify.net.dll" />
|
|
||||||
<None Include="net-libs\appindicator3-sharp.dll" />
|
|
||||||
<None Include="net-libs\Newtonsoft.Json.dll" />
|
|
||||||
<None Include="net-libs\libappindicator3sharpglue-12.10.0.so">
|
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
|
||||||
<Link>libappindicator3sharpglue-12.10.0.so</Link>
|
|
||||||
</None>
|
|
||||||
<None Include="net-libs\Newtonsoft.Json.xml" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||||
</Project>
|
</Project>
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 35 KiB |
@ -1,24 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Threading;
|
|
||||||
using Notify;
|
|
||||||
|
|
||||||
namespace MelonVPNClient
|
|
||||||
{
|
|
||||||
public class NotificationThreadMap : IDisposable
|
|
||||||
{
|
|
||||||
private readonly Notification notification;
|
|
||||||
private readonly Thread thread;
|
|
||||||
|
|
||||||
public NotificationThreadMap(Notification notification, Thread thread)
|
|
||||||
{
|
|
||||||
this.notification = notification;
|
|
||||||
this.thread = thread;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
thread.Abort();
|
|
||||||
notification.Close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -4,7 +4,6 @@
|
|||||||
<images-root-path>..</images-root-path>
|
<images-root-path>..</images-root-path>
|
||||||
</configuration>
|
</configuration>
|
||||||
<import>
|
<import>
|
||||||
<widget-library name="glade-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
|
|
||||||
<widget-library name="../net-libs/appindicator3-sharp.dll" />
|
<widget-library name="../net-libs/appindicator3-sharp.dll" />
|
||||||
<widget-library name="../bin/Debug/MelonVPNClient.exe" internal="true" />
|
<widget-library name="../bin/Debug/MelonVPNClient.exe" internal="true" />
|
||||||
</import>
|
</import>
|
||||||
|
Binary file not shown.
@ -7,7 +7,9 @@ namespace MelonVPNConnectedClientUpdate
|
|||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
Client.SendCustomMessage(Messages.GetClientListMessage(Console.ReadLine()));
|
GUISocketServer.oneConnect();
|
||||||
|
GUISocketServer.SendCustomMessage(Messages.GetClientListMessage(Console.ReadLine()));
|
||||||
|
GUISocketServer.oneDispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,41 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Net;
|
|
||||||
using System.Net.Sockets;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace MelonVPNCore
|
|
||||||
{
|
|
||||||
public static class Client
|
|
||||||
{
|
|
||||||
public static ClientResponseState SendDataMessage(DataMessage msg) => SendDataMessage(msg, false);
|
|
||||||
public static ClientResponseState SendDataMessage(DataMessage msg, bool IsSendingFromDaemon) => SendCustomMessage(Messages.GetMessage(msg), IsSendingFromDaemon);
|
|
||||||
public static ClientResponseState SendCustomMessage(string msg) => SendCustomMessage(msg, false);
|
|
||||||
public static ClientResponseState SendCustomMessage(string msg, bool IsSendingFromDaemon)
|
|
||||||
{
|
|
||||||
IPHostEntry host = Dns.GetHostEntry("localhost");
|
|
||||||
IPAddress ipAddress = host.AddressList[0];
|
|
||||||
IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, IsSendingFromDaemon ? 22036 : 22035);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (msg == Messages.EOF || msg == "") return ClientResponseState.Blank;
|
|
||||||
|
|
||||||
Socket client = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
|
||||||
client.Connect(remoteEndPoint);
|
|
||||||
client.ReceiveTimeout = 5000;
|
|
||||||
client.SendTimeout = 5000;
|
|
||||||
|
|
||||||
byte[] bytes = Encoding.ASCII.GetBytes(msg);
|
|
||||||
|
|
||||||
client.Send(bytes);
|
|
||||||
client.Close();
|
|
||||||
return ClientResponseState.Sent;
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Console.WriteLine(e);
|
|
||||||
return ClientResponseState.Error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
329
MelonVPNCore/DaemonMessaging.cs
Normal file
329
MelonVPNCore/DaemonMessaging.cs
Normal file
@ -0,0 +1,329 @@
|
|||||||
|
using System;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace MelonVPNCore
|
||||||
|
{
|
||||||
|
public class DaemonClient : IDisposable
|
||||||
|
{
|
||||||
|
|
||||||
|
public ClientResponseState SendDataMessage(DataMessage msg) => SendCustomMessage(Messages.GetMessage(msg));
|
||||||
|
private Socket _sock;
|
||||||
|
public static int timeout = 5000;
|
||||||
|
private Thread _recvThread;
|
||||||
|
private bool _stayConnected = false;
|
||||||
|
public event EventHandler<String> messageReceived;
|
||||||
|
public event EventHandler<Exception> connectionError;
|
||||||
|
|
||||||
|
public DaemonClient() { }
|
||||||
|
public DaemonClient(Socket sockIn)
|
||||||
|
{
|
||||||
|
if (sockIn is null) throw new ArgumentNullException(nameof(sockIn));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_sock is null)
|
||||||
|
{
|
||||||
|
IPHostEntry host = Dns.GetHostEntry("localhost");
|
||||||
|
IPAddress ipAddress = host.AddressList[0];
|
||||||
|
IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, DaemonServer.port);
|
||||||
|
_sock = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
||||||
|
_stayConnected = true;
|
||||||
|
_sock.Connect(remoteEndPoint);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_stayConnected = true;
|
||||||
|
}
|
||||||
|
_sock.ReceiveTimeout = timeout;
|
||||||
|
_sock.SendTimeout = timeout;
|
||||||
|
_recvThread = new Thread(recvThreader)
|
||||||
|
{
|
||||||
|
IsBackground = true
|
||||||
|
};
|
||||||
|
_recvThread.Start();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_stayConnected = false;
|
||||||
|
Console.WriteLine(e);
|
||||||
|
_sock = null;
|
||||||
|
if (!(_recvThread is null))
|
||||||
|
{
|
||||||
|
if (_recvThread.IsAlive) _recvThread.Abort();
|
||||||
|
_recvThread = null;
|
||||||
|
}
|
||||||
|
throw new InvalidOperationException("Daemon Client Init Failed.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClientResponseState SendCustomMessage(string msg)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (msg == Messages.EOF || msg == "") return ClientResponseState.Blank;
|
||||||
|
byte[] bytes = Encoding.ASCII.GetBytes(msg);
|
||||||
|
_sock.Send(bytes);
|
||||||
|
return ClientResponseState.Sent;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e);
|
||||||
|
connectionError?.Invoke(this, e);
|
||||||
|
return ClientResponseState.Error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void recvThreader()
|
||||||
|
{
|
||||||
|
while (_stayConnected)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string data = null;
|
||||||
|
byte[] bytes = null;
|
||||||
|
|
||||||
|
while (_stayConnected)
|
||||||
|
{
|
||||||
|
bytes = new byte[1024];
|
||||||
|
int bytesRec = _sock.Receive(bytes);
|
||||||
|
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
|
||||||
|
if (data.IndexOf(Messages.EOF, StringComparison.CurrentCulture) > -1) break;
|
||||||
|
}
|
||||||
|
messageReceived?.Invoke(this, data);
|
||||||
|
} catch (SocketException e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e);
|
||||||
|
connectionError?.Invoke(this, e);
|
||||||
|
} catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#region IDisposable Support
|
||||||
|
private bool disposedValue = false; // To detect redundant calls
|
||||||
|
|
||||||
|
protected virtual void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (!disposedValue)
|
||||||
|
{
|
||||||
|
if (disposing)
|
||||||
|
{
|
||||||
|
_stayConnected = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_sock.Shutdown(SocketShutdown.Both);
|
||||||
|
} catch (SocketException e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e);
|
||||||
|
}
|
||||||
|
if (!(_recvThread is null))
|
||||||
|
{
|
||||||
|
if (_recvThread.IsAlive) _recvThread.Abort();
|
||||||
|
_recvThread = null;
|
||||||
|
}
|
||||||
|
_sock.Close();
|
||||||
|
}
|
||||||
|
_sock = null;
|
||||||
|
_recvThread = null;
|
||||||
|
disposedValue = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This code added to correctly implement the disposable pattern.
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
|
||||||
|
Dispose(true);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DaemonServer : IDisposable
|
||||||
|
{
|
||||||
|
public static int port = 22037;
|
||||||
|
private Socket _sock;
|
||||||
|
private Thread _lThread;
|
||||||
|
private bool _stayListening = false;
|
||||||
|
public event EventHandler<DaemonClient> clientConnected;
|
||||||
|
|
||||||
|
public void start()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
IPHostEntry host = Dns.GetHostEntry("localhost");
|
||||||
|
IPAddress ipAddress = host.AddressList[0];
|
||||||
|
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
|
||||||
|
_sock = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
||||||
|
_stayListening = true;
|
||||||
|
_sock.Bind(localEndPoint);
|
||||||
|
_sock.Listen(64);
|
||||||
|
_lThread = new Thread(lThreader)
|
||||||
|
{
|
||||||
|
IsBackground = true
|
||||||
|
};
|
||||||
|
_lThread.Start();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_stayListening = false;
|
||||||
|
Console.WriteLine(e);
|
||||||
|
_sock = null;
|
||||||
|
if (!(_lThread is null))
|
||||||
|
{
|
||||||
|
if (_lThread.IsAlive) _lThread.Abort();
|
||||||
|
_lThread = null;
|
||||||
|
}
|
||||||
|
throw new InvalidOperationException("Daemon Server Init Failed.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void lThreader()
|
||||||
|
{
|
||||||
|
while (_stayListening)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
DaemonClient dc = new DaemonClient(_sock.Accept());
|
||||||
|
clientConnected?.Invoke(this,dc);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#region IDisposable Support
|
||||||
|
private bool disposedValue = false; // To detect redundant calls
|
||||||
|
|
||||||
|
protected virtual void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (!disposedValue)
|
||||||
|
{
|
||||||
|
if (disposing)
|
||||||
|
{
|
||||||
|
_stayListening = false;
|
||||||
|
if (!(_lThread is null))
|
||||||
|
{
|
||||||
|
if (_lThread.IsAlive) _lThread.Abort();
|
||||||
|
_lThread = null;
|
||||||
|
}
|
||||||
|
_sock.Close();
|
||||||
|
}
|
||||||
|
_sock = null;
|
||||||
|
_lThread = null;
|
||||||
|
disposedValue = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This code added to correctly implement the disposable pattern.
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
|
||||||
|
Dispose(true);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DaemonClientMultiplexor : IDisposable
|
||||||
|
{
|
||||||
|
public ClientResponseState SendDataMessage(DataMessage msg) => SendCustomMessage(Messages.GetMessage(msg));
|
||||||
|
private List<DaemonClient> plexed = new List<DaemonClient>();
|
||||||
|
private object slockplexed = new object();
|
||||||
|
private object slockcon = new object();
|
||||||
|
private object slockrecv = new object();
|
||||||
|
public event EventHandler<String> clientMessageReceived;
|
||||||
|
public event EventHandler<DaemonClient> clientConnectComplete;
|
||||||
|
public bool startClients = true;
|
||||||
|
public bool disconnectErroredClients = true;
|
||||||
|
|
||||||
|
public DaemonClientMultiplexor(DaemonServer dsIn)
|
||||||
|
{
|
||||||
|
if (dsIn is null) throw new ArgumentNullException(nameof(dsIn));
|
||||||
|
dsIn.clientConnected += DsIn_ClientConnected;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DsIn_ClientConnected(object sender, DaemonClient e)
|
||||||
|
{
|
||||||
|
lock (slockplexed) plexed.Add(e);
|
||||||
|
e.messageReceived += E_MessageReceived;
|
||||||
|
e.connectionError += E_ConnectionError;
|
||||||
|
if (startClients) e.start();
|
||||||
|
lock(slockcon) clientConnectComplete?.Invoke(this, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
void E_MessageReceived(object sender, string e)
|
||||||
|
{
|
||||||
|
lock(slockrecv) clientMessageReceived?.Invoke(this, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
void E_ConnectionError(object sender, Exception e)
|
||||||
|
{
|
||||||
|
if(disconnectErroredClients) ((DaemonClient)sender).Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClientResponseState SendCustomMessage(string msg)
|
||||||
|
{
|
||||||
|
var toret = ClientResponseState.Sent;
|
||||||
|
lock (slockplexed) {
|
||||||
|
foreach (DaemonClient c in plexed)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (c.SendCustomMessage(msg) != ClientResponseState.Sent) toret = ClientResponseState.Error;
|
||||||
|
} catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return toret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void disconnectAllClients()
|
||||||
|
{
|
||||||
|
lock (slockplexed) foreach (DaemonClient c in plexed) c.Dispose();
|
||||||
|
lock (slockplexed) plexed.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
#region IDisposable Support
|
||||||
|
private bool disposedValue = false; // To detect redundant calls
|
||||||
|
|
||||||
|
protected virtual void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (!disposedValue)
|
||||||
|
{
|
||||||
|
if (disposing)
|
||||||
|
{
|
||||||
|
lock(slockplexed) foreach (DaemonClient c in plexed) c.Dispose();
|
||||||
|
lock (slockplexed) plexed.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
plexed = null;
|
||||||
|
slockcon = null;
|
||||||
|
slockrecv = null;
|
||||||
|
slockplexed = null;
|
||||||
|
disposedValue = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This code added to correctly implement the disposable pattern.
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
|
||||||
|
Dispose(true);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@ -10,172 +9,161 @@ namespace MelonVPNCore
|
|||||||
{
|
{
|
||||||
public static class DaemonSocketServer
|
public static class DaemonSocketServer
|
||||||
{
|
{
|
||||||
private static Process currentVpnProcess;
|
private static Process currentVpnProcess = null;
|
||||||
private static bool shouldBeRunning;
|
private static bool shouldBeRunning = false;
|
||||||
private static bool isRestarting;
|
private static bool shouldRestart = false;
|
||||||
private const int startingTime = 3000;
|
private static bool isRestarting = false;
|
||||||
private const int restartDelay = 250;
|
private static int startingTime = 3000;
|
||||||
private static DaemonConfig config;
|
private static int restartDelay = 250;
|
||||||
public const string daemonConfigPath = "/etc/melon-vpn/daemon.cfg";
|
private static DaemonServer _srv;
|
||||||
|
private static DaemonClientMultiplexor _mlt;
|
||||||
private static void SaveConfig()
|
private static string lastClientUpdate = Messages.EOF;
|
||||||
{
|
|
||||||
File.WriteAllText(daemonConfigPath, config.ToJson());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void StartServer()
|
public static void StartServer()
|
||||||
{
|
{
|
||||||
if (File.Exists(daemonConfigPath)) config = DaemonConfig.FromJson(File.ReadAllText(daemonConfigPath));
|
_srv = new DaemonServer();
|
||||||
else config = new DaemonConfig();
|
_mlt = new DaemonClientMultiplexor(_srv);
|
||||||
|
_mlt.clientMessageReceived += clientMessageReceived;
|
||||||
|
_srv.start();
|
||||||
|
}
|
||||||
|
|
||||||
IPHostEntry host = Dns.GetHostEntry("localhost");
|
public static bool isProcessOnline(Process p)
|
||||||
IPAddress ipAddress = host.AddressList[0];
|
{
|
||||||
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 22035);
|
if (p == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (p.HasExited)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void clientMessageReceived(object sender, string data)
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
if (data.StartsWith(Messages.ClientListStartMsg, StringComparison.CurrentCulture) && shouldBeRunning)
|
||||||
listener.Bind(localEndPoint);
|
|
||||||
listener.Listen(32);
|
|
||||||
|
|
||||||
string lastClientUpdate = Messages.EOF;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
{
|
||||||
Socket handler = listener.Accept();
|
lastClientUpdate = data;
|
||||||
|
_mlt.SendCustomMessage(lastClientUpdate);
|
||||||
string data = null;
|
|
||||||
byte[] bytes = null;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
bytes = new byte[1024];
|
|
||||||
int bytesRec = handler.Receive(bytes);
|
|
||||||
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
|
|
||||||
if (data.IndexOf(Messages.EOF, StringComparison.CurrentCulture) > -1) break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.StartsWith(Messages.ClientListStartMsg, StringComparison.CurrentCulture) && shouldBeRunning)
|
|
||||||
{
|
|
||||||
lastClientUpdate = data;
|
|
||||||
Client.SendCustomMessage(lastClientUpdate, true);
|
|
||||||
}
|
|
||||||
else if (data == Messages.RestartOnMsg)
|
|
||||||
{
|
|
||||||
Console.WriteLine("Updated shouldRestart to true");
|
|
||||||
config.ShouldRestart = true;
|
|
||||||
SaveConfig();
|
|
||||||
Client.SendDataMessage(DataMessage.RestartOn, true);
|
|
||||||
}
|
|
||||||
else if (data == Messages.RestartOffMsg)
|
|
||||||
{
|
|
||||||
Console.WriteLine("Updated shouldRestart to false");
|
|
||||||
config.ShouldRestart = false;
|
|
||||||
SaveConfig();
|
|
||||||
Client.SendDataMessage(DataMessage.RestartOff, true);
|
|
||||||
}
|
|
||||||
else if (data == Messages.StatusMsg)
|
|
||||||
{
|
|
||||||
Console.WriteLine("Status requested");
|
|
||||||
if (IsProcessOnline(currentVpnProcess) || shouldBeRunning)
|
|
||||||
{
|
|
||||||
if (isRestarting)
|
|
||||||
{
|
|
||||||
Console.WriteLine("Sending response: restarting");
|
|
||||||
Client.SendDataMessage(DataMessage.Restarting, true);
|
|
||||||
Client.SendCustomMessage(lastClientUpdate, true);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Console.WriteLine("Sending response: online");
|
|
||||||
Client.SendDataMessage(DataMessage.Online, true);
|
|
||||||
Client.SendCustomMessage(lastClientUpdate, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Console.WriteLine("Sending response: offline");
|
|
||||||
Client.SendDataMessage(DataMessage.Offline, true);
|
|
||||||
Client.SendCustomMessage(Messages.ClientListEmptyMsg, true);
|
|
||||||
}
|
|
||||||
Client.SendDataMessage(config.ShouldRestart ? DataMessage.RestartOn : DataMessage.RestartOff);
|
|
||||||
}
|
|
||||||
else if (data == Messages.StartMsg)
|
|
||||||
{
|
|
||||||
if (! IsProcessOnline(currentVpnProcess))
|
|
||||||
{
|
|
||||||
shouldBeRunning = true;
|
|
||||||
Console.WriteLine("Starting VPN");
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Console.WriteLine("Starting embedded process");
|
|
||||||
Console.WriteLine("Sending starting reply");
|
|
||||||
Client.SendDataMessage(DataMessage.Starting, true);
|
|
||||||
if (StartEProcess(true))
|
|
||||||
{
|
|
||||||
Console.WriteLine("Sending online reply");
|
|
||||||
Client.SendDataMessage(DataMessage.Online, true);
|
|
||||||
currentVpnProcess.EnableRaisingEvents = true;
|
|
||||||
}
|
|
||||||
else { throw new InvalidOperationException("Client crashed!"); }
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
shouldBeRunning = false;
|
|
||||||
Console.WriteLine("There was an error. But I fixed it.");
|
|
||||||
Console.WriteLine("It looked like this: " + e);
|
|
||||||
currentVpnProcess.Dispose();
|
|
||||||
currentVpnProcess = null;
|
|
||||||
Client.SendDataMessage(DataMessage.Error, true);
|
|
||||||
Client.SendCustomMessage(Messages.ClientListEmptyMsg, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Console.WriteLine("VPN already started");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (data == Messages.StopMsg)
|
|
||||||
{
|
|
||||||
shouldBeRunning = false;
|
|
||||||
bool haderr = false;
|
|
||||||
if (IsProcessOnline(currentVpnProcess))
|
|
||||||
{
|
|
||||||
Console.WriteLine("Stopping VPN");
|
|
||||||
try
|
|
||||||
{
|
|
||||||
currentVpnProcess.EnableRaisingEvents = false;
|
|
||||||
Console.WriteLine("Stopping embedded process");
|
|
||||||
currentVpnProcess.Kill();
|
|
||||||
currentVpnProcess = null;
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Console.WriteLine("There was an error. But I fixed it.");
|
|
||||||
Console.WriteLine("It looked like this: " + e);
|
|
||||||
currentVpnProcess = null;
|
|
||||||
Client.SendDataMessage(DataMessage.Error, true);
|
|
||||||
Client.SendCustomMessage(Messages.ClientListEmptyMsg, true);
|
|
||||||
haderr = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Console.WriteLine("VPN already stopped");
|
|
||||||
}
|
|
||||||
if (!haderr)
|
|
||||||
{
|
|
||||||
Console.WriteLine("Sending offline reply");
|
|
||||||
Client.SendDataMessage(DataMessage.Offline, true);
|
|
||||||
Client.SendCustomMessage(Messages.ClientListEmptyMsg, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Client.SendDataMessage(DataMessage.Blank, true);
|
|
||||||
|
|
||||||
handler.Shutdown(SocketShutdown.Both);
|
|
||||||
handler.Close();
|
|
||||||
}
|
}
|
||||||
|
else if (data == Messages.RestartOnMsg)
|
||||||
|
{
|
||||||
|
shouldRestart = true;
|
||||||
|
_mlt.SendDataMessage(DataMessage.RestartOn);
|
||||||
|
}
|
||||||
|
else if (data == Messages.RestartOffMsg)
|
||||||
|
{
|
||||||
|
shouldRestart = false;
|
||||||
|
_mlt.SendDataMessage(DataMessage.RestartOff);
|
||||||
|
}
|
||||||
|
else if (data == Messages.StatusMsg)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Status requested");
|
||||||
|
if (isProcessOnline(currentVpnProcess) || shouldBeRunning)
|
||||||
|
{
|
||||||
|
if (isRestarting)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Sending response: restarting");
|
||||||
|
_mlt.SendDataMessage(DataMessage.Restarting);
|
||||||
|
_mlt.SendCustomMessage(lastClientUpdate);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("Sending response: online");
|
||||||
|
_mlt.SendDataMessage(DataMessage.Online);
|
||||||
|
_mlt.SendCustomMessage(lastClientUpdate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("Sending response: offline");
|
||||||
|
_mlt.SendDataMessage(DataMessage.Offline);
|
||||||
|
_mlt.SendCustomMessage(Messages.ClientListEmptyMsg);
|
||||||
|
}
|
||||||
|
_mlt.SendDataMessage((shouldRestart) ? DataMessage.RestartOn : DataMessage.RestartOff);
|
||||||
|
}
|
||||||
|
else if (data == Messages.StartMsg)
|
||||||
|
{
|
||||||
|
if (!isProcessOnline(currentVpnProcess))
|
||||||
|
{
|
||||||
|
shouldBeRunning = true;
|
||||||
|
Console.WriteLine("Starting VPN");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Console.WriteLine("Starting embedded process");
|
||||||
|
Console.WriteLine("Sending starting reply");
|
||||||
|
_mlt.SendDataMessage(DataMessage.Starting);
|
||||||
|
if (StartEProcess(true))
|
||||||
|
{
|
||||||
|
Console.WriteLine("Sending online reply");
|
||||||
|
_mlt.SendDataMessage(DataMessage.Online);
|
||||||
|
currentVpnProcess.EnableRaisingEvents = true;
|
||||||
|
}
|
||||||
|
else { throw new InvalidOperationException("Client crashed!"); }
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
shouldBeRunning = false;
|
||||||
|
Console.WriteLine("There was an error. But I fixed it.");
|
||||||
|
Console.WriteLine("It looked like this: " + e);
|
||||||
|
currentVpnProcess.Dispose();
|
||||||
|
currentVpnProcess = null;
|
||||||
|
_mlt.SendDataMessage(DataMessage.Error);
|
||||||
|
_mlt.SendCustomMessage(Messages.ClientListEmptyMsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("VPN already started");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (data == Messages.StopMsg)
|
||||||
|
{
|
||||||
|
shouldBeRunning = false;
|
||||||
|
bool haderr = false;
|
||||||
|
if (isProcessOnline(currentVpnProcess))
|
||||||
|
{
|
||||||
|
Console.WriteLine("Stopping VPN");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
currentVpnProcess.EnableRaisingEvents = false;
|
||||||
|
Console.WriteLine("Stopping embedded process");
|
||||||
|
currentVpnProcess.Kill();
|
||||||
|
currentVpnProcess = null;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.WriteLine("There was an error. But I fixed it.");
|
||||||
|
Console.WriteLine("It looked like this: " + e);
|
||||||
|
currentVpnProcess = null;
|
||||||
|
_mlt.SendDataMessage(DataMessage.Error);
|
||||||
|
_mlt.SendCustomMessage(Messages.ClientListEmptyMsg);
|
||||||
|
haderr = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("VPN already stopped");
|
||||||
|
}
|
||||||
|
if (!haderr)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Sending offline reply");
|
||||||
|
_mlt.SendDataMessage(DataMessage.Offline);
|
||||||
|
_mlt.SendCustomMessage(Messages.ClientListEmptyMsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_mlt.SendDataMessage(DataMessage.Blank);
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
@ -183,11 +171,6 @@ namespace MelonVPNCore
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool IsProcessOnline(Process p)
|
|
||||||
{
|
|
||||||
if (p == null) return false;
|
|
||||||
return !p.HasExited;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void CurrentVpnProcess_Exited(object sender, EventArgs e)
|
static void CurrentVpnProcess_Exited(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
@ -197,14 +180,14 @@ namespace MelonVPNCore
|
|||||||
Thread.Sleep(restartDelay);
|
Thread.Sleep(restartDelay);
|
||||||
bool imonline = false;
|
bool imonline = false;
|
||||||
isRestarting = true;
|
isRestarting = true;
|
||||||
while (config.ShouldRestart && shouldBeRunning)
|
while (shouldRestart && shouldBeRunning)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Sending restarting reply");
|
Console.WriteLine("Sending restarting reply");
|
||||||
Client.SendDataMessage(DataMessage.Restarting, true);
|
_mlt.SendDataMessage(DataMessage.Restarting);
|
||||||
if (StartEProcess(false))
|
if (StartEProcess(false))
|
||||||
{
|
{
|
||||||
Console.WriteLine("Sending online reply");
|
Console.WriteLine("Sending online reply");
|
||||||
Client.SendDataMessage(DataMessage.Online, true);
|
_mlt.SendDataMessage(DataMessage.Online);
|
||||||
currentVpnProcess.EnableRaisingEvents = true;
|
currentVpnProcess.EnableRaisingEvents = true;
|
||||||
imonline = true;
|
imonline = true;
|
||||||
break;
|
break;
|
||||||
@ -221,8 +204,8 @@ namespace MelonVPNCore
|
|||||||
{
|
{
|
||||||
shouldBeRunning = false;
|
shouldBeRunning = false;
|
||||||
Console.WriteLine("Sending offline reply");
|
Console.WriteLine("Sending offline reply");
|
||||||
Client.SendDataMessage(DataMessage.Offline, true);
|
_mlt.SendDataMessage(DataMessage.Offline);
|
||||||
Client.SendCustomMessage(Messages.ClientListEmptyMsg, true);
|
_mlt.SendCustomMessage(Messages.ClientListEmptyMsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,70 +2,105 @@
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace MelonVPNCore
|
namespace MelonVPNCore
|
||||||
{
|
{
|
||||||
public static class GUISocketServer
|
public static class GUISocketServer
|
||||||
{
|
{
|
||||||
|
public static ClientResponseState SendDataMessage(DataMessage msg) => SendCustomMessage(Messages.GetMessage(msg));
|
||||||
|
public static int reconnectTimeout = 2000;
|
||||||
|
public static DaemonClient client;
|
||||||
public static event EventHandler<ClientResponseState> Receive;
|
public static event EventHandler<ClientResponseState> Receive;
|
||||||
public static event EventHandler<ConnectedClient[]> ClientListUpdate;
|
public static event EventHandler<ConnectedClient[]> ClientListUpdate;
|
||||||
|
private static bool waitfcl = false;
|
||||||
|
private static bool exec = true;
|
||||||
|
|
||||||
public static void StartServer()
|
public static void StartServer()
|
||||||
{
|
{
|
||||||
IPHostEntry host = Dns.GetHostEntry("localhost");
|
|
||||||
IPAddress ipAddress = host.AddressList[0];
|
|
||||||
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 22036);
|
|
||||||
|
|
||||||
|
while (exec)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
client = new DaemonClient();
|
||||||
|
client.connectionError += Client_ConnectionError;
|
||||||
|
client.messageReceived += Client_MessageReceived;
|
||||||
|
waitfcl = true;
|
||||||
|
client.start();
|
||||||
|
while (waitfcl) Thread.Sleep(reconnectTimeout);
|
||||||
|
client.connectionError -= Client_ConnectionError;
|
||||||
|
client.messageReceived -= Client_MessageReceived;
|
||||||
|
client.Dispose();
|
||||||
|
}
|
||||||
|
catch (InvalidOperationException e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e);
|
||||||
|
}
|
||||||
|
Thread.Sleep(reconnectTimeout);
|
||||||
|
}
|
||||||
|
Console.WriteLine("GUISocketServer has reached the end.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void oneConnect()
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
client = new DaemonClient();
|
||||||
listener.Bind(localEndPoint);
|
client.messageReceived += Client_MessageReceived;
|
||||||
listener.Listen(32);
|
waitfcl = true;
|
||||||
|
client.start();
|
||||||
|
}
|
||||||
|
catch (InvalidOperationException e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
while (true)
|
public static void oneDispose()
|
||||||
|
{
|
||||||
|
client.messageReceived -= Client_MessageReceived;
|
||||||
|
client.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Client_ConnectionError(object sender, Exception e)
|
||||||
|
{
|
||||||
|
waitfcl = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Client_MessageReceived(object sender, string data)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ClientResponseState ret = ClientResponseState.None;
|
||||||
|
if (data == Messages.OnlineMsg) ret = ClientResponseState.Online;
|
||||||
|
if (data == Messages.OfflineMsg) ret = ClientResponseState.Offline;
|
||||||
|
if (data == Messages.ErrorMsg) ret = ClientResponseState.ServerError;
|
||||||
|
if (data == Messages.StartingMsg) ret = ClientResponseState.Starting;
|
||||||
|
if (data == Messages.RestartingMsg) ret = ClientResponseState.Restarting;
|
||||||
|
if (data == Messages.RestartOnMsg) ret = ClientResponseState.RestartOn;
|
||||||
|
if (data == Messages.RestartOffMsg) ret = ClientResponseState.RestartOff;
|
||||||
|
Console.WriteLine(data);
|
||||||
|
if (data.StartsWith(Messages.ClientListStartMsg, StringComparison.CurrentCulture))
|
||||||
{
|
{
|
||||||
Socket handler = listener.Accept();
|
string jsonWithEof = data.Substring(Messages.ClientListStartMsg.Length);
|
||||||
|
string jsonData = jsonWithEof.Substring(0, jsonWithEof.Length - Messages.EOF.Length);
|
||||||
string data = "";
|
Console.WriteLine("clients: " + jsonData);
|
||||||
byte[] bytes = null;
|
ConnectedClient[] clients = ClientListParser.Parse(jsonData);
|
||||||
|
ClientListUpdate?.Invoke(null, clients);
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
bytes = new byte[1024];
|
|
||||||
int bytesRec = handler.Receive(bytes);
|
|
||||||
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
|
|
||||||
if (data.IndexOf(Messages.EOF, StringComparison.CurrentCulture) > -1) break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ClientResponseState ret = ClientResponseState.None;
|
|
||||||
if (data == Messages.OnlineMsg) ret = ClientResponseState.Online;
|
|
||||||
if (data == Messages.OfflineMsg) ret = ClientResponseState.Offline;
|
|
||||||
if (data == Messages.ErrorMsg) ret = ClientResponseState.ServerError;
|
|
||||||
if (data == Messages.StartingMsg) ret = ClientResponseState.Starting;
|
|
||||||
if (data == Messages.RestartingMsg) ret = ClientResponseState.Restarting;
|
|
||||||
if (data == Messages.RestartOnMsg) ret = ClientResponseState.RestartOn;
|
|
||||||
if (data == Messages.RestartOffMsg) ret = ClientResponseState.RestartOff;
|
|
||||||
Console.WriteLine(data);
|
|
||||||
if (data.StartsWith(Messages.ClientListStartMsg, StringComparison.CurrentCulture))
|
|
||||||
{
|
|
||||||
string jsonWithEof = data.Substring(Messages.ClientListStartMsg.Length);
|
|
||||||
string jsonData = jsonWithEof.Substring(0, jsonWithEof.Length - Messages.EOF.Length);
|
|
||||||
Console.WriteLine("clients: " + jsonData);
|
|
||||||
ConnectedClient[] clients = ClientListParser.Parse(jsonData);
|
|
||||||
ClientListUpdate?.Invoke(null, clients);
|
|
||||||
}
|
|
||||||
|
|
||||||
handler.Shutdown(SocketShutdown.Both);
|
|
||||||
handler.Close();
|
|
||||||
|
|
||||||
Receive?.Invoke(null, ret);
|
|
||||||
}
|
}
|
||||||
|
Receive?.Invoke(null, ret);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Console.WriteLine(e);
|
Console.WriteLine(e);
|
||||||
}
|
}
|
||||||
Console.WriteLine("GUI socket server reached the end");
|
}
|
||||||
|
|
||||||
|
public static ClientResponseState SendCustomMessage(string msg)
|
||||||
|
{
|
||||||
|
var toret = client?.SendCustomMessage(msg);
|
||||||
|
return (toret.HasValue) ? toret.Value : ClientResponseState.Error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,80 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Globalization;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
using Newtonsoft.Json.Converters;
|
|
||||||
|
|
||||||
namespace MelonVPNCore
|
|
||||||
{
|
|
||||||
// I hate overhead but this code is just nice xD
|
|
||||||
|
|
||||||
// Base json class with type property FromJson and ToJson methods
|
|
||||||
public class JsonBase
|
|
||||||
{
|
|
||||||
[JsonProperty("type")]
|
|
||||||
public string Type { get; set; }
|
|
||||||
|
|
||||||
// Convert from json string to WSJson class
|
|
||||||
public static JsonBase FromJson(string json) => JsonConvert.DeserializeObject<JsonBase>(json, Converter.Settings);
|
|
||||||
|
|
||||||
// Convert any class into a json string
|
|
||||||
public string ToJson() => JsonConvert.SerializeObject(this, Converter.Settings);
|
|
||||||
|
|
||||||
// Converter with settings
|
|
||||||
internal static class Converter
|
|
||||||
{
|
|
||||||
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
|
|
||||||
{
|
|
||||||
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
|
|
||||||
DateParseHandling = DateParseHandling.None,
|
|
||||||
Converters =
|
|
||||||
{
|
|
||||||
new IsoDateTimeConverter{DateTimeStyles=DateTimeStyles.AssumeUniversal }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Base class for custom json with a type argument
|
|
||||||
public class JsonBase<T> : JsonBase
|
|
||||||
{
|
|
||||||
// Convert from json string to T class
|
|
||||||
public static new T FromJson(string json) => JsonConvert.DeserializeObject<T>(json, Converter.Settings);
|
|
||||||
|
|
||||||
// Used to convert json property into type long
|
|
||||||
internal class ParseStringConverter : JsonConverter
|
|
||||||
{
|
|
||||||
public override bool CanConvert(Type objectType) { return objectType == typeof(long) || objectType == typeof(long); }
|
|
||||||
|
|
||||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
|
||||||
{
|
|
||||||
if (reader.TokenType == JsonToken.Null) return null;
|
|
||||||
var value = serializer.Deserialize<string>(reader);
|
|
||||||
if (long.TryParse(value, out long l)) return l;
|
|
||||||
throw new Exception("Cannot unmarshal type long");
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
|
||||||
{
|
|
||||||
if (value == null)
|
|
||||||
{
|
|
||||||
serializer.Serialize(writer, null);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var v = (long)value;
|
|
||||||
serializer.Serialize(writer, v.ToString());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly ParseStringConverter Singleton = new ParseStringConverter();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Class for the daemon config
|
|
||||||
public class DaemonConfig : JsonBase<DaemonConfig>
|
|
||||||
{
|
|
||||||
public DaemonConfig() => Type = "DaemonConfig";
|
|
||||||
|
|
||||||
[JsonProperty("restart")]
|
|
||||||
public bool ShouldRestart { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
@ -29,13 +29,12 @@
|
|||||||
<Compile Include="DaemonSocketServer.cs" />
|
<Compile Include="DaemonSocketServer.cs" />
|
||||||
<Compile Include="ThreadWrapper.cs" />
|
<Compile Include="ThreadWrapper.cs" />
|
||||||
<Compile Include="Messages.cs" />
|
<Compile Include="Messages.cs" />
|
||||||
<Compile Include="Client.cs" />
|
|
||||||
<Compile Include="DataMessage.cs" />
|
<Compile Include="DataMessage.cs" />
|
||||||
<Compile Include="ClientResponseState.cs" />
|
<Compile Include="ClientResponseState.cs" />
|
||||||
<Compile Include="GUISocketServer.cs" />
|
<Compile Include="GUISocketServer.cs" />
|
||||||
<Compile Include="ClientListParser.cs" />
|
<Compile Include="ClientListParser.cs" />
|
||||||
<Compile Include="ConnectedClient.cs" />
|
<Compile Include="ConnectedClient.cs" />
|
||||||
<Compile Include="Json.cs" />
|
<Compile Include="DaemonMessaging.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
|
57
install
57
install
@ -1,61 +1,8 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# idiomatic parameter and option handling in bash
|
|
||||||
forceVpnInstall="no"
|
|
||||||
forceAppIndicator="no"
|
|
||||||
badoption=""
|
|
||||||
unknownargument=""
|
|
||||||
while test $# -gt 0
|
|
||||||
do
|
|
||||||
case "$1" in
|
|
||||||
--reinstall-vpn) forceVpnInstall="yes"
|
|
||||||
;;
|
|
||||||
--appindicator) forceAppIndicator="yes"
|
|
||||||
;;
|
|
||||||
--help) showHelp="yes"
|
|
||||||
;;
|
|
||||||
--*) badoption="$1"
|
|
||||||
;;
|
|
||||||
*) unknownargument="$1"
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
if [ -n "$badoption" ]; then
|
|
||||||
echo "Unknown option: $badoption"
|
|
||||||
echo "Run './install --help' for more information"
|
|
||||||
exit 1
|
|
||||||
elif [ -n "$unknownargument" ]; then
|
|
||||||
echo "Unknown argument: $unknownargument"
|
|
||||||
echo "Run './install --help' for more information"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
shift
|
|
||||||
done
|
|
||||||
|
|
||||||
firstTime="no"
|
|
||||||
firstTimePath=".git/melon-vpn-install"
|
|
||||||
if [ ! -f "$firstTimePath" ]; then
|
|
||||||
showHelp="yes"
|
|
||||||
firstTime="yes"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$showHelp" == "yes" ]; then
|
|
||||||
echo "MelonVPN installer"
|
|
||||||
echo "=================="
|
|
||||||
echo "Use './install' for a normal build"
|
|
||||||
echo "--help = display these help options"
|
|
||||||
echo "--reinstall-vpn = force reinstall simple-vpn"
|
|
||||||
echo "--appindicator = force copy appindicator dll into install directory"
|
|
||||||
if [ "$firstTime" == "yes" ]; then
|
|
||||||
echo -e "\nThis message was displayed as it is your first time using this installer"
|
|
||||||
echo "Repeat the command to install MelonVPN"
|
|
||||||
touch "$firstTimePath"
|
|
||||||
fi
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
echo "[info] Preparing to setup dependencies"
|
echo "[info] Preparing to setup dependencies"
|
||||||
./install-dependencies $forceVpnInstall
|
./install-dependencies $1
|
||||||
echo "[info] Preparing to build projects from source"
|
echo "[info] Preparing to build projects from source"
|
||||||
./build
|
./build
|
||||||
echo "[info] Preparing to install components"
|
echo "[info] Preparing to install components"
|
||||||
./install-components $forceAppIndicator
|
./install-components
|
||||||
echo "[info] Install process finished"
|
echo "[info] Install process finished"
|
||||||
|
@ -1,14 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# arguments
|
|
||||||
forceAppIndicator="$1"
|
|
||||||
|
|
||||||
# new lib folder
|
# new lib folder
|
||||||
sudo mkdir -p /usr/lib/melon-vpn/
|
sudo mkdir -p /usr/lib/melon-vpn/
|
||||||
|
|
||||||
# stop and disable daemon first
|
|
||||||
sudo systemctl stop melonvpndaemon
|
|
||||||
sudo systemctl disable melonvpndaemon
|
|
||||||
|
|
||||||
# copying
|
# copying
|
||||||
sudo cp MelonVPNCore/bin/Release/MelonVPNCore.dll /usr/lib/melon-vpn/
|
sudo cp MelonVPNCore/bin/Release/MelonVPNCore.dll /usr/lib/melon-vpn/
|
||||||
sudo cp MelonVPNCore/bin/Release/Newtonsoft.Json.dll /usr/lib/melon-vpn/
|
sudo cp MelonVPNCore/bin/Release/Newtonsoft.Json.dll /usr/lib/melon-vpn/
|
||||||
@ -43,17 +36,13 @@ fi
|
|||||||
sudo chown root:root /etc/melon-vpn/client.cfg
|
sudo chown root:root /etc/melon-vpn/client.cfg
|
||||||
|
|
||||||
# copy more files
|
# copy more files
|
||||||
if [ "$forceAppIndicator" == "yes" ]; then
|
sudo cp MelonVPNClient/bin/Release/appindicator3-sharp.dll /usr/lib/melon-vpn/
|
||||||
sudo cp MelonVPNClient/bin/Release/appindicator3-sharp.dll /usr/lib/melon-vpn/
|
echo "[warning] appindicator3-sharp.dll has been copied as a component, if appindicator was installed through software run ./remove-appindicator3-component"
|
||||||
echo "[warning] appindicator3-sharp.dll has been copied as a component, if appindicator was installed through software run ./remove-appindicator3-component"
|
|
||||||
fi
|
|
||||||
sudo cp MelonVPNClient/bin/Release/libnotify.net.dll /usr/lib/melon-vpn/
|
sudo cp MelonVPNClient/bin/Release/libnotify.net.dll /usr/lib/melon-vpn/
|
||||||
sudo cp MelonVPNClient/bin/Release/MelonVPNClient.exe /usr/lib/melon-vpn/
|
sudo cp MelonVPNClient/bin/Release/MelonVPNClient.exe /usr/lib/melon-vpn/
|
||||||
sudo cp MelonVPNClient/bin/Release/MelonVPNClient.exe.config /usr/lib/melon-vpn/
|
sudo cp MelonVPNClient/bin/Release/MelonVPNClient.exe.config /usr/lib/melon-vpn/
|
||||||
sudo cp MelonVPNClient/bin/Release/MiniMelonVPNIcon.png /usr/lib/melon-vpn/
|
sudo cp MelonVPNClient/bin/Release/MiniMelonVPNIcon.png /usr/lib/melon-vpn/
|
||||||
sudo cp MelonVPNClient/bin/Release/MiniMelonVPNOnline.png /usr/lib/melon-vpn/
|
sudo cp MelonVPNClient/bin/Release/MiniMelonVPNOnline.png /usr/lib/melon-vpn/
|
||||||
sudo cp MelonVPNClient/bin/Release/MelonVPNDesktopIcon.png /usr/lib/melon-vpn/
|
|
||||||
sudo cp MelonVPNClient/bin/Release/libappindicator3sharpglue-12.10.0.so /usr/lib/melon-vpn/
|
|
||||||
|
|
||||||
# copy app indicator icons
|
# copy app indicator icons
|
||||||
mkdir -p ~/.local/share/icons/hicolor/128x128/apps/
|
mkdir -p ~/.local/share/icons/hicolor/128x128/apps/
|
||||||
@ -69,6 +58,8 @@ sudo chmod +x /usr/bin/melonvpnclient
|
|||||||
|
|
||||||
echo "[info] Restarting daemon"
|
echo "[info] Restarting daemon"
|
||||||
# cuz science
|
# cuz science
|
||||||
|
sudo systemctl stop melonvpndaemon
|
||||||
|
sudo systemctl disable melonvpndaemon
|
||||||
sudo systemctl daemon-reload
|
sudo systemctl daemon-reload
|
||||||
sudo systemctl enable melonvpndaemon
|
sudo systemctl enable melonvpndaemon
|
||||||
sudo systemctl start melonvpndaemon
|
sudo systemctl start melonvpndaemon
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# arguments
|
|
||||||
forceVpnInstall="$1"
|
|
||||||
|
|
||||||
# Check if golang exists by the output of "go version"
|
# Check if golang exists by the output of "go version"
|
||||||
if [ "$(go version | head -c 13)" == "go version go" ]; then
|
if [ "$(go version | head -c 13)" == "go version go" ]; then
|
||||||
echo "Assuming go is installed"
|
echo "Assuming go is installed"
|
||||||
@ -14,7 +11,7 @@ if [ "$(go version | head -c 13)" == "go version go" ]; then
|
|||||||
shouldbuild="ye"
|
shouldbuild="ye"
|
||||||
fi
|
fi
|
||||||
# Look for an argument to force simple vpn re-install
|
# Look for an argument to force simple vpn re-install
|
||||||
if [ "$forceVpnInstall" = "yes" ]; then
|
if [ "$1" = "force" ]; then
|
||||||
shouldbuild="ye"
|
shouldbuild="ye"
|
||||||
echo "Install enforced!"
|
echo "Install enforced!"
|
||||||
fi
|
fi
|
||||||
|
Loading…
Reference in New Issue
Block a user