2022-11-03 14:11:42 +11:00
|
|
|
import { bytesToUtf8 } from "@waku/byte-utils";
|
2022-07-19 15:43:35 +10:00
|
|
|
import debug from "debug";
|
|
|
|
|
import { Endpoint, query, toEndpoint } from "dns-query";
|
2022-01-13 11:33:26 +11:00
|
|
|
|
2022-11-03 14:11:42 +11:00
|
|
|
import { DnsClient } from "./dns.js";
|
2022-01-13 11:33:26 +11:00
|
|
|
|
2022-07-19 15:43:35 +10:00
|
|
|
const log = debug("waku:dns-over-https");
|
2022-01-13 11:33:26 +11:00
|
|
|
|
|
|
|
|
export class DnsOverHttps implements DnsClient {
|
2022-07-19 15:43:35 +10:00
|
|
|
/**
|
|
|
|
|
* Default endpoints to use for DNS queries.
|
|
|
|
|
* Taken from https://github.com/martinheidegger/dns-query as static data
|
|
|
|
|
* to avoid dynamic queries.
|
|
|
|
|
*
|
|
|
|
|
* To dynamically retrieve other endpoints, use https://github.com/martinheidegger/dns-query#well-known-endpoints
|
|
|
|
|
*/
|
|
|
|
|
static DefaultEndpoints: Endpoint[] = [
|
|
|
|
|
toEndpoint({
|
2023-01-24 23:42:21 +05:30
|
|
|
name: "AhaDNS",
|
2022-07-19 15:43:35 +10:00
|
|
|
protocol: "https:",
|
2023-01-24 23:42:21 +05:30
|
|
|
host: "doh.la.ahadns.net",
|
|
|
|
|
ipv4: "45.67.219.208",
|
2022-07-19 15:43:35 +10:00
|
|
|
}),
|
|
|
|
|
toEndpoint({
|
|
|
|
|
name: "cloudflare",
|
|
|
|
|
protocol: "https:",
|
|
|
|
|
host: "dns.cloudflare.com",
|
|
|
|
|
ipv4: "1.0.0.1",
|
|
|
|
|
}),
|
|
|
|
|
];
|
|
|
|
|
|
2022-01-13 11:33:26 +11:00
|
|
|
/**
|
|
|
|
|
* Create new Dns-Over-Http DNS client.
|
|
|
|
|
*
|
2022-07-19 15:43:35 +10:00
|
|
|
* @param endpoints The endpoints for Dns-Over-Https queries;
|
|
|
|
|
* Defaults to [[DnsOverHttps.DefaultEndpoints]].
|
|
|
|
|
* @param retries Retries if a given endpoint fails.
|
2022-01-13 11:33:26 +11:00
|
|
|
*
|
|
|
|
|
* @throws {code: string} If DNS query fails.
|
|
|
|
|
*/
|
|
|
|
|
public constructor(
|
2022-07-19 15:43:35 +10:00
|
|
|
private endpoints: Endpoint[] = DnsOverHttps.DefaultEndpoints,
|
|
|
|
|
private retries: number = 3
|
2022-01-13 11:33:26 +11:00
|
|
|
) {}
|
|
|
|
|
|
2022-03-01 16:51:21 +11:00
|
|
|
/**
|
|
|
|
|
* Resolves a TXT record
|
|
|
|
|
*
|
|
|
|
|
* @param domain The domain name
|
|
|
|
|
*
|
2022-07-19 15:43:35 +10:00
|
|
|
* @throws if the query fails
|
2022-03-01 16:51:21 +11:00
|
|
|
*/
|
2022-01-13 11:33:26 +11:00
|
|
|
async resolveTXT(domain: string): Promise<string[]> {
|
2022-07-19 15:43:35 +10:00
|
|
|
let answers;
|
|
|
|
|
try {
|
|
|
|
|
const res = await query(
|
|
|
|
|
{
|
|
|
|
|
question: { type: "TXT", name: domain },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
endpoints: this.endpoints,
|
|
|
|
|
retries: this.retries,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
answers = res.answers;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
log("query failed: ", error);
|
|
|
|
|
throw new Error("DNS query failed");
|
|
|
|
|
}
|
2022-01-13 11:33:26 +11:00
|
|
|
|
2022-07-19 15:43:35 +10:00
|
|
|
if (!answers) throw new Error(`Could not resolve ${domain}`);
|
2022-01-13 11:33:26 +11:00
|
|
|
|
2022-07-19 15:43:35 +10:00
|
|
|
const data = answers.map((a) => a.data) as
|
|
|
|
|
| Array<string | Uint8Array>
|
|
|
|
|
| Array<Array<string | Uint8Array>>;
|
2022-01-13 11:33:26 +11:00
|
|
|
|
|
|
|
|
const result: string[] = [];
|
|
|
|
|
|
|
|
|
|
data.forEach((d) => {
|
2022-02-04 14:12:00 +11:00
|
|
|
if (typeof d === "string") {
|
2022-01-13 11:33:26 +11:00
|
|
|
result.push(d);
|
|
|
|
|
} else if (Array.isArray(d)) {
|
|
|
|
|
d.forEach((sd) => {
|
2022-02-04 14:12:00 +11:00
|
|
|
if (typeof sd === "string") {
|
2022-01-13 11:33:26 +11:00
|
|
|
result.push(sd);
|
|
|
|
|
} else {
|
2022-02-16 14:08:48 +11:00
|
|
|
result.push(bytesToUtf8(sd));
|
2022-01-13 11:33:26 +11:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2022-02-16 14:08:48 +11:00
|
|
|
result.push(bytesToUtf8(d));
|
2022-01-13 11:33:26 +11:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|