From 7d662dc1134137baec8df4d93917dc450bbab9f6 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Fri, 25 Nov 2022 12:05:34 +0100 Subject: [PATCH] Ignore broadcast messages from own PC --- HyperbolicDownloaderApi/Managment/ApiManager.cs | 17 ++++++++++------- .../Networking/HostsManager.cs | 11 ++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/HyperbolicDownloaderApi/Managment/ApiManager.cs b/HyperbolicDownloaderApi/Managment/ApiManager.cs index 1d3f806..b79aa73 100644 --- a/HyperbolicDownloaderApi/Managment/ApiManager.cs +++ b/HyperbolicDownloaderApi/Managment/ApiManager.cs @@ -35,10 +35,10 @@ public class ApiManager if (device is not null) { - ipAddress = (device.GetExternalIPAsync()).GetAwaiter().GetResult()?.ToString(); + ipAddress = device.GetExternalIPAsync().GetAwaiter().GetResult()?.ToString(); } - if (ipAddress is null || ipAddress == "0.0.0.0") + if (ipAddress is null or "0.0.0.0") { ipAddress = NetworkUtilities.GetIP4Adress()?.ToString(); port = ApiConfiguration.PrivatePort; @@ -136,24 +136,27 @@ public class ApiManager private async void BroadcastClient_OnBroadcastRecived(object? sender, BroadcastRecivedEventArgs recivedEventArgs) { - Debug.WriteLine($"Received broadcast \"{recivedEventArgs.Message}\" from {recivedEventArgs.IPEndPoint.Address}"); + IPAddress remoteIpAddress = recivedEventArgs.IPEndPoint.Address; + + Debug.WriteLine($"Received broadcast \"{recivedEventArgs.Message}\" from {remoteIpAddress}"); List hostsToSend = HostsManager.ToList(); NetworkSocket? localSocket = GetLocalSocket(); - if (localSocket is null) + if (localSocket is null || remoteIpAddress.Equals(PublicIpAddress) || remoteIpAddress.Equals(NetworkUtilities.GetIP4Adress())) { + Debug.WriteLine("Invalid broadcast!"); return; } - hostsToSend.RemoveAll(x => x.IPAddress == recivedEventArgs.IPEndPoint.Address.ToString()); + _ = hostsToSend.RemoveAll(x => x.IPAddress == recivedEventArgs.IPEndPoint.Address.ToString()); hostsToSend.Add(localSocket); bool success = int.TryParse(recivedEventArgs.Message, out int remotePort); if (success) { - HostsManager.Add(new NetworkSocket(recivedEventArgs.IPEndPoint.Address.ToString(), remotePort, DateTime.Now)); + HostsManager.Add(new NetworkSocket(remoteIpAddress.ToString(), remotePort, DateTime.Now)); try { await NetworkClient.SendAsync(recivedEventArgs.IPEndPoint.Address, remotePort, "DiscoverAnswer", hostsToSend); @@ -182,7 +185,7 @@ public class ApiManager return; } - hostsToSend.RemoveAll(x => x.IPAddress == recivedEventArgs.IpAddress.ToString()); + _ = hostsToSend.RemoveAll(x => x.IPAddress == recivedEventArgs.IpAddress.ToString()); hostsToSend.Add(localSocket); if (recivedEventArgs.Data.Port != 0) diff --git a/HyperbolicDownloaderApi/Networking/HostsManager.cs b/HyperbolicDownloaderApi/Networking/HostsManager.cs index 7c61b1c..86eddcc 100644 --- a/HyperbolicDownloaderApi/Networking/HostsManager.cs +++ b/HyperbolicDownloaderApi/Networking/HostsManager.cs @@ -14,10 +14,7 @@ public class HostsManager { foreach (NetworkSocket host in hosts) { - if (!Contains(host)) - { - this.hosts.Add(host); - } + Add(host); } SaveHosts(); @@ -36,7 +33,7 @@ public class HostsManager { if (DateTime.Now - host.LastActive >= new TimeSpan(24, 0, 0) || forceRemove) { - hosts.RemoveAll(x => x.IPAddress == host.IPAddress && x.Port == host.Port); + _ = hosts.RemoveAll(x => x.IPAddress == host.IPAddress && x.Port == host.Port); } SaveHosts(); } @@ -57,7 +54,7 @@ public class HostsManager try { - tcpClient.ConnectAsync(host.IPAddress, host.Port).Wait(500); + _ = tcpClient.ConnectAsync(host.IPAddress, host.Port).Wait(500); Console.CursorLeft = 0; if (tcpClient.Connected) { @@ -87,7 +84,7 @@ public class HostsManager foreach (NetworkSocket host in hostsToRemove) { - hosts.Remove(host); + _ = hosts.Remove(host); } SaveHosts();