-
-
Notifications
You must be signed in to change notification settings - Fork 727
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
Added support for /etc/hosts #225
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<?php | ||
|
||
namespace React\Dns\Query; | ||
|
||
use React\Dns\Model\Message; | ||
use React\Dns\Model\Record; | ||
use React\Dns\Query\ExecutorInterface; | ||
use React\Dns\Query\Query; | ||
use React\EventLoop\LoopInterface; | ||
use React\Promise\Deferred; | ||
use React\Promise\When; | ||
use React\Stream\Stream; | ||
|
||
class HostsFileExecutor implements ExecutorInterface | ||
{ | ||
private $loop; | ||
private $executor; | ||
private $byName; | ||
private $path; | ||
private $loadingPromise; | ||
|
||
public function __construct(LoopInterface $loop, ExecutorInterface $executor, $path = "/etc/hosts") | ||
{ | ||
$this->loop = $loop; | ||
$this->executor = $executor; | ||
$this->path = $path; | ||
} | ||
|
||
public function query($nameserver, Query $query) | ||
{ | ||
$that = $this; | ||
$executor = $this->executor; | ||
|
||
return $this | ||
->loadHosts() | ||
->then(function () use ($that, $query) { | ||
return $that->doQuery($query); | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be: |
||
->then(null, function () use ($query, $nameserver, $executor) { | ||
return $executor->query($nameserver, $query); | ||
}); | ||
} | ||
|
||
public function doQuery(Query $query) | ||
{ | ||
if (Message::TYPE_A !== $query->type) { | ||
return When::reject(); | ||
} | ||
|
||
if (!isset($this->byName[$query->name])) { | ||
return When::reject(); | ||
} | ||
|
||
$records = $this->byName[$query->name]; | ||
|
||
$response = $this->buildResponse($query, $records); | ||
|
||
return When::resolve($response); | ||
} | ||
|
||
private function loadHosts() | ||
{ | ||
if (null !== $this->loadingPromise) { | ||
return $this->loadingPromise; | ||
} | ||
|
||
$this->byName = array(); | ||
|
||
$deferred = new Deferred(); | ||
$this->loadingPromise = $deferred->promise(); | ||
|
||
$that = $this; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see comment above |
||
|
||
try { | ||
|
||
if (!file_exists($this->path)) { | ||
throw new \InvalidArgumentException(sprintf("Hosts file does not exist: %s", $this->path)); | ||
} | ||
|
||
$fd = fopen($this->path, "rb"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The general problem with anything filesystem related, ever is that I'm all for supporting this type of functionality, which we kind of already do with the resolv.conf, but ideally we'd make the blocking part more explicit, and possibly even force the user to do it up-front. |
||
|
||
if (!$fd) { | ||
throw new \InvalidArgumentException(sprintf("Unable to open hosts file: %s", $this->path)); | ||
} | ||
|
||
stream_set_blocking($fd, 0); | ||
|
||
$contents = ''; | ||
|
||
$stream = new Stream($fd, $this->loop); | ||
$stream->on('data', function ($data) use (&$contents, $that) { | ||
$contents = $that->parseHosts($contents . $data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the intention of this to do streaming parsing? In that case the parser itself should be a writable stream and the input stream should be piped into it. The parser can then emit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively, we could just assume the file will be small and read it all in one go. It's a one-off process anyway. |
||
}); | ||
$stream->on('end', function () use (&$contents, $deferred, $that) { | ||
$that->parseHosts($contents . "\n"); | ||
$deferred->resolve($contents); | ||
}); | ||
$stream->on('error', function ($error) use ($deferred) { | ||
$deferred->reject($error); | ||
}); | ||
|
||
} catch(\Exception $e) { | ||
$deferred->reject($e); | ||
} | ||
|
||
return $this->loadingPromise; | ||
} | ||
|
||
public function parseHosts($contents) | ||
{ | ||
$offset = 0; | ||
$end = 0; | ||
while (false !== $end = strpos($contents, "\n", $offset)) { | ||
|
||
$line = substr($contents, $offset, $end-$offset); | ||
$offset = $end + 1; | ||
|
||
if (false !== $i = strpos($line, '#')) { | ||
$line = substr($line, 0, $i); | ||
} | ||
|
||
$fields = preg_split("#[ \t]+#", $line, -1, PREG_SPLIT_NO_EMPTY); | ||
|
||
if (count($fields) < 2) { | ||
continue; | ||
} | ||
|
||
$addr = $fields[0]; | ||
|
||
if (false === filter_var($addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { | ||
continue; | ||
} | ||
|
||
for ($i = 1, $l = count($fields); $i < $l; ++$i) { | ||
$h = $fields[$i]; | ||
$this->byName[$h][] = new Record($h, Message::TYPE_A, Message::CLASS_IN, 300, $addr); | ||
} | ||
} | ||
|
||
return substr($contents, $offset); | ||
} | ||
|
||
public function buildResponse(Query $query, array $records) | ||
{ | ||
$response = new Message(); | ||
|
||
$response->header->set('id', $this->generateId()); | ||
$response->header->set('qr', 1); | ||
$response->header->set('opcode', Message::OPCODE_QUERY); | ||
$response->header->set('rd', 1); | ||
$response->header->set('rcode', Message::RCODE_OK); | ||
|
||
$response->questions[] = new Record($query->name, $query->type, $query->class); | ||
$response->answers = $records; | ||
|
||
$response->prepare(); | ||
|
||
return $response; | ||
} | ||
|
||
protected function generateId() | ||
{ | ||
return mt_rand(0, 0xffff); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
127.0.0.1 localhost | ||
|
||
# The following lines are desirable for IPv6 capable hosts | ||
::1 localhost ip6-localhost ip6-loopback | ||
fe00::0 ip6-localnet | ||
ff00::0 ip6-mcastprefix | ||
ff02::1 ip6-allnodes | ||
ff02::2 ip6-allrouters | ||
ff02::3 ip6-allhosts | ||
|
||
93.184.216.119 example.com | ||
2606:2800:220:6d:26bf:1447:1097:aa7 example.com | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace React\Tests\Dns\Query; | ||
|
||
use React\Dns\Model\Message; | ||
use React\Dns\Query\HostsFileExecutor; | ||
use React\Dns\Query\Query; | ||
|
||
class HostsFileExecutorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @covers React\Dns\Query\HostsFileExecutor | ||
*/ | ||
public function testQueryShouldUseFilesystem() | ||
{ | ||
$triggerListener = null; | ||
$capturedResponse = null; | ||
$query = new Query('localhost', Message::TYPE_A, Message::CLASS_IN, time()); | ||
|
||
$loop = $this->getMock('React\EventLoop\LoopInterface'); | ||
$loop | ||
->expects($this->once()) | ||
->method('addReadStream') | ||
->will($this->returnCallback(function ($stream, $listener) use (&$triggerListener) { | ||
$triggerListener = function () use ($stream, $listener) { | ||
call_user_func($listener, $stream); | ||
}; | ||
})); | ||
|
||
$fallback = $this->getMock('React\Dns\Query\ExecutorInterface'); | ||
|
||
$factory = new HostsFileExecutor($loop, $fallback, __DIR__.'/../Fixtures/etc/hosts'); | ||
|
||
$factory->query('8.8.8.8', $query)->then(function ($response) use (&$capturedResponse) { | ||
$capturedResponse = $response; | ||
}); | ||
|
||
$triggerListener(); | ||
|
||
$this->assertNotNull($capturedResponse); | ||
$this->assertCount(1, $capturedResponse->answers); | ||
$this->assertSame('127.0.0.1', $capturedResponse->answers[0]->data); | ||
} | ||
|
||
/** | ||
* @covers React\Dns\Query\HostsFileExecutor | ||
*/ | ||
public function testQueryShouldFallbackIfFileCannotBeRead() | ||
{ | ||
$triggerListener = null; | ||
$capturedResponse = null; | ||
$query = new Query('localhost', Message::TYPE_A, Message::CLASS_IN, time()); | ||
$expectedResponse = new Message; | ||
|
||
$loop = $this->getMock('React\EventLoop\LoopInterface'); | ||
|
||
$fallback = $this->getMock('React\Dns\Query\ExecutorInterface'); | ||
$fallback | ||
->expects($this->once()) | ||
->method('query') | ||
->with('8.8.8.8', $query) | ||
->will($this->returnValue($expectedResponse)); | ||
|
||
$factory = new HostsFileExecutor($loop, $fallback, __DIR__.'/../Fixtures/unexistant'); | ||
|
||
$factory->query('8.8.8.8', $query)->then(function ($response) use (&$capturedResponse) { | ||
$capturedResponse = $response; | ||
}); | ||
|
||
$this->assertSame($expectedResponse, $capturedResponse); | ||
} | ||
|
||
/** | ||
* @covers React\Dns\Query\HostsFileExecutor | ||
*/ | ||
public function testQueryShouldFallbackIfNameNotFoundInFile() | ||
{ | ||
$triggerListener = null; | ||
$capturedResponse = null; | ||
$query = new Query('unexistant.example.com', Message::TYPE_A, Message::CLASS_IN, time()); | ||
$expectedResponse = new Message; | ||
|
||
$loop = $this->getMock('React\EventLoop\LoopInterface'); | ||
$loop | ||
->expects($this->once()) | ||
->method('addReadStream') | ||
->will($this->returnCallback(function ($stream, $listener) use (&$triggerListener) { | ||
$triggerListener = function () use ($stream, $listener) { | ||
call_user_func($listener, $stream); | ||
}; | ||
})); | ||
|
||
$fallback = $this->getMock('React\Dns\Query\ExecutorInterface'); | ||
$fallback | ||
->expects($this->once()) | ||
->method('query') | ||
->with('8.8.8.8', $query) | ||
->will($this->returnValue($expectedResponse)); | ||
|
||
$factory = new HostsFileExecutor($loop, $fallback, __DIR__.'/../Fixtures/etc/hosts'); | ||
|
||
$factory->query('8.8.8.8', $query)->then(function ($response) use (&$capturedResponse) { | ||
$capturedResponse = $response; | ||
}); | ||
|
||
$triggerListener(); | ||
|
||
$this->assertSame($expectedResponse, $capturedResponse); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
master is now 5.4+, so this stuff can go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is great news :)