Change some behaviours

This commit is contained in:
Daniil Gentili 2019-06-11 19:56:34 +02:00
parent 29b9d08c7d
commit c56664c52b
4 changed files with 33 additions and 28 deletions

View File

@ -17,7 +17,7 @@ final class DoHConfig
{
private $nameservers;
private $artax;
private $simpleResolver;
private $subResolver;
private $configLoader;
private $cache;
@ -30,9 +30,6 @@ final class DoHConfig
foreach ($nameservers as $nameserver) {
$this->validateNameserver($nameserver);
}
if ($resolver instanceof Rfc8484StubResolver) {
throw new ConfigException("Can't use Rfc8484StubResolver as subresolver for Rfc8484StubResolver");
}
$this->nameservers = $nameservers;
$this->artax = $artax ?? new DefaultClient();
@ -40,7 +37,7 @@ final class DoHConfig
$this->configLoader = $configLoader ?? (\stripos(PHP_OS, "win") === 0
? new WindowsConfigLoader
: new UnixConfigLoader);
$this->simpleResolver = $resolver ?? new Rfc1035StubResolver(null, $this->configLoader);
$this->subResolver = $resolver ?? new Rfc1035StubResolver(null, $this->configLoader);
}
@ -79,8 +76,8 @@ final class DoHConfig
{
return $this->configLoader;
}
public function getSimpleResolver(): Resolver
public function getSubResolver(): Resolver
{
return $this->simpleResolver;
return $this->subResolver;
}
}

View File

@ -19,6 +19,7 @@ use function Amp\Dns\normalizeName;
use LibDNS\Messages\Message;
use LibDNS\Records\Question;
use LibDNS\Records\QuestionFactory;
use Amp\Dns\ConfigException;
final class Rfc8484StubResolver implements Resolver
{
@ -46,15 +47,19 @@ final class Rfc8484StubResolver implements Resolver
private $pendingQueries = [];
/** @var \Amp\Dns\Rfc1035StubResolver */
private $simpleResolver;
private $subResolver;
public function __construct(DoHConfig $config)
{
$resolver = $config->getSubResolver();
if ($resolver instanceof Rfc8484StubResolver) {
throw new ConfigException("Can't use Rfc8484StubResolver as subresolver for Rfc8484StubResolver");
}
$this->cache = $config->getCache();
$this->configLoader = $config->getConfigLoader();
$this->simpleResolver = $config->getSimpleResolver();
$this->subResolver = $resolver;
$this->dohConfig = $config;
$this->questionFactory = new QuestionFactory;
}
@ -115,7 +120,7 @@ final class Rfc8484StubResolver implements Resolver
// See https://github.com/amphp/dns/issues/58.
// See https://bugs.php.net/bug.php?id=74840.
$records = yield $this->simpleResolver->resolve($name, $typeRestriction);
$records = yield $this->subResolver->resolve($name, $typeRestriction);
return $records;
}
@ -173,6 +178,8 @@ final class Rfc8484StubResolver implements Resolver
*/
public function reloadConfig(): Promise
{
$this->subResolver->reloadConfig();
if ($this->pendingConfig) {
return $this->pendingConfig;
}

View File

@ -107,23 +107,6 @@ class DoHConfigTest extends TestCase
[new Rfc1035StubResolver()],
];
}
/**
* @param $resolver \Amp\Dns\Resolver invalid resolver instance
*
* @dataProvider provideInvalidResolver
*/
public function testRejectsInvalidResolver($resolver)
{
$this->expectException(ConfigException::class);
new DoHConfig([new Nameserver('https://cloudflare-dns.com/dns-query')], null, $resolver);
}
public function provideInvalidResolver()
{
return [
[new Rfc8484StubResolver(new DoHConfig([new Nameserver('https://mozilla.cloudflare-dns.com/dns-query')]))],
];
}
/**
* @param $configLoader \Amp\Dns\ConfigLoader Valid ConfigLoader instance
*

View File

@ -9,6 +9,8 @@ use Amp\DoH;
use Amp\DoH\Rfc8484StubResolver;
use Amp\Loop;
use Amp\PHPUnit\TestCase;
use Amp\Dns\Rfc1035StubResolver;
use Amp\Dns\ConfigException;
class Rfc8484StubResolverTest extends TestCase
{
@ -48,4 +50,20 @@ class Rfc8484StubResolverTest extends TestCase
yield $this->getResolver()->resolve("go@gle.com", Record::A);
});
}
public function testValidSubResolver()
{
Loop::run(function () {
$DohConfig = new DoH\DoHConfig([new DoH\Nameserver('https://mozilla.cloudflare-dns.com/dns-query')], null, new Rfc1035StubResolver());
$this->assertInstanceOf(Rfc8484StubResolver::class, new Rfc8484StubResolver($DohConfig));
});
}
public function testInvalidSubResolver()
{
Loop::run(function () {
$DohConfig = new DoH\DoHConfig([new DoH\Nameserver('https://mozilla.cloudflare-dns.com/dns-query')]);
$DohConfig = new DoH\DoHConfig([new DoH\Nameserver('https://mozilla.cloudflare-dns.com/dns-query')], null, new Rfc8484StubResolver($DohConfig));
$this->expectException(ConfigException::class);
new Rfc8484StubResolver($DohConfig);
});
}
}