Skip to content
Merged
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
12 changes: 11 additions & 1 deletion src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,20 @@ public static function loadResolvConfBlocking($path = null)
throw new RuntimeException('Unable to load resolv.conf file "' . $path . '"');
}

$matches = array();
preg_match_all('/^nameserver\s+(\S+)\s*$/m', $contents, $matches);

$config = new self();
$config->nameservers = $matches[1];
foreach ($matches[1] as $ip) {
// remove IPv6 zone ID (`fe80::1%lo0` => `fe80:1`)
if (strpos($ip, ':') !== false && ($pos = strpos($ip, '%')) !== false) {
$ip = substr($ip, 0, $pos);
}

if (@inet_pton($ip) !== false) {
$config->nameservers[] = $ip;
}
}

return $config;
}
Expand Down
11 changes: 10 additions & 1 deletion tests/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public function testParsesSingleEntryFile()
$this->assertEquals($expected, $config->nameservers);
}

public function testParsesNameserverWithoutIpv6ScopeId()
{
$contents = 'nameserver ::1%lo';
$expected = array('::1');

$config = Config::loadResolvConfBlocking('data://text/plain;base64,' . base64_encode($contents));
$this->assertEquals($expected, $config->nameservers);
}

public function testParsesNameserverEntriesFromAverageFileCorrectly()
{
$contents = '#
Expand All @@ -70,7 +79,6 @@ public function testParsesNameserverEntriesFromAverageFileCorrectly()

public function testParsesEmptyFileWithoutNameserverEntries()
{
$contents = '';
$expected = array();

$config = Config::loadResolvConfBlocking('data://text/plain;base64,');
Expand All @@ -87,6 +95,7 @@ public function testParsesFileAndIgnoresCommentsAndInvalidNameserverEntries()
nameserver 4.5.6.7 5.6.7.8
nameserver 6.7.8.9
NameServer 7.8.9.10
nameserver localhost
';
$expected = array();

Expand Down