mirror of
https://github.com/Stone-Red-Code/HyperbolicDownloader.git
synced 2026-09-04 09:06:10 +02:00
Resolve domains and code structure improvements
- Resolve domains when adding a new host - Change `NotificationMessageType.Raw` to `NotificationMessageType.Info` - Fix wrong color when using the `info` command
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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,17 +108,35 @@ 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);
|
||||
ipAddress = Dns.GetHostAddresses(ipAddressInput).FirstOrDefault();
|
||||
}
|
||||
catch (SocketException ex)
|
||||
{
|
||||
Debug.WriteLine(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);
|
||||
|
||||
@@ -140,9 +159,4 @@ public class HostCommands
|
||||
ApiManager.SendMessageNewLine($"Invalid host! Error message: {ex.Message}", NotificationMessageType.Error);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ApiManager.SendMessageNewLine("Invalid IP address!", NotificationMessageType.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<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;
|
||||
@@ -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<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);
|
||||
@@ -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<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);
|
||||
}
|
||||
|
||||
@@ -198,15 +198,15 @@ public class ApiManager
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ public class NotificationMessageEventArgs : EventArgs
|
||||
|
||||
public enum NotificationMessageType
|
||||
{
|
||||
Raw,
|
||||
Info,
|
||||
Success,
|
||||
Warning,
|
||||
Error
|
||||
|
||||
Reference in New Issue
Block a user