melonvpn-original-cs/MelonVPNCore/DaemonSocketServer.cs

240 lines
9.1 KiB
C#
Raw Permalink Normal View History

2020-09-22 23:10:16 +01:00
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace MelonVPNCore
{
public static class DaemonSocketServer
{
2021-03-08 16:55:26 +00:00
private static Process currentVpnProcess = null;
private static bool shouldBeRunning = false;
2021-03-19 18:40:50 +00:00
private static bool shouldRestart = false;
2021-03-19 18:51:50 +00:00
private static bool isRestarting = false;
2021-03-19 09:13:32 +00:00
private static int startingTime = 3000;
private static int restartDelay = 250;
2021-03-23 18:07:47 +00:00
private static DaemonServer _srv;
private static DaemonClientMultiplexor _mlt;
private static string lastClientUpdate = Messages.EOF;
2021-03-08 16:55:26 +00:00
2020-09-22 23:10:16 +01:00
public static void StartServer()
{
2021-03-23 18:07:47 +00:00
_srv = new DaemonServer();
_mlt = new DaemonClientMultiplexor(_srv);
_mlt.clientMessageReceived += clientMessageReceived;
_srv.start();
}
2020-09-22 23:10:16 +01:00
2021-03-23 18:07:47 +00:00
public static bool isProcessOnline(Process p)
{
if (p == null)
2020-09-22 23:10:16 +01:00
{
2021-03-23 18:07:47 +00:00
return false;
}
else
{
if (p.HasExited)
2020-09-22 23:10:16 +01:00
{
2021-03-23 18:07:47 +00:00
return false;
}
else
{
return true;
}
}
}
2020-09-22 23:10:16 +01:00
2021-03-23 18:07:47 +00:00
static void clientMessageReceived(object sender, string data)
{
try
{
if (data.StartsWith(Messages.ClientListStartMsg, StringComparison.CurrentCulture) && shouldBeRunning)
{
lastClientUpdate = data;
_mlt.SendCustomMessage(lastClientUpdate);
}
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)
2020-09-22 23:10:16 +01:00
{
2021-03-23 18:07:47 +00:00
if (isRestarting)
2020-09-22 23:10:16 +01:00
{
2021-03-23 18:07:47 +00:00
Console.WriteLine("Sending response: restarting");
_mlt.SendDataMessage(DataMessage.Restarting);
_mlt.SendCustomMessage(lastClientUpdate);
2020-09-22 23:10:16 +01:00
}
else
{
2021-03-23 18:07:47 +00:00
Console.WriteLine("Sending response: online");
_mlt.SendDataMessage(DataMessage.Online);
_mlt.SendCustomMessage(lastClientUpdate);
2020-09-22 23:10:16 +01:00
}
}
2021-03-23 18:07:47 +00:00
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))
2020-09-22 23:10:16 +01:00
{
2021-03-23 18:07:47 +00:00
shouldBeRunning = true;
Console.WriteLine("Starting VPN");
try
2020-09-22 23:10:16 +01:00
{
2021-03-23 18:07:47 +00:00
Console.WriteLine("Starting embedded process");
Console.WriteLine("Sending starting reply");
_mlt.SendDataMessage(DataMessage.Starting);
if (StartEProcess(true))
2020-09-22 23:10:16 +01:00
{
2021-03-23 18:07:47 +00:00
Console.WriteLine("Sending online reply");
_mlt.SendDataMessage(DataMessage.Online);
currentVpnProcess.EnableRaisingEvents = true;
2020-09-22 23:10:16 +01:00
}
2021-03-23 18:07:47 +00:00
else { throw new InvalidOperationException("Client crashed!"); }
2020-09-22 23:10:16 +01:00
}
2021-03-23 18:07:47 +00:00
catch (Exception e)
2020-09-22 23:10:16 +01:00
{
2021-03-23 18:07:47 +00:00
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);
2020-09-22 23:10:16 +01:00
}
}
2021-03-23 18:07:47 +00:00
else
2020-09-22 23:10:16 +01:00
{
2021-03-23 18:07:47 +00:00
Console.WriteLine("VPN already started");
}
}
else if (data == Messages.StopMsg)
{
shouldBeRunning = false;
bool haderr = false;
if (isProcessOnline(currentVpnProcess))
{
Console.WriteLine("Stopping VPN");
try
2020-09-22 23:10:16 +01:00
{
2021-03-23 18:07:47 +00:00
currentVpnProcess.EnableRaisingEvents = false;
Console.WriteLine("Stopping embedded process");
currentVpnProcess.Kill();
currentVpnProcess = null;
2020-09-22 23:10:16 +01:00
}
2021-03-23 18:07:47 +00:00
catch (Exception e)
2021-03-08 16:55:26 +00:00
{
2021-03-23 18:07:47 +00:00
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;
2021-03-08 16:55:26 +00:00
}
2020-09-22 23:10:16 +01:00
}
2021-03-23 18:07:47 +00:00
else
{
Console.WriteLine("VPN already stopped");
}
if (!haderr)
{
Console.WriteLine("Sending offline reply");
_mlt.SendDataMessage(DataMessage.Offline);
_mlt.SendCustomMessage(Messages.ClientListEmptyMsg);
}
}
2020-09-22 23:10:16 +01:00
2021-03-23 18:07:47 +00:00
_mlt.SendDataMessage(DataMessage.Blank);
2020-09-22 23:10:16 +01:00
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
2021-03-08 16:55:26 +00:00
static void CurrentVpnProcess_Exited(object sender, EventArgs e)
{
Console.WriteLine("Restarting embedded process");
2021-03-19 09:13:32 +00:00
currentVpnProcess.Dispose();
2021-03-08 16:55:26 +00:00
currentVpnProcess = null;
2021-03-19 09:13:32 +00:00
Thread.Sleep(restartDelay);
2021-03-19 11:17:39 +00:00
bool imonline = false;
2021-03-19 18:51:50 +00:00
isRestarting = true;
2021-03-19 11:17:39 +00:00
while (shouldRestart && shouldBeRunning)
2021-03-19 09:13:32 +00:00
{
Console.WriteLine("Sending restarting reply");
2021-03-23 18:07:47 +00:00
_mlt.SendDataMessage(DataMessage.Restarting);
2021-03-19 09:13:32 +00:00
if (StartEProcess(false))
{
Console.WriteLine("Sending online reply");
2021-03-23 18:07:47 +00:00
_mlt.SendDataMessage(DataMessage.Online);
2021-03-19 11:17:39 +00:00
currentVpnProcess.EnableRaisingEvents = true;
imonline = true;
break;
}
else
{
currentVpnProcess.Dispose();
currentVpnProcess = null;
Thread.Sleep(restartDelay);
2021-03-19 09:13:32 +00:00
}
}
2021-03-19 18:51:50 +00:00
isRestarting = false;
2021-03-19 11:17:39 +00:00
if (! imonline)
2021-03-19 09:13:32 +00:00
{
2021-03-19 11:17:39 +00:00
shouldBeRunning = false;
Console.WriteLine("Sending offline reply");
2021-03-23 18:07:47 +00:00
_mlt.SendDataMessage(DataMessage.Offline);
_mlt.SendCustomMessage(Messages.ClientListEmptyMsg);
2021-03-19 09:13:32 +00:00
}
2021-03-08 16:55:26 +00:00
}
2021-03-19 09:13:32 +00:00
static bool StartEProcess(bool reraisee)
2021-03-08 16:55:26 +00:00
{
if (shouldBeRunning)
{
try
{
currentVpnProcess = new Process
{
2021-03-19 09:13:32 +00:00
StartInfo = new ProcessStartInfo("simple-vpn", "client /etc/melon-vpn/client.cfg"),
2021-03-19 11:17:39 +00:00
EnableRaisingEvents = false
2021-03-08 16:55:26 +00:00
};
currentVpnProcess.Exited += CurrentVpnProcess_Exited;
2021-03-19 09:13:32 +00:00
currentVpnProcess.Start();
Thread.Sleep(startingTime);
return !currentVpnProcess.HasExited;
2021-03-08 16:55:26 +00:00
}
catch (Exception e)
{
2021-03-19 09:13:32 +00:00
//Not really fixed is it?
2021-03-08 16:55:26 +00:00
Console.WriteLine("There was an error. But I fixed it.");
Console.WriteLine("It looked like this: " + e);
if (reraisee) { throw e; }
}
}
2021-03-19 09:13:32 +00:00
return false;
2021-03-08 16:55:26 +00:00
}
2020-09-22 23:10:16 +01:00
}
}