2016-12-30 04:16:14 +01:00
|
|
|
<?php
|
2015-07-28 03:53:44 +02:00
|
|
|
|
|
|
|
namespace Amp\Dns;
|
2016-08-24 06:14:10 +02:00
|
|
|
|
2017-06-15 22:06:50 +02:00
|
|
|
use Amp\Loop;
|
|
|
|
use Amp\Promise;
|
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
|
|
|
/**
|
2017-06-17 10:49:54 +02:00
|
|
|
* Retrieve the application-wide dns resolver instance.
|
2016-03-19 22:23:55 +01:00
|
|
|
*
|
2016-08-02 23:57:40 +02:00
|
|
|
* @param \Amp\Dns\Resolver $resolver Optionally specify a new default dns resolver instance
|
2017-06-17 12:30:38 +02:00
|
|
|
*
|
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);
|
2017-06-15 22:06:50 +02:00
|
|
|
|
2016-08-02 23:57:40 +02:00
|
|
|
if ($resolver) {
|
|
|
|
return $resolver;
|
|
|
|
}
|
|
|
|
|
|
|
|
$resolver = driver();
|
2016-03-19 22:23:55 +01:00
|
|
|
}
|
2017-06-15 22:06:50 +02:00
|
|
|
|
2016-08-12 17:51:40 +02:00
|
|
|
Loop::setState(LOOP_STATE_IDENTIFIER, $resolver);
|
2017-06-15 22:06:50 +02:00
|
|
|
|
2016-08-02 23:57:40 +02:00
|
|
|
return $resolver;
|
2016-03-19 22:23:55 +01:00
|
|
|
}
|
2017-06-15 22:06:50 +02:00
|
|
|
|
2016-03-19 22:23:55 +01:00
|
|
|
/**
|
2017-06-17 10:49:54 +02:00
|
|
|
* Create a new dns resolver best-suited for the current environment.
|
2016-03-19 22:23:55 +01:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
|
|
|
/**
|
2017-06-22 19:22:45 +02:00
|
|
|
* Resolve a hostname name to an IP address [hostname as defined by RFC 3986].
|
2015-08-02 04:18:44 +02:00
|
|
|
*
|
2017-06-22 19:22:45 +02:00
|
|
|
* Upon success the returned promise resolves to an array of Record objects.
|
2015-08-02 04:18:44 +02:00
|
|
|
*
|
2017-06-22 19:22:45 +02:00
|
|
|
* A null $ttl value indicates the DNS name was resolved from the cache or the local hosts file.
|
2015-08-02 04:18:44 +02:00
|
|
|
*
|
|
|
|
* @param string $name The hostname to resolve
|
2017-06-17 12:30:38 +02:00
|
|
|
*
|
2017-06-23 01:02:07 +02:00
|
|
|
* @return Promise
|
2015-08-02 04:18:44 +02:00
|
|
|
*/
|
2017-06-23 01:02:07 +02:00
|
|
|
function resolve(string $name): Promise {
|
|
|
|
return resolver()->resolve($name);
|
2015-08-30 16:39:04 +02:00
|
|
|
}
|
2017-06-15 22:06:50 +02:00
|
|
|
|
2015-09-18 15:09:28 +02:00
|
|
|
/**
|
|
|
|
* Query specific DNS records.
|
|
|
|
*
|
2017-06-17 12:30:38 +02: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
|
2017-06-17 12:30:38 +02:00
|
|
|
*
|
2017-06-23 01:02:07 +02:00
|
|
|
* @return Promise
|
2015-09-18 15:09:28 +02:00
|
|
|
*/
|
2017-06-23 01:02:07 +02:00
|
|
|
function query(string $name, $type): Promise {
|
|
|
|
return resolver()->query($name, $type);
|
2017-01-25 16:36:19 +01:00
|
|
|
}
|
2017-06-15 22:06:50 +02:00
|
|
|
|
2017-06-17 12:30:38 +02:00
|
|
|
/**
|
|
|
|
* Checks whether a string is a valid DNS name.
|
|
|
|
*
|
2017-06-22 19:06:49 +02:00
|
|
|
* @param string $name String to check.
|
2017-06-17 12:30:38 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-06-22 19:06:49 +02:00
|
|
|
function isValidDnsName(string $name) {
|
|
|
|
try {
|
2017-06-22 19:22:45 +02:00
|
|
|
normalizeDnsName($name);
|
2017-06-22 19:06:49 +02:00
|
|
|
return true;
|
|
|
|
} catch (InvalidNameError $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-17 12:30:38 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 19:06:49 +02:00
|
|
|
/**
|
|
|
|
* Normalizes a DNS name and automatically checks it for validity.
|
|
|
|
*
|
|
|
|
* @param string $name DNS name.
|
|
|
|
*
|
|
|
|
* @return string Normalized DNS name.
|
|
|
|
*
|
|
|
|
* @throws InvalidNameError If an invalid name or an IDN name without ext/intl being installed has been passed.
|
|
|
|
*/
|
|
|
|
function normalizeDnsName(string $name): string {
|
|
|
|
static $pattern = '/^(?<name>[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)(\.(?&name))*$/i';
|
|
|
|
|
|
|
|
if (\function_exists('idn_to_ascii')) {
|
|
|
|
if (false === $result = \idn_to_ascii($name, 0, INTL_IDNA_VARIANT_UTS46)) {
|
|
|
|
throw new InvalidNameError("Name '{$name}' could not be processed for IDN.");
|
2017-06-15 22:06:50 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 19:06:49 +02:00
|
|
|
$name = $result;
|
|
|
|
} else {
|
|
|
|
if (\preg_match('/[\x80-\xff]/', $name)) {
|
2017-06-17 12:30:38 +02:00
|
|
|
throw new InvalidNameError(
|
2017-06-22 19:06:49 +02:00
|
|
|
"Name '{$name}' contains non-ASCII characters and IDN support is not available. " .
|
|
|
|
"Verify that ext/intl is installed for IDN support."
|
2017-06-15 22:06:50 +02:00
|
|
|
);
|
|
|
|
}
|
2017-06-22 19:06:49 +02:00
|
|
|
}
|
2017-06-15 22:06:50 +02:00
|
|
|
|
2017-06-22 19:06:49 +02:00
|
|
|
if (isset($name[253]) || !\preg_match($pattern, $name)) {
|
|
|
|
throw new InvalidNameError("Name '{$name}' is not a valid hostname.");
|
2017-06-15 22:06:50 +02:00
|
|
|
}
|
2017-06-22 19:06:49 +02:00
|
|
|
|
|
|
|
return $name;
|
2017-06-15 22:06:50 +02:00
|
|
|
}
|