From af712e4f1ff91d941ebc2c5654b1516daee56658 Mon Sep 17 00:00:00 2001 From: Derek S <44935661+Derek-R-S@users.noreply.github.com> Date: Mon, 5 Apr 2021 13:34:06 -0500 Subject: [PATCH] Fixed load balancer http issues --- .../LRM_LoadBalancer/Endpoint.cs | 36 +++++-- .../LRM_LoadBalancer/FodyWeavers.xml | 3 + .../LRM_LoadBalancer/LRM_LoadBalancer.csproj | 4 + .../LRM_LoadBalancer/Program.cs | 94 +++++++++++-------- .../LRM/Program.cs | 4 +- 5 files changed, 92 insertions(+), 49 deletions(-) create mode 100644 LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/FodyWeavers.xml diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs index c7e3808..45ace4e 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs @@ -5,7 +5,10 @@ using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; +using System.Net; +using System.Net.Sockets; using System.Threading.Tasks; +using HttpStatusCode = Grapevine.HttpStatusCode; namespace LightReflectiveMirror.LoadBalancing { @@ -15,18 +18,19 @@ namespace LightReflectiveMirror.LoadBalancing [RestRoute("Get", "/api/auth")] public async Task ReceiveAuthKey(IHttpContext context) { - var req = context.Request.Headers; - string receivedAuthKey = req[0]; - string address = req[1]; + var req = context.Request; + string receivedAuthKey = req.Headers["Auth"]; + string port = req.Headers["Port"]; + string address = context.Request.RemoteEndPoint.Address.ToString(); - Console.WriteLine("Received auth req [" + req[0] + "] == [" + Program.conf.AuthKey+"]"); + Console.WriteLine("Received auth req [" + receivedAuthKey + "] == [" + Program.conf.AuthKey+"]"); // if server is authenticated - if (receivedAuthKey == Program.conf.AuthKey) + if (receivedAuthKey != null && address != null && port != null && receivedAuthKey == Program.conf.AuthKey) { - Console.WriteLine("Server accepted: " + address); + Console.WriteLine($"Server accepted: {address}:{port}"); - await Program.instance.AddServer(address); + await Program.instance.AddServer($"{address}:{port}"); await context.Response.SendResponseAsync(HttpStatusCode.Ok); } @@ -87,7 +91,8 @@ namespace LightReflectiveMirror.LoadBalancing services.Configure(options => options.MinLevel = LogLevel.None); }, (server) => { - server.Prefixes.Add($"http://*:{port}/"); + server.Prefixes.Add($"http://{GetLocalIp()}:{port}/"); + server.Prefixes.Add($"http://127.0.0.1:{port}/"); }).Build(); server.Router.Options.SendExceptionMessages = false; @@ -100,5 +105,20 @@ namespace LightReflectiveMirror.LoadBalancing return false; } } + + private static string GetLocalIp() + { + var host = Dns.GetHostEntry(Dns.GetHostName()); + + foreach (var ip in host.AddressList) + { + if (ip.AddressFamily == AddressFamily.InterNetwork && ip.ToString() != "127.0.0.1") + { + return ip.ToString(); + } + } + + return null; + } } } diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/FodyWeavers.xml b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/FodyWeavers.xml new file mode 100644 index 0000000..5029e70 --- /dev/null +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/FodyWeavers.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/LRM_LoadBalancer.csproj b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/LRM_LoadBalancer.csproj index 25e5202..773df2e 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/LRM_LoadBalancer.csproj +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/LRM_LoadBalancer.csproj @@ -6,6 +6,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs index e15b721..9e129bb 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs @@ -60,27 +60,25 @@ namespace LightReflectiveMirror.LoadBalancing { var stats = await ManualPingServer(serverIP); - if(stats.PublicRoomCount != -1) - availableRelayServers.Add(serverIP, stats); + if(stats.HasValue) + availableRelayServers.Add(serverIP, stats.Value); } - async Task ManualPingServer(string serverIP) + async Task ManualPingServer(string serverIP) { - HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://"+serverIP); - - try + using (WebClient wc = new WebClient()) { - WebResponse response = await myRequest.GetResponseAsync(); - var reader = new StreamReader(response.GetResponseStream()); + try + { + string receivedStats = await wc.DownloadStringTaskAsync($"http://{serverIP}{API_PATH}"); - return JsonConvert.DeserializeObject(reader.ReadToEnd()); - } - catch (WebException ex) - { - // server doesnt exist anymore probably - // do more shit here - - return new RelayStats { PublicRoomCount = -1 }; + return JsonConvert.DeserializeObject(receivedStats); + } + catch(Exception e) + { + // Server failed to respond to stats, dont add to load balancer. + return null; + } } } @@ -90,28 +88,35 @@ namespace LightReflectiveMirror.LoadBalancing { WriteLogMessage("Pinging " + availableRelayServers.Count + " available relays"); - foreach (var server in availableRelayServers) + // 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++) { - string url = server + API_PATH; - HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); + string url = $"http://{keys[i]}{API_PATH}"; - try + using (WebClient wc = new WebClient()) { - WebResponse response = await myRequest.GetResponseAsync(); + try + { + var serverStats = wc.DownloadString(url); + Console.WriteLine(serverStats); - var reader = new StreamReader(response.GetResponseStream()); + WriteLogMessage("Server " + keys[i] + " still exists, keeping in collection."); - WriteLogMessage("Server " + server.Key + " still exists, keeping in collection."); - availableRelayServers.Remove(server.Key); - availableRelayServers.Add(server.Key, JsonConvert.DeserializeObject(reader.ReadToEnd())); - } - catch (Exception ex) - { - // server doesnt exist anymore probably - // do more shit here + if (availableRelayServers.ContainsKey(keys[i])) + availableRelayServers[keys[i]] = JsonConvert.DeserializeObject(serverStats); + else + availableRelayServers.Add(keys[i], JsonConvert.DeserializeObject(serverStats)); - WriteLogMessage("Server " + server.Key + " does not exist anymore, removing", ConsoleColor.Red); - availableRelayServers.Remove(server.Key); + } + catch (Exception ex) + { + // server doesnt exist anymore probably + // do more shit here + WriteLogMessage("Server " + keys[i] + " does not exist anymore, removing", ConsoleColor.Red); + availableRelayServers.Remove(keys[i]); + } } } @@ -122,13 +127,24 @@ namespace LightReflectiveMirror.LoadBalancing void WriteTitle() { string t = @" - w c(..)o ( - _ _____ __ __ \__(-) __) - | | | __ \ | \/ | /\ ( - | | | |__) || \ / | /(_)___) - | | | _ / | |\/| | w /| - | |____ | | \ \ | | | | | \ - |______||_| \_\|_| |_| m m copyright monkesoft 2021 + _ _____ __ __ + | | | __ \ | \/ | + | | | |__) | | \ / | + | | | _ / | |\/| | + | |____ | | \ \ | | | | w c(..)o ( + |______| |_| \_\ |_| |_| \__(-) __) + _ ____ _____ /\ ( + | | / __ \ /\ | __ \ /(_)___) + | | | | | | / \ | | | | w /| + | | | | | | / /\ \ | | | | | \ + | |____ | |__| | / ____ \ | |__| | m m copyright monkesoft 2021 + |______| \____/ /_/ \_\ |_____/ + ____ _ _ _ _____ ______ _____ + | _ \ /\ | | /\ | \ | | / ____| | ____| | __ \ + | |_) | / \ | | / \ | \| | | | | |__ | |__) | + | _ < / /\ \ | | / /\ \ | . ` | | | | __| | _ / + | |_) | / ____ \ | |____ / ____ \ | |\ | | |____ | |____ | | \ \ + |____/ /_/ \_\ |______| /_/ \_\ |_| \_| \_____| |______| |_| \_\ "; string load = $"Chimp Event Listener Initializing... OK" + diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Program.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Program.cs index 6098630..4650955 100644 --- a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Program.cs +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Program.cs @@ -230,13 +230,13 @@ namespace LightReflectiveMirror HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(uri); myRequest.Headers.Add("Auth", "AuthKey"); - myRequest.Headers.Add("Address", externalip + ":" + port); + myRequest.Headers.Add("Port", port); WebResponse myResponse = await myRequest.GetResponseAsync(); return true; } - catch (Exception e) + catch { // error adding or load balancer unavailable WriteLogMessage("Error registering", ConsoleColor.Red);