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

Fixed Windows nameserver detection by only enumerating real NICs #114

Merged
merged 1 commit into from
Jan 30, 2024
Merged
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
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),
);
}
}
Loading