Skip to content

Commit 3e3f413

Browse files
authored
Fixed Windows nameserver detection by only enumerating real NICs. (#114)
1 parent 235b3de commit 3e3f413

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

‎src/WindowsDnsConfigLoader.php‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ final class WindowsDnsConfigLoader implements DnsConfigLoader
1212
use ForbidCloning;
1313
use ForbidSerialization;
1414

15+
private const NETWORK_CARDS_KEY =
16+
'HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards';
17+
private const TCPIP_PARAMETERS_KEY =
18+
'HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces';
19+
1520
public function __construct(
1621
private readonly HostLoader $hostLoader = new HostLoader(),
1722
) {
@@ -35,13 +40,10 @@ public function loadConfig(): DnsConfig
3540
}
3641

3742
if ($nameserver === "") {
38-
$interfaces = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces";
39-
$subKeys = WindowsRegistry::listKeys($interfaces);
40-
41-
foreach ($subKeys as $key) {
43+
foreach (self::findNetworkCardGuids() as $guid) {
4244
foreach (["NameServer", "DhcpNameServer"] as $property) {
4345
try {
44-
$nameserver = WindowsRegistry::read("{$key}\\{$property}");
46+
$nameserver = WindowsRegistry::read(self::TCPIP_PARAMETERS_KEY . "\\$guid\\$property");
4547

4648
if ($nameserver !== "") {
4749
break 2;
@@ -59,7 +61,7 @@ public function loadConfig(): DnsConfig
5961

6062
$nameservers = [];
6163

62-
// Microsoft documents space as delimiter, AppVeyor uses comma, we just accept both
64+
// Comma is the delimiter for the NameServer key, but space is used for the DhcpNameServer key.
6365
foreach (\explode(" ", \strtr($nameserver, ",", " ")) as $nameserver) {
6466
$nameserver = \trim($nameserver);
6567
$ip = \inet_pton($nameserver);
@@ -79,4 +81,12 @@ public function loadConfig(): DnsConfig
7981

8082
return new DnsConfig($nameservers, $hosts);
8183
}
84+
85+
private static function findNetworkCardGuids(): array
86+
{
87+
return \array_map(
88+
static fn (string $key): string => WindowsRegistry::read("$key\\ServiceName"),
89+
WindowsRegistry::listKeys(self::NETWORK_CARDS_KEY),
90+
);
91+
}
8292
}

0 commit comments

Comments
 (0)