mirror of
https://github.com/Stone-Red-Code/HyperbolicDownloader.git
synced 2026-09-06 15:56:22 +02:00
Add list files remote command
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -107,5 +107,6 @@ internal static class Program
|
||||
{
|
||||
ApiManager.ClosePorts();
|
||||
apiManager.HostsManager.SaveHosts();
|
||||
Environment.Exit(0);
|
||||
}
|
||||
}
|
||||
@@ -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<List<HyperFileDto>?> sendTask = NetworkClient.SendAsync<List<HyperFileDto>>(ipAddress, port, "GetFilesList", string.Empty);
|
||||
|
||||
_ = sendTask.Wait(1000);
|
||||
|
||||
if (!sendTask.IsCompletedSuccessfully)
|
||||
{
|
||||
ApiManager.SendNotificationMessageNewLine("Invalid host!", NotificationMessageType.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
List<HyperFileDto>? 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");
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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<List<NetworkSocket>>("DiscoverAnswer", DiscoverAnswer);
|
||||
networkClient.ListenTo<string>("Message", ReciveMessage);
|
||||
networkClient.ListenTo<string>("HasFile", HasFile);
|
||||
networkClient.ListenTo<string>("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<string> 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<string> recivedEventArgs)
|
||||
{
|
||||
SendNotificationMessageNewLine($"Received \"{recivedEventArgs.Data}\" from {recivedEventArgs.IpAddress}.", NotificationMessageType.Info);
|
||||
|
||||
Reference in New Issue
Block a user