From e5439c1a8f9b23cd3eb31c6202cea28f7855fad0 Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Tue, 29 Aug 2023 08:56:50 -0700 Subject: [PATCH] fix: Adds a timeout and retry to public IP detection (#1478) --- Projects/UOContent/Misc/ServerList.cs | 28 +++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/Projects/UOContent/Misc/ServerList.cs b/Projects/UOContent/Misc/ServerList.cs index 7b20de027..ab02ba343 100644 --- a/Projects/UOContent/Misc/ServerList.cs +++ b/Projects/UOContent/Misc/ServerList.cs @@ -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; } } }