diff --git a/HyperbolicDownloader/Program.cs b/HyperbolicDownloader/Program.cs index f9b5668..e99036c 100644 --- a/HyperbolicDownloader/Program.cs +++ b/HyperbolicDownloader/Program.cs @@ -84,7 +84,7 @@ internal static class Program { switch (e.NotificationMessageType) { - case NotificationMessageType.Raw: Console.Write(e.Message); break; + case NotificationMessageType.Info: Console.Write(e.Message); break; case NotificationMessageType.Success: ConsoleExt.Write(e.Message, ConsoleColor.Green); break; case NotificationMessageType.Warning: ConsoleExt.Write(e.Message, ConsoleColor.DarkYellow); break; case NotificationMessageType.Error: ConsoleExt.Write(e.Message, ConsoleColor.Red); break; diff --git a/HyperbolicDownloaderApi/Commands/ClientCommands.cs b/HyperbolicDownloaderApi/Commands/ClientCommands.cs index 425dae6..c7f91ad 100644 --- a/HyperbolicDownloaderApi/Commands/ClientCommands.cs +++ b/HyperbolicDownloaderApi/Commands/ClientCommands.cs @@ -9,12 +9,12 @@ public class ClientCommands { if (ApiManager.PublicIpAddress is not null) { - ApiManager.SendMessageNewLine($"The public IP address is: {ApiManager.PublicIpAddress}", NotificationMessageType.Success); - ApiManager.SendMessageNewLine($"The public port is: {ApiConfiguration.PublicPort}", NotificationMessageType.Success); - ApiManager.SendMessageNewLine(string.Empty, NotificationMessageType.Raw); + ApiManager.SendMessageNewLine($"The public IP address is: {ApiManager.PublicIpAddress}", NotificationMessageType.Info); + ApiManager.SendMessageNewLine($"The public port is: {ApiConfiguration.PublicPort}", NotificationMessageType.Info); + ApiManager.SendMessageNewLine(string.Empty, NotificationMessageType.Info); } - ApiManager.SendMessageNewLine($"The private IP address is: {NetworkUtilities.GetIP4Adress()}", NotificationMessageType.Success); - ApiManager.SendMessageNewLine($"The private port is: {ApiConfiguration.PrivatePort}", NotificationMessageType.Success); + ApiManager.SendMessageNewLine($"The private IP address is: {NetworkUtilities.GetIP4Adress()}", NotificationMessageType.Info); + ApiManager.SendMessageNewLine($"The private port is: {ApiConfiguration.PrivatePort}", NotificationMessageType.Info); } } \ No newline at end of file diff --git a/HyperbolicDownloaderApi/Commands/HostCommands.cs b/HyperbolicDownloaderApi/Commands/HostCommands.cs index 9cbfaea..796acba 100644 --- a/HyperbolicDownloaderApi/Commands/HostCommands.cs +++ b/HyperbolicDownloaderApi/Commands/HostCommands.cs @@ -1,6 +1,7 @@ using HyperbolicDownloaderApi.Managment; using HyperbolicDownloaderApi.Networking; +using System.Diagnostics; using System.Net; using System.Net.Sockets; @@ -17,7 +18,7 @@ public class HostCommands public void Discover(string _) { - ApiManager.SendMessageNewLine("Running local discovery routine...", NotificationMessageType.Raw); + ApiManager.SendMessageNewLine("Running local discovery routine...", NotificationMessageType.Info); BroadcastClient.Send(ApiConfiguration.BroadcastPort, ApiConfiguration.PrivatePort.ToString()); Thread.Sleep(3000); } @@ -31,9 +32,9 @@ public class HostCommands ApiManager.SendMessageNewLine("Use 'add host xxx.xxx.xxx.xxx:yyyy' to add a new host.", NotificationMessageType.Error); } - ApiManager.SendMessageNewLine(string.Empty, NotificationMessageType.Raw); - ApiManager.SendMessageNewLine($"{hostsManager.Count} known host(s).", NotificationMessageType.Raw); - ApiManager.SendMessageNewLine($"{activeHostsCount} active host(s).", NotificationMessageType.Raw); + ApiManager.SendMessageNewLine(string.Empty, NotificationMessageType.Info); + ApiManager.SendMessageNewLine($"{hostsManager.Count} known host(s).", NotificationMessageType.Info); + ApiManager.SendMessageNewLine($"{activeHostsCount} active host(s).", NotificationMessageType.Info); } public void ListHosts(string _) @@ -50,9 +51,9 @@ public class HostCommands foreach (NetworkSocket host in hosts) { index++; - ApiManager.SendMessageNewLine($"{index}) {host.IPAddress}:{host.Port}", NotificationMessageType.Raw); - ApiManager.SendMessageNewLine($"Last active: {host.LastActive}", NotificationMessageType.Raw); - ApiManager.SendMessageNewLine(string.Empty, NotificationMessageType.Raw); + ApiManager.SendMessageNewLine($"{index}) {host.IPAddress}:{host.Port}", NotificationMessageType.Info); + ApiManager.SendMessageNewLine($"Last active: {host.LastActive}", NotificationMessageType.Info); + ApiManager.SendMessageNewLine(string.Empty, NotificationMessageType.Info); } Console.CursorTop--; } @@ -107,42 +108,55 @@ public class HostCommands string ipAddressInput = parts[0]; string portInput = parts[1]; + IPAddress? ipAddress = null; _ = int.TryParse(portInput, out int port); if (port < 1000 || port >= 6000) { ApiManager.SendMessageNewLine("Invalid port number!", NotificationMessageType.Error); + return; } - else if (IPAddress.TryParse(ipAddressInput, out IPAddress? ipAddress)) + else if (!IPAddress.TryParse(ipAddressInput, out ipAddress)) { try { - ApiManager.SendMessageNewLine("Waiting for response...", NotificationMessageType.Raw); - NetworkSocket? localSocket = ApiManager.GetLocalSocket() ?? new NetworkSocket("0.0.0.0", 0, DateTime.MinValue); - List? recivedHosts = NetworkClient.Send>(ipAddress, port, "GetHostsList", localSocket); - - if (recivedHosts is not null) - { - ApiManager.SendMessageNewLine($"Success! Added {recivedHosts.Count} new host(s).", NotificationMessageType.Success); - hostsManager.AddRange(recivedHosts); - } - else - { - ApiManager.SendMessageNewLine($"Invalid response!", NotificationMessageType.Error); - } + ipAddress = Dns.GetHostAddresses(ipAddressInput).FirstOrDefault(); } catch (SocketException ex) { - ApiManager.SendMessageNewLine($"Invalid host! Error message: {ex.Message}", NotificationMessageType.Error); - } - catch (IOException ex) - { - ApiManager.SendMessageNewLine($"Invalid host! Error message: {ex.Message}", NotificationMessageType.Error); + Debug.WriteLine(ex); } } - else + + if (ipAddress is null) { ApiManager.SendMessageNewLine("Invalid IP address!", NotificationMessageType.Error); + return; + } + + try + { + ApiManager.SendMessageNewLine("Waiting for response...", NotificationMessageType.Info); + NetworkSocket? localSocket = ApiManager.GetLocalSocket() ?? new NetworkSocket("0.0.0.0", 0, DateTime.MinValue); + List? recivedHosts = NetworkClient.Send>(ipAddress, port, "GetHostsList", localSocket); + + if (recivedHosts is not null) + { + ApiManager.SendMessageNewLine($"Success! Added {recivedHosts.Count} new host(s).", NotificationMessageType.Success); + hostsManager.AddRange(recivedHosts); + } + else + { + ApiManager.SendMessageNewLine($"Invalid response!", NotificationMessageType.Error); + } + } + catch (SocketException ex) + { + ApiManager.SendMessageNewLine($"Invalid host! Error message: {ex.Message}", NotificationMessageType.Error); + } + catch (IOException ex) + { + ApiManager.SendMessageNewLine($"Invalid host! Error message: {ex.Message}", NotificationMessageType.Error); } } } \ No newline at end of file diff --git a/HyperbolicDownloaderApi/Managment/ApiManager.cs b/HyperbolicDownloaderApi/Managment/ApiManager.cs index 41a2884..c0abc8f 100644 --- a/HyperbolicDownloaderApi/Managment/ApiManager.cs +++ b/HyperbolicDownloaderApi/Managment/ApiManager.cs @@ -28,32 +28,6 @@ public class ApiManager networkClient = new(FilesManager); } - public static void ClosePorts() - { - ApiManager.SendMessageNewLine("Closing ports...", NotificationMessageType.Success); - if (device is not null) - { - try - { - IEnumerable? mappings = device.GetAllMappingsAsync().GetAwaiter().GetResult(); - foreach (Mapping? mapping in mappings) - { - if (mapping.Description.Contains("HyperbolicDowloader") && mapping.PrivateIP.ToString() == portMapping?.PrivateIP.ToString()) - { - device.DeletePortMapAsync(mapping).GetAwaiter().GetResult(); - } - } - } - catch (Exception ex) - { - ApiManager.SendMessageNewLine(ex.ToString(), NotificationMessageType.Error); - } - } - - ApiManager.SendMessageNewLine("Ports closed!", NotificationMessageType.Warning); - Environment.Exit(0); - } - public static NetworkSocket? GetLocalSocket() { int port = ApiConfiguration.PublicPort; @@ -88,27 +62,53 @@ public class ApiManager device = await discoverer.DiscoverDeviceAsync(); IPAddress? ip = await device.GetExternalIPAsync(); - ApiManager.SendMessageNewLine($"The public IP address is: {ip} ", NotificationMessageType.Success); + SendMessageNewLine($"The public IP address is: {ip} ", NotificationMessageType.Success); portMapping = new Mapping(Protocol.Tcp, ApiConfiguration.PrivatePort, ApiConfiguration.PublicPort, "HyperbolicDowloader"); await device.CreatePortMapAsync(portMapping); - ApiManager.SendMessageNewLine($"The public port is: {ApiConfiguration.PublicPort}", NotificationMessageType.Success); + SendMessageNewLine($"The public port is: {ApiConfiguration.PublicPort}", NotificationMessageType.Success); return true; } catch (NatDeviceNotFoundException) { - ApiManager.SendMessageNewLine($"Could not find a UPnP or NAT-PMP device!", NotificationMessageType.Error); + SendMessageNewLine($"Could not find a UPnP or NAT-PMP device!", NotificationMessageType.Error); return false; } catch (MappingException ex) { - ApiManager.SendMessageNewLine($"An error occurred while mapping the private port ({ApiConfiguration.PrivatePort}) to the public port ({ApiConfiguration.PublicPort})! Error message: {ex.Message}", NotificationMessageType.Error); + SendMessageNewLine($"An error occurred while mapping the private port ({ApiConfiguration.PrivatePort}) to the public port ({ApiConfiguration.PublicPort})! Error message: {ex.Message}", NotificationMessageType.Error); return false; } } + public static void ClosePorts() + { + SendMessageNewLine("Closing ports...", NotificationMessageType.Info); + if (device is not null) + { + try + { + IEnumerable? mappings = device.GetAllMappingsAsync().GetAwaiter().GetResult(); + foreach (Mapping? mapping in mappings) + { + if (mapping.Description.Contains("HyperbolicDowloader") && mapping.PrivateIP.ToString() == portMapping?.PrivateIP.ToString()) + { + device.DeletePortMapAsync(mapping).GetAwaiter().GetResult(); + } + } + } + catch (Exception ex) + { + SendMessageNewLine(ex.ToString(), NotificationMessageType.Error); + } + } + + SendMessageNewLine("Ports closed!", NotificationMessageType.Warning); + Environment.Exit(0); + } + public void StartBroadcastListener() { broadcastClient.StartListening(ApiConfiguration.BroadcastPort); @@ -127,7 +127,7 @@ public class ApiManager } catch (SocketException ex) { - ApiManager.SendMessageNewLine($"An error occurred while starting the TCP listener! Error message: {ex.Message}", NotificationMessageType.Error); // net stop hns && net start hns + SendMessageNewLine($"An error occurred while starting the TCP listener! Error message: {ex.Message}", NotificationMessageType.Error); // net stop hens && net start hns Console.ReadKey(); } } @@ -165,7 +165,7 @@ public class ApiManager private void DiscoverAnswer(object? sender, MessageRecivedEventArgs> recivedEventArgs) { - ApiManager.SendMessageNewLine($"Received answer from {recivedEventArgs.IpAddress}. Returned {recivedEventArgs.Data.Count} host(s).", NotificationMessageType.Raw); + SendMessageNewLine($"Received answer from {recivedEventArgs.IpAddress}. Returned {recivedEventArgs.Data.Count} host(s).", NotificationMessageType.Info); HostsManager.AddRange(recivedEventArgs.Data); } @@ -198,15 +198,15 @@ public class ApiManager private void ReciveMessage(object? sender, MessageRecivedEventArgs recivedEventArgs) { - ApiManager.SendMessageNewLine($"Received \"{recivedEventArgs.Data}\" from {recivedEventArgs.IpAddress}.", NotificationMessageType.Raw); + SendMessageNewLine($"Received \"{recivedEventArgs.Data}\" from {recivedEventArgs.IpAddress}.", NotificationMessageType.Info); } - internal static void SendMessage(string message, NotificationMessageType messageType = NotificationMessageType.Raw) + internal static void SendMessage(string message, NotificationMessageType messageType = NotificationMessageType.Info) { OnNotificationMessageRecived?.Invoke(null, new NotificationMessageEventArgs(messageType, message)); } - internal static void SendMessageNewLine(string message, NotificationMessageType messageType = NotificationMessageType.Raw) + internal static void SendMessageNewLine(string message, NotificationMessageType messageType = NotificationMessageType.Info) { OnNotificationMessageRecived?.Invoke(null, new NotificationMessageEventArgs(messageType, message + Environment.NewLine)); } diff --git a/HyperbolicDownloaderApi/Managment/NotificationMessageEventArgs.cs b/HyperbolicDownloaderApi/Managment/NotificationMessageEventArgs.cs index b012309..24aba57 100644 --- a/HyperbolicDownloaderApi/Managment/NotificationMessageEventArgs.cs +++ b/HyperbolicDownloaderApi/Managment/NotificationMessageEventArgs.cs @@ -14,7 +14,7 @@ public class NotificationMessageEventArgs : EventArgs public enum NotificationMessageType { - Raw, + Info, Success, Warning, Error