Merge pull request #1 from Captain-ALM/master

Add a hacked in restarting system.
This commit is contained in:
Sean Pointon 2021-03-09 11:22:52 +00:00
commit 4ef717daf6

View File

@ -9,6 +9,9 @@ namespace MelonVPNCore
{
public static class DaemonSocketServer
{
private static Process currentVpnProcess = null;
private static bool shouldBeRunning = false;
public static void StartServer()
{
IPHostEntry host = Dns.GetHostEntry("localhost");
@ -21,7 +24,6 @@ namespace MelonVPNCore
listener.Bind(localEndPoint);
listener.Listen(10);
Process currentVpnProcess = null;
string lastClientUpdate = Messages.EOF;
while (true)
@ -47,7 +49,7 @@ namespace MelonVPNCore
else if (data == Messages.StatusMsg)
{
Console.WriteLine("Status requested");
if (currentVpnProcess != null && !currentVpnProcess.HasExited)
if (isProcessOnline(currentVpnProcess) || shouldBeRunning)
{
Console.WriteLine("Sending response: online");
Client.SendDataMessage(DataMessage.Online, true);
@ -62,28 +64,22 @@ namespace MelonVPNCore
}
else if (data == Messages.StartMsg)
{
if (currentVpnProcess == null)
if (! isProcessOnline(currentVpnProcess))
{
shouldBeRunning = true;
Console.WriteLine("Starting VPN");
try
{
Console.WriteLine("Starting embedded process");
currentVpnProcess = new Process
{
StartInfo = new ProcessStartInfo("simple-vpn", "client /etc/melon-vpn/client.cfg")
};
currentVpnProcess.Exited += delegate
{
Client.SendDataMessage(DataMessage.Offline, true);
currentVpnProcess = null;
};
currentVpnProcess.Start();
StartEProcess(true);
Console.WriteLine("Sending online reply");
Thread.Sleep(1500);
Client.SendDataMessage(DataMessage.Online, true);
currentVpnProcess.EnableRaisingEvents = true;
}
catch (Exception e)
{
shouldBeRunning = false;
Console.WriteLine("There was an error. But I fixed it.");
Console.WriteLine("It looked like this: " + e);
currentVpnProcess = null;
@ -98,17 +94,17 @@ namespace MelonVPNCore
}
else if (data == Messages.StopMsg)
{
if (currentVpnProcess != null)
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;
Console.WriteLine("Sending offline reply");
Client.SendDataMessage(DataMessage.Offline, true);
Client.SendCustomMessage(Messages.ClientListEmptyMsg, true);
}
catch (Exception e)
{
@ -117,12 +113,19 @@ namespace MelonVPNCore
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);
@ -136,5 +139,57 @@ namespace MelonVPNCore
Console.WriteLine(e);
}
}
public static bool isProcessOnline(Process p)
{
if (p == null)
{
return false;
}
else
{
if (p.HasExited)
{
return false;
}
else
{
return true;
}
}
}
static void CurrentVpnProcess_Exited(object sender, EventArgs e)
{
Console.WriteLine("Restarting embedded process");
currentVpnProcess = null;
Thread.Sleep(1500);
StartEProcess(false);
}
static void StartEProcess(bool reraisee)
{
if (shouldBeRunning)
{
try
{
currentVpnProcess = new Process
{
StartInfo = new ProcessStartInfo("simple-vpn", "client /etc/melon-vpn/client.cfg")
};
currentVpnProcess.Start();
currentVpnProcess.EnableRaisingEvents = false;
currentVpnProcess.Exited += CurrentVpnProcess_Exited;
Thread.Sleep(1500);
currentVpnProcess.EnableRaisingEvents = true;
}
catch (Exception e)
{
Console.WriteLine("There was an error. But I fixed it.");
Console.WriteLine("It looked like this: " + e);
if (reraisee) { throw e; }
}
}
}
}
}