diff --git a/HyperbolicDownloader/Networking/HostsManager.cs b/HyperbolicDownloader/Networking/HostsManager.cs index e7f4b18..88266f3 100644 --- a/HyperbolicDownloader/Networking/HostsManager.cs +++ b/HyperbolicDownloader/Networking/HostsManager.cs @@ -16,7 +16,7 @@ internal class HostsManager { foreach (NetworkSocket host in hosts) { - if (!this.hosts.Any(x => x.IPAddress == host.IPAddress && x.Port == host.Port)) + if (!Contains(host)) { this.hosts.Add(host); } @@ -27,7 +27,7 @@ internal class HostsManager public void Add(NetworkSocket host) { - if (!hosts.Any(x => x.IPAddress == host.IPAddress && x.Port == host.Port)) + if (!Contains(host)) { hosts.Add(host); } @@ -43,6 +43,11 @@ internal class HostsManager SaveHosts(); } + public bool Contains(NetworkSocket host) + { + return hosts.Any(x => x.IPAddress == host.IPAddress && x.Port == host.Port); + } + public int CheckHostsActivity() { List hostsToRemove = new List(); diff --git a/HyperbolicDownloader/UserInterface/Commands/FileCommands.cs b/HyperbolicDownloader/UserInterface/Commands/FileCommands.cs index 88f684b..2d64795 100644 --- a/HyperbolicDownloader/UserInterface/Commands/FileCommands.cs +++ b/HyperbolicDownloader/UserInterface/Commands/FileCommands.cs @@ -39,7 +39,7 @@ internal class FileCommands if (filesManager.TryRemove(hash)) { - ConsoleExt.WriteLine($"Removed file successfully!", ConsoleColor.Green); + ConsoleExt.WriteLine($"Successfully removed file!", ConsoleColor.Green); } else { diff --git a/HyperbolicDownloader/UserInterface/Commands/HostCommands.cs b/HyperbolicDownloader/UserInterface/Commands/HostCommands.cs index dd4905f..e3947d9 100644 --- a/HyperbolicDownloader/UserInterface/Commands/HostCommands.cs +++ b/HyperbolicDownloader/UserInterface/Commands/HostCommands.cs @@ -39,7 +39,15 @@ internal class HostCommands public void ListHosts(string _) { int index = 0; - foreach (NetworkSocket host in hostsManager.ToList()) + List hosts = hostsManager.ToList(); + + if (hosts.Count == 0) + { + ConsoleExt.WriteLine("No known hosts", ConsoleColor.DarkYellow); + return; + } + + foreach (NetworkSocket host in hosts) { index++; Console.WriteLine($"{index}) {host.IPAddress}:{host.Port}"); @@ -49,6 +57,44 @@ internal class HostCommands Console.CursorTop--; } + public void RemoveHost(string args) + { + string[] parts = args.Split(":"); + + if (parts.Length != 2) + { + ConsoleExt.WriteLine("Invalid format! Use this format: (xxx.xxx.xxx.xxx:yyyy)", ConsoleColor.Red); + return; + } + + string ipAddressInput = parts[0]; + string portInput = parts[1]; + + _ = int.TryParse(portInput, out int port); + if (port < 1000 || port >= 6000) + { + ConsoleExt.WriteLine("Invalid port number!", ConsoleColor.Red); + return; + } + + if (!IPAddress.TryParse(ipAddressInput, out IPAddress? ipAddress)) + { + ConsoleExt.WriteLine("Invalid IP address!", ConsoleColor.Red); + return; + } + + NetworkSocket hostToRemove = new NetworkSocket(ipAddress.ToString(), port, DateTime.MinValue); + + if (!hostsManager.Contains(hostToRemove)) + { + ConsoleExt.WriteLine("Host not in list", ConsoleColor.Red); + return; + } + + hostsManager.Remove(new NetworkSocket(ipAddress.ToString(), port, DateTime.MinValue), true); + ConsoleExt.WriteLine($"Successfully Removed host!", ConsoleColor.Green); + } + public void AddHost(string args) { string[] parts = args.Split(":"); diff --git a/HyperbolicDownloader/UserInterface/InputHandler.cs b/HyperbolicDownloader/UserInterface/InputHandler.cs index 9552656..43e438e 100644 --- a/HyperbolicDownloader/UserInterface/InputHandler.cs +++ b/HyperbolicDownloader/UserInterface/InputHandler.cs @@ -37,7 +37,9 @@ internal class InputHandler addCommand.Register(hostCommands.AddHost, "host"); addCommand.Register(fileCommands.AddFile, "file"); - commander.Register(fileCommands.RemoveFile, "remove", "rm"); + Command removeCommand = commander.Register(fileCommands.RemoveFile, "remove", "rm"); + removeCommand.Register(hostCommands.RemoveHost, "host"); + removeCommand.Register(fileCommands.RemoveFile, "file"); Command listCommand = commander.Register(fileCommands.ListFiles, "list", "ls"); listCommand.Register(fileCommands.ListFiles, "files"); @@ -57,9 +59,16 @@ internal class InputHandler string input = Console.ReadLine()?.Trim() ?? string.Empty; Console.CursorVisible = false; - if (!commander.Execute(input)) + try { - ConsoleExt.WriteLine("Unknown command!", ConsoleColor.Red); + if (!commander.Execute(input)) + { + ConsoleExt.WriteLine("Unknown command!", ConsoleColor.Red); + } + } + catch (Exception ex) + { + ConsoleExt.WriteLine($"An unexpected error occurred! {ex}", ConsoleColor.Red); } } }