Skip to content

Commit

Permalink
Fixed Windows nameserver detection by only enumerating real NICs. (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge authored Jan 30, 2024
1 parent 235b3de commit 3e3f413
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/WindowsDnsConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
) {
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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),
);
}
}

0 comments on commit 3e3f413

Please sign in to comment.