fix: Adds a timeout and retry to public IP detection (#1478)

This commit is contained in:
Kamron Batman 2023-08-29 08:56:50 -07:00 committed by GitHub
parent ea7302de59
commit e5439c1a8f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -108,7 +108,7 @@ namespace Server.Misc
}
else
{
logger.Warning("Could not auto-detect public IP address");
logger.Error("Could not auto-detect public IP address. Users will not be able to connect!");
}
}
}
@ -170,17 +170,25 @@ namespace Server.Misc
private static IPAddress FindPublicAddress()
{
try
const int count = 3;
for (var i = 0; i < count; i++)
{
// This isn't called often so we don't need to optimize
using HttpClient hc = new HttpClient();
var ipAddress = hc.GetStringAsync(_ipifyUrl).Result;
return IPAddress.Parse(ipAddress);
}
catch
{
return null;
try
{
// This isn't called often so we don't need to optimize
using HttpClient hc = new HttpClient();
hc.Timeout = TimeSpan.FromSeconds(1); // Only wait 1 second
var ipAddress = hc.GetStringAsync(_ipifyUrl).Result;
return IPAddress.Parse(ipAddress);
}
catch (Exception e)
{
logger.Error(e, "Failed to get public IP address.");
}
}
logger.Warning("Attempted to get a public IP address {Count} times from {RemoteIPService} and failed.", count, _ipifyUrl);
return null;
}
}
}