2024-10-16 13:38:15 +02:00
|
|
|
|
using Flowframes.Forms;
|
|
|
|
|
|
using Flowframes.IO;
|
2021-09-07 18:03:13 +02:00
|
|
|
|
using System;
|
2021-08-31 15:57:48 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Net.NetworkInformation;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2021-09-07 14:51:53 +02:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
2021-08-31 15:57:48 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Flowframes.Data
|
|
|
|
|
|
{
|
|
|
|
|
|
class Servers
|
|
|
|
|
|
{
|
2021-09-07 14:51:53 +02:00
|
|
|
|
public static Server hetznerEu = new Server { name = "Germany (Nürnberg)", host = "nmkd-hz.de", pattern = "https://dl.*" };
|
|
|
|
|
|
public static Server contaboUs = new Server { name = "USA (St. Louis)", host = "nmkd-cb.de", pattern = "https://dl.*" };
|
2021-08-31 15:57:48 +02:00
|
|
|
|
|
|
|
|
|
|
public static List<Server> serverList = new List<Server> { hetznerEu, contaboUs };
|
|
|
|
|
|
|
2021-09-07 18:03:13 +02:00
|
|
|
|
private static Server closestServer = serverList[0];
|
2021-08-31 15:57:48 +02:00
|
|
|
|
|
|
|
|
|
|
public class Server
|
|
|
|
|
|
{
|
2021-09-07 14:51:53 +02:00
|
|
|
|
public string name = "";
|
2021-08-31 15:57:48 +02:00
|
|
|
|
public string host = "";
|
|
|
|
|
|
public string pattern = "*";
|
|
|
|
|
|
|
2021-09-07 14:51:53 +02:00
|
|
|
|
public string GetUrl()
|
2021-08-31 15:57:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
return pattern.Replace("*", host);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-16 13:38:15 +02:00
|
|
|
|
public static async Task InitAsync ()
|
|
|
|
|
|
{
|
|
|
|
|
|
await Task.Run(() => Init());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-25 20:22:24 +01:00
|
|
|
|
private const int _defaultPing = 10000;
|
|
|
|
|
|
|
2024-10-16 13:38:15 +02:00
|
|
|
|
public static void Init(ComboBox comboBox = null)
|
2021-08-31 15:57:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
Dictionary<string[], long> serversPings = new Dictionary<string[], long>();
|
|
|
|
|
|
|
2021-09-07 14:51:53 +02:00
|
|
|
|
foreach (Server server in serverList)
|
2021-08-31 15:57:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2024-11-25 20:22:24 +01:00
|
|
|
|
long ping = _defaultPing;
|
|
|
|
|
|
int attempts = 0;
|
|
|
|
|
|
|
|
|
|
|
|
while(ping == _defaultPing && attempts < 3)
|
|
|
|
|
|
{
|
|
|
|
|
|
Ping p = new Ping();
|
|
|
|
|
|
PingReply pReply = p.Send(server.host, 3000);
|
|
|
|
|
|
|
|
|
|
|
|
if(pReply.RoundtripTime > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
ping = pReply.RoundtripTime;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
attempts++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
serversPings[new string[] { server.name, server.host, server.pattern }] = ping;
|
|
|
|
|
|
Logger.Log($"[Servers] Ping to {server.host}: {ping} ms", true);
|
2021-08-31 15:57:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger.Log($"[Servers] Failed to ping {server.host}: {e.Message}", true);
|
2024-11-25 20:22:24 +01:00
|
|
|
|
serversPings[new string[] { server.name, server.host, server.pattern }] = _defaultPing;
|
2021-08-31 15:57:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var closest = serversPings.Aggregate((l, r) => l.Value < r.Value ? l : r);
|
|
|
|
|
|
Logger.Log($"[Servers] Closest Server: {closest.Key[0]} ({closest.Value} ms)", true);
|
2021-09-07 14:51:53 +02:00
|
|
|
|
closestServer = new Server { name = closest.Key[0], host = closest.Key[1], pattern = closest.Key[2] };
|
|
|
|
|
|
|
|
|
|
|
|
if (comboBox != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int i = 0; i < comboBox.Items.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (comboBox.Items[i].ToString() == closestServer.name)
|
|
|
|
|
|
comboBox.SelectedIndex = i;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-08-31 15:57:48 +02:00
|
|
|
|
}
|
2021-09-07 18:03:13 +02:00
|
|
|
|
|
|
|
|
|
|
public static Server GetServer ()
|
|
|
|
|
|
{
|
|
|
|
|
|
int server = Config.GetInt("serverCombox");
|
|
|
|
|
|
|
|
|
|
|
|
if (server == 0)
|
|
|
|
|
|
return closestServer;
|
|
|
|
|
|
else
|
|
|
|
|
|
return serverList[server - 1];
|
|
|
|
|
|
}
|
2021-08-31 15:57:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|