fix: Adds a timeout and retry to public IP detection (#1478)
This commit is contained in:
parent
ea7302de59
commit
e5439c1a8f
1 changed files with 18 additions and 10 deletions
|
|
@ -108,7 +108,7 @@ namespace Server.Misc
|
||||||
}
|
}
|
||||||
else
|
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()
|
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
|
try
|
||||||
using HttpClient hc = new HttpClient();
|
{
|
||||||
var ipAddress = hc.GetStringAsync(_ipifyUrl).Result;
|
// This isn't called often so we don't need to optimize
|
||||||
return IPAddress.Parse(ipAddress);
|
using HttpClient hc = new HttpClient();
|
||||||
}
|
hc.Timeout = TimeSpan.FromSeconds(1); // Only wait 1 second
|
||||||
catch
|
var ipAddress = hc.GetStringAsync(_ipifyUrl).Result;
|
||||||
{
|
return IPAddress.Parse(ipAddress);
|
||||||
return null;
|
}
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue