melonvpn-original-cs/MelonVPNCore/DaemonSocketServer.cs

261 lines
11 KiB
C#
Raw 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-08 16:55:26 +00:00
2020-09-22 23:10:16 +01:00
public static void StartServer()
{
IPHostEntry host = Dns.GetHostEntry("localhost");
IPAddress ipAddress = host.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 22035);
try
{
Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(localEndPoint);
2021-03-19 18:40:50 +00:00
listener.Listen(32);
2020-09-22 23:10:16 +01:00
string lastClientUpdate = Messages.EOF;
while (true)
{
Socket handler = listener.Accept();
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;
}
2021-03-19 11:17:39 +00:00
if (data.StartsWith(Messages.ClientListStartMsg, StringComparison.CurrentCulture) && shouldBeRunning)
2020-09-22 23:10:16 +01:00
{
lastClientUpdate = data;
Client.SendCustomMessage(lastClientUpdate, true);
}
2021-03-19 09:13:32 +00:00
else if (data == Messages.RestartOnMsg)
{
shouldRestart = true;
Client.SendDataMessage(DataMessage.RestartOn, true);
}
else if (data == Messages.RestartOffMsg)
{
shouldRestart = false;
Client.SendDataMessage(DataMessage.RestartOff, true);
}
2020-09-22 23:10:16 +01:00
else if (data == Messages.StatusMsg)
{
Console.WriteLine("Status requested");
2021-03-08 16:55:26 +00:00
if (isProcessOnline(currentVpnProcess) || shouldBeRunning)
2020-09-22 23:10:16 +01:00
{
2021-03-19 18:51:50 +00:00
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);
}
2020-09-22 23:10:16 +01:00
}
else
{
Console.WriteLine("Sending response: offline");
Client.SendDataMessage(DataMessage.Offline, true);
2020-09-23 20:24:30 +01:00
Client.SendCustomMessage(Messages.ClientListEmptyMsg, true);
2020-09-22 23:10:16 +01:00
}
2021-03-19 09:13:32 +00:00
Client.SendDataMessage((shouldRestart) ? DataMessage.RestartOn : DataMessage.RestartOff);
2020-09-22 23:10:16 +01:00
}
else if (data == Messages.StartMsg)
{
2021-03-08 16:55:26 +00:00
if (! isProcessOnline(currentVpnProcess))
2020-09-22 23:10:16 +01:00
{
2021-03-08 16:55:26 +00:00
shouldBeRunning = true;
2020-09-22 23:10:16 +01:00
Console.WriteLine("Starting VPN");
try
{
Console.WriteLine("Starting embedded process");
2021-03-19 09:13:32 +00:00
Console.WriteLine("Sending starting reply");
Client.SendDataMessage(DataMessage.Starting, true);
if (StartEProcess(true))
{
Console.WriteLine("Sending online reply");
Client.SendDataMessage(DataMessage.Online, true);
2021-03-19 11:17:39 +00:00
currentVpnProcess.EnableRaisingEvents = true;
}
else
{
shouldBeRunning = false;
Client.SendDataMessage(DataMessage.Offline, true);
Client.SendCustomMessage(Messages.ClientListEmptyMsg, true);
2021-03-19 09:13:32 +00:00
}
2020-09-22 23:10:16 +01:00
}
catch (Exception e)
{
2021-03-08 16:55:26 +00:00
shouldBeRunning = false;
2020-09-22 23:10:16 +01:00
Console.WriteLine("There was an error. But I fixed it.");
Console.WriteLine("It looked like this: " + e);
2021-03-19 09:13:32 +00:00
currentVpnProcess.Dispose();
2020-09-22 23:10:16 +01:00
currentVpnProcess = null;
Client.SendDataMessage(DataMessage.Error, true);
2020-09-23 20:24:30 +01:00
Client.SendCustomMessage(Messages.ClientListEmptyMsg, true);
2020-09-22 23:10:16 +01:00
}
}
else
{
Console.WriteLine("VPN already started");
}
}
else if (data == Messages.StopMsg)
{
2021-03-08 16:55:26 +00:00
shouldBeRunning = false;
bool haderr = false;
if (isProcessOnline(currentVpnProcess))
2020-09-22 23:10:16 +01:00
{
Console.WriteLine("Stopping VPN");
try
{
2021-03-08 16:55:26 +00:00
currentVpnProcess.EnableRaisingEvents = false;
2020-09-22 23:10:16 +01:00
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);
2020-09-23 20:24:30 +01:00
Client.SendCustomMessage(Messages.ClientListEmptyMsg, true);
2021-03-08 16:55:26 +00:00
haderr = true;
2020-09-22 23:10:16 +01:00
}
}
else
{
Console.WriteLine("VPN already stopped");
}
2021-03-08 16:55:26 +00:00
if (!haderr)
{
Console.WriteLine("Sending offline reply");
Client.SendDataMessage(DataMessage.Offline, true);
Client.SendCustomMessage(Messages.ClientListEmptyMsg, true);
}
2020-09-22 23:10:16 +01:00
}
Client.SendDataMessage(DataMessage.Blank, true);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
2021-03-08 16:55:26 +00:00
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");
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");
Client.SendDataMessage(DataMessage.Restarting, true);
if (StartEProcess(false))
{
Console.WriteLine("Sending online reply");
Client.SendDataMessage(DataMessage.Online, true);
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-19 09:13:32 +00:00
Client.SendDataMessage(DataMessage.Offline, true);
Client.SendCustomMessage(Messages.ClientListEmptyMsg, true);
}
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
}
}