mirror of
https://github.com/Stone-Red-Code/HyperbolicDownloader.git
synced 2026-09-04 00:56:09 +02:00
Ignore broadcast messages from own PC
This commit is contained in:
@@ -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<NetworkSocket> 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)
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user