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;
}
$validRecords = gethostbynamel($state);
if ($validRecords) {
$ip = get_ip_from_hostname($state);
if ($ip) {
$set('dns', true);
$set('ip', collect($validRecords)->first());
return;
}
$set('ip', $ip);
} else {
$set('dns', false);
}
})
->maxLength(255),

View File

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

View File

@ -81,3 +81,20 @@ if (!function_exists('resolve_path')) {
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;
}
}