Backwards compatibility

This commit is contained in:
Daniil Gentili 2022-10-30 20:47:30 +01:00
parent 9022700aec
commit 3108b6f63b
5 changed files with 37 additions and 11 deletions

View File

@ -41,7 +41,7 @@
} }
], ],
"require": { "require": {
"php": ">=7.0", "php": ">=8.1",
"amphp/cache": "^v2-dev", "amphp/cache": "^v2-dev",
"amphp/parser": "^1", "amphp/parser": "^1",
"danog/libdns-json": "^0.1", "danog/libdns-json": "^0.1",

View File

@ -15,7 +15,7 @@ use Amp\Http\Client\HttpClientBuilder;
final class DoHConfig final class DoHConfig
{ {
/** /**
* @var non-empty-array<NameServer> $nameservers * @var non-empty-array<Nameserver> $nameservers
*/ */
private readonly array $nameservers; private readonly array $nameservers;
private readonly DelegateHttpClient $httpClient; private readonly DelegateHttpClient $httpClient;
@ -24,15 +24,17 @@ final class DoHConfig
private readonly Cache $cache; private readonly Cache $cache;
/** /**
* @param non-empty-array<NameServer> $nameservers * @param non-empty-array<Nameserver> $nameservers
*/ */
public function __construct(array $nameservers, ?DelegateHttpClient $httpClient = null, ?Rfc1035StubResolver $resolver = null, ?ConfigLoader $configLoader = null, ?Cache $cache = null) public function __construct(array $nameservers, ?DelegateHttpClient $httpClient = null, ?Rfc1035StubResolver $resolver = null, ?ConfigLoader $configLoader = null, ?Cache $cache = null)
{ {
/** @psalm-suppress TypeDoesNotContainType */
if (\count($nameservers) < 1) { if (\count($nameservers) < 1) {
throw new ConfigException("At least one nameserver is required for a valid config"); throw new ConfigException("At least one nameserver is required for a valid config");
} }
foreach ($nameservers as $nameserver) { foreach ($nameservers as $nameserver) {
/** @psalm-suppress DocblockContradiction */
if (!($nameserver instanceof Nameserver)) { if (!($nameserver instanceof Nameserver)) {
throw new ConfigException("Invalid nameserver: {$nameserver}"); throw new ConfigException("Invalid nameserver: {$nameserver}");
} }

View File

@ -6,6 +6,10 @@ use Amp\Dns\ConfigException;
final class Nameserver final class Nameserver
{ {
public const RFC8484_GET = NameserverType::RFC8484_GET;
public const RFC8484_POST = NameserverType::RFC8484_POST;
public const GOOGLE_JSON = NameserverType::GOOGLE_JSON;
private readonly string $host; private readonly string $host;
public function __construct( public function __construct(

View File

@ -18,10 +18,8 @@ use Amp\Future;
use Amp\Http\Client\DelegateHttpClient; use Amp\Http\Client\DelegateHttpClient;
use Amp\Http\Client\Request; use Amp\Http\Client\Request;
use Amp\NullCancellation; use Amp\NullCancellation;
use Amp\Promise;
use danog\LibDNSJson\JsonDecoder; use danog\LibDNSJson\JsonDecoder;
use danog\LibDNSJson\JsonDecoderFactory; use danog\LibDNSJson\JsonDecoderFactory;
use danog\LibDNSJson\QueryEncoder;
use danog\LibDNSJson\QueryEncoderFactory; use danog\LibDNSJson\QueryEncoderFactory;
use LibDNS\Decoder\Decoder; use LibDNS\Decoder\Decoder;
use LibDNS\Decoder\DecoderFactory; use LibDNS\Decoder\DecoderFactory;
@ -48,13 +46,13 @@ final class Rfc8484StubResolver implements Resolver
private Cache $cache; private Cache $cache;
/** @var Promise[] */ /** @var Future[] */
private array $pendingQueries = []; private array $pendingQueries = [];
private Rfc1035StubResolver $subResolver; private Rfc1035StubResolver $subResolver;
private Encoder $encoder; private Encoder $encoder;
private Decoder $decoder; private Decoder $decoder;
private QueryEncoder $encoderJson; private Encoder $encoderJson;
private JsonDecoder $decoderJson; private JsonDecoder $decoderJson;
private MessageFactory $messageFactory; private MessageFactory $messageFactory;
private DelegateHttpClient $httpClient; private DelegateHttpClient $httpClient;
@ -234,6 +232,7 @@ final class Rfc8484StubResolver implements Resolver
private function queryHosts(string $name, int $typeRestriction = null): array private function queryHosts(string $name, int $typeRestriction = null): array
{ {
\assert($this->config !== null);
$hosts = $this->config->getKnownHosts(); $hosts = $this->config->getKnownHosts();
$records = []; $records = [];
@ -271,6 +270,8 @@ final class Rfc8484StubResolver implements Resolver
} }
} }
\assert($this->config !== null);
$name = $this->normalizeName($name, $type); $name = $this->normalizeName($name, $type);
$question = $this->createQuestion($name, $type); $question = $this->createQuestion($name, $type);
@ -301,7 +302,6 @@ final class Rfc8484StubResolver implements Resolver
$result = []; $result = [];
$ttls = []; $ttls = [];
/** @var \LibDNS\Records\Resource $record */
foreach ($answers as $record) { foreach ($answers as $record) {
$recordType = $record->getType(); $recordType = $record->getType();
$result[$recordType][] = (string) $record->getData(); $result[$recordType][] = (string) $record->getData();
@ -351,6 +351,7 @@ final class Rfc8484StubResolver implements Resolver
private function ask(Nameserver $nameserver, Question $question, Cancellation $cancellation): Message private function ask(Nameserver $nameserver, Question $question, Cancellation $cancellation): Message
{ {
$message = $this->createMessage($question, \random_int(0, 0xffff)); $message = $this->createMessage($question, \random_int(0, 0xffff));
$request = null;
switch ($nameserver->getType()) { switch ($nameserver->getType()) {
case NameserverType::RFC8484_GET: case NameserverType::RFC8484_GET:
$data = $this->encoder->encode($message); $data = $this->encoder->encode($message);
@ -364,7 +365,7 @@ final class Rfc8484StubResolver implements Resolver
$request->setBody($data); $request->setBody($data);
$request->setHeader('content-type', 'application/dns-message'); $request->setHeader('content-type', 'application/dns-message');
$request->setHeader('accept', 'application/dns-message'); $request->setHeader('accept', 'application/dns-message');
$request->setHeader('content-length', \strlen($data)); $request->setHeader('content-length', (string) \strlen($data));
$request->setHeaders($nameserver->getHeaders()); $request->setHeaders($nameserver->getHeaders());
break; break;
case NameserverType::GOOGLE_JSON: case NameserverType::GOOGLE_JSON:
@ -374,6 +375,7 @@ final class Rfc8484StubResolver implements Resolver
$request->setHeaders($nameserver->getHeaders()); $request->setHeaders($nameserver->getHeaders());
break; break;
} }
\assert($request !== null);
$response = $this->httpClient->request($request, $cancellation); $response = $this->httpClient->request($request, $cancellation);
if ($response->getStatus() !== 200) { if ($response->getStatus() !== 200) {
@ -390,7 +392,7 @@ final class Rfc8484StubResolver implements Resolver
} }
} }
private function normalizeName(string $name, int $type) private function normalizeName(string $name, int $type): string
{ {
if ($type === Record::PTR) { if ($type === Record::PTR) {
if (($packedIp = @\inet_pton($name)) !== false) { if (($packedIp = @\inet_pton($name)) !== false) {
@ -434,7 +436,10 @@ final class Rfc8484StubResolver implements Resolver
return self::CACHE_PREFIX.$name."#".$type; return self::CACHE_PREFIX.$name."#".$type;
} }
private function decodeCachedResult(string $name, int $type, string $encoded) /**
* @return list<Record>
*/
private function decodeCachedResult(string $name, int $type, string $encoded): array
{ {
$decoded = \json_decode($encoded, true); $decoded = \json_decode($encoded, true);

15
psalm.xml Normal file
View File

@ -0,0 +1,15 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="lib" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>