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

Support resolving from default hosts file #75

Merged
merged 10 commits into from
Aug 18, 2017
Prev Previous commit
Next Next commit
Properly filter to only return IPv4 addresses for type A queries
  • Loading branch information
clue committed Aug 15, 2017
commit d8554e59c8755293fc617c1f9e76821f64bfef35
10 changes: 6 additions & 4 deletions src/Query/HostsFileExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ public function __construct(HostsFile $hosts, ExecutorInterface $fallback)
public function query($nameserver, Query $query)
{
if ($query->class === Message::CLASS_IN && $query->type === Message::TYPE_A) {
$ips = $this->hosts->getIpsForHost($query->name);
if ($ips) {
$records = array();
foreach ($ips as $ip) {
$records = array();
foreach ($this->hosts->getIpsForHost($query->name) as $ip) {
// ensure this is an IPv4 address
if (strpos($ip, ':') === false) {
$records[] = new Record($query->name, $query->type, $query->class, 0, $ip);
}
}

if ($records) {
return Promise\resolve(
Message::createResponseWithAnswersForQuery($query, $records)
);
Expand Down
7 changes: 7 additions & 0 deletions tests/Config/HostsFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,11 @@ public function testMergesEntriesOverMultipleLines()

$this->assertEquals(array('127.0.0.1', '127.0.0.2', '127.0.0.3', '127.0.0.4'), $hosts->getIpsForHost('localhost'));
}

public function testMergesIpv4AndIpv6EntriesOverMultipleLines()
{
$hosts = new HostsFile("127.0.0.1 localhost\n::1 localhost");

$this->assertEquals(array('127.0.0.1', '::1'), $hosts->getIpsForHost('localhost'));
}
}
8 changes: 8 additions & 0 deletions tests/Query/HostsFileExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,12 @@ public function testReturnsResponseMessageIfIpsWereFound()

$ret = $this->executor->query('8.8.8.8', new Query('google.com', Message::TYPE_A, Message::CLASS_IN, 0));
}

public function testFallsBackIfNoIpv4Matches()
{
$this->hosts->expects($this->once())->method('getIpsForHost')->willReturn(array('::1'));
$this->fallback->expects($this->once())->method('query');

$ret = $this->executor->query('8.8.8.8', new Query('google.com', Message::TYPE_A, Message::CLASS_IN, 0));
}
}