Skip to content

Commit 1dd3210

Browse files
committed
fix(dns-discovery): Use DOH list from dns-query
To make the library more robust as not all DOH allow CORS. Previous default DOH got CORS disabled.
1 parent e385652 commit 1dd3210

9 files changed

+70
-46
lines changed

package-lock.json

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/dns-discovery/karma.conf.cjs

+15-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ const webpack = require("webpack");
44
module.exports = function (config) {
55
config.set({
66
frameworks: ["webpack", "mocha"],
7-
files: ["src/**/*.ts"],
7+
files: ["src/**/!(node).spec.ts"],
88
preprocessors: {
9-
"src/**/*.ts": ["webpack"],
9+
"src/**/!(node).spec.ts": ["webpack"],
1010
},
1111
envPreprocessor: ["CI"],
1212
reporters: ["progress"],
@@ -20,7 +20,19 @@ module.exports = function (config) {
2020
webpack: {
2121
mode: "development",
2222
module: {
23-
rules: [{ test: /\.([cm]?ts|tsx)$/, loader: "ts-loader" }],
23+
rules: [
24+
{
25+
test: /\.([cm]?ts|tsx)$/,
26+
use: [
27+
{
28+
loader: "ts-loader",
29+
options: {
30+
configFile: "tsconfig.karma.json",
31+
},
32+
},
33+
],
34+
},
35+
],
2436
},
2537
plugins: [
2638
new webpack.DefinePlugin({

packages/dns-discovery/package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
"prepublish": "npm run build",
5454
"reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build",
5555
"test": "run-s test:*",
56-
"test:node": "TS_NODE_PROJECT=./tsconfig.dev.json mocha"
56+
"test:node": "TS_NODE_PROJECT=./tsconfig.dev.json mocha",
57+
"test:browser": "karma start karma.conf.cjs"
5758
},
5859
"engines": {
5960
"node": ">=16"
@@ -90,6 +91,10 @@
9091
"eslint-plugin-functional": "^5.0.4",
9192
"eslint-plugin-import": "^2.27.5",
9293
"eslint-plugin-prettier": "^4.2.1",
94+
"karma": "^6.4.1",
95+
"karma-chrome-launcher": "^3.1.1",
96+
"karma-mocha": "^2.0.1",
97+
"karma-webpack": "^5.0.0",
9398
"mocha": "^10.2.0",
9499
"npm-run-all": "^4.1.5",
95100
"prettier": "^2.8.4",

packages/dns-discovery/src/dns.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ describe("DNS Node Discovery [live data]", function () {
273273
it(`should retrieve ${maxQuantity} multiaddrs for test.waku.nodes.status.im`, async function () {
274274
this.timeout(10000);
275275
// Google's dns server address. Needs to be set explicitly to run in CI
276-
const dnsNodeDiscovery = DnsNodeDiscovery.dnsOverHttp();
276+
const dnsNodeDiscovery = await DnsNodeDiscovery.dnsOverHttp();
277277
const peers = await dnsNodeDiscovery.getPeers([enrTree.TEST], {
278278
relay: maxQuantity,
279279
store: maxQuantity,
@@ -296,7 +296,7 @@ describe("DNS Node Discovery [live data]", function () {
296296
it(`should retrieve ${maxQuantity} multiaddrs for prod.waku.nodes.status.im`, async function () {
297297
this.timeout(10000);
298298
// Google's dns server address. Needs to be set explicitly to run in CI
299-
const dnsNodeDiscovery = DnsNodeDiscovery.dnsOverHttp();
299+
const dnsNodeDiscovery = await DnsNodeDiscovery.dnsOverHttp();
300300
const peers = await dnsNodeDiscovery.getPeers([enrTree.PROD], {
301301
relay: maxQuantity,
302302
store: maxQuantity,

packages/dns-discovery/src/dns.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ export class DnsNodeDiscovery {
3333
private readonly _DNSTreeCache: { [key: string]: string };
3434
private readonly _errorTolerance: number = 10;
3535

36-
public static dnsOverHttp(dnsClient?: DnsClient): DnsNodeDiscovery {
36+
public static async dnsOverHttp(
37+
dnsClient?: DnsClient
38+
): Promise<DnsNodeDiscovery> {
3739
if (!dnsClient) {
38-
dnsClient = new DnsOverHttps();
40+
dnsClient = await DnsOverHttps.create();
3941
}
4042
return new DnsNodeDiscovery(dnsClient);
4143
}

packages/dns-discovery/src/dns_over_https.ts

+13-26
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,32 @@
11
import { bytesToUtf8 } from "@waku/utils/bytes";
22
import debug from "debug";
3-
import { Endpoint, query, toEndpoint } from "dns-query";
3+
import { Endpoint, query, wellknown } from "dns-query";
44

55
import { DnsClient } from "./dns.js";
66

77
const log = debug("waku:dns-over-https");
88

99
export class DnsOverHttps implements DnsClient {
10-
/**
11-
* Default endpoints to use for DNS queries.
12-
* Taken from https://github.com/martinheidegger/dns-query as static data
13-
* to avoid dynamic queries.
14-
*
15-
* To dynamically retrieve other endpoints, use https://github.com/martinheidegger/dns-query#well-known-endpoints
16-
*/
17-
static DefaultEndpoints: Endpoint[] = [
18-
toEndpoint({
19-
name: "AhaDNS",
20-
protocol: "https:",
21-
host: "doh.la.ahadns.net",
22-
ipv4: "45.67.219.208",
23-
}),
24-
toEndpoint({
25-
name: "cloudflare",
26-
protocol: "https:",
27-
host: "dns.cloudflare.com",
28-
ipv4: "1.0.0.1",
29-
}),
30-
];
31-
3210
/**
3311
* Create new Dns-Over-Http DNS client.
3412
*
3513
* @param endpoints The endpoints for Dns-Over-Https queries;
36-
* Defaults to [[DnsOverHttps.DefaultEndpoints]].
14+
* Defaults to using dns-query's API..
3715
* @param retries Retries if a given endpoint fails.
3816
*
3917
* @throws {code: string} If DNS query fails.
4018
*/
41-
public constructor(
42-
private endpoints: Endpoint[] = DnsOverHttps.DefaultEndpoints,
19+
public static async create(
20+
endpoints?: Endpoint[],
21+
retries?: number
22+
): Promise<DnsOverHttps> {
23+
const _endpoints = endpoints ?? (await wellknown.endpoints("doh"));
24+
25+
return new DnsOverHttps(_endpoints, retries);
26+
}
27+
28+
private constructor(
29+
private endpoints: Endpoint[],
4330
private retries: number = 3
4431
) {}
4532

packages/dns-discovery/src/index.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class PeerDiscoveryDns
6060
extends EventEmitter<PeerDiscoveryEvents>
6161
implements PeerDiscovery
6262
{
63-
private readonly nextPeer: () => AsyncGenerator<IEnr>;
63+
private nextPeer: (() => AsyncGenerator<IEnr>) | undefined;
6464
private _started: boolean;
6565
private _components: DnsDiscoveryComponents;
6666
private _options: Options;
@@ -71,17 +71,8 @@ export class PeerDiscoveryDns
7171
this._components = components;
7272
this._options = options;
7373

74-
const { enrUrl, wantedNodeCapabilityCount } = options;
75-
74+
const { enrUrl } = options;
7675
log("Use following EIP-1459 ENR Tree URL: ", enrUrl);
77-
78-
const dns = DnsNodeDiscovery.dnsOverHttp();
79-
80-
this.nextPeer = dns.getNextPeer.bind(
81-
dns,
82-
[enrUrl],
83-
wantedNodeCapabilityCount
84-
);
8576
}
8677

8778
/**
@@ -91,6 +82,18 @@ export class PeerDiscoveryDns
9182
log("Starting peer discovery via dns");
9283

9384
this._started = true;
85+
86+
if (this.nextPeer === undefined) {
87+
const { enrUrl, wantedNodeCapabilityCount } = this._options;
88+
const dns = await DnsNodeDiscovery.dnsOverHttp();
89+
90+
this.nextPeer = dns.getNextPeer.bind(
91+
dns,
92+
[enrUrl],
93+
wantedNodeCapabilityCount
94+
);
95+
}
96+
9497
for await (const peer of this.nextPeer()) {
9598
if (!this._started) return;
9699

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig",
3+
"compilerOptions": {
4+
"module": "esnext"
5+
},
6+
"exclude": []
7+
}

packages/tests/tests/dns-peer-discovery.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe("DNS Node Discovery [live data]", function () {
9090

9191
this.timeout(10000);
9292
// Google's dns server address. Needs to be set explicitly to run in CI
93-
const dnsNodeDiscovery = DnsNodeDiscovery.dnsOverHttp();
93+
const dnsNodeDiscovery = await DnsNodeDiscovery.dnsOverHttp();
9494

9595
const peers = await dnsNodeDiscovery.getPeers([enrTree["PROD"]], {
9696
relay: maxQuantity,

0 commit comments

Comments
 (0)