Replace gethostbynamel with dns_get_record (#1479)

This commit is contained in:
Boy132 2025-07-06 22:42:59 +02:00 committed by GitHub
parent c5aa8a3980
commit 23ddded61e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 17 deletions

View File

@ -89,16 +89,14 @@ class CreateNode extends CreateRecord
return; return;
} }
$validRecords = gethostbynamel($state); $ip = get_ip_from_hostname($state);
if ($validRecords) { if ($ip) {
$set('dns', true); $set('dns', true);
$set('ip', collect($validRecords)->first()); $set('ip', $ip);
} else {
return; $set('dns', false);
} }
$set('dns', false);
}) })
->maxLength(255), ->maxLength(255),

View File

@ -154,16 +154,14 @@ class EditNode extends EditRecord
return; return;
} }
$validRecords = gethostbynamel($state); $ip = get_ip_from_hostname($state);
if ($validRecords) { if ($ip) {
$set('dns', true); $set('dns', true);
$set('ip', collect($validRecords)->first()); $set('ip', $ip);
} else {
return; $set('dns', false);
} }
$set('dns', false);
}) })
->maxLength(255), ->maxLength(255),
TextInput::make('ip') TextInput::make('ip')
@ -618,10 +616,10 @@ class EditNode extends EditRecord
$data['config'] = $node->getYamlConfiguration(); $data['config'] = $node->getYamlConfiguration();
if (!is_ip($node->fqdn)) { if (!is_ip($node->fqdn)) {
$validRecords = gethostbynamel($node->fqdn); $ip = get_ip_from_hostname($node->fqdn);
if ($validRecords) { if ($ip) {
$data['dns'] = true; $data['dns'] = true;
$data['ip'] = collect($validRecords)->first(); $data['ip'] = $ip;
} else { } else {
$data['dns'] = false; $data['dns'] = false;
} }

View File

@ -81,3 +81,20 @@ if (!function_exists('resolve_path')) {
return implode('/', $absolutes); return implode('/', $absolutes);
} }
} }
if (!function_exists('get_ip_from_hostname')) {
function get_ip_from_hostname(string $hostname): string|bool
{
$validARecords = @dns_get_record($hostname, DNS_A);
if ($validARecords) {
return collect($validARecords)->first()['ip'];
}
$validAAAARecords = @dns_get_record($hostname, DNS_AAAA);
if ($validAAAARecords) {
return collect($validAAAARecords)->first()['ipv6'];
}
return false;
}
}