2017-06-17 12:30:38 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Dns;
|
|
|
|
|
|
|
|
class Config {
|
|
|
|
private $nameservers;
|
|
|
|
private $knownHosts;
|
|
|
|
private $timeout;
|
|
|
|
private $attempts;
|
|
|
|
|
|
|
|
public function __construct(array $nameservers, array $knownHosts = [], int $timeout = 3000, int $attempts = 2) {
|
|
|
|
if (\count($nameservers) < 1) {
|
|
|
|
throw new ConfigException("At least one nameserver is required for a valid config");
|
|
|
|
}
|
|
|
|
|
2017-06-22 23:25:21 +02:00
|
|
|
foreach ($nameservers as $nameserver) {
|
|
|
|
$this->validateNameserver($nameserver);
|
|
|
|
}
|
|
|
|
|
2017-06-17 12:30:38 +02:00
|
|
|
if ($timeout < 0) {
|
|
|
|
throw new ConfigException("Invalid timeout ({$timeout}), must be 0 or greater");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($attempts < 1) {
|
|
|
|
throw new ConfigException("Invalid attempt count ({$attempts}), must be 1 or greater");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->nameservers = $nameservers;
|
|
|
|
$this->knownHosts = $knownHosts;
|
|
|
|
$this->timeout = $timeout;
|
|
|
|
$this->attempts = $attempts;
|
|
|
|
}
|
|
|
|
|
2017-06-22 23:25:21 +02:00
|
|
|
private function validateNameserver(string $nameserver) {
|
|
|
|
if (!$nameserver) {
|
|
|
|
throw new ConfigException("Invalid nameserver: {$nameserver}");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($nameserver[0] === "[") { // IPv6
|
|
|
|
$addr = \strstr(\substr($nameserver, 1), "]", true);
|
|
|
|
$port = \substr($nameserver, \strrpos($nameserver, "]") + 1);
|
|
|
|
|
|
|
|
if ($port !== "" && !\preg_match("(^:(\\d+)$)", $port, $match)) {
|
|
|
|
throw new ConfigException("Invalid nameserver: {$nameserver}");
|
|
|
|
}
|
|
|
|
|
|
|
|
$port = $port === "" ? 53 : \substr($port, 1);
|
|
|
|
} else { // IPv4
|
|
|
|
$arr = \explode(":", $nameserver, 2);
|
|
|
|
|
|
|
|
if (\count($arr) === 2) {
|
|
|
|
list($addr, $port) = $arr;
|
|
|
|
} else {
|
|
|
|
$addr = $arr[0];
|
|
|
|
$port = 53;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$addr = \trim($addr, "[]");
|
|
|
|
$port = (int) $port;
|
|
|
|
|
|
|
|
if (!$inAddr = @\inet_pton($addr)) {
|
|
|
|
throw new ConfigException("Invalid server IP: {$addr}");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($port < 1 || $port > 65535) {
|
|
|
|
throw new ConfigException("Invalid server port: {$port}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-17 12:30:38 +02:00
|
|
|
public function getNameservers(): array {
|
|
|
|
return $this->nameservers;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getKnownHosts(): array {
|
|
|
|
return $this->knownHosts;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTimeout(): int {
|
|
|
|
return $this->timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAttempts(): int {
|
|
|
|
return $this->attempts;
|
|
|
|
}
|
|
|
|
}
|