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>
This commit is contained in:
Adam DaleyandCopilot Autofix powered by AI authored and GitHub committed 2026-03-17 15:34:51 +00:00
1 parent e42504b274
commit 8a5e0c82d8
1 file changed
+23 -16
+23 -16
View File
@@ -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;
}