Fix saving config file and remember to listen after binding

This commit is contained in:
Melon 2021-03-23 21:07:45 +00:00
parent c05139a333
commit 18fd6a9d58
2 changed files with 7 additions and 4 deletions

View File

@ -26,17 +26,18 @@ namespace MelonVPNCore
public static void StartServer() public static void StartServer()
{ {
if (File.Exists(daemonConfigPath)) config = DaemonConfig.FromJson(File.ReadAllText(daemonConfigPath));
else config = new DaemonConfig();
IPHostEntry host = Dns.GetHostEntry("localhost"); IPHostEntry host = Dns.GetHostEntry("localhost");
IPAddress ipAddress = host.AddressList[0]; IPAddress ipAddress = host.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 22035); IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 22035);
if (File.Exists(daemonConfigPath)) config = DaemonConfig.FromJson(File.ReadAllText(daemonConfigPath));
else config = new DaemonConfig();
try try
{ {
Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(localEndPoint); listener.Bind(localEndPoint);
listener.Listen(32);
string lastClientUpdate = Messages.EOF; string lastClientUpdate = Messages.EOF;
@ -63,12 +64,14 @@ namespace MelonVPNCore
else if (data == Messages.RestartOnMsg) else if (data == Messages.RestartOnMsg)
{ {
shouldRestart = true; shouldRestart = true;
config.ShouldRestart = true;
SaveConfig(); SaveConfig();
Client.SendDataMessage(DataMessage.RestartOn, true); Client.SendDataMessage(DataMessage.RestartOn, true);
} }
else if (data == Messages.RestartOffMsg) else if (data == Messages.RestartOffMsg)
{ {
shouldRestart = false; shouldRestart = false;
config.ShouldRestart = false;
SaveConfig(); SaveConfig();
Client.SendDataMessage(DataMessage.RestartOff, true); Client.SendDataMessage(DataMessage.RestartOff, true);
} }

View File

@ -75,6 +75,6 @@ namespace MelonVPNCore
public DaemonConfig() => Type = "DaemonConfig"; public DaemonConfig() => Type = "DaemonConfig";
[JsonProperty("restart")] [JsonProperty("restart")]
public string ShouldRestart { get; set; } public bool ShouldRestart { get; set; }
} }
} }