mirror of
https://github.com/danog/dns.git
synced 2025-01-22 21:41:11 +01:00
parent
7c38a632c0
commit
8220e170b1
@ -19,12 +19,12 @@ final class DnsConfig
|
||||
private bool $rotation = false;
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
* @throws DnsConfigException
|
||||
*/
|
||||
public function __construct(array $nameservers, array $knownHosts = [])
|
||||
{
|
||||
if (\count($nameservers) < 1) {
|
||||
throw new ConfigException("At least one nameserver is required for a valid config");
|
||||
throw new DnsConfigException("At least one nameserver is required for a valid config");
|
||||
}
|
||||
|
||||
foreach ($nameservers as $nameserver) {
|
||||
@ -55,12 +55,12 @@ final class DnsConfig
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
* @throws DnsConfigException
|
||||
*/
|
||||
public function withNdots(int $ndots): self
|
||||
{
|
||||
if ($ndots < 0) {
|
||||
throw new ConfigException("Invalid ndots ($ndots), must be greater or equal to 0");
|
||||
throw new DnsConfigException("Invalid ndots ($ndots), must be greater or equal to 0");
|
||||
}
|
||||
|
||||
$self = clone $this;
|
||||
@ -80,7 +80,7 @@ final class DnsConfig
|
||||
public function withTimeout(float $timeout): self
|
||||
{
|
||||
if ($timeout < 0) {
|
||||
throw new ConfigException("Invalid timeout ($timeout), must be 0 or greater");
|
||||
throw new DnsConfigException("Invalid timeout ($timeout), must be 0 or greater");
|
||||
}
|
||||
|
||||
$self = clone $this;
|
||||
@ -92,7 +92,7 @@ final class DnsConfig
|
||||
public function withAttempts(int $attempts): self
|
||||
{
|
||||
if ($attempts < 1) {
|
||||
throw new ConfigException("Invalid attempt count ($attempts), must be 1 or greater");
|
||||
throw new DnsConfigException("Invalid attempt count ($attempts), must be 1 or greater");
|
||||
}
|
||||
|
||||
$self = clone $this;
|
||||
@ -137,7 +137,7 @@ final class DnsConfig
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
* @throws DnsConfigException
|
||||
*/
|
||||
private function validateNameserver(string $nameserver): void
|
||||
{
|
||||
@ -145,13 +145,13 @@ final class DnsConfig
|
||||
$addr = \strstr(\substr($nameserver, 1), "]", true);
|
||||
$addrEnd = \strrpos($nameserver, "]");
|
||||
if ($addrEnd === false) {
|
||||
throw new ConfigException("Invalid nameserver: $nameserver");
|
||||
throw new DnsConfigException("Invalid nameserver: $nameserver");
|
||||
}
|
||||
|
||||
$port = \substr($nameserver, $addrEnd + 1);
|
||||
|
||||
if ($port !== "" && !\preg_match("(^:(\\d+)$)", $port)) {
|
||||
throw new ConfigException("Invalid nameserver: $nameserver");
|
||||
throw new DnsConfigException("Invalid nameserver: $nameserver");
|
||||
}
|
||||
|
||||
$port = $port === "" ? 53 : \substr($port, 1);
|
||||
@ -170,11 +170,11 @@ final class DnsConfig
|
||||
$port = (int) $port;
|
||||
|
||||
if (!@\inet_pton($addr)) {
|
||||
throw new ConfigException("Invalid server IP: $addr");
|
||||
throw new DnsConfigException("Invalid server IP: $addr");
|
||||
}
|
||||
|
||||
if ($port < 1 || $port > 65535) {
|
||||
throw new ConfigException("Invalid server port: $port");
|
||||
throw new DnsConfigException("Invalid server port: $port");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ namespace Amp\Dns;
|
||||
/**
|
||||
* MUST be thrown in case the config can't be read and no fallback is available.
|
||||
*/
|
||||
class ConfigException extends DnsException
|
||||
class DnsConfigException extends DnsException
|
||||
{
|
||||
public function __construct(string $message, ?\Throwable $previous = null)
|
||||
{
|
@ -21,7 +21,7 @@ final class HostLoader
|
||||
{
|
||||
try {
|
||||
$contents = $this->readFile($this->path);
|
||||
} catch (ConfigException) {
|
||||
} catch (DnsConfigException) {
|
||||
return [];
|
||||
}
|
||||
|
||||
@ -62,7 +62,7 @@ final class HostLoader
|
||||
public function readFile(string $path): string
|
||||
{
|
||||
\set_error_handler(static function (int $errno, string $message) use ($path) {
|
||||
throw new ConfigException("Could not read configuration file '{$path}' ({$errno}): $message");
|
||||
throw new DnsConfigException("Could not read configuration file '{$path}' ({$errno}): $message");
|
||||
});
|
||||
|
||||
try {
|
||||
|
@ -239,7 +239,7 @@ final class Rfc1035StubDnsResolver implements DnsResolver
|
||||
try {
|
||||
$this->config = $this->configLoader->loadConfig();
|
||||
$this->configStatus = self::CONFIG_LOADED;
|
||||
} catch (ConfigException $e) {
|
||||
} catch (DnsConfigException $e) {
|
||||
$this->configStatus = self::CONFIG_FAILED;
|
||||
|
||||
$message = "Could not load the system's DNS configuration; "
|
||||
|
@ -135,7 +135,7 @@ final class UnixDnsConfigLoader implements DnsConfigLoader
|
||||
private function readFile(string $path): string
|
||||
{
|
||||
\set_error_handler(static function (int $errno, string $message) use ($path) {
|
||||
throw new ConfigException("Could not read configuration file '{$path}' ({$errno}) $message");
|
||||
throw new DnsConfigException("Could not read configuration file '{$path}' ({$errno}) $message");
|
||||
});
|
||||
|
||||
try {
|
||||
|
@ -55,7 +55,7 @@ final class WindowsDnsConfigLoader implements DnsConfigLoader
|
||||
}
|
||||
|
||||
if ($nameserver === "") {
|
||||
throw new ConfigException("Could not find a nameserver in the Windows Registry");
|
||||
throw new DnsConfigException("Could not find a nameserver in the Windows Registry");
|
||||
}
|
||||
|
||||
$nameservers = [];
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace Amp\Dns\Test;
|
||||
|
||||
use Amp\Dns\ConfigException;
|
||||
use Amp\Dns\DnsConfigException;
|
||||
use Amp\Dns\DnsConfig;
|
||||
use Amp\PHPUnit\AsyncTestCase;
|
||||
|
||||
@ -35,7 +35,7 @@ class DnsConfigTest extends AsyncTestCase
|
||||
*/
|
||||
public function testRejectsInvalidServers(array $nameservers): void
|
||||
{
|
||||
$this->expectException(ConfigException::class);
|
||||
$this->expectException(DnsConfigException::class);
|
||||
new DnsConfig($nameservers);
|
||||
}
|
||||
|
||||
@ -62,21 +62,21 @@ class DnsConfigTest extends AsyncTestCase
|
||||
|
||||
public function testInvalidTimeout(): void
|
||||
{
|
||||
$this->expectException(ConfigException::class);
|
||||
$this->expectException(DnsConfigException::class);
|
||||
$config = new DnsConfig(["127.0.0.1"]);
|
||||
$config->withTimeout(-1);
|
||||
}
|
||||
|
||||
public function testInvalidAttempts(): void
|
||||
{
|
||||
$this->expectException(ConfigException::class);
|
||||
$this->expectException(DnsConfigException::class);
|
||||
$config = new DnsConfig(["127.0.0.1"]);
|
||||
$config->withAttempts(0);
|
||||
}
|
||||
|
||||
public function testInvalidNdots(): void
|
||||
{
|
||||
$this->expectException(ConfigException::class);
|
||||
$this->expectException(DnsConfigException::class);
|
||||
$config = new DnsConfig(["127.0.0.1"]);
|
||||
$config->withNdots(-1);
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace Amp\Dns\Test;
|
||||
|
||||
use Amp\Dns\ConfigException;
|
||||
use Amp\Dns\DnsConfigException;
|
||||
use Amp\Dns\UnixDnsConfigLoader;
|
||||
use Amp\PHPUnit\AsyncTestCase;
|
||||
|
||||
@ -106,7 +106,7 @@ class UnixDnsConfigLoaderTest extends AsyncTestCase
|
||||
|
||||
public function testNoDefaultsOnConfNotFound(): void
|
||||
{
|
||||
$this->expectException(ConfigException::class);
|
||||
$this->expectException(DnsConfigException::class);
|
||||
(new UnixDnsConfigLoader(__DIR__ . "/data/non-existent.conf"))->loadConfig();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user