From 697819bc4e34c89d6f7bd73176970b707a3eecb0 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 21 Aug 2022 21:48:46 -0400 Subject: [PATCH] *Update to new dns resolver framework --- package.json | 1 + src/contentProviders/baseProvider.ts | 34 +++++++++++++++++--------- src/contentProviders/ipfsProvider.ts | 12 ++++++--- src/contentProviders/serverProvider.ts | 9 +++++-- src/contentProviders/skynetProvider.ts | 10 ++++++-- src/dns.ts | 27 +++++++++++++++----- src/main/background.ts | 18 ++++++++++++++ yarn.lock | 4 +++ 8 files changed, 90 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index 7140076..aac49cb 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "dependencies": { "@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/libresolver": "https://github.com/LumeWeb/libresolver.git", "@lumeweb/tld-enum": "https://github.com/LumeWeb/list-of-top-level-domains.git", "dexie": "^3.2.2", "ejs": "^3.1.8", diff --git a/src/contentProviders/baseProvider.ts b/src/contentProviders/baseProvider.ts index 04325a1..0734ba9 100644 --- a/src/contentProviders/baseProvider.ts +++ b/src/contentProviders/baseProvider.ts @@ -10,6 +10,7 @@ import { getTld, isDomain, isIp, normalizeDomain } from "../util.js"; import tldEnum from "@lumeweb/tld-enum"; import { getAuthStatus } from "../main/vars.js"; import { resolve } from "../dns.js"; +import { DNS_RECORD_TYPE } from "@lumeweb/libresolver"; export default abstract class BaseProvider { private engine: WebEngine; @@ -67,21 +68,32 @@ export default abstract class BaseProvider { return cached; } - let result; + let dnsResult; - try { - result = await resolve(hostname, {}); - } catch (e) { - debugger; - } - if (!result) { - this.setData(details, "dns", false); - return false; + for (const type of [ + DNS_RECORD_TYPE.CONTENT, + DNS_RECORD_TYPE.A, + DNS_RECORD_TYPE.CNAME, + ]) { + let result = await resolve(hostname); + + if (result instanceof Error) { + continue; + } + + if (0 < result.records.length) { + dnsResult = result.records.shift(); + break; + } } - this.setData(details, "dns", result); + if (!dnsResult) { + dnsResult = false; + } - return result; + this.setData(details, "dns", dnsResult); + + return dnsResult; } protected getData(details: OnBeforeRequestDetailsType, key: string) { diff --git a/src/contentProviders/ipfsProvider.ts b/src/contentProviders/ipfsProvider.ts index 42797b5..afdb5a0 100644 --- a/src/contentProviders/ipfsProvider.ts +++ b/src/contentProviders/ipfsProvider.ts @@ -23,6 +23,7 @@ import { contentModes, } from "../mimes.js"; import { cacheDb } from "../databases.js"; +import { DNSRecord } from "@lumeweb/libresolver"; const INDEX_HTML_FILES = ["index.html", "index.htm", "index.shtml"]; @@ -93,12 +94,15 @@ export default class IpfsProvider extends BaseProvider { async shouldHandleRequest( details: OnBeforeRequestDetailsType ): Promise { - let dns = await this.resolveDns(details); - if (dns) { - dns = "/" + dns.replace("://", "/"); - dns = dns.replace(/^\+/, "/"); + let dns: DNSRecord | boolean | string = await this.resolveDns(details); + if (!dns) { + return false; } + dns = (dns as DNSRecord).value; + dns = dns = "/" + dns.replace("://", "/"); + dns = dns.replace(/^\+/, "/"); + if (dns && path(dns)) { this.setData(details, "hash", dns); return true; diff --git a/src/contentProviders/serverProvider.ts b/src/contentProviders/serverProvider.ts index 20b22e9..1d68f1e 100644 --- a/src/contentProviders/serverProvider.ts +++ b/src/contentProviders/serverProvider.ts @@ -1,14 +1,19 @@ import BaseProvider from "./baseProvider.js"; import { OnBeforeRequestDetailsType, OnRequestDetailsType } from "../types.js"; import { isDomain, isIp } from "../util.js"; +import { DNSRecord } from "@lumeweb/libresolver"; export default class ServerProvider extends BaseProvider { async shouldHandleRequest( details: OnBeforeRequestDetailsType ): Promise { - 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; } diff --git a/src/contentProviders/skynetProvider.ts b/src/contentProviders/skynetProvider.ts index ed82b85..ab5081e 100644 --- a/src/contentProviders/skynetProvider.ts +++ b/src/contentProviders/skynetProvider.ts @@ -9,14 +9,20 @@ import { import { validSkylink } from "libskynet"; import { downloadSkylink, getRelayProxies } from "../util.js"; import browser from "@lumeweb/webextension-polyfill"; +import { DNSRecord } from "@lumeweb/libresolver"; export default class SkynetProvider extends BaseProvider { async shouldHandleRequest( details: OnBeforeRequestDetailsType ): Promise { - 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; } diff --git a/src/dns.ts b/src/dns.ts index e8efe7f..f88edb1 100644 --- a/src/dns.ts +++ b/src/dns.ts @@ -1,18 +1,33 @@ import NodeCache from "node-cache"; 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 }); -export async function resolve(domain: string, params: any, force = false) { - if (cache.has(domain)) { - cache.ttl(domain); - return cache.get(domain); +export async function resolve( + domain: string, + options?: ResolverOptions, + bypassCache = false +): Promise { + 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) { - cache.set(domain, res); + cache.set(cacheId, res); } return res; diff --git a/src/main/background.ts b/src/main/background.ts index 133e423..98681b7 100644 --- a/src/main/background.ts +++ b/src/main/background.ts @@ -36,6 +36,7 @@ import { } from "./vars.js"; import browser from "@lumeweb/webextension-polyfill"; import setupContextMenus from "../contextMenu.js"; +import { callModule } from "libkernel"; function logLargeObjects() { let queriesLen = Object.keys(getQueries()).length; @@ -213,7 +214,24 @@ async function boot() { document.body.appendChild(getKernelIframe()); setupContextMenus(engine); + await registerResolvers(); 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(); diff --git a/yarn.lock b/yarn.lock index 4ceb611..983a21a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -79,6 +79,10 @@ libkernel "^0.1.43" 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": version "2.0.7" resolved "https://github.com/LumeWeb/list-of-top-level-domains.git#ae60882496bb6e12896fe25436370a3bf02df249"