HostDnsDriver.php: add

This commit is contained in:
Zankaria 2025-04-23 23:38:02 +02:00
parent 9feabab4ea
commit a86801e9b2

View File

@ -0,0 +1,43 @@
<?php
namespace Vichan\Data\Driver\Dns;
/**
* Relies on the `host` command line executable.
*/
class HostDnsDriver implements DnsDriver {
private int $timeout;
private static function matchOrEmpty(string $pattern, string $subject): array {
$ret = \preg_match_all($pattern, $subject, $out);
if ($ret === false || $ret === 0) {
return [];
}
return $out[1];
}
public function __construct(int $timeout) {
$this->timeout = $timeout;
}
public function nameToIPs(string $name): ?array {
$ret = shell_exec_error("host -W {$this->timeout} {$name}");
if ($ret === false) {
return null;
}
$ipv4 = self::matchOrEmpty('/has address ([^\s]+)/', $ret);
$ipv6 = self::matchOrEmpty('/has IPv6 address ([^\s]+)/', $ret);
return \array_merge($ipv4, $ipv6);
}
public function IPToNames(string $ip): ?array {
$ret = shell_exec_error("host -W {$this->timeout} {$ip}");
if ($ret === false) {
return null;
}
$names = self::matchOrEmpty('/domain name pointer ([^\s]+)\./', $ret);
return \array_map(fn($n) => \strtolower(\rtrim($n, '.')), $names);
}
}