From 7504a541ac2834b2080db227bad7bccbda1ce400 Mon Sep 17 00:00:00 2001 From: Bilge Date: Tue, 30 Jan 2024 22:35:22 +0000 Subject: [PATCH] Fixed Windows nameserver detection by only enumerating real NICs. --- src/WindowsDnsConfigLoader.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/WindowsDnsConfigLoader.php b/src/WindowsDnsConfigLoader.php index 89d4b59..32a7dcf 100644 --- a/src/WindowsDnsConfigLoader.php +++ b/src/WindowsDnsConfigLoader.php @@ -12,6 +12,11 @@ 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(), ) { @@ -35,13 +40,10 @@ public function loadConfig(): DnsConfig } if ($nameserver === "") { - $interfaces = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces"; - $subKeys = WindowsRegistry::listKeys($interfaces); - - foreach ($subKeys as $key) { + foreach (self::findNetworkCardGuids() as $guid) { foreach (["NameServer", "DhcpNameServer"] as $property) { try { - $nameserver = WindowsRegistry::read("{$key}\\{$property}") ?? ''; + $nameserver = WindowsRegistry::read(self::TCPIP_PARAMETERS_KEY . "\\$guid\\$property") ?? ''; if ($nameserver !== "") { break 2; @@ -59,7 +61,7 @@ public function loadConfig(): DnsConfig $nameservers = []; - // Microsoft documents space as delimiter, AppVeyor uses comma, we just accept both + // 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); @@ -79,4 +81,12 @@ public function loadConfig(): DnsConfig 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), + ); + } }