This commit is contained in:
cxxpxr 2021-04-05 13:13:00 -04:00
parent e2833d53bb
commit 7ea91c9a90
4 changed files with 44 additions and 12 deletions

View file

@ -10,6 +10,7 @@ namespace LightReflectiveMirror.LoadBalancing
{
public int ConnectedServerPingRate = 10000;
public string AuthKey = "AuthKey";
public ushort EndpointPort = 8080;
public ushort EndpointPort = 7070;
public ushort RelayEndpointPort = 8080;
}
}

View file

@ -16,15 +16,18 @@ namespace LightReflectiveMirror.LoadBalancing
public async Task ReceiveAuthKey(IHttpContext context)
{
var req = context.Request.Headers;
string receivedAuthKey = req[0];
string address = req[1];
Console.WriteLine("Received auth req [" + req[0] + "] == [" + Program.conf.AuthKey+"]");
// if server is authenticated
if (req[0] == Program.conf.AuthKey)
if (receivedAuthKey == Program.conf.AuthKey)
{
var address = context.Request.RemoteEndPoint.Address.ToString();
await Program.instance.AddServer(address);
Console.WriteLine("Server accepted: " + address);
await Program.instance.AddServer(address);
await context.Response.SendResponseAsync(HttpStatusCode.Ok);
}
else
@ -40,6 +43,8 @@ namespace LightReflectiveMirror.LoadBalancing
[RestRoute("Get", "/api/join/")]
public async Task JoinRelay(IHttpContext context)
{
// need to copy over in order to avoid
// collection being modified while iterating.
var servers = Program.instance.availableRelayServers.ToList();
if(servers.Count == 0)
@ -48,8 +53,6 @@ namespace LightReflectiveMirror.LoadBalancing
return;
}
// need to copy over in order to avoid
// collection being modified while iterating.
KeyValuePair<string, RelayStats> lowest = new("Dummy", new RelayStats { ConnectedClients = int.MaxValue });
for (int i = 0; i < servers.Count; i++)
@ -68,7 +71,7 @@ namespace LightReflectiveMirror.LoadBalancing
public class EndpointServer
{
public bool Start(ushort port = 8080)
public bool Start(ushort port = 7070)
{
try
{

View file

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31025.194
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LRM_LoadBalancer", "LRM_LoadBalancer\LRM_LoadBalancer.csproj", "{B9B714BE-27A8-4815-933D-C122CA431750}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B9B714BE-27A8-4815-933D-C122CA431750}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B9B714BE-27A8-4815-933D-C122CA431750}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9B714BE-27A8-4815-933D-C122CA431750}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B9B714BE-27A8-4815-933D-C122CA431750}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {881733E9-B7F1-471A-9651-19257E8C56E7}
EndGlobalSection
EndGlobal

View file

@ -66,8 +66,7 @@ namespace LightReflectiveMirror.LoadBalancing
async Task<RelayStats> ManualPingServer(string serverIP)
{
string url = serverIP + API_PATH;
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://"+serverIP);
try
{
@ -76,7 +75,7 @@ namespace LightReflectiveMirror.LoadBalancing
return JsonConvert.DeserializeObject<RelayStats>(reader.ReadToEnd());
}
catch (Exception ex)
catch (WebException ex)
{
// server doesnt exist anymore probably
// do more shit here
@ -89,6 +88,8 @@ namespace LightReflectiveMirror.LoadBalancing
{
while (true)
{
WriteLogMessage("Pinging " + availableRelayServers.Count + " available relays");
foreach (var server in availableRelayServers)
{
string url = server + API_PATH;
@ -100,6 +101,7 @@ namespace LightReflectiveMirror.LoadBalancing
var reader = new StreamReader(response.GetResponseStream());
WriteLogMessage("Server " + server.Key + " still exists, keeping in collection.");
availableRelayServers.Remove(server.Key);
availableRelayServers.Add(server.Key, JsonConvert.DeserializeObject<RelayStats>(reader.ReadToEnd()));
}
@ -108,6 +110,7 @@ namespace LightReflectiveMirror.LoadBalancing
// server doesnt exist anymore probably
// do more shit here
WriteLogMessage("Server " + server.Key + " does not exist anymore, removing", ConsoleColor.Red);
availableRelayServers.Remove(server.Key);
}
}