Skip to content

Commit

Permalink
Resolve domains and code structure improvements
Browse files Browse the repository at this point in the history
- Resolve domains when adding a new host
- Change `NotificationMessageType.Raw` to `NotificationMessageType.Info`
- Fix wrong color when using the `info` command
  • Loading branch information
Stone-Red-Code committed Feb 26, 2022
1 parent cf7f952 commit a2fa7dd
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 68 deletions.
2 changes: 1 addition & 1 deletion HyperbolicDownloader/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static void ApiManager_OnNotificationMessageRecived(object? sender, Noti
{
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;
Expand Down
10 changes: 5 additions & 5 deletions HyperbolicDownloaderApi/Commands/ClientCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public void ShowInfo(string _)
{
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);
}
}
66 changes: 40 additions & 26 deletions HyperbolicDownloaderApi/Commands/HostCommands.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using HyperbolicDownloaderApi.Managment;
using HyperbolicDownloaderApi.Networking;

using System.Diagnostics;
using System.Net;
using System.Net.Sockets;

Expand All @@ -17,7 +18,7 @@ public HostCommands(HostsManager hostsManager)

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);
}
Expand All @@ -31,9 +32,9 @@ public void CheckActiveHosts(string _)
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 _)
Expand All @@ -50,9 +51,9 @@ public void ListHosts(string _)
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--;
}
Expand Down Expand Up @@ -107,42 +108,55 @@ public void AddHost(string args)

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<NetworkSocket>? recivedHosts = NetworkClient.Send<List<NetworkSocket>>(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);
Debug.WriteLine(ex);
}
catch (IOException ex)
}

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<NetworkSocket>? recivedHosts = NetworkClient.Send<List<NetworkSocket>>(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 host! Error message: {ex.Message}", NotificationMessageType.Error);
ApiManager.SendMessageNewLine($"Invalid response!", NotificationMessageType.Error);
}
}
else
catch (SocketException ex)
{
ApiManager.SendMessageNewLine("Invalid IP address!", NotificationMessageType.Error);
ApiManager.SendMessageNewLine($"Invalid host! Error message: {ex.Message}", NotificationMessageType.Error);
}
catch (IOException ex)
{
ApiManager.SendMessageNewLine($"Invalid host! Error message: {ex.Message}", NotificationMessageType.Error);
}
}
}
70 changes: 35 additions & 35 deletions HyperbolicDownloaderApi/Managment/ApiManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,6 @@ public ApiManager()
networkClient = new(FilesManager);
}

public static void ClosePorts()
{
ApiManager.SendMessageNewLine("Closing ports...", NotificationMessageType.Success);
if (device is not null)
{
try
{
IEnumerable<Mapping>? 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;
Expand Down Expand Up @@ -88,27 +62,53 @@ public static async Task<bool> OpenPorts()
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<Mapping>? 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);
Expand All @@ -127,7 +127,7 @@ public void StartTcpListener()
}
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();
}
}
Expand Down Expand Up @@ -165,7 +165,7 @@ private async void BroadcastClient_OnBroadcastRecived(object? sender, BroadcastR

private void DiscoverAnswer(object? sender, MessageRecivedEventArgs<List<NetworkSocket>> 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);
}

Expand Down Expand Up @@ -198,15 +198,15 @@ private async void HasFile(object? sender, MessageRecivedEventArgs<string> reciv

private void ReciveMessage(object? sender, MessageRecivedEventArgs<string> 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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public NotificationMessageEventArgs(NotificationMessageType notificationMessageT

public enum NotificationMessageType
{
Raw,
Info,
Success,
Warning,
Error
Expand Down

0 comments on commit a2fa7dd

Please sign in to comment.