dns-over-https/lib/DoHConfig.php

82 lines
2.2 KiB
PHP
Raw Normal View History

2019-06-10 19:32:28 +02:00
<?php
namespace Amp\DoH;
use Amp\Artax\Client;
use Amp\Artax\DefaultClient;
2019-06-11 15:22:50 +02:00
use Amp\Cache\ArrayCache;
use Amp\Cache\Cache;
2019-06-11 20:07:46 +02:00
use Amp\Dns\ConfigException;
2019-06-11 15:22:50 +02:00
use Amp\Dns\ConfigLoader;
use Amp\Dns\Resolver;
2019-06-11 20:07:46 +02:00
use Amp\Dns\Rfc1035StubResolver;
2019-06-11 15:22:50 +02:00
use Amp\Dns\UnixConfigLoader;
use Amp\Dns\WindowsConfigLoader;
2019-06-10 19:32:28 +02:00
final class DoHConfig
{
private $nameservers;
private $artax;
2019-06-11 19:56:34 +02:00
private $subResolver;
2019-06-11 15:22:50 +02:00
private $configLoader;
private $cache;
2019-06-10 19:32:28 +02:00
2019-06-11 15:22:50 +02:00
public function __construct(array $nameservers, Client $artax = null, Resolver $resolver = null, ConfigLoader $configLoader = null, Cache $cache = null)
2019-06-10 19:32:28 +02:00
{
if (\count($nameservers) < 1) {
throw new ConfigException("At least one nameserver is required for a valid config");
}
foreach ($nameservers as $nameserver) {
$this->validateNameserver($nameserver);
}
$this->nameservers = $nameservers;
2019-06-11 15:22:50 +02:00
$this->artax = $artax ?? new DefaultClient();
$this->cache = $cache ?? new ArrayCache(5000/* default gc interval */, 256/* size */);
$this->configLoader = $configLoader ?? (\stripos(PHP_OS, "win") === 0
? new WindowsConfigLoader
: new UnixConfigLoader);
2019-06-11 19:56:34 +02:00
$this->subResolver = $resolver ?? new Rfc1035StubResolver(null, $this->configLoader);
2019-06-10 19:32:28 +02:00
}
private function validateNameserver($nameserver)
{
if (!($nameserver instanceof Nameserver)) {
throw new ConfigException("Invalid nameserver: {$nameserver}");
}
}
public function getNameservers(): array
{
return $this->nameservers;
}
2019-06-10 21:04:10 +02:00
public function isNameserver($string): bool
{
foreach ($this->nameservers as $nameserver) {
2019-06-11 15:22:50 +02:00
if ($nameserver->getHost() === $string) {
return true;
}
2019-06-10 21:04:10 +02:00
}
return false;
}
2019-06-10 19:32:28 +02:00
public function getArtax(): Client
{
return $this->artax;
}
2019-06-11 15:22:50 +02:00
public function getCache(): Cache
{
return $this->cache;
}
public function getConfigLoader(): ConfigLoader
{
return $this->configLoader;
}
2019-06-11 19:56:34 +02:00
public function getSubResolver(): Resolver
2019-06-11 15:22:50 +02:00
{
2019-06-11 19:56:34 +02:00
return $this->subResolver;
2019-06-11 15:22:50 +02:00
}
2019-06-10 19:32:28 +02:00
}