2015-07-28 03:53:44 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Dns;
|
2016-08-24 06:14:10 +02:00
|
|
|
|
|
|
|
use Interop\Async\{ Awaitable, Loop };
|
2016-08-02 23:57:40 +02:00
|
|
|
|
|
|
|
const LOOP_STATE_IDENTIFIER = Resolver::class;
|
2015-07-28 03:53:44 +02:00
|
|
|
|
2016-03-19 22:23:55 +01:00
|
|
|
/**
|
|
|
|
* Retrieve the application-wide dns resolver instance
|
|
|
|
*
|
2016-08-02 23:57:40 +02:00
|
|
|
* @param \Amp\Dns\Resolver $resolver Optionally specify a new default dns resolver instance
|
2016-03-19 22:23:55 +01:00
|
|
|
* @return \Amp\Dns\Resolver Returns the application-wide dns resolver instance
|
|
|
|
*/
|
2016-08-24 06:14:10 +02:00
|
|
|
function resolver(Resolver $resolver = null): Resolver {
|
2016-08-02 23:57:40 +02:00
|
|
|
if ($resolver === null) {
|
2016-08-12 17:51:40 +02:00
|
|
|
$resolver = Loop::getState(LOOP_STATE_IDENTIFIER);
|
2016-08-02 23:57:40 +02:00
|
|
|
if ($resolver) {
|
|
|
|
return $resolver;
|
|
|
|
}
|
|
|
|
|
|
|
|
$resolver = driver();
|
2016-03-19 22:23:55 +01:00
|
|
|
}
|
2016-08-12 17:51:40 +02:00
|
|
|
Loop::setState(LOOP_STATE_IDENTIFIER, $resolver);
|
2016-08-02 23:57:40 +02:00
|
|
|
return $resolver;
|
2016-03-19 22:23:55 +01:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Create a new dns resolver best-suited for the current environment
|
|
|
|
*
|
|
|
|
* @return \Amp\Dns\Resolver
|
|
|
|
*/
|
2016-08-24 06:14:10 +02:00
|
|
|
function driver(): Resolver {
|
2016-03-20 11:40:08 +01:00
|
|
|
return new DefaultResolver;
|
2016-03-19 22:23:55 +01:00
|
|
|
}
|
2015-08-02 04:18:44 +02:00
|
|
|
|
|
|
|
/**
|
2016-02-25 12:36:51 +01:00
|
|
|
* Resolve a hostname name to an IP address
|
|
|
|
* [hostname as defined by RFC 3986]
|
2015-08-02 04:18:44 +02:00
|
|
|
*
|
|
|
|
* Upon success the returned promise resolves to an indexed array of the form:
|
|
|
|
*
|
2015-09-08 23:03:25 +02:00
|
|
|
* [string $recordValue, int $type, int $ttl]
|
2015-08-02 04:18:44 +02:00
|
|
|
*
|
|
|
|
* A null $ttl value indicates the DNS name was resolved from the cache or the
|
|
|
|
* local hosts file.
|
2015-08-30 16:39:04 +02:00
|
|
|
* $type being one constant from Amp\Dns\Record
|
2015-08-02 04:18:44 +02:00
|
|
|
*
|
|
|
|
* Options:
|
|
|
|
*
|
2015-09-08 23:03:25 +02:00
|
|
|
* - "server" | string Custom DNS server address in ip or ip:port format (Default: 8.8.8.8:53)
|
|
|
|
* - "timeout" | int DNS server query timeout (Default: 3000ms)
|
|
|
|
* - "hosts" | bool Use the hosts file (Default: true)
|
2015-08-30 16:39:04 +02:00
|
|
|
* - "reload_hosts" | bool Reload the hosts file (Default: false), only active when no_hosts not true
|
2015-09-08 23:03:25 +02:00
|
|
|
* - "cache" | bool Use local DNS cache when querying (Default: true)
|
2015-08-30 16:39:04 +02:00
|
|
|
* - "types" | array Default: [Record::A, Record::AAAA] (only for resolve())
|
2015-09-18 03:37:10 +02:00
|
|
|
* - "recurse" | bool Check for DNAME and CNAME records (always active for resolve(), Default: false for query())
|
2015-08-02 04:18:44 +02:00
|
|
|
*
|
|
|
|
* If the custom per-request "server" option is not present the resolver will
|
2015-09-18 15:09:28 +02:00
|
|
|
* use the first nameserver in /etc/resolv.conf or default to Google's public
|
|
|
|
* DNS servers on Windows or if /etc/resolv.conf is not found.
|
2015-08-02 04:18:44 +02:00
|
|
|
*
|
|
|
|
* @param string $name The hostname to resolve
|
|
|
|
* @param array $options
|
2016-08-02 23:57:40 +02:00
|
|
|
* @return \Interop\Async\Awaitable
|
2015-08-02 04:18:44 +02:00
|
|
|
* @TODO add boolean "clear_cache" option flag
|
|
|
|
*/
|
2016-08-24 06:14:10 +02:00
|
|
|
function resolve(string $name, array $options = []): Awaitable {
|
2016-03-19 22:23:55 +01:00
|
|
|
return resolver()->resolve($name, $options);
|
2015-08-30 16:39:04 +02:00
|
|
|
}
|
2015-09-18 15:09:28 +02:00
|
|
|
/**
|
|
|
|
* Query specific DNS records.
|
|
|
|
*
|
2016-02-25 12:36:51 +01:00
|
|
|
* @param string $name Unlike resolve(), query() allows for requesting _any_ name (as DNS RFC allows for arbitrary strings)
|
2015-09-18 15:09:28 +02:00
|
|
|
* @param int|int[] $type Use constants of Amp\Dns\Record
|
2016-02-25 12:36:51 +01:00
|
|
|
* @param array $options @see resolve documentation
|
2016-08-02 23:57:40 +02:00
|
|
|
* @return \Interop\Async\Awaitable
|
2015-09-18 15:09:28 +02:00
|
|
|
*/
|
2016-08-24 06:14:10 +02:00
|
|
|
function query(string $name, $type, array $options = []): Awaitable {
|
2016-03-19 22:23:55 +01:00
|
|
|
return resolver()->query($name, $type, $options);
|
|
|
|
}
|