Update naming and add auto retry if hash is wrong

This commit is contained in:
Stone_Red
2022-04-21 07:35:52 +02:00
parent 69929fd58a
commit f906d4581c
9 changed files with 93 additions and 93 deletions
@@ -5,7 +5,7 @@ using HyperbolicDownloaderApi.FileProcessing;
using Stone_Red_Utilities.ConsoleExtentions; using Stone_Red_Utilities.ConsoleExtentions;
namespace HyperbolicDownloaderApi.UserInterface; namespace HyperbolicDownloaderApi;
internal class InputHandler internal class InputHandler
{ {
-1
View File
@@ -1,7 +1,6 @@
using HyperbolicDownloaderApi.FileProcessing; using HyperbolicDownloaderApi.FileProcessing;
using HyperbolicDownloaderApi.Managment; using HyperbolicDownloaderApi.Managment;
using HyperbolicDownloaderApi.Networking; using HyperbolicDownloaderApi.Networking;
using HyperbolicDownloaderApi.UserInterface;
using Stone_Red_Utilities.ConsoleExtentions; using Stone_Red_Utilities.ConsoleExtentions;
@@ -9,12 +9,12 @@ public class ClientCommands
{ {
if (ApiManager.PublicIpAddress is not null) if (ApiManager.PublicIpAddress is not null)
{ {
ApiManager.SendMessageNewLine($"The public IP address is: {ApiManager.PublicIpAddress}", NotificationMessageType.Info); ApiManager.SendNotificationMessageNewLine($"The public IP address is: {ApiManager.PublicIpAddress}", NotificationMessageType.Info);
ApiManager.SendMessageNewLine($"The public port is: {ApiConfiguration.PublicPort}", NotificationMessageType.Info); ApiManager.SendNotificationMessageNewLine($"The public port is: {ApiConfiguration.PublicPort}", NotificationMessageType.Info);
ApiManager.SendMessageNewLine(string.Empty, NotificationMessageType.Info); ApiManager.SendNotificationMessageNewLine(string.Empty, NotificationMessageType.Info);
} }
ApiManager.SendMessageNewLine($"The private IP address is: {NetworkUtilities.GetIP4Adress()}", NotificationMessageType.Info); ApiManager.SendNotificationMessageNewLine($"The private IP address is: {NetworkUtilities.GetIP4Adress()}", NotificationMessageType.Info);
ApiManager.SendMessageNewLine($"The private port is: {ApiConfiguration.PrivatePort}", NotificationMessageType.Info); ApiManager.SendNotificationMessageNewLine($"The private port is: {ApiConfiguration.PrivatePort}", NotificationMessageType.Info);
} }
} }
@@ -27,7 +27,7 @@ public class DownloadCommands
{ {
if (string.IsNullOrWhiteSpace(path)) if (string.IsNullOrWhiteSpace(path))
{ {
ApiManager.SendMessageNewLine("Path is empty!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("Path is empty!", NotificationMessageType.Error);
return; return;
} }
@@ -35,7 +35,7 @@ public class DownloadCommands
if (!File.Exists(fullPath)) if (!File.Exists(fullPath))
{ {
ApiManager.SendMessageNewLine("Invalid file path!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("Invalid file path!", NotificationMessageType.Error);
} }
string json = File.ReadAllText(fullPath); string json = File.ReadAllText(fullPath);
@@ -43,7 +43,7 @@ public class DownloadCommands
PublicHyperFileInfo? publicHyperFileInfo = JsonSerializer.Deserialize<PublicHyperFileInfo>(json); PublicHyperFileInfo? publicHyperFileInfo = JsonSerializer.Deserialize<PublicHyperFileInfo>(json);
if (publicHyperFileInfo == null) if (publicHyperFileInfo == null)
{ {
ApiManager.SendMessageNewLine("Parsing file failed!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("Parsing file failed!", NotificationMessageType.Error);
return; return;
} }
@@ -55,7 +55,7 @@ public class DownloadCommands
{ {
if (string.IsNullOrEmpty(hash)) if (string.IsNullOrEmpty(hash))
{ {
ApiManager.SendMessageNewLine("No hash value specified!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("No hash value specified!", NotificationMessageType.Error);
return; return;
} }
@@ -71,7 +71,7 @@ public class DownloadCommands
continue; continue;
} }
ApiManager.SendMessage($"{host.IPAddress}:{host.Port} > ???", NotificationMessageType.Warning); ApiManager.SendNotificationMessage($"{host.IPAddress}:{host.Port} > ???", NotificationMessageType.Warning);
Console.CursorLeft = 0; Console.CursorLeft = 0;
@@ -82,7 +82,7 @@ public class DownloadCommands
if (!sendTask.IsCompletedSuccessfully) if (!sendTask.IsCompletedSuccessfully)
{ {
Console.CursorLeft = 0; Console.CursorLeft = 0;
ApiManager.SendMessageNewLine($"{host.IPAddress}:{host.Port} > Inactive", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine($"{host.IPAddress}:{host.Port} > Inactive", NotificationMessageType.Error);
hostsManager.Remove(host); hostsManager.Remove(host);
continue; continue;
@@ -90,14 +90,14 @@ public class DownloadCommands
else if (!sendTask.Result) else if (!sendTask.Result)
{ {
host.LastActive = DateTime.Now; host.LastActive = DateTime.Now;
ApiManager.SendMessageNewLine($"{host.IPAddress}:{host.Port} > Does not have the requested file", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine($"{host.IPAddress}:{host.Port} > Does not have the requested file", NotificationMessageType.Error);
continue; continue;
} }
host.LastActive = DateTime.Now; host.LastActive = DateTime.Now;
ApiManager.SendMessageNewLine($"{host.IPAddress}:{host.Port} > Has the requested file", NotificationMessageType.Success); ApiManager.SendNotificationMessageNewLine($"{host.IPAddress}:{host.Port} > Has the requested file", NotificationMessageType.Success);
ApiManager.SendMessageNewLine("Requesting file..."); ApiManager.SendNotificationMessageNewLine("Requesting file...");
using TcpClient tcpClient = new TcpClient(); using TcpClient tcpClient = new TcpClient();
tcpClient.Connect(ipAddress!, host.Port); tcpClient.Connect(ipAddress!, host.Port);
@@ -118,8 +118,8 @@ public class DownloadCommands
} }
catch (IOException) catch (IOException)
{ {
ApiManager.SendMessageNewLine(string.Empty); ApiManager.SendNotificationMessageNewLine(string.Empty);
ApiManager.SendMessageNewLine("Lost connection to other host!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("Lost connection to other host!", NotificationMessageType.Error);
continue; continue;
} }
@@ -129,7 +129,7 @@ public class DownloadCommands
if (parts.Length != 2) //If received data does not contain 2 parts -> error if (parts.Length != 2) //If received data does not contain 2 parts -> error
{ {
ApiManager.SendMessageNewLine(dataReceived, NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine(dataReceived, NotificationMessageType.Error);
continue; continue;
} }
@@ -137,7 +137,7 @@ public class DownloadCommands
if (!validFileSize || fileSize <= 0) if (!validFileSize || fileSize <= 0)
{ {
ApiManager.SendMessageNewLine("Invalid file size!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("Invalid file size!", NotificationMessageType.Error);
continue; continue;
} }
@@ -145,8 +145,8 @@ public class DownloadCommands
string directoryPath = Path.Combine(ApiConfiguration.BasePath, "Downloads"); string directoryPath = Path.Combine(ApiConfiguration.BasePath, "Downloads");
string filePath = Path.Combine(directoryPath, fileName); string filePath = Path.Combine(directoryPath, fileName);
ApiManager.SendMessageNewLine($"File name: {fileName}"); ApiManager.SendNotificationMessageNewLine($"File name: {fileName}");
ApiManager.SendMessageNewLine($"Starting download..."); ApiManager.SendNotificationMessageNewLine($"Starting download...");
int totalBytesRead = 0; int totalBytesRead = 0;
@@ -172,8 +172,8 @@ public class DownloadCommands
catch (IOException ex) catch (IOException ex)
{ {
Debug.WriteLine(ex); Debug.WriteLine(ex);
ApiManager.SendMessageNewLine(string.Empty); ApiManager.SendNotificationMessageNewLine(string.Empty);
ApiManager.SendMessageNewLine("Lost connection to other host!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("Lost connection to other host!", NotificationMessageType.Error);
break; break;
} }
@@ -201,7 +201,7 @@ public class DownloadCommands
stopWatch.Restart(); stopWatch.Restart();
} }
ApiManager.SendMessage($"\rDownloading: {Math.Clamp(Math.Ceiling(100d / fileSize * totalBytesRead), 0, 100)}% {totalBytesRead / 1000}/{fileSize / 1000}KB [{unitsPerSecond}{unit}/s] "); ApiManager.SendNotificationMessage($"\rDownloading: {Math.Clamp(Math.Ceiling(100d / fileSize * totalBytesRead), 0, 100)}% {totalBytesRead / 1000}/{fileSize / 1000}KB [{unitsPerSecond}{unit}/s] ");
} }
fileStream.Close(); fileStream.Close();
@@ -211,25 +211,26 @@ public class DownloadCommands
continue; continue;
} }
ApiManager.SendMessageNewLine(string.Empty); ApiManager.SendNotificationMessageNewLine(string.Empty);
ApiManager.SendMessageNewLine("Validating file..."); ApiManager.SendNotificationMessageNewLine("Validating file...");
if (FileValidator.ValidateHash(filePath, hash)) if (FileValidator.ValidateHash(filePath, hash))
{ {
_ = filesManager.TryAdd(filePath, out _, out _); _ = filesManager.TryAdd(filePath, out _, out _);
} }
else else
{ {
ApiManager.SendMessageNewLine("Warning: File hash does not match! File might me corrupted or manipulated!", NotificationMessageType.Warning); ApiManager.SendNotificationMessageNewLine("Warning: File hash does not match! File might me corrupted or manipulated! Trying next host...", NotificationMessageType.Warning);
continue;
} }
ApiManager.SendMessageNewLine($"File saved at: {Path.GetFullPath(filePath)}"); ApiManager.SendNotificationMessageNewLine($"File saved at: {Path.GetFullPath(filePath)}");
ApiManager.SendMessageNewLine("Done", NotificationMessageType.Success); ApiManager.SendNotificationMessageNewLine("Done", NotificationMessageType.Success);
stopWatch.Stop(); stopWatch.Stop();
hostsManager.SaveHosts(); hostsManager.SaveHosts();
return; return;
} }
ApiManager.SendMessageNewLine("None of the available hosts have the requested file!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("None of the available hosts have the requested file!", NotificationMessageType.Error);
hostsManager.SaveHosts(); hostsManager.SaveHosts();
} }
} }
@@ -23,12 +23,12 @@ public class FileCommands
{ {
if (filesManager.TryAdd(path, out PrivateHyperFileInfo? fileInfo, out string? message)) if (filesManager.TryAdd(path, out PrivateHyperFileInfo? fileInfo, out string? message))
{ {
ApiManager.SendMessageNewLine($"Added file: {fileInfo!.FilePath}", NotificationMessageType.Success); ApiManager.SendNotificationMessageNewLine($"Added file: {fileInfo!.FilePath}", NotificationMessageType.Success);
ApiManager.SendMessageNewLine($"Hash: {fileInfo.Hash}"); ApiManager.SendNotificationMessageNewLine($"Hash: {fileInfo.Hash}");
} }
else else
{ {
ApiManager.SendMessageNewLine(message!, NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine(message!, NotificationMessageType.Error);
} }
} }
@@ -38,11 +38,11 @@ public class FileCommands
if (filesManager.TryRemove(hash)) if (filesManager.TryRemove(hash))
{ {
ApiManager.SendMessageNewLine($"Successfully removed file!", NotificationMessageType.Success); ApiManager.SendNotificationMessageNewLine($"Successfully removed file!", NotificationMessageType.Success);
} }
else else
{ {
ApiManager.SendMessageNewLine("The file is not being tracked!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("The file is not being tracked!", NotificationMessageType.Error);
} }
} }
@@ -53,16 +53,16 @@ public class FileCommands
if (fileInfos.Count == 0) if (fileInfos.Count == 0)
{ {
ApiManager.SendMessageNewLine("No tracked files!", NotificationMessageType.Warning); ApiManager.SendNotificationMessageNewLine("No tracked files!", NotificationMessageType.Warning);
return; return;
} }
foreach (PrivateHyperFileInfo fileInfo in fileInfos) foreach (PrivateHyperFileInfo fileInfo in fileInfos)
{ {
index++; index++;
ApiManager.SendMessageNewLine($"{index}) {fileInfo.FilePath}"); ApiManager.SendNotificationMessageNewLine($"{index}) {fileInfo.FilePath}");
ApiManager.SendMessageNewLine($"Hash: {fileInfo.Hash}"); ApiManager.SendNotificationMessageNewLine($"Hash: {fileInfo.Hash}");
ApiManager.SendMessageNewLine(string.Empty); ApiManager.SendNotificationMessageNewLine(string.Empty);
} }
Console.CursorTop--; Console.CursorTop--;
} }
@@ -79,7 +79,7 @@ public class FileCommands
if (!filesManager.TryGet(hash, out PrivateHyperFileInfo? localHyperFileInfo)) if (!filesManager.TryGet(hash, out PrivateHyperFileInfo? localHyperFileInfo))
{ {
ApiManager.SendMessageNewLine("The file is not being tracked!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("The file is not being tracked!", NotificationMessageType.Error);
return; return;
} }
@@ -91,7 +91,7 @@ public class FileCommands
NetworkSocket? localHost = ApiManager.GetLocalSocket(); NetworkSocket? localHost = ApiManager.GetLocalSocket();
if (localHost is null) if (localHost is null)
{ {
ApiManager.SendMessageNewLine("Network error!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("Network error!", NotificationMessageType.Error);
return; return;
} }
@@ -102,8 +102,8 @@ public class FileCommands
File.WriteAllText(filePath, json); File.WriteAllText(filePath, json);
ApiManager.SendMessageNewLine("Done", NotificationMessageType.Success); ApiManager.SendNotificationMessageNewLine("Done", NotificationMessageType.Success);
ApiManager.SendMessageNewLine($"File saved at: {Path.GetFullPath(filePath)}"); ApiManager.SendNotificationMessageNewLine($"File saved at: {Path.GetFullPath(filePath)}");
} }
public void GenerateFileFull(string hash) public void GenerateFileFull(string hash)
@@ -118,7 +118,7 @@ public class FileCommands
if (!filesManager.TryGet(hash, out PrivateHyperFileInfo? localHyperFileInfo)) if (!filesManager.TryGet(hash, out PrivateHyperFileInfo? localHyperFileInfo))
{ {
ApiManager.SendMessageNewLine("The file is not being tracked!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("The file is not being tracked!", NotificationMessageType.Error);
return; return;
} }
@@ -130,7 +130,7 @@ public class FileCommands
NetworkSocket? localHost = ApiManager.GetLocalSocket(); NetworkSocket? localHost = ApiManager.GetLocalSocket();
if (localHost is null) if (localHost is null)
{ {
ApiManager.SendMessageNewLine("Network error!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("Network error!", NotificationMessageType.Error);
return; return;
} }
@@ -144,7 +144,7 @@ public class FileCommands
continue; continue;
} }
ApiManager.SendMessage($"{host.IPAddress}:{host.Port} > ???", NotificationMessageType.Warning); ApiManager.SendNotificationMessage($"{host.IPAddress}:{host.Port} > ???", NotificationMessageType.Warning);
Console.CursorLeft = 0; Console.CursorLeft = 0;
@@ -155,7 +155,7 @@ public class FileCommands
if (!sendTask.IsCompletedSuccessfully) if (!sendTask.IsCompletedSuccessfully)
{ {
Console.CursorLeft = 0; Console.CursorLeft = 0;
ApiManager.SendMessageNewLine($"{host.IPAddress}:{host.Port} > Inactive", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine($"{host.IPAddress}:{host.Port} > Inactive", NotificationMessageType.Error);
hostsManager.Remove(host); hostsManager.Remove(host);
continue; continue;
@@ -163,13 +163,13 @@ public class FileCommands
else if (!sendTask.Result) else if (!sendTask.Result)
{ {
host.LastActive = DateTime.Now; host.LastActive = DateTime.Now;
ApiManager.SendMessageNewLine($"{host.IPAddress}:{host.Port} > Does not have the requested file", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine($"{host.IPAddress}:{host.Port} > Does not have the requested file", NotificationMessageType.Error);
continue; continue;
} }
host.LastActive = DateTime.Now; host.LastActive = DateTime.Now;
ApiManager.SendMessageNewLine($"{host.IPAddress}:{host.Port} > Has the requested file", NotificationMessageType.Success); ApiManager.SendNotificationMessageNewLine($"{host.IPAddress}:{host.Port} > Has the requested file", NotificationMessageType.Success);
publicHyperFileInfo.Hosts.Add(host); publicHyperFileInfo.Hosts.Add(host);
} }
@@ -180,7 +180,7 @@ public class FileCommands
File.WriteAllText(filePath, json); File.WriteAllText(filePath, json);
ApiManager.SendMessageNewLine("Done", NotificationMessageType.Success); ApiManager.SendNotificationMessageNewLine("Done", NotificationMessageType.Success);
ApiManager.SendMessageNewLine($"File saved at: {Path.GetFullPath(filePath)}"); ApiManager.SendNotificationMessageNewLine($"File saved at: {Path.GetFullPath(filePath)}");
} }
} }
@@ -18,7 +18,7 @@ public class HostCommands
public void Discover(string _) public void Discover(string _)
{ {
ApiManager.SendMessageNewLine("Running local discovery routine...", NotificationMessageType.Info); ApiManager.SendNotificationMessageNewLine("Running local discovery routine...", NotificationMessageType.Info);
BroadcastClient.Send(ApiConfiguration.BroadcastPort, ApiConfiguration.PrivatePort.ToString()); BroadcastClient.Send(ApiConfiguration.BroadcastPort, ApiConfiguration.PrivatePort.ToString());
Thread.Sleep(3000); Thread.Sleep(3000);
} }
@@ -28,13 +28,13 @@ public class HostCommands
int activeHostsCount = hostsManager.CheckHostsActivity(); int activeHostsCount = hostsManager.CheckHostsActivity();
if (activeHostsCount == 0) if (activeHostsCount == 0)
{ {
ApiManager.SendMessageNewLine("No active hosts found!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("No active hosts found!", NotificationMessageType.Error);
ApiManager.SendMessageNewLine("Use 'add host xxx.xxx.xxx.xxx:yyyy' to add a new host.", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("Use 'add host xxx.xxx.xxx.xxx:yyyy' to add a new host.", NotificationMessageType.Error);
} }
ApiManager.SendMessageNewLine(string.Empty, NotificationMessageType.Info); ApiManager.SendNotificationMessageNewLine(string.Empty, NotificationMessageType.Info);
ApiManager.SendMessageNewLine($"{hostsManager.Count} known host(s).", NotificationMessageType.Info); ApiManager.SendNotificationMessageNewLine($"{hostsManager.Count} known host(s).", NotificationMessageType.Info);
ApiManager.SendMessageNewLine($"{activeHostsCount} active host(s).", NotificationMessageType.Info); ApiManager.SendNotificationMessageNewLine($"{activeHostsCount} active host(s).", NotificationMessageType.Info);
} }
public void ListHosts(string _) public void ListHosts(string _)
@@ -44,16 +44,16 @@ public class HostCommands
if (hosts.Count == 0) if (hosts.Count == 0)
{ {
ApiManager.SendMessageNewLine("No known hosts", NotificationMessageType.Warning); ApiManager.SendNotificationMessageNewLine("No known hosts", NotificationMessageType.Warning);
return; return;
} }
foreach (NetworkSocket host in hosts) foreach (NetworkSocket host in hosts)
{ {
index++; index++;
ApiManager.SendMessageNewLine($"{index}) {host.IPAddress}:{host.Port}", NotificationMessageType.Info); ApiManager.SendNotificationMessageNewLine($"{index}) {host.IPAddress}:{host.Port}", NotificationMessageType.Info);
ApiManager.SendMessageNewLine($"Last active: {host.LastActive}", NotificationMessageType.Info); ApiManager.SendNotificationMessageNewLine($"Last active: {host.LastActive}", NotificationMessageType.Info);
ApiManager.SendMessageNewLine(string.Empty, NotificationMessageType.Info); ApiManager.SendNotificationMessageNewLine(string.Empty, NotificationMessageType.Info);
} }
Console.CursorTop--; Console.CursorTop--;
} }
@@ -64,7 +64,7 @@ public class HostCommands
if (parts.Length != 2) if (parts.Length != 2)
{ {
ApiManager.SendMessageNewLine("Invalid format! Use this format: (xxx.xxx.xxx.xxx:yyyy)", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("Invalid format! Use this format: (xxx.xxx.xxx.xxx:yyyy)", NotificationMessageType.Error);
return; return;
} }
@@ -74,13 +74,13 @@ public class HostCommands
_ = int.TryParse(portInput, out int port); _ = int.TryParse(portInput, out int port);
if (port < 1000 || port >= 6000) if (port < 1000 || port >= 6000)
{ {
ApiManager.SendMessageNewLine("Invalid port number!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("Invalid port number!", NotificationMessageType.Error);
return; return;
} }
if (!IPAddress.TryParse(ipAddressInput, out IPAddress? ipAddress)) if (!IPAddress.TryParse(ipAddressInput, out IPAddress? ipAddress))
{ {
ApiManager.SendMessageNewLine("Invalid IP address!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("Invalid IP address!", NotificationMessageType.Error);
return; return;
} }
@@ -88,12 +88,12 @@ public class HostCommands
if (!hostsManager.Contains(hostToRemove)) if (!hostsManager.Contains(hostToRemove))
{ {
ApiManager.SendMessageNewLine("Host not in list", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("Host not in list", NotificationMessageType.Error);
return; return;
} }
hostsManager.Remove(new NetworkSocket(ipAddress.ToString(), port, DateTime.MinValue), true); hostsManager.Remove(new NetworkSocket(ipAddress.ToString(), port, DateTime.MinValue), true);
ApiManager.SendMessageNewLine($"Successfully Removed host!", NotificationMessageType.Success); ApiManager.SendNotificationMessageNewLine($"Successfully Removed host!", NotificationMessageType.Success);
} }
public void AddHost(string args) public void AddHost(string args)
@@ -102,7 +102,7 @@ public class HostCommands
if (parts.Length != 2) if (parts.Length != 2)
{ {
ApiManager.SendMessageNewLine("Invalid format! Use this format: (xxx.xxx.xxx.xxx:yyyy)", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("Invalid format! Use this format: (xxx.xxx.xxx.xxx:yyyy)", NotificationMessageType.Error);
return; return;
} }
@@ -114,7 +114,7 @@ public class HostCommands
if (port < 1000 || port >= 6000) if (port < 1000 || port >= 6000)
{ {
ApiManager.SendMessageNewLine("Invalid port number!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("Invalid port number!", NotificationMessageType.Error);
return; return;
} }
else if (!IPAddress.TryParse(ipAddressInput, out ipAddress)) else if (!IPAddress.TryParse(ipAddressInput, out ipAddress))
@@ -131,33 +131,33 @@ public class HostCommands
if (ipAddress is null) if (ipAddress is null)
{ {
ApiManager.SendMessageNewLine("Invalid IP address!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("Invalid IP address!", NotificationMessageType.Error);
return; return;
} }
try try
{ {
ApiManager.SendMessageNewLine("Waiting for response...", NotificationMessageType.Info); ApiManager.SendNotificationMessageNewLine("Waiting for response...", NotificationMessageType.Info);
NetworkSocket? localSocket = ApiManager.GetLocalSocket() ?? new NetworkSocket("0.0.0.0", 0, DateTime.MinValue); NetworkSocket? localSocket = ApiManager.GetLocalSocket() ?? new NetworkSocket("0.0.0.0", 0, DateTime.MinValue);
List<NetworkSocket>? recivedHosts = NetworkClient.Send<List<NetworkSocket>>(ipAddress, port, "GetHostsList", localSocket); List<NetworkSocket>? recivedHosts = NetworkClient.Send<List<NetworkSocket>>(ipAddress, port, "GetHostsList", localSocket);
if (recivedHosts is not null) if (recivedHosts is not null)
{ {
ApiManager.SendMessageNewLine($"Success! Added {recivedHosts.Count} new host(s).", NotificationMessageType.Success); ApiManager.SendNotificationMessageNewLine($"Success! Added {recivedHosts.Count} new host(s).", NotificationMessageType.Success);
hostsManager.AddRange(recivedHosts); hostsManager.AddRange(recivedHosts);
} }
else else
{ {
ApiManager.SendMessageNewLine($"Invalid response!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine($"Invalid response!", NotificationMessageType.Error);
} }
} }
catch (SocketException ex) catch (SocketException ex)
{ {
ApiManager.SendMessageNewLine($"Invalid host! Error message: {ex.Message}", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine($"Invalid host! Error message: {ex.Message}", NotificationMessageType.Error);
} }
catch (IOException ex) catch (IOException ex)
{ {
ApiManager.SendMessageNewLine($"Invalid host! Error message: {ex.Message}", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine($"Invalid host! Error message: {ex.Message}", NotificationMessageType.Error);
} }
} }
} }
+12 -12
View File
@@ -62,30 +62,30 @@ public class ApiManager
device = await discoverer.DiscoverDeviceAsync(); device = await discoverer.DiscoverDeviceAsync();
IPAddress? ip = await device.GetExternalIPAsync(); IPAddress? ip = await device.GetExternalIPAsync();
SendMessageNewLine($"The public IP address is: {ip} ", NotificationMessageType.Success); SendNotificationMessageNewLine($"The public IP address is: {ip} ", NotificationMessageType.Success);
portMapping = new Mapping(Protocol.Tcp, ApiConfiguration.PrivatePort, ApiConfiguration.PublicPort, "HyperbolicDowloader"); portMapping = new Mapping(Protocol.Tcp, ApiConfiguration.PrivatePort, ApiConfiguration.PublicPort, "HyperbolicDowloader");
await device.CreatePortMapAsync(portMapping); await device.CreatePortMapAsync(portMapping);
SendMessageNewLine($"The public port is: {ApiConfiguration.PublicPort}", NotificationMessageType.Success); SendNotificationMessageNewLine($"The public port is: {ApiConfiguration.PublicPort}", NotificationMessageType.Success);
return true; return true;
} }
catch (NatDeviceNotFoundException) catch (NatDeviceNotFoundException)
{ {
SendMessageNewLine($"Could not find a UPnP or NAT-PMP device!", NotificationMessageType.Error); SendNotificationMessageNewLine($"Could not find a UPnP or NAT-PMP device!", NotificationMessageType.Error);
return false; return false;
} }
catch (MappingException ex) catch (MappingException ex)
{ {
SendMessageNewLine($"An error occurred while mapping the private port ({ApiConfiguration.PrivatePort}) to the public port ({ApiConfiguration.PublicPort})! Error message: {ex.Message}", NotificationMessageType.Error); SendNotificationMessageNewLine($"An error occurred while mapping the private port ({ApiConfiguration.PrivatePort}) to the public port ({ApiConfiguration.PublicPort})! Error message: {ex.Message}", NotificationMessageType.Error);
return false; return false;
} }
} }
public static void ClosePorts() public static void ClosePorts()
{ {
SendMessageNewLine("Closing ports...", NotificationMessageType.Info); SendNotificationMessageNewLine("Closing ports...", NotificationMessageType.Info);
if (device is not null) if (device is not null)
{ {
try try
@@ -101,11 +101,11 @@ public class ApiManager
} }
catch (Exception ex) catch (Exception ex)
{ {
SendMessageNewLine(ex.ToString(), NotificationMessageType.Error); SendNotificationMessageNewLine(ex.ToString(), NotificationMessageType.Error);
} }
} }
SendMessageNewLine("Ports closed!", NotificationMessageType.Warning); SendNotificationMessageNewLine("Ports closed!", NotificationMessageType.Warning);
Environment.Exit(0); Environment.Exit(0);
} }
@@ -127,7 +127,7 @@ public class ApiManager
} }
catch (SocketException ex) catch (SocketException ex)
{ {
SendMessageNewLine($"An error occurred while starting the TCP listener! Error message: {ex.Message}", NotificationMessageType.Error); // net stop hens && net start hns SendNotificationMessageNewLine($"An error occurred while starting the TCP listener! Error message: {ex.Message}", NotificationMessageType.Error); // net stop hens && net start hns
Console.ReadKey(); Console.ReadKey();
} }
} }
@@ -165,7 +165,7 @@ public class ApiManager
private void DiscoverAnswer(object? sender, MessageRecivedEventArgs<List<NetworkSocket>> recivedEventArgs) private void DiscoverAnswer(object? sender, MessageRecivedEventArgs<List<NetworkSocket>> recivedEventArgs)
{ {
SendMessageNewLine($"Received answer from {recivedEventArgs.IpAddress}. Returned {recivedEventArgs.Data.Count} host(s).", NotificationMessageType.Info); SendNotificationMessageNewLine($"Received answer from {recivedEventArgs.IpAddress}. Returned {recivedEventArgs.Data.Count} host(s).", NotificationMessageType.Info);
HostsManager.AddRange(recivedEventArgs.Data); HostsManager.AddRange(recivedEventArgs.Data);
} }
@@ -198,15 +198,15 @@ public class ApiManager
private void ReciveMessage(object? sender, MessageRecivedEventArgs<string> recivedEventArgs) private void ReciveMessage(object? sender, MessageRecivedEventArgs<string> recivedEventArgs)
{ {
SendMessageNewLine($"Received \"{recivedEventArgs.Data}\" from {recivedEventArgs.IpAddress}.", NotificationMessageType.Info); SendNotificationMessageNewLine($"Received \"{recivedEventArgs.Data}\" from {recivedEventArgs.IpAddress}.", NotificationMessageType.Info);
} }
internal static void SendMessage(string message, NotificationMessageType messageType = NotificationMessageType.Info) internal static void SendNotificationMessage(string message, NotificationMessageType messageType = NotificationMessageType.Info)
{ {
OnNotificationMessageRecived?.Invoke(null, new NotificationMessageEventArgs(messageType, message)); OnNotificationMessageRecived?.Invoke(null, new NotificationMessageEventArgs(messageType, message));
} }
internal static void SendMessageNewLine(string message, NotificationMessageType messageType = NotificationMessageType.Info) internal static void SendNotificationMessageNewLine(string message, NotificationMessageType messageType = NotificationMessageType.Info)
{ {
OnNotificationMessageRecived?.Invoke(null, new NotificationMessageEventArgs(messageType, message + Environment.NewLine)); OnNotificationMessageRecived?.Invoke(null, new NotificationMessageEventArgs(messageType, message + Environment.NewLine));
} }
@@ -26,7 +26,7 @@ internal class BroadcastClient
if (ip4Address is null) if (ip4Address is null)
{ {
ApiManager.SendMessageNewLine("Could not find suitable network adapter!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("Could not find suitable network adapter!", NotificationMessageType.Error);
return; return;
} }
@@ -34,7 +34,7 @@ internal class BroadcastClient
if (addressInformation is null) if (addressInformation is null)
{ {
ApiManager.SendMessageNewLine("Could not find suitable network adapter!", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine("Could not find suitable network adapter!", NotificationMessageType.Error);
return; return;
} }
@@ -53,7 +53,7 @@ public class HostsManager
int activeHostsCount = 0; int activeHostsCount = 0;
foreach (NetworkSocket host in hosts) foreach (NetworkSocket host in hosts)
{ {
ApiManager.SendMessage($"{host.IPAddress}:{host.Port} > ???", NotificationMessageType.Warning); ApiManager.SendNotificationMessage($"{host.IPAddress}:{host.Port} > ???", NotificationMessageType.Warning);
using TcpClient tcpClient = new TcpClient(); using TcpClient tcpClient = new TcpClient();
try try
@@ -62,7 +62,7 @@ public class HostsManager
Console.CursorLeft = 0; Console.CursorLeft = 0;
if (tcpClient.Connected) if (tcpClient.Connected)
{ {
ApiManager.SendMessageNewLine($"{host.IPAddress}:{host.Port} > Active", NotificationMessageType.Success); ApiManager.SendNotificationMessageNewLine($"{host.IPAddress}:{host.Port} > Active", NotificationMessageType.Success);
host.LastActive = DateTime.Now; host.LastActive = DateTime.Now;
activeHostsCount++; activeHostsCount++;
} }
@@ -72,7 +72,7 @@ public class HostsManager
{ {
hostsToRemove.Add(host); hostsToRemove.Add(host);
} }
ApiManager.SendMessageNewLine($"{host.IPAddress}:{host.Port} > Inactive", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine($"{host.IPAddress}:{host.Port} > Inactive", NotificationMessageType.Error);
} }
} }
catch catch
@@ -82,7 +82,7 @@ public class HostsManager
hostsToRemove.Add(host); hostsToRemove.Add(host);
} }
Console.CursorLeft = 0; Console.CursorLeft = 0;
ApiManager.SendMessageNewLine($"{host.IPAddress}:{host.Port} > Inactive", NotificationMessageType.Error); ApiManager.SendNotificationMessageNewLine($"{host.IPAddress}:{host.Port} > Inactive", NotificationMessageType.Error);
} }
} }