1
0
mirror of https://github.com/danog/dns.git synced 2025-01-22 13:31:12 +01:00

Initial commit

WIP
This commit is contained in:
Chris Wright 2014-06-13 18:17:49 +01:00
commit 1d418d3e82
16 changed files with 263 additions and 0 deletions

6
.gitmodules vendored Normal file
View File

@ -0,0 +1,6 @@
[submodule "vendor/Alert"]
path = vendor/Alert
url = git@github.com:/rdlowrey/Alert.git
[submodule "vendor/LibDNS"]
path = vendor/LibDNS
url = git@github.com:/DaveRandom/LibDNS.git

1
.idea/.name generated Normal file
View File

@ -0,0 +1 @@
Addr

9
.idea/Addr.iml generated Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

5
.idea/encodings.xml generated Normal file
View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
</project>

5
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" />
</project>

9
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Addr.iml" filepath="$PROJECT_DIR$/.idea/Addr.iml" />
</modules>
</component>
</project>

5
.idea/scopes/scope_settings.xml generated Normal file
View File

@ -0,0 +1,5 @@
<component name="DependencyValidationManager">
<state>
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
</state>
</component>

8
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

4
README.md Normal file
View File

@ -0,0 +1,4 @@
Addr
====
Asynchronous DNS resolver using [Alert](https://github.com/rdlowrey/Alert).

24
composer.json Normal file
View File

@ -0,0 +1,24 @@
{
"name": "daverandom/addr",
"homepage": "https://github.com/DaveRandom/Addr",
"description": "Asynchronous DNS resolver using Alert",
"keywords": ["dns", "resolve", "client", "async"],
"license": "MIT",
"authors": [
{
"name": "Chris Wright",
"email": "addr@daverandom.com",
"role": "Creator / Lead Developer"
}
],
"require": {
"php": ">=5.4.0",
"rdlowrey/alert": "~0.8.1",
"daverandom/libdns": "~0.2.1"
},
"autoload": {
"psr-4": {
"Addr": "lib/Addr/"
}
}
}

28
lib/Addr/Cache.php Normal file
View File

@ -0,0 +1,28 @@
<?php
namespace Addr;
class Cache
{
/**
* @param string $name
* @return string|null
* @todo
*/
public function fetch($name)
{
}
/**
* @param string $name
* @param string $addr
* @param int $ttl
* @return string|null
* @todo
*/
public function store($name, $addr, $ttl)
{
}
}

131
lib/Addr/Resolver.php Normal file
View File

@ -0,0 +1,131 @@
<?php
namespace Addr;
use Alert\Reactor;
class Resolver
{
const INET4_ADDR = 1;
const INET6_ADDR = 2;
const PREFER_INET4 = 4;
private $domainNameMatchExpr = '/^(?:[a-z][a-z0-9\-]{0,61}[a-z0-9])(?:\.[a-z][a-z0-9\-]{0,61}[a-z0-9])*/i';
/**
* @var Reactor
*/
private $reactor;
/**
* @var Cache
*/
private $cache;
private $hostsFilePath;
private $hostsFileLastModTime = 0;
private $hostsFileData = [];
/**
* @param Reactor $reactor
* @param Cache $cache
*/
public function __construct(Reactor $reactor, Cache $cache = null)
{
$this->reactor = $reactor;
$path = stripos(PHP_OS, 'win') === 0 ? 'C:\Windows\system32\drivers\etc\hosts' : '/etc/hosts';
if (is_file($path) && is_readable($path)) {
$this->hostsFilePath = $path;
}
}
/**
* Parse a hosts file into an array
*/
private function reloadHostsFileData()
{
$this->hostsFileData = [];
foreach (file($this->hostsFilePath) as $line) {
$line = trim($line);
if ($line[0] === '#') {
continue;
}
$parts = preg_split('/\s+/', $line);
if (!filter_var($parts[0], FILTER_VALIDATE_IP)) {
continue;
}
for ($i = 1, $l = count($parts); $i < $l; $i++) {
if (preg_match($this->domainNameMatchExpr, $parts[$i])) {
$this->hostsFileData[$parts[$i]] = $parts[0];
}
}
}
}
/**
* Lookup a name in the hosts file
*
* @param string $name
* @return string|null
*/
private function resolveFromHostsFile($name)
{
if ($this->hostsFilePath) {
clearstatcache(true, $this->hostsFilePath);
$mtime = filemtime($this->hostsFilePath);
if ($mtime > $this->hostsFileLastModTime) {
$this->reloadHostsFileData();
}
}
return isset($this->hostsFileData[$name]) ? $this->hostsFileData[$name] : null;
}
/**
* Try and resolve a name through the hosts file or the cache
*
* @param string $name
* @param callable $callback
* @param int $mode
* @return bool
*/
private function resolveNameLocally($name, callable $callback, $mode = 7)
{
if (null !== $result = $this->resolveFromHostsFile($name)) {
$this->reactor->immediately(function() use($callback, $result) {
call_user_func($callback, $result);
});
return true;
}
if ($this->cache && null !== $result = $this->cache->fetch($name)) {
$this->reactor->immediately(function() use($callback, $result) {
call_user_func($callback, $result);
});
return true;
}
return false;
}
/**
* @param string $name
* @param callable $callback
*/
public function resolve($name, callable $callback)
{
if ($this->resolveNameLocally($name, $callback)) {
return;
}
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Addr;
use Alert\Reactor;
class ResolverFactory
{
/**
* Create a new resolver instance
*
* @param Reactor $reactor
* @return Resolver
*/
public function createResolver(Reactor $reactor)
{
return new Resolver($reactor, new Cache);
}
}

7
src/bootstrap.php Normal file
View File

@ -0,0 +1,7 @@
<?php
spl_autoload_register(function($className) {
if (strpos($className, 'Addr\\') === 0) {
require dirname(__DIR__) . '/lib/' . strtr($className, '\\', '/') . '.php';
}
});

1
vendor/Alert vendored Submodule

@ -0,0 +1 @@
Subproject commit a09cc62f81d8950e05e6c715668be3cb2a07d716

1
vendor/LibDNS vendored Submodule

@ -0,0 +1 @@
Subproject commit f94f92f3fa8b2776ff2593c87d93d87480efeb73