diff --git a/HyperbolicDownloader/InputHandler.cs b/HyperbolicDownloader/InputHandler.cs index 1e25aa6..7679148 100644 --- a/HyperbolicDownloader/InputHandler.cs +++ b/HyperbolicDownloader/InputHandler.cs @@ -43,7 +43,8 @@ internal class InputHandler _ = removeCommand.Register(hostCommands.RemoveHost, (HelpText)"Removes a host from the list of known hosts.", "host"); Command listCommand = commander.Register(fileCommands.ListFiles, (HelpText)"Lists all files.", "list", "ls"); - _ = listCommand.Register(fileCommands.ListFiles, (HelpText)"Lists all files.", "files"); + Command listFilesCommand = listCommand.Register(fileCommands.ListFiles, (HelpText)"Lists all files.", "files"); + _ = listFilesCommand.Register(fileCommands.ListFilesRemote, (HelpText)"Lists all files of another host.", "remote"); _ = listCommand.Register(hostCommands.ListHosts, (HelpText)"lists all hosts.", "hosts"); _ = commander.Register(hostCommands.CheckActiveHosts, (HelpText)"Checks the status of known hosts.", "status", "check"); diff --git a/HyperbolicDownloader/Program.cs b/HyperbolicDownloader/Program.cs index 90ce820..d036066 100644 --- a/HyperbolicDownloader/Program.cs +++ b/HyperbolicDownloader/Program.cs @@ -107,5 +107,6 @@ internal static class Program { ApiManager.ClosePorts(); apiManager.HostsManager.SaveHosts(); + Environment.Exit(0); } } \ No newline at end of file diff --git a/HyperbolicDownloaderApi/Commands/FileCommands.cs b/HyperbolicDownloaderApi/Commands/FileCommands.cs index 60d337f..20f2884 100644 --- a/HyperbolicDownloaderApi/Commands/FileCommands.cs +++ b/HyperbolicDownloaderApi/Commands/FileCommands.cs @@ -2,7 +2,9 @@ using HyperbolicDownloaderApi.Managment; using HyperbolicDownloaderApi.Networking; +using System.Diagnostics; using System.Net; +using System.Net.Sockets; using System.Text.Json; namespace HyperbolicDownloaderApi.Commands; @@ -86,6 +88,76 @@ public class FileCommands Console.CursorTop--; } + public void ListFilesRemote(string args) + { + string[] parts = args.Split(":"); + + if (parts.Length != 2) + { + ApiManager.SendNotificationMessageNewLine("Invalid format! Use this format: (xxx.xxx.xxx.xxx:yyyy)", NotificationMessageType.Error); + return; + } + + string ipAddressInput = parts[0]; + string portInput = parts[1]; + IPAddress? ipAddress; + + _ = int.TryParse(portInput, out int port); + + if (port is < 1000 or >= 6000) + { + ApiManager.SendNotificationMessageNewLine("Invalid port number!", NotificationMessageType.Error); + return; + } + else if (!IPAddress.TryParse(ipAddressInput, out ipAddress)) + { + try + { + ipAddress = Dns.GetHostAddresses(ipAddressInput).FirstOrDefault(); + } + catch (SocketException ex) + { + Debug.WriteLine(ex); + } + } + + if (ipAddress is null) + { + ApiManager.SendNotificationMessageNewLine("Invalid IP address!", NotificationMessageType.Error); + return; + } + + Task?> sendTask = NetworkClient.SendAsync>(ipAddress, port, "GetFilesList", string.Empty); + + _ = sendTask.Wait(1000); + + if (!sendTask.IsCompletedSuccessfully) + { + ApiManager.SendNotificationMessageNewLine("Invalid host!", NotificationMessageType.Error); + return; + } + + List? fileInfos = sendTask.Result; + + if (fileInfos is null || fileInfos.Count == 0) + { + ApiManager.SendNotificationMessageNewLine("No tracked files!", NotificationMessageType.Warning); + return; + } + + int index = 0; + + foreach (HyperFileDto fileInfo in fileInfos) + { + index++; + + ApiManager.SendNotificationMessageNewLine($"{index}) {fileInfo.Name}"); + ApiManager.SendNotificationMessageNewLine($"Hash: {fileInfo.Hash}"); + ApiManager.SendNotificationMessageNewLine(string.Empty); + } + Console.CursorTop--; + } + public void GenerateFileSingle(string args) { string directoryPath = Path.Combine(ApiConfiguration.BasePath, "GeneratedFiles"); diff --git a/HyperbolicDownloaderApi/FileProcessing/HyperFileDto.cs b/HyperbolicDownloaderApi/FileProcessing/HyperFileDto.cs new file mode 100644 index 0000000..c1f7363 --- /dev/null +++ b/HyperbolicDownloaderApi/FileProcessing/HyperFileDto.cs @@ -0,0 +1,23 @@ +using System.Text.Json.Serialization; + +namespace HyperbolicDownloaderApi.FileProcessing; + +internal class HyperFileDto +{ + public string Hash { get; set; } + + public string Name { get; set; } + + [JsonConstructor] + public HyperFileDto(string hash, string name) + { + Hash = hash; + Name = name; + } + + public HyperFileDto(PrivateHyperFileInfo privateHyperFileInfo) + { + Hash = privateHyperFileInfo.Hash; + Name = Path.GetFileName(privateHyperFileInfo.FilePath); + } +} \ No newline at end of file diff --git a/HyperbolicDownloaderApi/Managment/ApiManager.cs b/HyperbolicDownloaderApi/Managment/ApiManager.cs index b6135e0..fa65ed7 100644 --- a/HyperbolicDownloaderApi/Managment/ApiManager.cs +++ b/HyperbolicDownloaderApi/Managment/ApiManager.cs @@ -103,7 +103,6 @@ public class ApiManager } SendNotificationMessageNewLine("Ports closed!", NotificationMessageType.Warning); - Environment.Exit(0); } public bool StartBroadcastListener() @@ -130,6 +129,7 @@ public class ApiManager networkClient.ListenTo>("DiscoverAnswer", DiscoverAnswer); networkClient.ListenTo("Message", ReciveMessage); networkClient.ListenTo("HasFile", HasFile); + networkClient.ListenTo("GetFilesList", GetFileList); networkClient.StartListening(ApiConfiguration.PrivatePort); } catch (SocketException ex) @@ -223,6 +223,13 @@ public class ApiManager await recivedEventArgs.SendResponseAsync(FilesManager.Contains(recivedEventArgs.Data)); } + private async void GetFileList(object? sender, MessageRecivedEventArgs recivedEventArgs) + { + SendNotificationMessageNewLine($"{recivedEventArgs.IpAddress} > Requesting file list", NotificationMessageType.Log); + + await recivedEventArgs.SendResponseAsync(FilesManager.ToList().Select(f => new HyperFileDto(f)).ToList()); + } + private void ReciveMessage(object? sender, MessageRecivedEventArgs recivedEventArgs) { SendNotificationMessageNewLine($"Received \"{recivedEventArgs.Data}\" from {recivedEventArgs.IpAddress}.", NotificationMessageType.Info);