*Update to new dns resolver framework

This commit is contained in:
Derrick Hammer 2022-08-21 21:48:46 -04:00
parent a8607150e8
commit 697819bc4e
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
8 changed files with 90 additions and 25 deletions

View File

@ -40,6 +40,7 @@
"dependencies": { "dependencies": {
"@lumeweb/kernel-dht-client": "https://github.com/LumeWeb/kernel-dht-client.git", "@lumeweb/kernel-dht-client": "https://github.com/LumeWeb/kernel-dht-client.git",
"@lumeweb/kernel-ipfs-client": "https://github.com/LumeWeb/kernel-ipfs-client.git", "@lumeweb/kernel-ipfs-client": "https://github.com/LumeWeb/kernel-ipfs-client.git",
"@lumeweb/libresolver": "https://github.com/LumeWeb/libresolver.git",
"@lumeweb/tld-enum": "https://github.com/LumeWeb/list-of-top-level-domains.git", "@lumeweb/tld-enum": "https://github.com/LumeWeb/list-of-top-level-domains.git",
"dexie": "^3.2.2", "dexie": "^3.2.2",
"ejs": "^3.1.8", "ejs": "^3.1.8",

View File

@ -10,6 +10,7 @@ import { getTld, isDomain, isIp, normalizeDomain } from "../util.js";
import tldEnum from "@lumeweb/tld-enum"; import tldEnum from "@lumeweb/tld-enum";
import { getAuthStatus } from "../main/vars.js"; import { getAuthStatus } from "../main/vars.js";
import { resolve } from "../dns.js"; import { resolve } from "../dns.js";
import { DNS_RECORD_TYPE } from "@lumeweb/libresolver";
export default abstract class BaseProvider { export default abstract class BaseProvider {
private engine: WebEngine; private engine: WebEngine;
@ -67,21 +68,32 @@ export default abstract class BaseProvider {
return cached; return cached;
} }
let result; let dnsResult;
try { for (const type of [
result = await resolve(hostname, {}); DNS_RECORD_TYPE.CONTENT,
} catch (e) { DNS_RECORD_TYPE.A,
debugger; DNS_RECORD_TYPE.CNAME,
} ]) {
if (!result) { let result = await resolve(hostname);
this.setData(details, "dns", false);
return false; if (result instanceof Error) {
continue;
} }
this.setData(details, "dns", result); if (0 < result.records.length) {
dnsResult = result.records.shift();
break;
}
}
return result; if (!dnsResult) {
dnsResult = false;
}
this.setData(details, "dns", dnsResult);
return dnsResult;
} }
protected getData(details: OnBeforeRequestDetailsType, key: string) { protected getData(details: OnBeforeRequestDetailsType, key: string) {

View File

@ -23,6 +23,7 @@ import {
contentModes, contentModes,
} from "../mimes.js"; } from "../mimes.js";
import { cacheDb } from "../databases.js"; import { cacheDb } from "../databases.js";
import { DNSRecord } from "@lumeweb/libresolver";
const INDEX_HTML_FILES = ["index.html", "index.htm", "index.shtml"]; const INDEX_HTML_FILES = ["index.html", "index.htm", "index.shtml"];
@ -93,12 +94,15 @@ export default class IpfsProvider extends BaseProvider {
async shouldHandleRequest( async shouldHandleRequest(
details: OnBeforeRequestDetailsType details: OnBeforeRequestDetailsType
): Promise<boolean> { ): Promise<boolean> {
let dns = await this.resolveDns(details); let dns: DNSRecord | boolean | string = await this.resolveDns(details);
if (dns) { if (!dns) {
dns = "/" + dns.replace("://", "/"); return false;
dns = dns.replace(/^\+/, "/");
} }
dns = (dns as DNSRecord).value;
dns = dns = "/" + dns.replace("://", "/");
dns = dns.replace(/^\+/, "/");
if (dns && path(dns)) { if (dns && path(dns)) {
this.setData(details, "hash", dns); this.setData(details, "hash", dns);
return true; return true;

View File

@ -1,14 +1,19 @@
import BaseProvider from "./baseProvider.js"; import BaseProvider from "./baseProvider.js";
import { OnBeforeRequestDetailsType, OnRequestDetailsType } from "../types.js"; import { OnBeforeRequestDetailsType, OnRequestDetailsType } from "../types.js";
import { isDomain, isIp } from "../util.js"; import { isDomain, isIp } from "../util.js";
import { DNSRecord } from "@lumeweb/libresolver";
export default class ServerProvider extends BaseProvider { export default class ServerProvider extends BaseProvider {
async shouldHandleRequest( async shouldHandleRequest(
details: OnBeforeRequestDetailsType details: OnBeforeRequestDetailsType
): Promise<boolean> { ): Promise<boolean> {
const dns = await this.resolveDns(details); let dns: DNSRecord | boolean = await this.resolveDns(details);
if (!dns) {
return false;
}
dns = dns as DNSRecord;
if (dns && (isDomain(dns) || isIp(dns))) { if (dns && (isDomain(dns.value) || isIp(dns.value))) {
return true; return true;
} }

View File

@ -9,14 +9,20 @@ import {
import { validSkylink } from "libskynet"; import { validSkylink } from "libskynet";
import { downloadSkylink, getRelayProxies } from "../util.js"; import { downloadSkylink, getRelayProxies } from "../util.js";
import browser from "@lumeweb/webextension-polyfill"; import browser from "@lumeweb/webextension-polyfill";
import { DNSRecord } from "@lumeweb/libresolver";
export default class SkynetProvider extends BaseProvider { export default class SkynetProvider extends BaseProvider {
async shouldHandleRequest( async shouldHandleRequest(
details: OnBeforeRequestDetailsType details: OnBeforeRequestDetailsType
): Promise<boolean> { ): Promise<boolean> {
const dns = await this.resolveDns(details); let dns: DNSRecord | boolean = await this.resolveDns(details);
if (!dns) {
return false;
}
if (dns && validSkylink(dns)) { dns = dns as DNSRecord;
if (dns && validSkylink(dns.value)) {
return true; return true;
} }

View File

@ -1,18 +1,33 @@
import NodeCache from "node-cache"; import NodeCache from "node-cache";
import { resolve as resolveDns } from "@lumeweb/kernel-dns-client"; import { resolve as resolveDns } from "@lumeweb/kernel-dns-client";
import { DNSResult, ResolverOptions } from "@lumeweb/libresolver";
import { blake2b, Err } from "libskynet/dist";
const cache = new NodeCache({ stdTTL: 60 }); const cache = new NodeCache({ stdTTL: 60 });
export async function resolve(domain: string, params: any, force = false) { export async function resolve(
if (cache.has(domain)) { domain: string,
cache.ttl(domain); options?: ResolverOptions,
return cache.get(domain); bypassCache = false
): Promise<DNSResult | Error> {
let cacheId = `${domain}:{${blake2b(
new TextEncoder().encode(JSON.stringify(options))
)}`;
if (cache.has(cacheId)) {
cache.ttl(cacheId);
return cache.get(cacheId) as DNSResult;
} }
let res = await resolveDns(domain, params, force); let res;
try {
res = await resolveDns(domain, options, bypassCache);
} catch (e: any) {
return e as Error;
}
if (res) { if (res) {
cache.set(domain, res); cache.set(cacheId, res);
} }
return res; return res;

View File

@ -36,6 +36,7 @@ import {
} from "./vars.js"; } from "./vars.js";
import browser from "@lumeweb/webextension-polyfill"; import browser from "@lumeweb/webextension-polyfill";
import setupContextMenus from "../contextMenu.js"; import setupContextMenus from "../contextMenu.js";
import { callModule } from "libkernel";
function logLargeObjects() { function logLargeObjects() {
let queriesLen = Object.keys(getQueries()).length; let queriesLen = Object.keys(getQueries()).length;
@ -213,7 +214,24 @@ async function boot() {
document.body.appendChild(getKernelIframe()); document.body.appendChild(getKernelIframe());
setupContextMenus(engine); setupContextMenus(engine);
await registerResolvers();
await dnsReady(); await dnsReady();
} }
async function registerResolvers() {
const resolvers = [
"AQBXtVkPDbZ5Qmjl8dzJ0siSYaFcS3XbDZHapxmZCLfwfg", // icann
"AQAI3TbarrXRxWtrb_5XO-gMYg-UsjVAChue5JEoqywbAw", // eip137
"AQD0s0wZNpZCVg_iO96E6Ff66WxGa2CZst_DCYR_DoQPxw", // solana
"AQDtYcJGbquAHA-idtZ-gPOlNBgEVeCZtZUtsyL_J5ZiUA", // algorand
"AQDkqoCzCR6s5MP_k6Ee9QWfEwaH5-7XleCKFL1CdxExxQ", // avax
"AQC7ALr-OtkFT7qZby2BdMMNbTRXHNMGlpV6r96b35Z79Q", // evmos
"AQAmQoZLu1DqIiZaRWRpomvMarQ8Uc3kdHJQBo0r-9uYtg", // handshake
];
for (const resolver of resolvers) {
await callModule(resolver, "register");
}
}
boot(); boot();

View File

@ -79,6 +79,10 @@
libkernel "^0.1.43" libkernel "^0.1.43"
libkmodule "^0.2.44" libkmodule "^0.2.44"
"@lumeweb/libresolver@https://github.com/LumeWeb/libresolver.git":
version "0.1.0"
resolved "https://github.com/LumeWeb/libresolver.git#1cdfd12ce9a0e49af59095d395159811ef1cfd59"
"@lumeweb/tld-enum@https://github.com/LumeWeb/list-of-top-level-domains.git": "@lumeweb/tld-enum@https://github.com/LumeWeb/list-of-top-level-domains.git":
version "2.0.7" version "2.0.7"
resolved "https://github.com/LumeWeb/list-of-top-level-domains.git#ae60882496bb6e12896fe25436370a3bf02df249" resolved "https://github.com/LumeWeb/list-of-top-level-domains.git#ae60882496bb6e12896fe25436370a3bf02df249"