mirror of
https://github.com/Stone-Red-Code/HyperbolicDownloader.git
synced 2026-09-04 09:06:10 +02:00
- Improve some messages of failed commands
- Add `remove host` command
This commit is contained in:
@@ -16,7 +16,7 @@ internal class HostsManager
|
|||||||
{
|
{
|
||||||
foreach (NetworkSocket host in hosts)
|
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);
|
this.hosts.Add(host);
|
||||||
}
|
}
|
||||||
@@ -27,7 +27,7 @@ internal class HostsManager
|
|||||||
|
|
||||||
public void Add(NetworkSocket host)
|
public void Add(NetworkSocket host)
|
||||||
{
|
{
|
||||||
if (!hosts.Any(x => x.IPAddress == host.IPAddress && x.Port == host.Port))
|
if (!Contains(host))
|
||||||
{
|
{
|
||||||
hosts.Add(host);
|
hosts.Add(host);
|
||||||
}
|
}
|
||||||
@@ -43,6 +43,11 @@ internal class HostsManager
|
|||||||
SaveHosts();
|
SaveHosts();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool Contains(NetworkSocket host)
|
||||||
|
{
|
||||||
|
return hosts.Any(x => x.IPAddress == host.IPAddress && x.Port == host.Port);
|
||||||
|
}
|
||||||
|
|
||||||
public int CheckHostsActivity()
|
public int CheckHostsActivity()
|
||||||
{
|
{
|
||||||
List<NetworkSocket> hostsToRemove = new List<NetworkSocket>();
|
List<NetworkSocket> hostsToRemove = new List<NetworkSocket>();
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ internal class FileCommands
|
|||||||
|
|
||||||
if (filesManager.TryRemove(hash))
|
if (filesManager.TryRemove(hash))
|
||||||
{
|
{
|
||||||
ConsoleExt.WriteLine($"Removed file successfully!", ConsoleColor.Green);
|
ConsoleExt.WriteLine($"Successfully removed file!", ConsoleColor.Green);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -39,7 +39,15 @@ internal class HostCommands
|
|||||||
public void ListHosts(string _)
|
public void ListHosts(string _)
|
||||||
{
|
{
|
||||||
int index = 0;
|
int index = 0;
|
||||||
foreach (NetworkSocket host in hostsManager.ToList())
|
List<NetworkSocket> hosts = hostsManager.ToList();
|
||||||
|
|
||||||
|
if (hosts.Count == 0)
|
||||||
|
{
|
||||||
|
ConsoleExt.WriteLine("No known hosts", ConsoleColor.DarkYellow);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (NetworkSocket host in hosts)
|
||||||
{
|
{
|
||||||
index++;
|
index++;
|
||||||
Console.WriteLine($"{index}) {host.IPAddress}:{host.Port}");
|
Console.WriteLine($"{index}) {host.IPAddress}:{host.Port}");
|
||||||
@@ -49,6 +57,44 @@ internal class HostCommands
|
|||||||
Console.CursorTop--;
|
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)
|
public void AddHost(string args)
|
||||||
{
|
{
|
||||||
string[] parts = args.Split(":");
|
string[] parts = args.Split(":");
|
||||||
|
|||||||
@@ -37,7 +37,9 @@ internal class InputHandler
|
|||||||
addCommand.Register(hostCommands.AddHost, "host");
|
addCommand.Register(hostCommands.AddHost, "host");
|
||||||
addCommand.Register(fileCommands.AddFile, "file");
|
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");
|
Command listCommand = commander.Register(fileCommands.ListFiles, "list", "ls");
|
||||||
listCommand.Register(fileCommands.ListFiles, "files");
|
listCommand.Register(fileCommands.ListFiles, "files");
|
||||||
@@ -57,9 +59,16 @@ internal class InputHandler
|
|||||||
string input = Console.ReadLine()?.Trim() ?? string.Empty;
|
string input = Console.ReadLine()?.Trim() ?? string.Empty;
|
||||||
|
|
||||||
Console.CursorVisible = false;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user