*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": {
"@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",

View File

@ -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) {

View File

@ -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<boolean> {
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;

View File

@ -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<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;
}

View File

@ -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<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;
}

View File

@ -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<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) {
cache.set(domain, res);
cache.set(cacheId, res);
}
return res;

View File

@ -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();

View File

@ -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"