From e9cc15011ba6bcc1070bd89e42e3dc7cad3ddac3 Mon Sep 17 00:00:00 2001 From: cxxpxr <60411087+cxxpxr@users.noreply.github.com> Date: Tue, 6 Apr 2021 18:31:48 -0400 Subject: [PATCH 1/4] Update Endpoint.cs --- .../LRM_LoadBalancer/Endpoint.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs index 8fe741d..c1e97df 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs @@ -181,7 +181,7 @@ namespace LightReflectiveMirror.LoadBalancing }, (server) => { server.Prefixes.Add($"http://{GetLocalIp()}:{port}/"); - server.Prefixes.Add($"http://127.0.0.1:{port}/"); + server.Prefixes.Add($"http://*:{port}/"); }).Build(); server.Router.Options.SendExceptionMessages = false; From 41166cb82d8f0a0d273dac73777f366ac98f3b82 Mon Sep 17 00:00:00 2001 From: cxxpxr <60411087+cxxpxr@users.noreply.github.com> Date: Tue, 6 Apr 2021 18:53:27 -0400 Subject: [PATCH 2/4] Dedicated Logger class --- .../LRM_LoadBalancer/Endpoint.cs | 8 ++-- .../LRM_LoadBalancer/Logger.cs | 37 +++++++++++++++++++ .../LRM_LoadBalancer/Program.cs | 36 +++++++----------- 3 files changed, 53 insertions(+), 28 deletions(-) create mode 100644 LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Logger.cs diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs index c1e97df..abc6146 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs @@ -1,4 +1,5 @@ using Grapevine; +using LightReflectiveMirror.Debug; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -44,15 +45,12 @@ namespace LightReflectiveMirror.LoadBalancing string publicIP = req.Headers["PIP"]; string address = context.Request.RemoteEndPoint.Address.ToString(); - - if(Program.showDebugLogs) - Console.WriteLine("Received auth req [" + receivedAuthKey + "] == [" + Program.conf.AuthKey + "]"); + Logger.WriteLogMessage("Received auth req [" + receivedAuthKey + "] == [" + Program.conf.AuthKey + "]"); // if server is authenticated if (receivedAuthKey != null && address != null && endpointPort != null && gamePort != null && receivedAuthKey == Program.conf.AuthKey) { - if(Program.showDebugLogs) - Console.WriteLine($"Server accepted: {address}:{gamePort}"); + Logger.WriteLogMessage($"Server accepted: {address}:{gamePort}"); try { diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Logger.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Logger.cs new file mode 100644 index 0000000..7b3ef6b --- /dev/null +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Logger.cs @@ -0,0 +1,37 @@ +using System; + +namespace LightReflectiveMirror.Debug +{ + public static class Logger + { + private static LogConfiguration conf; + + public static void ConfigureLogger(LogConfiguration config) => conf = config; + + public static void WriteLogMessage(string message, ConsoleColor color = ConsoleColor.White, bool oneLine = false) + { + if(!conf.sendLogs) { return; } + + Console.ForegroundColor = color; + if (oneLine) + Console.Write(message); + else + Console.WriteLine(message); + } + + public static void ForceLogMessage(string message, ConsoleColor color = ConsoleColor.White, bool oneLine = false) + { + Console.ForegroundColor = color; + + if (oneLine) + Console.Write(message); + else + Console.WriteLine(message); + } + + public struct LogConfiguration + { + public bool sendLogs; + } + } +} diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs index bf2f0c1..0f7d0ec 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs @@ -1,3 +1,4 @@ +using LightReflectiveMirror.Debug; using Newtonsoft.Json; using System; using System.Collections.Generic; @@ -30,26 +31,29 @@ namespace LightReflectiveMirror.LoadBalancing public async Task MainAsync() { WriteTitle(); + instance = this; startupTime = DateTime.Now; if (!File.Exists(CONFIG_PATH)) { File.WriteAllText(CONFIG_PATH, JsonConvert.SerializeObject(new Config(), Formatting.Indented)); - WriteLogMessage("A config.json file was generated. Please configure it to the proper settings and re-run!", ConsoleColor.Yellow); + Logger.ForceLogMessage("A config.json file was generated. Please configure it to the proper settings and re-run!", ConsoleColor.Yellow); Console.ReadKey(); Environment.Exit(0); } else { conf = JsonConvert.DeserializeObject(File.ReadAllText(CONFIG_PATH)); + Logger.ConfigureLogger(new Logger.LogConfiguration { sendLogs = conf.ShowDebugLogs }); + _pingDelay = conf.ConnectedServerPingRate; showDebugLogs = conf.ShowDebugLogs; if (new EndpointServer().Start(conf.EndpointPort)) - WriteLogMessage("Endpoint server started successfully", ConsoleColor.Green); + Logger.ForceLogMessage("Endpoint server started successfully", ConsoleColor.Green); else - WriteLogMessage("Endpoint server started unsuccessfully", ConsoleColor.Red); + Logger.ForceLogMessage("Endpoint server started unsuccessfully", ConsoleColor.Red); } var pingThread = new Thread(new ThreadStart(PingServers)); @@ -66,7 +70,7 @@ namespace LightReflectiveMirror.LoadBalancing if (availableRelayServers.ContainsKey(relayAddr)) { - WriteLogMessage($"LRM Node {serverIP}:{port} tried to register while already registered!"); + Logger.ForceLogMessage($"LRM Node {serverIP}:{port} tried to register while already registered!"); return; } @@ -115,8 +119,7 @@ namespace LightReflectiveMirror.LoadBalancing { while (true) { - if (showDebugLogs) - WriteLogMessage("Pinging " + availableRelayServers.Count + " available relays"); + Logger.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); @@ -132,8 +135,7 @@ namespace LightReflectiveMirror.LoadBalancing var serverStats = wc.DownloadString(url); var deserializedData = JsonConvert.DeserializeObject(serverStats); - if (showDebugLogs) - WriteLogMessage("Server " + keys[i].Address + " still exists, keeping in collection."); + Logger.ForceLogMessage("Server " + keys[i].Address + " still exists, keeping in collection."); if (availableRelayServers.ContainsKey(keys[i])) availableRelayServers[keys[i]] = deserializedData; @@ -143,9 +145,7 @@ namespace LightReflectiveMirror.LoadBalancing catch (Exception ex) { // server doesnt exist anymore probably - // do more shit here - if (showDebugLogs) - WriteLogMessage("Server " + keys[i] + " does not exist anymore, removing", ConsoleColor.Red); + Logger.WriteLogMessage("Server " + keys[i] + " does not exist anymore, removing", ConsoleColor.Red); availableRelayServers.Remove(keys[i]); } } @@ -182,19 +182,9 @@ namespace LightReflectiveMirror.LoadBalancing "\nHarambe Memorial Initializing... OK" + "\nBananas Initializing... OK\n"; - WriteLogMessage(t, ConsoleColor.Green); - WriteLogMessage(load, ConsoleColor.Cyan); + Logger.ForceLogMessage(t, ConsoleColor.Green); + Logger.ForceLogMessage(load, ConsoleColor.Cyan); } - - static void WriteLogMessage(string message, ConsoleColor color = ConsoleColor.White, bool oneLine = false) - { - Console.ForegroundColor = color; - if (oneLine) - Console.Write(message); - else - Console.WriteLine(message); - } - } } From 084a8ab0c13408412172dfce422e61712a75df28 Mon Sep 17 00:00:00 2001 From: cxxpxr <60411087+cxxpxr@users.noreply.github.com> Date: Tue, 6 Apr 2021 19:00:53 -0400 Subject: [PATCH 3/4] Append headers --- .../LRM_LoadBalancer/Endpoint.cs | 8 ++++---- .../LRM/Program/Program.cs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs index abc6146..db0963e 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs @@ -39,10 +39,10 @@ namespace LightReflectiveMirror.LoadBalancing public async Task ReceiveAuthKey(IHttpContext context) { var req = context.Request; - string receivedAuthKey = req.Headers["Auth"]; - string endpointPort = req.Headers["EndpointPort"]; - string gamePort = req.Headers["GamePort"]; - string publicIP = req.Headers["PIP"]; + string receivedAuthKey = req.Headers["x-Auth"]; + string endpointPort = req.Headers["x-EndpointPort"]; + string gamePort = req.Headers["x-GamePort"]; + string publicIP = req.Headers["x-PIP"]; string address = context.Request.RemoteEndPoint.Address.ToString(); Logger.WriteLogMessage("Received auth req [" + receivedAuthKey + "] == [" + Program.conf.AuthKey + "]"); diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Program/Program.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Program/Program.cs index bf18d58..d6254e9 100644 --- a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Program/Program.cs +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Program/Program.cs @@ -215,10 +215,10 @@ namespace LightReflectiveMirror string gamePort = 7777.ToString(); HttpWebRequest authReq = (HttpWebRequest)WebRequest.Create(uri); - authReq.Headers.Add("Auth", conf.LoadBalancerAuthKey); - authReq.Headers.Add("EndpointPort", endpointPort); - authReq.Headers.Add("GamePort", gamePort); - authReq.Headers.Add("PIP", publicIP); // Public IP + authReq.Headers.Add("x-Auth", conf.LoadBalancerAuthKey); + authReq.Headers.Add("x-EndpointPort", endpointPort); + authReq.Headers.Add("x-GamePort", gamePort); + authReq.Headers.Add("x-PIP", publicIP); // Public IP var res = await authReq.GetResponseAsync(); From 31b9accb3b9548a28032c38164380e197ea12636 Mon Sep 17 00:00:00 2001 From: cxxpxr <60411087+cxxpxr@users.noreply.github.com> Date: Tue, 6 Apr 2021 19:09:29 -0400 Subject: [PATCH 4/4] Update Logger.cs --- .../LRM_LoadBalancer/Logger.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Logger.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Logger.cs index 7b3ef6b..d289b93 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Logger.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Logger.cs @@ -4,13 +4,13 @@ namespace LightReflectiveMirror.Debug { public static class Logger { - private static LogConfiguration conf; + private static LogConfiguration _conf; - public static void ConfigureLogger(LogConfiguration config) => conf = config; + public static void ConfigureLogger(LogConfiguration config) => _conf = config; public static void WriteLogMessage(string message, ConsoleColor color = ConsoleColor.White, bool oneLine = false) { - if(!conf.sendLogs) { return; } + if(!_conf.sendLogs) { return; } Console.ForegroundColor = color; if (oneLine)