*Add resolveDns shared provider method to parse a domain, check icann, sanitize, check if we have already queried for the current request id, and cache the result

*Add setData and getData helper methods
This commit is contained in:
Derrick Hammer 2022-07-31 00:54:42 -04:00
parent 4b1717c021
commit e917c0e7b4
1 changed files with 50 additions and 0 deletions

View File

@ -5,6 +5,9 @@ import {
OnRequestDetailsType,
} from "../types";
import WebEngine from "../webEngine.js";
import { getTld, isDomain, isIp, normalizeDomain } from "../util.js";
import tldEnum from "@lumeweb/tld-enum";
import { resolve } from "@lumeweb/kernel-dns-client";
export default abstract class BaseProvider {
private engine: WebEngine;
@ -33,4 +36,51 @@ export default abstract class BaseProvider {
): Promise<BlockingResponse | boolean> {
return false;
}
protected async resolveDns(details: OnBeforeRequestDetailsType) {
const originalUrl = new URL(details.url);
const hostname = normalizeDomain(originalUrl.hostname);
if (tldEnum.list.includes(getTld(hostname))) {
return false;
}
if (isIp(hostname) && !isDomain(hostname)) {
return false;
}
const cached = this.getData(details, "dns");
if (cached !== undefined) {
return cached;
}
let result;
try {
result = await resolve(hostname, {});
} catch (e) {
debugger;
}
if (!result) {
this.setData(details, "dns", false);
return false;
}
this.setData(details, "dns", result);
return result;
}
protected getData(details: OnBeforeRequestDetailsType, key: string) {
return this.engine.getRequestData(details.requestId, key);
}
protected setData(
details: OnBeforeRequestDetailsType,
key: string,
value: any
) {
return this.engine.setRequestData(details.requestId, key, value);
}
}