using HyperbolicDowloader.Networking; using Stone_Red_Utilities.ConsoleExtentions; using System.Net.Sockets; using System.Text.Json; namespace HyperbolicDowloader; internal class HostsManager { private readonly List hosts = new List(); public int Count => hosts.Count; public void AddRange(IEnumerable hosts) { foreach (NetworkSocket host in hosts) { if (!this.hosts.Any(x => x.IPAddress == host.IPAddress && x.Port == host.Port)) { this.hosts.Add(host); } } SaveHosts(); } public void Add(NetworkSocket host) { if (!hosts.Any(x => x.IPAddress == host.IPAddress && x.Port == host.Port)) { hosts.Add(host); } SaveHosts(); } public void Remove(NetworkSocket host) { hosts.RemoveAll(x => x.IPAddress == host.IPAddress && x.Port == host.Port); SaveHosts(); } public void RemoveInactiveHosts() { List hostsToRemove = new List(); foreach (NetworkSocket host in hosts) { ConsoleExt.Write($"{host.IPAddress}:{host.Port} > ???", ConsoleColor.DarkYellow); using TcpClient tcpClient = new TcpClient(); try { tcpClient.ConnectAsync(host.IPAddress, host.Port).Wait(1000); Console.CursorLeft = 0; if (tcpClient.Connected) { ConsoleExt.WriteLine($"{host.IPAddress}:{host.Port} > Active", ConsoleColor.Green); } else { hostsToRemove.Add(host); ConsoleExt.WriteLine($"{host.IPAddress}:{host.Port} > Inactive", ConsoleColor.Red); } } catch { hostsToRemove.Add(host); Console.CursorLeft = 0; ConsoleExt.WriteLine($"{host.IPAddress}:{host.Port} > Inactive", ConsoleColor.Red); } } foreach (NetworkSocket host in hostsToRemove) { hosts.Remove(host); } SaveHosts(); } public List ToList() { return hosts.ToList(); } private void SaveHosts() { File.WriteAllText(Program.HostsFilePath, JsonSerializer.Serialize(hosts)); } }