mirror of
https://github.com/n00mkrad/flowframes.git
synced 2025-12-21 10:49:25 +01:00
56 lines
1.9 KiB
C#
56 lines
1.9 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Net.NetworkInformation;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
|
|||
|
|
namespace Flowframes.Data
|
|||
|
|
{
|
|||
|
|
class Servers
|
|||
|
|
{
|
|||
|
|
public static Server hetznerEu = new Server { host = "nmkd-hz.de", pattern = "https://dl.*" };
|
|||
|
|
public static Server contaboUs = new Server { host = "209.126.5.164", pattern = "http://*/dl" };
|
|||
|
|
|
|||
|
|
public static List<Server> serverList = new List<Server> { hetznerEu, contaboUs };
|
|||
|
|
|
|||
|
|
public static Server closestServer = serverList[0];
|
|||
|
|
|
|||
|
|
public class Server
|
|||
|
|
{
|
|||
|
|
public string host = "";
|
|||
|
|
public string pattern = "*";
|
|||
|
|
|
|||
|
|
public string GetUrl ()
|
|||
|
|
{
|
|||
|
|
return pattern.Replace("*", host);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static async Task Init ()
|
|||
|
|
{
|
|||
|
|
Dictionary<string[], long> serversPings = new Dictionary<string[], long>();
|
|||
|
|
|
|||
|
|
foreach(Server server in serverList)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
Ping p = new Ping();
|
|||
|
|
PingReply replyEur = p.Send(server.host, 2000);
|
|||
|
|
serversPings[new string[] { server.host, server.pattern }] = replyEur.RoundtripTime;
|
|||
|
|
Logger.Log($"[Servers] Ping to {server.host}: {replyEur.RoundtripTime} ms", true);
|
|||
|
|
}
|
|||
|
|
catch (Exception e)
|
|||
|
|
{
|
|||
|
|
Logger.Log($"[Servers] Failed to ping {server.host}: {e.Message}", true);
|
|||
|
|
serversPings[new string[] { server.host, server.pattern }] = 10000;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var closest = serversPings.Aggregate((l, r) => l.Value < r.Value ? l : r);
|
|||
|
|
Logger.Log($"[Servers] Closest Server: {closest.Key[0]} ({closest.Value} ms)", true);
|
|||
|
|
closestServer = new Server { host = closest.Key[0], pattern = closest.Key[1] };
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|