Skip to content

Commit b98f463

Browse files
committed
Adapt for new amphp/dns
1 parent 1f6935b commit b98f463

15 files changed

Lines changed: 226 additions & 188 deletions

‎README.md‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,34 @@ require __DIR__ . '/examples/_bootstrap.php';
2121

2222
use Amp\DoH;
2323
use Amp\Dns;
24-
use Amp\Loop;
24+
use Amp\Dns\DnsRecord;
25+
26+
use function Amp\Future\awaitFirst;
2527

2628
// Set default resolver to DNS-over-HTTPS resolver
2729
$DohConfig = new DoH\DoHConfig([new DoH\Nameserver('https://mozilla.cloudflare-dns.com/dns-query')]); // Defaults to DoH\NameserverType::RFC8484_POST
28-
Dns\resolver(new DoH\Rfc8484StubResolver($DohConfig));
30+
Dns\dnsResolver(new DoH\Rfc8484StubDohResolver($DohConfig));
2931

30-
$githubIpv4 = Dns\resolve("github.com", Dns\Record::A);
32+
$githubIpv4 = Dns\resolve("github.com", DnsRecord::A);
3133
pretty_print_records("github.com", $githubIpv4);
3234

33-
$googleIpv4 = \Amp\async(fn () => Amp\Dns\resolve("google.com", Dns\Record::A));
34-
$googleIpv6 = \Amp\async(fn () => Amp\Dns\resolve("google.com", Dns\Record::AAAA));
35+
$googleIpv4 = \Amp\async(fn () => Amp\Dns\resolve("google.com", DnsRecord::A));
36+
$googleIpv6 = \Amp\async(fn () => Amp\Dns\resolve("google.com", DnsRecord::AAAA));
37+
38+
$firstGoogleResult = awaitFirst([$googleIpv4, $googleIpv6]);
39+
pretty_print_records("google.com", $firstGoogleResult);
3540

36-
$firstGoogleResult = Amp\awaitAll([$googleIpv4, $googleIpv6]);
41+
$combinedGoogleResult = Amp\Dns\resolve("google.com");
42+
pretty_print_records("google.com", $combinedGoogleResult);
43+
44+
$googleMx = Amp\Dns\query("google.com", DnsRecord::MX);
45+
pretty_print_records("google.com", $googleMx);
46+
$firstGoogleResult = awaitFirst([$googleIpv4, $googleIpv6]);
3747
pretty_print_records("google.com", $firstGoogleResult);
3848

3949
$combinedGoogleResult = Amp\Dns\resolve("google.com");
4050
pretty_print_records("google.com", $combinedGoogleResult);
4151

42-
$googleMx = Amp\Dns\query("google.com", Amp\Dns\Record::MX);
52+
$googleMx = Amp\Dns\query("google.com", DnsRecord::MX);
4353
pretty_print_records("google.com", $googleMx);
4454
```

‎composer.json‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,25 @@
4040
"email": "aaron@trowski.com"
4141
}
4242
],
43+
"minimum-stablity": "dev",
4344
"require": {
4445
"php": ">=8.1",
45-
"amphp/cache": "^v2-dev",
46+
"amphp/cache": "^2",
4647
"amphp/parser": "^1",
4748
"danog/libdns-json": "^0.2",
4849
"daverandom/libdns": "^2.0.1",
49-
"amphp/amp": "^v3-dev",
50-
"amphp/http-client": "^v5-dev",
51-
"amphp/dns": "^v2-dev",
50+
"amphp/amp": "^3",
51+
"amphp/http-client": "v5.x-dev",
52+
"amphp/socket": "v2.x-dev",
53+
"amphp/dns": "^2",
5254
"ext-filter": "*",
5355
"ext-json": "*"
5456
},
55-
"minimum-stability": "dev",
5657
"prefer-stable": true,
5758
"require-dev": {
5859
"amphp/phpunit-util": "^3",
5960
"phpunit/phpunit": "^9",
60-
"amphp/php-cs-fixer-config": "^2-dev",
61+
"amphp/php-cs-fixer-config": "^2",
6162
"psalm/phar": "^5"
6263
},
6364
"autoload": {

‎docs/index.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,23 @@ $nameservers []= new DoH\Nameserver('https://google.com/resolve', DoH\Nameserver
3939
$DohConfig = new DoH\DoHConfig($nameservers);
4040

4141
// Set default resolver for all AMPHP apps to DNS-over-HTTPS resolver
42-
Dns\resolver(new DoH\Rfc8484StubResolver($DohConfig));
42+
Dns\dnsResolver(new DoH\Rfc8484StubDohResolver($DohConfig));
4343
```
4444

4545
In the last example, [domain fronting](https://en.wikipedia.org/wiki/Domain_fronting), useful to bypass censorship in non-free countries: from the outside, it looks like the DoH client is connecting to `https://google.com`, but by sending a custom Host HTTP header to the server after the TLS handshake is finished, the server that actually replies is `https://dns.google.com` (this is only possible if both servers are behind a common CDN that allows domain fronting, like google's CDN).
4646
In normal conditions, it is recommended that you use mozilla+cloudflare's DoH endpoint (`https://mozilla.cloudflare-dns.com/dns-query`), for greater privacy.
4747

4848
Other parameters that can be passed to the DoHConfig constructor are:
4949
```php
50-
public function __construct(array $nameservers, \Amp\Artax\Client $artax = null, \Amp\Dns\Resolver $resolver = null, \Amp\Dns\ConfigLoader $configLoader = null, \Amp\Cache\Cache $cache = null);
50+
public function __construct(array $nameservers, \Amp\Artax\Client $artax = null, \Amp\Dns\dnsResolver $resolver = null, \Amp\Dns\ConfigLoader $configLoader = null, \Amp\Cache\Cache $cache = null);
5151
```
5252

5353
You can provide a custom HTTP client to use for resolution, or use a custom subresolver (the subresolver is used to make the first and only plaintext DNS request to obtain the address of the DoH nameserver), or use a [custom configuration](https://amphp.org/dns/#configuration) for the DoH client (and the subresolver, too, if the configuration is provided but the resolver isn't).
5454
The last parameter can be a custom async caching object.
5555

5656
### Address Resolution
5757

58-
To resolve addresses using `dns-over-https` first set the global DNS resolver as explained in the [configuration section](#configuration), or use an instance of `Rfc8484StubResolver` instead of `Rfc1035StubResolver`.
58+
To resolve addresses using `dns-over-https` first set the global DNS resolver as explained in the [configuration section](#configuration), or use an instance of `Rfc8484StubDohResolver` instead of `Rfc1035StubResolver`.
5959

6060
`Amp\Dns\resolve` provides hostname to IP address resolution. It returns an array of IPv4 and IPv6 addresses by default. The type of IP addresses returned can be restricted by passing a second argument with the respective type.
6161

@@ -76,7 +76,7 @@ $records = Amp\Dns\resolve("github.com", Amp\Dns\Record::A);
7676

7777
### Custom Queries
7878

79-
To resolve addresses using `dns-over-https` first set the global DNS resolver as explained in the [configuration section](#configuration), or use an instance of `Rfc8484StubResolver` instead of `Rfc1035StubResolver`.
79+
To resolve addresses using `dns-over-https` first set the global DNS resolver as explained in the [configuration section](#configuration), or use an instance of `Rfc8484StubDohResolver` instead of `Rfc1035StubResolver`.
8080

8181
`Amp\Dns\query` supports the various other DNS record types such as `MX`, `PTR`, or `TXT`. It automatically rewrites passed IP addresses for `PTR` lookups.
8282

@@ -92,15 +92,15 @@ $records = Amp\Dns\query("8.8.8.8", Amp\Dns\Record::PTR);
9292

9393
### Caching
9494

95-
The `Rfc8484StubResolver` caches responses by default in an `Amp\Cache\LocalCache`. You can set any other `Amp\Cache\Cache` implementation by creating a custom instance of `Rfc8484StubResolver` and setting that via `Amp\Dns\resolver()`, but it's usually unnecessary. If you have a lot of very short running scripts, you might want to consider using a local DNS resolver with a cache instead of setting a custom cache implementation, such as `dnsmasq`.
95+
The `Rfc8484StubDohResolver` caches responses by default in an `Amp\Cache\LocalCache`. You can set any other `Amp\Cache\Cache` implementation by creating a custom instance of `Rfc8484StubDohResolver` and setting that via `Amp\Dns\dnsResolver()`, but it's usually unnecessary. If you have a lot of very short running scripts, you might want to consider using a local DNS resolver with a cache instead of setting a custom cache implementation, such as `dnsmasq`.
9696

9797
### Reloading Configuration
9898

9999
The subresolver (which is the resolver set in the `DoHConfig`, `Rfc1035StubResolver` by default) will cache the configuration of `/etc/resolv.conf` / the Windows Registry and the read host files by default. If you wish to reload them, you can set a periodic timer that requests a background reload of the configuration.
100100

101101
```php
102102
Loop::repeat(60000, function () use ($resolver) {
103-
Amp\Dns\resolver()->reloadConfig();
103+
Amp\Dns\dnsResolver()->reloadConfig();
104104
});
105105
```
106106

‎examples/_bootstrap.php‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
<?php declare(strict_types=1);
22

3-
use Amp\Dns\Record;
3+
use Amp\Dns\DnsRecord;
44

55
require __DIR__ . "/../vendor/autoload.php";
66

7-
function pretty_print_records(string $queryName, array $records)
7+
/**
8+
* @param array<DnsRecord> $records
9+
*/
10+
function pretty_print_records(string $queryName, array $records): void
811
{
912
print "---------- " . $queryName . " " . str_repeat("-", 55 - strlen($queryName)) . " TTL --\r\n";
1013

1114
$format = "%-10s %-56s %-5d\r\n";
1215

1316
foreach ($records as $record) {
14-
print sprintf($format, Record::getName($record->getType()), $record->getValue(), $record->getTtl());
17+
print sprintf($format, DnsRecord::getName($record->getType()), $record->getValue(), $record->getTtl());
1518
}
1619
}
1720

18-
function pretty_print_error(string $queryName, \Throwable $error)
21+
function pretty_print_error(string $queryName, \Throwable $error): void
1922
{
2023
print "-- " . $queryName . " " . str_repeat("-", 70 - strlen($queryName)) . "\r\n";
2124
print get_class($error) . ": " . $error->getMessage() . "\r\n";

‎examples/benchmark.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
array_shift($domains);
1717

1818
$DohConfig = new DoH\DoHConfig([new DoH\Nameserver('https://mozilla.cloudflare-dns.com/dns-query')]);
19-
Dns\resolver(new DoH\Rfc8484StubResolver($DohConfig));
19+
Dns\dnsResolver(new DoH\Rfc8484StubDohResolver($DohConfig));
2020

2121
print "Starting sequential queries...\r\n\r\n";
2222

2323
$timings = [];
2424

2525
for ($i = 0; $i < 10; $i++) {
26-
$start = microtime(1);
26+
$start = microtime(true);
2727
$domain = $domains[random_int(0, count($domains) - 1)];
2828

2929
try {
@@ -32,7 +32,7 @@
3232
pretty_print_error($domain, $e);
3333
}
3434

35-
$time = round(microtime(1) - $start, 2);
35+
$time = round(microtime(true) - $start, 2);
3636
$timings[] = $time;
3737

3838
printf("%'-74s\r\n\r\n", " in " . $time . " ms");

‎examples/custom-config.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function loadConfig(): Dns\DnsConfig
1313
return new Dns\DnsConfig([
1414
"8.8.8.8:53",
1515
"[2001:4860:4860::8888]:53",
16-
], $hosts, 5, 3);
16+
], $hosts);
1717
}
1818
};
1919

@@ -28,7 +28,7 @@ public function loadConfig(): Dns\DnsConfig
2828
null,
2929
$customConfigLoader
3030
);
31-
Dns\resolver(new DoH\Rfc8484StubResolver($DohConfig));
31+
Dns\dnsResolver(new DoH\Rfc8484StubDohResolver($DohConfig));
3232

3333
$hostname = $argv[1] ?? "amphp.org";
3434

‎examples/ptr-record.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
require __DIR__ . "/_bootstrap.php";
44

55
use Amp\Dns;
6+
use Amp\Dns\DnsRecord;
67
use Amp\DoH;
78

89
// Set default resolver to DNS-over-HTTPS resolver
910
$DohConfig = new DoH\DoHConfig([new DoH\Nameserver('https://mozilla.cloudflare-dns.com/dns-query')]);
10-
Dns\resolver(new DoH\Rfc8484StubResolver($DohConfig));
11+
Dns\dnsResolver(new DoH\Rfc8484StubDohResolver($DohConfig));
1112

1213
$ip = $argv[1] ?? "8.8.8.8";
1314

1415
try {
15-
pretty_print_records($ip, Dns\query($ip, Dns\Record::PTR));
16+
pretty_print_records($ip, Dns\query($ip, DnsRecord::PTR));
1617
} catch (Dns\DnsException $e) {
1718
pretty_print_error($ip, $e);
1819
}

‎lib/DoHConfig.php‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use Amp\Cache\Cache;
66
use Amp\Cache\LocalCache;
7-
use Amp\Dns\ConfigException;
7+
use Amp\Dns\DnsConfigException;
88
use Amp\Dns\DnsConfigLoader;
9-
use Amp\Dns\Rfc1035StubResolver;
9+
use Amp\Dns\DnsResolver;
10+
use Amp\Dns\Rfc1035StubDnsResolver;
1011
use Amp\Dns\UnixDnsConfigLoader;
1112
use Amp\Dns\WindowsDnsConfigLoader;
1213
use Amp\Http\Client\DelegateHttpClient;
@@ -19,24 +20,24 @@ final class DoHConfig
1920
*/
2021
private readonly array $nameservers;
2122
private readonly DelegateHttpClient $httpClient;
22-
private readonly Rfc1035StubResolver $subResolver;
23+
private readonly DnsResolver $subResolver;
2324
private readonly DnsConfigLoader $configLoader;
2425
private readonly Cache $cache;
2526

2627
/**
2728
* @param non-empty-array<Nameserver> $nameservers
2829
*/
29-
public function __construct(array $nameservers, ?DelegateHttpClient $httpClient = null, ?Rfc1035StubResolver $resolver = null, ?DnsConfigLoader $configLoader = null, ?Cache $cache = null)
30+
public function __construct(array $nameservers, ?DelegateHttpClient $httpClient = null, ?DnsResolver $resolver = null, ?DnsConfigLoader $configLoader = null, ?Cache $cache = null)
3031
{
3132
/** @psalm-suppress TypeDoesNotContainType */
3233
if (\count($nameservers) < 1) {
33-
throw new ConfigException("At least one nameserver is required for a valid config");
34+
throw new DnsConfigException("At least one nameserver is required for a valid config");
3435
}
3536

3637
foreach ($nameservers as $nameserver) {
3738
/** @psalm-suppress DocblockContradiction */
3839
if (!($nameserver instanceof Nameserver)) {
39-
throw new ConfigException("Invalid nameserver: {$nameserver}");
40+
throw new DnsConfigException("Invalid nameserver: {$nameserver}");
4041
}
4142
}
4243

@@ -46,7 +47,7 @@ public function __construct(array $nameservers, ?DelegateHttpClient $httpClient
4647
$this->configLoader = $configLoader ?? (\stripos(PHP_OS, "win") === 0
4748
? new WindowsDnsConfigLoader()
4849
: new UnixDnsConfigLoader());
49-
$this->subResolver = $resolver ?? new Rfc1035StubResolver(null, $this->configLoader);
50+
$this->subResolver = $resolver ?? new Rfc1035StubDnsResolver(null, $this->configLoader);
5051
}
5152

5253
/**
@@ -80,7 +81,7 @@ public function getConfigLoader(): DnsConfigLoader
8081
{
8182
return $this->configLoader;
8283
}
83-
public function getSubResolver(): Rfc1035StubResolver
84+
public function getSubResolver(): DnsResolver
8485
{
8586
return $this->subResolver;
8687
}

‎lib/Nameserver.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Amp\DoH;
44

5-
use Amp\Dns\ConfigException;
5+
use Amp\Dns\DnsConfigException;
66

77
final class Nameserver
88
{
@@ -18,7 +18,7 @@ public function __construct(
1818
private readonly array $headers = []
1919
) {
2020
if (\parse_url($uri, PHP_URL_SCHEME) !== 'https') {
21-
throw new ConfigException('Did not provide a valid HTTPS url!');
21+
throw new DnsConfigException('Did not provide a valid HTTPS url!');
2222
}
2323
$this->host = \parse_url($uri, PHP_URL_HOST);
2424
}

0 commit comments

Comments
 (0)