2021-09-07 18:03:13 +02:00
|
|
|
|
using Flowframes.IO;
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-07 14:51:53 +02:00
|
|
|
|
public static async Task 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
|
|
|
|
|
|
{
|
|
|
|
|
|
Ping p = new Ping();
|
|
|
|
|
|
PingReply replyEur = p.Send(server.host, 2000);
|
2021-09-07 14:51:53 +02:00
|
|
|
|
serversPings[new string[] { server.name, server.host, server.pattern }] = replyEur.RoundtripTime;
|
2021-08-31 15:57:48 +02:00
|
|
|
|
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);
|
2021-09-07 14:51:53 +02:00
|
|
|
|
serversPings[new string[] { server.name, server.host, server.pattern }] = 10000;
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|