2022-08-18 19:21:41 +00:00
|
|
|
import tldEnum from "@lumeweb/tld-enum";
|
|
|
|
import {
|
|
|
|
AbstractResolverModule,
|
|
|
|
DNS_RECORD_TYPE,
|
|
|
|
DNSResult,
|
|
|
|
isDomain,
|
|
|
|
isIp,
|
2022-08-19 00:11:22 +00:00
|
|
|
isPromise,
|
2022-08-18 19:21:41 +00:00
|
|
|
normalizeDomain,
|
|
|
|
resolverEmptyResponse,
|
|
|
|
ResolverOptions,
|
2022-08-19 16:49:09 +00:00
|
|
|
resolveSuccess,
|
2022-08-29 05:03:00 +00:00
|
|
|
ensureUniqueRecords,
|
|
|
|
DNSRecord,
|
|
|
|
getTld,
|
2022-08-31 02:25:44 +00:00
|
|
|
resolverError,
|
2022-08-29 05:03:00 +00:00
|
|
|
} from "@lumeweb/libresolver";
|
2023-02-20 16:21:52 +00:00
|
|
|
import {
|
|
|
|
createClient,
|
|
|
|
Response as HandshakeResponse,
|
|
|
|
} from "@lumeweb/kernel-handshake-client";
|
|
|
|
import { ResolverModule } from "@lumeweb/kernel-libresolver";
|
|
|
|
|
|
|
|
const client = createClient();
|
2022-08-18 19:21:41 +00:00
|
|
|
|
|
|
|
const HIP5_EXTENSIONS = ["eth", "_eth"];
|
|
|
|
|
2022-08-19 12:38:53 +00:00
|
|
|
interface HnsRecord {
|
|
|
|
type: string;
|
|
|
|
address: string;
|
|
|
|
txt: string[];
|
|
|
|
ns: string;
|
|
|
|
}
|
|
|
|
|
2022-08-18 19:21:41 +00:00
|
|
|
export default class Handshake extends AbstractResolverModule {
|
2022-08-19 00:11:22 +00:00
|
|
|
private async buildBlacklist(): Promise<Set<string>> {
|
|
|
|
const blacklist = new Set<string>();
|
2023-02-20 16:21:52 +00:00
|
|
|
let resolvers = this.resolver.resolvers as unknown as Set<ResolverModule>;
|
2022-08-19 13:49:02 +00:00
|
|
|
if (isPromise(resolvers as any)) {
|
|
|
|
resolvers = await resolvers;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const resolver of resolvers) {
|
2023-02-20 16:21:52 +00:00
|
|
|
let tlds: string[] | Promise<string[]> = resolver.getSupportedTlds();
|
2022-08-19 00:11:22 +00:00
|
|
|
if (isPromise(tlds as any)) {
|
|
|
|
tlds = await tlds;
|
|
|
|
}
|
2023-02-20 16:21:52 +00:00
|
|
|
(tlds as string[]).map((item: string) => blacklist.add(item));
|
2022-08-19 00:11:22 +00:00
|
|
|
}
|
2022-08-18 19:21:41 +00:00
|
|
|
|
2022-08-19 00:11:22 +00:00
|
|
|
return blacklist;
|
|
|
|
}
|
2022-08-18 19:21:41 +00:00
|
|
|
|
|
|
|
async resolve(
|
|
|
|
domain: string,
|
|
|
|
options: ResolverOptions,
|
|
|
|
bypassCache: boolean
|
|
|
|
): Promise<DNSResult> {
|
2022-08-20 16:09:09 +00:00
|
|
|
options.options = options.options || {};
|
2022-08-18 19:21:41 +00:00
|
|
|
const tld = getTld(domain);
|
|
|
|
|
2022-08-19 00:11:22 +00:00
|
|
|
const blacklist = await this.buildBlacklist();
|
|
|
|
|
|
|
|
if (blacklist.has(tld)) {
|
|
|
|
return resolverEmptyResponse();
|
|
|
|
}
|
2022-08-18 19:21:41 +00:00
|
|
|
|
|
|
|
if (isIp(domain)) {
|
|
|
|
return resolverEmptyResponse();
|
|
|
|
}
|
|
|
|
|
2022-08-19 16:15:31 +00:00
|
|
|
if (options?.options && "subquery" in options.options) {
|
2022-08-18 19:21:41 +00:00
|
|
|
return resolverEmptyResponse();
|
|
|
|
}
|
|
|
|
|
2023-02-20 16:21:52 +00:00
|
|
|
const chainRecords = await this.query(tld);
|
2022-08-31 02:25:44 +00:00
|
|
|
if (chainRecords.error) {
|
|
|
|
return resolverError(chainRecords.error);
|
|
|
|
}
|
|
|
|
|
2023-02-20 16:21:52 +00:00
|
|
|
if (!chainRecords.result?.records.length) {
|
2022-08-18 19:21:41 +00:00
|
|
|
return resolverEmptyResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
let records: DNSRecord[] = [];
|
|
|
|
|
2023-02-20 16:21:52 +00:00
|
|
|
for (const record of chainRecords.result?.records) {
|
2022-08-18 19:21:41 +00:00
|
|
|
switch (record.type) {
|
|
|
|
case "NS": {
|
2022-08-20 16:09:09 +00:00
|
|
|
await this.processNs(
|
|
|
|
domain,
|
|
|
|
record,
|
|
|
|
records,
|
2023-02-20 16:21:52 +00:00
|
|
|
chainRecords.result?.records,
|
2022-08-20 16:09:09 +00:00
|
|
|
options,
|
|
|
|
bypassCache
|
|
|
|
);
|
2022-08-18 19:21:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "GLUE4": {
|
|
|
|
await this.processGlue(domain, record, records, options, bypassCache);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "TXT": {
|
|
|
|
await this.processTxt(record, records, options);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "SYNTH6": {
|
|
|
|
if (
|
|
|
|
options.type === DNS_RECORD_TYPE.A &&
|
|
|
|
"ipv6" in options.options &&
|
|
|
|
options.options.ipv6
|
|
|
|
) {
|
|
|
|
records.push({
|
|
|
|
type: options.type,
|
|
|
|
value: record.address,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "SYNTH4": {
|
|
|
|
if (options.type === DNS_RECORD_TYPE.A) {
|
|
|
|
records.push({
|
|
|
|
type: options.type,
|
|
|
|
value: record.address,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 05:03:00 +00:00
|
|
|
records = ensureUniqueRecords(records);
|
2022-08-20 16:09:09 +00:00
|
|
|
|
2022-08-19 16:49:09 +00:00
|
|
|
if (0 < records.length) {
|
|
|
|
return resolveSuccess(records);
|
|
|
|
}
|
|
|
|
|
2022-08-18 19:21:41 +00:00
|
|
|
return resolverEmptyResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
private async processNs(
|
|
|
|
domain: string,
|
2022-08-19 12:38:53 +00:00
|
|
|
record: HnsRecord,
|
|
|
|
records: DNSRecord[],
|
2022-08-20 16:09:09 +00:00
|
|
|
hnsRecords: HnsRecord[],
|
2022-08-18 19:21:41 +00:00
|
|
|
options: ResolverOptions,
|
|
|
|
bypassCache: boolean
|
|
|
|
) {
|
|
|
|
if (
|
|
|
|
![DNS_RECORD_TYPE.A, DNS_RECORD_TYPE.CNAME, DNS_RECORD_TYPE.NS].includes(
|
|
|
|
options.type
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// @ts-ignore
|
2022-08-20 16:09:09 +00:00
|
|
|
const glue = hnsRecords.slice().find(
|
2022-08-18 19:21:41 +00:00
|
|
|
(item: object) =>
|
|
|
|
// @ts-ignore
|
|
|
|
["GLUE4", "GLUE6"].includes(item.type) && item.ns === record.ns
|
|
|
|
);
|
|
|
|
|
2022-08-20 16:09:09 +00:00
|
|
|
if (glue && options.type !== DNS_RECORD_TYPE.NS) {
|
|
|
|
return this.processGlue(domain, glue, records, options, bypassCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.type === DNS_RECORD_TYPE.NS) {
|
|
|
|
records.push({ type: options.type, value: record.ns });
|
|
|
|
return;
|
2022-08-18 19:21:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const foundDomain = normalizeDomain(record.ns);
|
|
|
|
|
2022-08-20 16:09:09 +00:00
|
|
|
let isIcann = false;
|
2022-08-18 19:21:41 +00:00
|
|
|
let isHip5 = false;
|
|
|
|
|
|
|
|
let hip5Parts = foundDomain.split(".");
|
|
|
|
|
|
|
|
if (
|
|
|
|
hip5Parts.length >= 2 &&
|
2022-08-20 16:09:09 +00:00
|
|
|
[...(options.options?.hip5 ?? []), ...HIP5_EXTENSIONS].includes(
|
2022-08-18 19:21:41 +00:00
|
|
|
hip5Parts[hip5Parts.length - 1]
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
isHip5 = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
(isDomain(foundDomain) || /[a-zA-Z0-9\-]+/.test(foundDomain)) &&
|
|
|
|
!isHip5
|
|
|
|
) {
|
|
|
|
if (foundDomain.includes(".")) {
|
|
|
|
const tld = foundDomain.split(".")[foundDomain.split(".").length - 1];
|
|
|
|
|
|
|
|
isIcann = tldEnum.list.includes(tld);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isIcann) {
|
|
|
|
const hnsNs = await this.resolver.resolve(foundDomain, options);
|
|
|
|
|
|
|
|
if (hnsNs.records.length) {
|
2022-08-23 18:34:44 +00:00
|
|
|
let icannRecords = await this.resolver.resolve(domain, {
|
2022-08-18 19:21:41 +00:00
|
|
|
...options,
|
|
|
|
options: {
|
|
|
|
subquery: true,
|
2022-08-23 18:24:43 +00:00
|
|
|
nameserver: hnsNs.records.pop()?.value,
|
2022-08-18 19:21:41 +00:00
|
|
|
},
|
|
|
|
});
|
2022-08-23 18:34:44 +00:00
|
|
|
if (icannRecords.records.length) {
|
|
|
|
records.push.apply(records, icannRecords.records);
|
|
|
|
}
|
2022-08-18 19:21:41 +00:00
|
|
|
}
|
2022-08-20 16:09:09 +00:00
|
|
|
|
|
|
|
return resolverEmptyResponse();
|
2022-08-18 19:21:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 18:34:44 +00:00
|
|
|
let icannRecords = await this.resolver.resolve(domain, {
|
2022-08-18 19:21:41 +00:00
|
|
|
...options,
|
|
|
|
options: { subquery: true, nameserver: foundDomain },
|
|
|
|
});
|
2022-08-23 18:34:44 +00:00
|
|
|
if (icannRecords.records.length) {
|
|
|
|
records.push.apply(records, icannRecords.records);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolverEmptyResponse();
|
2022-08-18 19:21:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let result = await this.resolver.resolve(record.ns, options, bypassCache);
|
|
|
|
|
|
|
|
if (!result.records.length) {
|
|
|
|
result.records.push({ type: DNS_RECORD_TYPE.NS, value: record.ns });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
records.push.apply(records, result.records);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async processGlue(
|
|
|
|
domain: string,
|
2022-08-19 12:38:53 +00:00
|
|
|
record: HnsRecord,
|
2022-08-18 19:21:41 +00:00
|
|
|
records: DNSRecord[],
|
|
|
|
options: ResolverOptions,
|
|
|
|
bypassCache: boolean
|
|
|
|
) {
|
2022-08-20 16:09:09 +00:00
|
|
|
if (![DNS_RECORD_TYPE.A, DNS_RECORD_TYPE.CNAME].includes(options.type)) {
|
2022-08-19 16:43:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-08-18 19:21:41 +00:00
|
|
|
if (isDomain(record.ns) && isIp(record.address)) {
|
|
|
|
let results = await this.resolver.resolve(
|
|
|
|
domain,
|
|
|
|
{
|
|
|
|
...options,
|
|
|
|
options: {
|
|
|
|
subquery: true,
|
|
|
|
nameserver: record.address,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
bypassCache
|
|
|
|
);
|
|
|
|
if (results.records.length) {
|
|
|
|
records.push.apply(records, results.records);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-20 16:21:52 +00:00
|
|
|
private async query(tld: string): Promise<HandshakeResponse> {
|
|
|
|
return client.query("getnameresource", [tld, true]);
|
2022-08-18 19:21:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async processTxt(
|
2022-08-19 12:38:53 +00:00
|
|
|
record: HnsRecord,
|
2022-08-18 19:21:41 +00:00
|
|
|
records: DNSRecord[],
|
|
|
|
options: ResolverOptions
|
2022-08-19 12:38:53 +00:00
|
|
|
) {
|
|
|
|
const content = record.txt.slice().pop() as string;
|
2022-08-18 19:21:41 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
[DNS_RECORD_TYPE.TEXT, DNS_RECORD_TYPE.CONTENT].includes(options.type)
|
|
|
|
) {
|
|
|
|
records.push({
|
|
|
|
type: DNS_RECORD_TYPE.TEXT,
|
|
|
|
value: content,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|