mirror of
https://github.com/nextcloud/all-in-one.git
synced 2026-06-10 08:37:02 +00:00
0722cf95be
Agent-Logs-Url: https://github.com/nextcloud/all-in-one/sessions/c71fba87-d463-4682-9cb3-abb659b2ca40 refactor: deduplicate resolveHostname into DataConst Agent-Logs-Url: https://github.com/nextcloud/all-in-one/sessions/42427bd4-05e6-4197-bdb7-db3761815113 refactor: move resolveHostname from DataConst into new NetworkHelper class Agent-Logs-Url: https://github.com/nextcloud/all-in-one/sessions/73cb1d89-ab85-43b6-adfe-a90c00ad60a1
24 lines
729 B
PHP
24 lines
729 B
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace AIO\Helper;
|
|
|
|
class NetworkHelper {
|
|
/**
|
|
* Resolve a hostname to its IP address, trying IPv4 first and falling back
|
|
* to IPv6 (AAAA record) when no A record is found. Returns the hostname
|
|
* unchanged when neither record resolves successfully.
|
|
*/
|
|
public static function resolveHostname(string $hostname): string {
|
|
$ipv4 = gethostbyname($hostname);
|
|
if ($ipv4 !== $hostname) {
|
|
return $ipv4;
|
|
}
|
|
$records = dns_get_record($hostname, DNS_AAAA);
|
|
if (is_array($records) && isset($records[0]['ipv6']) && $records[0]['ipv6'] !== '') {
|
|
return $records[0]['ipv6'];
|
|
}
|
|
return $hostname;
|
|
}
|
|
}
|