Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced WMIC nameserver lookup with PowerShell equivalent #116

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@
"require": {
"php": ">=8.1",
"ext-filter": "*",
"ext-json": "*",
"amphp/amp": "^3",
"amphp/byte-stream": "^2",
"amphp/cache": "^2",
"amphp/parser": "^1",
"amphp/windows-registry": "^1.0.1",
"amphp/process": "^2",
"daverandom/libdns": "^2.0.2",
"revolt/event-loop": "^1 || ^0.2"
},
Expand Down
82 changes: 21 additions & 61 deletions src/WindowsDnsConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,89 +4,49 @@

use Amp\ForbidCloning;
use Amp\ForbidSerialization;
use Amp\WindowsRegistry\KeyNotFoundException;
use Amp\WindowsRegistry\WindowsRegistry;
use Amp\Process\Process;
use function Amp\ByteStream\buffer;
use function Amp\ByteStream\splitLines;

final class WindowsDnsConfigLoader implements DnsConfigLoader
{
use ForbidCloning;
use ForbidSerialization;

private const NETWORK_CARDS_KEY =
'HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards';
private const TCPIP_PARAMETERS_KEY =
'HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces';

public function __construct(
private readonly HostLoader $hostLoader = new HostLoader(),
) {
}

public function loadConfig(): DnsConfig
{
$keys = [
"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\NameServer",
"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\DhcpNameServer",
];

$nameserver = "";

while ($nameserver === "" && ($key = \array_shift($keys))) {
try {
$nameserver = WindowsRegistry::read($key);
} catch (KeyNotFoundException) {
// retry other possible locations
}
$powershell = Process::start([
'powershell',
'-Command',
'Get-WmiObject -Class Win32_NetworkAdapterConfiguration |
Select-Object -ExpandProperty DNSServerSearchOrder',
]);

if ($powershell->join() !== 0) {
throw new DnsConfigException("Could not fetch DNS servers from WMI: " . buffer($powershell->getStderr()));
}

if ($nameserver === "") {
foreach (self::findNetworkCardGuids() as $guid) {
foreach (["NameServer", "DhcpNameServer"] as $property) {
try {
$nameserver = WindowsRegistry::read(self::TCPIP_PARAMETERS_KEY . "\\$guid\\$property");

if ($nameserver !== "") {
break 2;
}
} catch (KeyNotFoundException) {
// retry other possible locations
}
}
}
}

if ($nameserver === "") {
throw new DnsConfigException("Could not find a nameserver in the Windows Registry");
}
$output = \iterator_to_array(splitLines($powershell->getStdout()));

$nameservers = [];

// Comma is the delimiter for the NameServer key, but space is used for the DhcpNameServer key.
foreach (\explode(" ", \strtr($nameserver, ",", " ")) as $nameserver) {
$nameserver = \trim($nameserver);
$ip = \inet_pton($nameserver);

if ($ip === false) {
continue;
}
$nameservers = \array_reduce($output, static function (array $nameservers, string $address): array {
$ip = \inet_pton($address);

if (isset($ip[15])) { // IPv6
$nameservers[] = "[" . $nameserver . "]:53";
} else { // IPv4
$nameservers[] = $nameserver . ":53";
$nameservers[] = "[$address]:53";
} elseif (isset($ip[3])) { // IPv4
$nameservers[] = "$address:53";
}
}

return $nameservers;
}, []);

$hosts = $this->hostLoader->loadHosts();

return new DnsConfig($nameservers, $hosts);
}

private static function findNetworkCardGuids(): array
{
return \array_map(
static fn (string $key): string => WindowsRegistry::read("$key\\ServiceName"),
WindowsRegistry::listKeys(self::NETWORK_CARDS_KEY),
);
}
}
Loading