Repair restart system.

This commit is contained in:
Captain ALM 2021-03-19 09:13:32 +00:00
parent 7697a74ee4
commit fd8c1e89fa
5 changed files with 73 additions and 15 deletions

View File

@ -8,6 +8,10 @@
ServerError, ServerError,
None, None,
Blank, Blank,
Sent Sent,
Restarting,
Starting,
RestartOn,
RestartOff
} }
} }

View File

@ -11,6 +11,9 @@ namespace MelonVPNCore
{ {
private static Process currentVpnProcess = null; private static Process currentVpnProcess = null;
private static bool shouldBeRunning = false; private static bool shouldBeRunning = false;
private static bool shouldRestart = true;
private static int startingTime = 3000;
private static int restartDelay = 250;
public static void StartServer() public static void StartServer()
{ {
@ -46,6 +49,16 @@ namespace MelonVPNCore
lastClientUpdate = data; lastClientUpdate = data;
Client.SendCustomMessage(lastClientUpdate, true); Client.SendCustomMessage(lastClientUpdate, true);
} }
else if (data == Messages.RestartOnMsg)
{
shouldRestart = true;
Client.SendDataMessage(DataMessage.RestartOn, true);
}
else if (data == Messages.RestartOffMsg)
{
shouldRestart = false;
Client.SendDataMessage(DataMessage.RestartOff, true);
}
else if (data == Messages.StatusMsg) else if (data == Messages.StatusMsg)
{ {
Console.WriteLine("Status requested"); Console.WriteLine("Status requested");
@ -61,6 +74,7 @@ namespace MelonVPNCore
Client.SendDataMessage(DataMessage.Offline, true); Client.SendDataMessage(DataMessage.Offline, true);
Client.SendCustomMessage(Messages.ClientListEmptyMsg, true); Client.SendCustomMessage(Messages.ClientListEmptyMsg, true);
} }
Client.SendDataMessage((shouldRestart) ? DataMessage.RestartOn : DataMessage.RestartOff);
} }
else if (data == Messages.StartMsg) else if (data == Messages.StartMsg)
{ {
@ -71,17 +85,20 @@ namespace MelonVPNCore
try try
{ {
Console.WriteLine("Starting embedded process"); Console.WriteLine("Starting embedded process");
StartEProcess(true); Console.WriteLine("Sending starting reply");
Client.SendDataMessage(DataMessage.Starting, true);
if (StartEProcess(true))
{
Console.WriteLine("Sending online reply"); Console.WriteLine("Sending online reply");
Thread.Sleep(1500);
Client.SendDataMessage(DataMessage.Online, true); Client.SendDataMessage(DataMessage.Online, true);
currentVpnProcess.EnableRaisingEvents = true; }
} }
catch (Exception e) catch (Exception e)
{ {
shouldBeRunning = false; shouldBeRunning = false;
Console.WriteLine("There was an error. But I fixed it."); Console.WriteLine("There was an error. But I fixed it.");
Console.WriteLine("It looked like this: " + e); Console.WriteLine("It looked like this: " + e);
currentVpnProcess.Dispose();
currentVpnProcess = null; currentVpnProcess = null;
Client.SendDataMessage(DataMessage.Error, true); Client.SendDataMessage(DataMessage.Error, true);
Client.SendCustomMessage(Messages.ClientListEmptyMsg, true); Client.SendCustomMessage(Messages.ClientListEmptyMsg, true);
@ -161,13 +178,29 @@ namespace MelonVPNCore
static void CurrentVpnProcess_Exited(object sender, EventArgs e) static void CurrentVpnProcess_Exited(object sender, EventArgs e)
{ {
Client.SendDataMessage(DataMessage.Restarting, true);
Console.WriteLine("Restarting embedded process"); Console.WriteLine("Restarting embedded process");
currentVpnProcess.Dispose();
currentVpnProcess = null; currentVpnProcess = null;
Thread.Sleep(1500); Thread.Sleep(restartDelay);
StartEProcess(false); if (shouldRestart)
{
Console.WriteLine("Sending restarting reply");
Client.SendDataMessage(DataMessage.Restarting, true);
if (StartEProcess(false))
{
Console.WriteLine("Sending online reply");
Client.SendDataMessage(DataMessage.Online, true);
}
}
else
{
Client.SendDataMessage(DataMessage.Offline, true);
Client.SendCustomMessage(Messages.ClientListEmptyMsg, true);
}
} }
static void StartEProcess(bool reraisee) static bool StartEProcess(bool reraisee)
{ {
if (shouldBeRunning) if (shouldBeRunning)
{ {
@ -175,21 +208,23 @@ namespace MelonVPNCore
{ {
currentVpnProcess = new Process currentVpnProcess = new Process
{ {
StartInfo = new ProcessStartInfo("simple-vpn", "client /etc/melon-vpn/client.cfg") StartInfo = new ProcessStartInfo("simple-vpn", "client /etc/melon-vpn/client.cfg"),
EnableRaisingEvents = true
}; };
currentVpnProcess.Start();
currentVpnProcess.EnableRaisingEvents = false;
currentVpnProcess.Exited += CurrentVpnProcess_Exited; currentVpnProcess.Exited += CurrentVpnProcess_Exited;
Thread.Sleep(1500); currentVpnProcess.Start();
currentVpnProcess.EnableRaisingEvents = true; Thread.Sleep(startingTime);
return !currentVpnProcess.HasExited;
} }
catch (Exception e) catch (Exception e)
{ {
//Not really fixed is it?
Console.WriteLine("There was an error. But I fixed it."); Console.WriteLine("There was an error. But I fixed it.");
Console.WriteLine("It looked like this: " + e); Console.WriteLine("It looked like this: " + e);
if (reraisee) { throw e; } if (reraisee) { throw e; }
} }
} }
return false;
} }
} }
} }

View File

@ -8,6 +8,10 @@
Start, Start,
Stop, Stop,
Blank, Blank,
Error Error,
Starting,
Restarting,
RestartOn,
RestartOff
} }
} }

View File

@ -9,6 +9,7 @@ namespace MelonVPNCore
{ {
public static event EventHandler<ClientResponseState> Receive; public static event EventHandler<ClientResponseState> Receive;
public static event EventHandler<ConnectedClient[]> ClientListUpdate; public static event EventHandler<ConnectedClient[]> ClientListUpdate;
public static event EventHandler<bool> RestartModeSync;
public static void StartServer() public static void StartServer()
{ {
@ -41,6 +42,11 @@ namespace MelonVPNCore
if (data == Messages.OnlineMsg) ret = ClientResponseState.Online; if (data == Messages.OnlineMsg) ret = ClientResponseState.Online;
if (data == Messages.OfflineMsg) ret = ClientResponseState.Offline; if (data == Messages.OfflineMsg) ret = ClientResponseState.Offline;
if (data == Messages.ErrorMsg) ret = ClientResponseState.ServerError; if (data == Messages.ErrorMsg) ret = ClientResponseState.ServerError;
if (data == Messages.StartingMsg) ret = ClientResponseState.Starting;
if (data == Messages.RestartingMsg) ret = ClientResponseState.Restarting;
bool rsm = false;
if (data == Messages.RestartOnMsg) rsm = true;
if (data == Messages.RestartOffMsg) rsm = false;
Console.WriteLine(data); Console.WriteLine(data);
if (data.StartsWith(Messages.ClientListStartMsg, StringComparison.CurrentCulture)) if (data.StartsWith(Messages.ClientListStartMsg, StringComparison.CurrentCulture))
{ {
@ -55,6 +61,7 @@ namespace MelonVPNCore
handler.Close(); handler.Close();
Receive?.Invoke(null, ret); Receive?.Invoke(null, ret);
RestartModeSync?.Invoke(null, rsm);
} }
} }
catch (Exception e) catch (Exception e)

View File

@ -12,6 +12,10 @@
public static string ErrorMsg = "ERROR::" + Code + EOF; public static string ErrorMsg = "ERROR::" + Code + EOF;
public static string ClientListStartMsg = "CLIENTLIST::" + Code + "::"; public static string ClientListStartMsg = "CLIENTLIST::" + Code + "::";
public static string ClientListEmptyMsg = ClientListStartMsg + "[]" + EOF; public static string ClientListEmptyMsg = ClientListStartMsg + "[]" + EOF;
public static string StartingMsg = "STARTING::" + Code + EOF;
public static string RestartingMsg = "RESTARTING::" + Code + EOF;
public static string RestartOnMsg = "RESTART::ON::" + Code + EOF;
public static string RestartOffMsg = "RESTART::OFF::" + Code + EOF;
public static string GetMessage(DataMessage a) public static string GetMessage(DataMessage a)
{ {
@ -21,6 +25,10 @@
if (a == DataMessage.Start) return StartMsg; if (a == DataMessage.Start) return StartMsg;
if (a == DataMessage.Stop) return StopMsg; if (a == DataMessage.Stop) return StopMsg;
if (a == DataMessage.Error) return ErrorMsg; if (a == DataMessage.Error) return ErrorMsg;
if (a == DataMessage.Starting) return StartingMsg;
if (a == DataMessage.Restarting) return RestartingMsg;
if (a == DataMessage.RestartOn) return RestartOnMsg;
if (a == DataMessage.RestartOff) return RestartOffMsg;
if (a == DataMessage.Blank) return EOF; if (a == DataMessage.Blank) return EOF;
return EOF; return EOF;
} }