From 137ff64d17e5d0fd80af5921187ed7cc64400b24 Mon Sep 17 00:00:00 2001 From: Derek S <44935661+Derek-R-S@users.noreply.github.com> Date: Mon, 5 Apr 2021 23:43:48 -0500 Subject: [PATCH] Prevent LLB from registering the same LRM node twice --- .../LRM_LoadBalancer/Config.cs | 1 + .../LRM_LoadBalancer/Endpoint.cs | 20 +++++++++---- .../LRM_LoadBalancer/Program.cs | 30 +++++++++++++------ 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Config.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Config.cs index 639a869..b265bc9 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Config.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Config.cs @@ -12,5 +12,6 @@ namespace LightReflectiveMirror.LoadBalancing public string AuthKey = "AuthKey"; public ushort EndpointPort = 7070; public ushort RelayEndpointPort = 8080; + public bool ShowDebugLogs = false; } } diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs index b22adbd..0b3f369 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs @@ -33,15 +33,25 @@ namespace LightReflectiveMirror.LoadBalancing string address = context.Request.RemoteEndPoint.Address.ToString(); - Console.WriteLine("Received auth req [" + receivedAuthKey + "] == [" + Program.conf.AuthKey + "]"); + if(Program.showDebugLogs) + Console.WriteLine("Received auth req [" + receivedAuthKey + "] == [" + Program.conf.AuthKey + "]"); // if server is authenticated if (receivedAuthKey != null && address != null && endpointPort != null && gamePort != null && receivedAuthKey == Program.conf.AuthKey) { - Console.WriteLine($"Server accepted: {address}:{gamePort}"); - var _gamePort = Convert.ToUInt16(gamePort); - var _endpointPort = Convert.ToUInt16(endpointPort); - await Program.instance.AddServer(address, _gamePort, _endpointPort); + if(Program.showDebugLogs) + Console.WriteLine($"Server accepted: {address}:{gamePort}"); + + try + { + var _gamePort = Convert.ToUInt16(gamePort); + var _endpointPort = Convert.ToUInt16(endpointPort); + await Program.instance.AddServer(address, _gamePort, _endpointPort); + } + catch + { + await context.Response.SendResponseAsync(HttpStatusCode.BadRequest); + } await context.Response.SendResponseAsync(HttpStatusCode.Ok); } diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs index 256513b..782c8c8 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs @@ -17,6 +17,7 @@ namespace LightReflectiveMirror.LoadBalancing public Dictionary availableRelayServers = new(); private int _pingDelay = 10000; + public static bool showDebugLogs = false; const string API_PATH = "/api/stats"; const string CONFIG_PATH = "config.json"; @@ -41,6 +42,7 @@ namespace LightReflectiveMirror.LoadBalancing { conf = JsonConvert.DeserializeObject(File.ReadAllText(CONFIG_PATH)); _pingDelay = conf.ConnectedServerPingRate; + showDebugLogs = conf.ShowDebugLogs; if (new EndpointServer().Start(conf.EndpointPort)) WriteLogMessage("Endpoint server started successfully", ConsoleColor.Green); @@ -58,13 +60,21 @@ namespace LightReflectiveMirror.LoadBalancing public async Task AddServer(string serverIP, ushort port, ushort endpointPort) { + var relayAddr = new RelayAddress { Port = port, EndpointPort = endpointPort, Address = serverIP }; + + if (availableRelayServers.ContainsKey(relayAddr)) + { + WriteLogMessage($"LRM Node {serverIP}:{port} tried to register while already registered!"); + return; + } + var stats = await ManualPingServer(serverIP, endpointPort); - if(stats.HasValue) - availableRelayServers.Add(new RelayAddress { Port = port, EndpointPort = endpointPort, Address = serverIP }, stats.Value); + if (stats.HasValue) + availableRelayServers.Add(relayAddr, stats.Value); } - public async Task ManualPingServer(string serverIP, ushort port) + public async Task ManualPingServer(string serverIP, ushort port) { using (WebClient wc = new WebClient()) { @@ -74,7 +84,7 @@ namespace LightReflectiveMirror.LoadBalancing return JsonConvert.DeserializeObject(receivedStats); } - catch(Exception e) + catch (Exception e) { // Server failed to respond to stats, dont add to load balancer. return null; @@ -103,12 +113,13 @@ namespace LightReflectiveMirror.LoadBalancing { while (true) { - WriteLogMessage("Pinging " + availableRelayServers.Count + " available relays"); + if (showDebugLogs) + WriteLogMessage("Pinging " + availableRelayServers.Count + " available relays"); // Create a new list so we can modify the collection in our loop. var keys = new List(availableRelayServers.Keys); - for(int i = 0; i < keys.Count; i++) + for (int i = 0; i < keys.Count; i++) { string url = $"http://{keys[i].Address}:{keys[i].EndpointPort}{API_PATH}"; @@ -119,7 +130,8 @@ namespace LightReflectiveMirror.LoadBalancing var serverStats = wc.DownloadString(url); var deserializedData = JsonConvert.DeserializeObject(serverStats); - WriteLogMessage("Server " + keys[i].Address + " still exists, keeping in collection."); + if (showDebugLogs) + WriteLogMessage("Server " + keys[i].Address + " still exists, keeping in collection."); if (availableRelayServers.ContainsKey(keys[i])) availableRelayServers[keys[i]] = deserializedData; @@ -130,7 +142,8 @@ namespace LightReflectiveMirror.LoadBalancing { // server doesnt exist anymore probably // do more shit here - WriteLogMessage("Server " + keys[i] + " does not exist anymore, removing", ConsoleColor.Red); + if (showDebugLogs) + WriteLogMessage("Server " + keys[i] + " does not exist anymore, removing", ConsoleColor.Red); availableRelayServers.Remove(keys[i]); } } @@ -214,5 +227,4 @@ namespace LightReflectiveMirror.LoadBalancing public RelayAddress relayInfo; } - }