Fixed load balancer http issues
This commit is contained in:
parent
d6f0d9ca63
commit
af712e4f1f
5 changed files with 92 additions and 49 deletions
|
|
@ -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<LoggerFilterOptions>(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
|
||||
<Costura />
|
||||
</Weavers>
|
||||
|
|
@ -6,6 +6,10 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Costura.Fody" Version="5.1.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Grapevine" Version="5.0.0-rc.5" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
</ItemGroup>
|
||||
|
|
|
|||
|
|
@ -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<RelayStats> ManualPingServer(string serverIP)
|
||||
async Task<RelayStats?> 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<RelayStats>(reader.ReadToEnd());
|
||||
}
|
||||
catch (WebException ex)
|
||||
{
|
||||
// server doesnt exist anymore probably
|
||||
// do more shit here
|
||||
|
||||
return new RelayStats { PublicRoomCount = -1 };
|
||||
return JsonConvert.DeserializeObject<RelayStats>(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<string>(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<RelayStats>(reader.ReadToEnd()));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// server doesnt exist anymore probably
|
||||
// do more shit here
|
||||
if (availableRelayServers.ContainsKey(keys[i]))
|
||||
availableRelayServers[keys[i]] = JsonConvert.DeserializeObject<RelayStats>(serverStats);
|
||||
else
|
||||
availableRelayServers.Add(keys[i], JsonConvert.DeserializeObject<RelayStats>(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" +
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue