2017-06-17 12:30:38 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Dns;
|
|
|
|
|
2019-01-04 18:20:52 +01:00
|
|
|
use Amp\Failure;
|
2017-06-17 12:30:38 +02:00
|
|
|
use Amp\Promise;
|
2019-01-04 18:20:52 +01:00
|
|
|
use Amp\Success;
|
2017-06-17 12:30:38 +02:00
|
|
|
use function Amp\call;
|
|
|
|
|
2019-01-25 02:27:47 +01:00
|
|
|
class UnixConfigLoader implements ConfigLoader
|
|
|
|
{
|
2019-07-30 18:09:33 +02:00
|
|
|
const MAX_NAMESERVERS = 3;
|
|
|
|
const MAX_DNS_SEARCH = 6;
|
|
|
|
|
|
|
|
const MAX_TIMEOUT = 30 * 1000;
|
|
|
|
const MAX_ATTEMPTS = 5;
|
|
|
|
const MAX_NDOTS = 15;
|
|
|
|
|
|
|
|
const DEFAULT_TIMEOUT = 5 * 1000;
|
|
|
|
const DEFAULT_ATTEMPTS = 2;
|
|
|
|
const DEFAULT_NDOTS = 1;
|
|
|
|
|
|
|
|
const DEFAULT_OPTIONS = [
|
|
|
|
"timeout" => self::DEFAULT_TIMEOUT,
|
|
|
|
"attempts" => self::DEFAULT_ATTEMPTS,
|
|
|
|
"ndots" => self::DEFAULT_NDOTS,
|
|
|
|
"rotate" => false,
|
|
|
|
];
|
2017-06-17 12:30:38 +02:00
|
|
|
private $path;
|
|
|
|
private $hostLoader;
|
|
|
|
|
2019-01-25 02:27:47 +01:00
|
|
|
public function __construct(string $path = "/etc/resolv.conf", HostLoader $hostLoader = null)
|
|
|
|
{
|
2017-06-17 12:30:38 +02:00
|
|
|
$this->path = $path;
|
|
|
|
$this->hostLoader = $hostLoader ?? new HostLoader;
|
|
|
|
}
|
|
|
|
|
2019-01-25 02:27:47 +01:00
|
|
|
protected function readFile(string $path): Promise
|
|
|
|
{
|
2019-01-04 18:20:52 +01:00
|
|
|
\set_error_handler(function (int $errno, string $message) use ($path) {
|
|
|
|
throw new ConfigException("Could not read configuration file '{$path}' ({$errno}) $message");
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Blocking file access, but this file should be local and usually loaded only once.
|
|
|
|
$fileContent = \file_get_contents($path);
|
|
|
|
} catch (ConfigException $exception) {
|
|
|
|
return new Failure($exception);
|
|
|
|
} finally {
|
|
|
|
\restore_error_handler();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Success($fileContent);
|
|
|
|
}
|
|
|
|
|
2019-01-25 02:27:47 +01:00
|
|
|
final public function loadConfig(): Promise
|
|
|
|
{
|
2017-06-17 12:30:38 +02:00
|
|
|
return call(function () {
|
|
|
|
$nameservers = [];
|
2019-07-30 18:09:33 +02:00
|
|
|
$searchList = [];
|
|
|
|
$options = self::DEFAULT_OPTIONS;
|
|
|
|
$haveLocaldomainEnv = false;
|
|
|
|
|
|
|
|
/* Allow user to override the local domain definition. */
|
|
|
|
if ($localdomain = \getenv("LOCALDOMAIN")) {
|
|
|
|
/* Set search list to be blank-separated strings from rest of
|
|
|
|
env value. Permits users of LOCALDOMAIN to still have a
|
|
|
|
search list, and anyone to set the one that they want to use
|
|
|
|
as an individual (even more important now that the rfc1535
|
|
|
|
stuff restricts searches). */
|
|
|
|
$searchList = $this->splitOnWhitespace($localdomain);
|
|
|
|
$haveLocaldomainEnv = true;
|
|
|
|
}
|
2017-06-17 12:30:38 +02:00
|
|
|
|
2019-01-04 18:20:52 +01:00
|
|
|
$fileContent = yield $this->readFile($this->path);
|
2017-06-17 12:30:38 +02:00
|
|
|
|
2019-01-04 18:20:52 +01:00
|
|
|
$lines = \explode("\n", $fileContent);
|
2017-06-17 12:30:38 +02:00
|
|
|
|
2019-01-04 18:20:52 +01:00
|
|
|
foreach ($lines as $line) {
|
|
|
|
$line = \preg_split('#\s+#', $line, 2);
|
2017-06-17 12:30:38 +02:00
|
|
|
|
2019-01-04 18:20:52 +01:00
|
|
|
if (\count($line) !== 2) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-17 12:30:38 +02:00
|
|
|
|
2019-01-04 18:20:52 +01:00
|
|
|
list($type, $value) = $line;
|
2017-06-17 12:30:38 +02:00
|
|
|
|
2019-01-04 18:20:52 +01:00
|
|
|
if ($type === "nameserver") {
|
2019-07-30 18:09:33 +02:00
|
|
|
if (\count($nameservers) === self::MAX_NAMESERVERS) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-04 18:20:52 +01:00
|
|
|
$value = \trim($value);
|
|
|
|
$ip = @\inet_pton($value);
|
2017-06-17 12:30:38 +02:00
|
|
|
|
2019-01-04 18:20:52 +01:00
|
|
|
if ($ip === false) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-17 12:30:38 +02:00
|
|
|
|
2019-01-04 18:20:52 +01:00
|
|
|
if (isset($ip[15])) { // IPv6
|
|
|
|
$nameservers[] = "[" . $value . "]:53";
|
|
|
|
} else { // IPv4
|
|
|
|
$nameservers[] = $value . ":53";
|
|
|
|
}
|
2019-07-30 18:09:33 +02:00
|
|
|
} elseif ($type === "domain" && !$haveLocaldomainEnv) { // LOCALDOMAIN env overrides config
|
|
|
|
$searchList = $this->splitOnWhitespace($value);
|
|
|
|
} elseif ($type === "search" && !$haveLocaldomainEnv) { // LOCALDOMAIN env overrides config
|
|
|
|
$searchList = $this->splitOnWhitespace($value);
|
2019-01-04 18:20:52 +01:00
|
|
|
} elseif ($type === "options") {
|
2019-07-30 18:09:33 +02:00
|
|
|
$option = $this->parseOption($value);
|
|
|
|
if (\count($option) === 2) {
|
|
|
|
$options[$option[0]] = $option[1];
|
2019-01-04 18:20:52 +01:00
|
|
|
}
|
2019-07-30 18:09:33 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-17 12:30:38 +02:00
|
|
|
|
2019-07-30 18:09:33 +02:00
|
|
|
$hosts = yield $this->hostLoader->loadHosts();
|
2017-06-17 12:30:38 +02:00
|
|
|
|
2019-07-30 18:09:33 +02:00
|
|
|
if (\count($searchList) === 0) {
|
|
|
|
$hostname = \gethostname();
|
|
|
|
$dot = \strpos(".", $hostname);
|
|
|
|
if ($dot !== false && $dot < \strlen($hostname)) {
|
|
|
|
$searchList = [
|
|
|
|
\substr($hostname, $dot),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (\count($searchList) > self::MAX_DNS_SEARCH) {
|
|
|
|
$searchList = \array_slice($searchList, 0, self::MAX_DNS_SEARCH);
|
|
|
|
}
|
2017-06-17 12:30:38 +02:00
|
|
|
|
2019-07-30 18:09:33 +02:00
|
|
|
$resOptions = \getenv("RES_OPTIONS");
|
|
|
|
if ($resOptions) {
|
|
|
|
foreach ($this->splitOnWhitespace($resOptions) as $option) {
|
|
|
|
$option = $this->parseOption($option);
|
|
|
|
if (\count($option) === 2) {
|
|
|
|
$options[$option[0]] = $option[1];
|
2017-06-17 12:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:09:33 +02:00
|
|
|
$config = new Config($nameservers, $hosts, $options["timeout"], $options["attempts"]);
|
2017-06-17 12:30:38 +02:00
|
|
|
|
2019-07-30 18:09:33 +02:00
|
|
|
return $config->withSearchList($searchList)
|
|
|
|
->withNdots($options["ndots"])
|
|
|
|
->withRotationEnabled($options["rotate"]);
|
2017-06-17 12:30:38 +02:00
|
|
|
});
|
|
|
|
}
|
2019-07-30 18:09:33 +02:00
|
|
|
|
|
|
|
private function splitOnWhitespace(string $names): array
|
|
|
|
{
|
|
|
|
return \preg_split("#\s+#", \trim($names));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function parseOption(string $option): array
|
|
|
|
{
|
|
|
|
$optline = \explode(':', $option, 2);
|
|
|
|
list($name, $value) = $optline + [1 => null];
|
|
|
|
|
|
|
|
switch ($name) {
|
|
|
|
case "timeout":
|
|
|
|
$value = (int) $value;
|
|
|
|
if ($value < 0) {
|
|
|
|
return []; // don't overwrite option value
|
|
|
|
}
|
|
|
|
// The value for this option is silently capped to 30s
|
|
|
|
return ["timeout", (int) \min($value * 1000, self::MAX_TIMEOUT)];
|
|
|
|
|
|
|
|
case "attempts":
|
|
|
|
$value = (int) $value;
|
|
|
|
if ($value < 0) {
|
|
|
|
return []; // don't overwrite option value
|
|
|
|
}
|
|
|
|
// The value for this option is silently capped to 5
|
|
|
|
return ["attempts", (int) \min($value, self::MAX_ATTEMPTS)];
|
|
|
|
|
|
|
|
case "ndots":
|
|
|
|
$value = (int) $value;
|
|
|
|
if ($value < 0) {
|
|
|
|
return []; // don't overwrite option value
|
|
|
|
}
|
|
|
|
// The value for this option is silently capped to 15
|
|
|
|
return ["ndots", (int) \min($value, self::MAX_NDOTS)];
|
|
|
|
|
|
|
|
case "rotate":
|
|
|
|
return ["rotate", true];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
2017-06-17 12:30:38 +02:00
|
|
|
}
|