Merge branch 'main' of https://github.com/Derek-R-S/Light-Reflective-Mirror into main
This commit is contained in:
commit
35704ea649
4 changed files with 62 additions and 37 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
using Grapevine;
|
using Grapevine;
|
||||||
|
using LightReflectiveMirror.Debug;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
@ -38,21 +39,18 @@ namespace LightReflectiveMirror.LoadBalancing
|
||||||
public async Task ReceiveAuthKey(IHttpContext context)
|
public async Task ReceiveAuthKey(IHttpContext context)
|
||||||
{
|
{
|
||||||
var req = context.Request;
|
var req = context.Request;
|
||||||
string receivedAuthKey = req.Headers["Auth"];
|
string receivedAuthKey = req.Headers["x-Auth"];
|
||||||
string endpointPort = req.Headers["EndpointPort"];
|
string endpointPort = req.Headers["x-EndpointPort"];
|
||||||
string gamePort = req.Headers["GamePort"];
|
string gamePort = req.Headers["x-GamePort"];
|
||||||
string publicIP = req.Headers["PIP"];
|
string publicIP = req.Headers["x-PIP"];
|
||||||
|
|
||||||
string address = context.Request.RemoteEndPoint.Address.ToString();
|
string address = context.Request.RemoteEndPoint.Address.ToString();
|
||||||
|
Logger.WriteLogMessage("Received auth req [" + receivedAuthKey + "] == [" + Program.conf.AuthKey + "]");
|
||||||
if(Program.showDebugLogs)
|
|
||||||
Console.WriteLine("Received auth req [" + receivedAuthKey + "] == [" + Program.conf.AuthKey + "]");
|
|
||||||
|
|
||||||
// if server is authenticated
|
// if server is authenticated
|
||||||
if (receivedAuthKey != null && address != null && endpointPort != null && gamePort != null && receivedAuthKey == Program.conf.AuthKey)
|
if (receivedAuthKey != null && address != null && endpointPort != null && gamePort != null && receivedAuthKey == Program.conf.AuthKey)
|
||||||
{
|
{
|
||||||
if(Program.showDebugLogs)
|
Logger.WriteLogMessage($"Server accepted: {address}:{gamePort}");
|
||||||
Console.WriteLine($"Server accepted: {address}:{gamePort}");
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
@ -181,7 +179,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
||||||
}, (server) =>
|
}, (server) =>
|
||||||
{
|
{
|
||||||
server.Prefixes.Add($"http://{GetLocalIp()}:{port}/");
|
server.Prefixes.Add($"http://{GetLocalIp()}:{port}/");
|
||||||
server.Prefixes.Add($"http://127.0.0.1:{port}/");
|
server.Prefixes.Add($"http://*:{port}/");
|
||||||
}).Build();
|
}).Build();
|
||||||
|
|
||||||
server.Router.Options.SendExceptionMessages = false;
|
server.Router.Options.SendExceptionMessages = false;
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
using LightReflectiveMirror.Debug;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
@ -30,26 +31,29 @@ namespace LightReflectiveMirror.LoadBalancing
|
||||||
public async Task MainAsync()
|
public async Task MainAsync()
|
||||||
{
|
{
|
||||||
WriteTitle();
|
WriteTitle();
|
||||||
|
|
||||||
instance = this;
|
instance = this;
|
||||||
startupTime = DateTime.Now;
|
startupTime = DateTime.Now;
|
||||||
|
|
||||||
if (!File.Exists(CONFIG_PATH))
|
if (!File.Exists(CONFIG_PATH))
|
||||||
{
|
{
|
||||||
File.WriteAllText(CONFIG_PATH, JsonConvert.SerializeObject(new Config(), Formatting.Indented));
|
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();
|
Console.ReadKey();
|
||||||
Environment.Exit(0);
|
Environment.Exit(0);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
conf = JsonConvert.DeserializeObject<Config>(File.ReadAllText(CONFIG_PATH));
|
conf = JsonConvert.DeserializeObject<Config>(File.ReadAllText(CONFIG_PATH));
|
||||||
|
Logger.ConfigureLogger(new Logger.LogConfiguration { sendLogs = conf.ShowDebugLogs });
|
||||||
|
|
||||||
_pingDelay = conf.ConnectedServerPingRate;
|
_pingDelay = conf.ConnectedServerPingRate;
|
||||||
showDebugLogs = conf.ShowDebugLogs;
|
showDebugLogs = conf.ShowDebugLogs;
|
||||||
|
|
||||||
if (new EndpointServer().Start(conf.EndpointPort))
|
if (new EndpointServer().Start(conf.EndpointPort))
|
||||||
WriteLogMessage("Endpoint server started successfully", ConsoleColor.Green);
|
Logger.ForceLogMessage("Endpoint server started successfully", ConsoleColor.Green);
|
||||||
else
|
else
|
||||||
WriteLogMessage("Endpoint server started unsuccessfully", ConsoleColor.Red);
|
Logger.ForceLogMessage("Endpoint server started unsuccessfully", ConsoleColor.Red);
|
||||||
}
|
}
|
||||||
|
|
||||||
var pingThread = new Thread(new ThreadStart(PingServers));
|
var pingThread = new Thread(new ThreadStart(PingServers));
|
||||||
|
|
@ -66,7 +70,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
||||||
|
|
||||||
if (availableRelayServers.ContainsKey(relayAddr))
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -115,8 +119,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (showDebugLogs)
|
Logger.WriteLogMessage("Pinging " + availableRelayServers.Count + " available relays");
|
||||||
WriteLogMessage("Pinging " + availableRelayServers.Count + " available relays");
|
|
||||||
|
|
||||||
// Create a new list so we can modify the collection in our loop.
|
// Create a new list so we can modify the collection in our loop.
|
||||||
var keys = new List<RelayAddress>(availableRelayServers.Keys);
|
var keys = new List<RelayAddress>(availableRelayServers.Keys);
|
||||||
|
|
@ -132,8 +135,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
||||||
var serverStats = wc.DownloadString(url);
|
var serverStats = wc.DownloadString(url);
|
||||||
var deserializedData = JsonConvert.DeserializeObject<RelayServerInfo>(serverStats);
|
var deserializedData = JsonConvert.DeserializeObject<RelayServerInfo>(serverStats);
|
||||||
|
|
||||||
if (showDebugLogs)
|
Logger.ForceLogMessage("Server " + keys[i].Address + " still exists, keeping in collection.");
|
||||||
WriteLogMessage("Server " + keys[i].Address + " still exists, keeping in collection.");
|
|
||||||
|
|
||||||
if (availableRelayServers.ContainsKey(keys[i]))
|
if (availableRelayServers.ContainsKey(keys[i]))
|
||||||
availableRelayServers[keys[i]] = deserializedData;
|
availableRelayServers[keys[i]] = deserializedData;
|
||||||
|
|
@ -143,9 +145,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
// server doesnt exist anymore probably
|
// server doesnt exist anymore probably
|
||||||
// do more shit here
|
Logger.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]);
|
availableRelayServers.Remove(keys[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -182,19 +182,9 @@ namespace LightReflectiveMirror.LoadBalancing
|
||||||
"\nHarambe Memorial Initializing... OK" +
|
"\nHarambe Memorial Initializing... OK" +
|
||||||
"\nBananas Initializing... OK\n";
|
"\nBananas Initializing... OK\n";
|
||||||
|
|
||||||
WriteLogMessage(t, ConsoleColor.Green);
|
Logger.ForceLogMessage(t, ConsoleColor.Green);
|
||||||
WriteLogMessage(load, ConsoleColor.Cyan);
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -215,10 +215,10 @@ namespace LightReflectiveMirror
|
||||||
string gamePort = 7777.ToString();
|
string gamePort = 7777.ToString();
|
||||||
HttpWebRequest authReq = (HttpWebRequest)WebRequest.Create(uri);
|
HttpWebRequest authReq = (HttpWebRequest)WebRequest.Create(uri);
|
||||||
|
|
||||||
authReq.Headers.Add("Auth", conf.LoadBalancerAuthKey);
|
authReq.Headers.Add("x-Auth", conf.LoadBalancerAuthKey);
|
||||||
authReq.Headers.Add("EndpointPort", endpointPort);
|
authReq.Headers.Add("x-EndpointPort", endpointPort);
|
||||||
authReq.Headers.Add("GamePort", gamePort);
|
authReq.Headers.Add("x-GamePort", gamePort);
|
||||||
authReq.Headers.Add("PIP", publicIP); // Public IP
|
authReq.Headers.Add("x-PIP", publicIP); // Public IP
|
||||||
|
|
||||||
var res = await authReq.GetResponseAsync();
|
var res = await authReq.GetResponseAsync();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue