From 8a5e0c82d8f3160303a65c78fdecff4e5593d924 Mon Sep 17 00:00:00 2001 From: Adam Daley Date: Tue, 17 Mar 2026 15:34:51 +0000 Subject: [PATCH] Switch to per-service `HttpClient` for external IP (#3343) * Switch to per-service `HttpClient` for external IP Revert `getExternalIP()` back to foreach loop that explicitly tries each service, as `RetryableHttpClient` doesn't properly retry on timeout errors with multiple base URLs. * Fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/library/FOSSBilling/Tools.php | 39 ++++++++++++++++++------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/library/FOSSBilling/Tools.php b/src/library/FOSSBilling/Tools.php index 9df477173..f32e3b03c 100644 --- a/src/library/FOSSBilling/Tools.php +++ b/src/library/FOSSBilling/Tools.php @@ -19,7 +19,6 @@ use Egulias\EmailValidator\Validation\RFCValidation; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Path; use Symfony\Component\HttpClient\HttpClient; -use Symfony\Component\HttpClient\RetryableHttpClient; class Tools { @@ -311,7 +310,7 @@ class Tools * Try order: ipify.org, ifconfig.io, ip.hestiacp.com. * * @param bool $throw if the function should throw an exception on an error - * @param ?string $bind overrides the default network interface bind. Set to `null` to disable this behavior. + * @param ?string $bind overrides the default network interface bind. When `null` (default), the configured default (BIND_TO) is used. * * @return ?string `null` if there was an error, otherwise an IP address will be returned */ @@ -319,24 +318,32 @@ class Tools { $services = ['https://api64.ipify.org', 'https://ifconfig.io/ip', 'https://ip.hestiacp.com/']; $bind ??= BIND_TO; + $client = HttpClient::create(['bindto' => $bind]); - try { - $client = new RetryableHttpClient(HttpClient::create(['bindto' => $bind])); - $response = $client->request('GET', '', [ - 'base_uri' => $services, - 'timeout' => 2, - ]); - $ip = filter_var($response->getContent(), FILTER_VALIDATE_IP); - if ($ip) { - return $ip; - } - } catch (\Exception $e) { - error_log($e->getMessage()); - if ($throw) { - throw $e; + foreach ($services as $service) { + try { + $response = $client->request('GET', $service, [ + 'timeout' => 2, + ]); + + $ip = filter_var($response->getContent(), FILTER_VALIDATE_IP); + if ($ip) { + return $ip; + } + } catch (\Exception $e) { + error_log(sprintf( + 'Error fetching external IP from "%s" (%s): %s', + $service, + get_class($e), + $e->getMessage() + )); } } + if ($throw) { + throw new \Exception('Unable to determine external IP address from any service.'); + } + return null; }