diff --git a/package.json b/package.json index 4829808..d351a6a 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "eslint": "eslint src --fix", "lint": "npm run deps && npm run prettier && npm run eslint", "compile": "node build.js", - "build": "npm run compile && rimraf node_modules/@lumeweb/kernel-dns-client/node_modules && rimraf node_modules/@lumeweb/kernel-ipfs-client/node_modules && cpy \"assets/*\" dist" + "build": "rimraf node_modules/@lumeweb/kernel-dns-client/node_modules node_modules/@lumeweb/kernel-dht-client/node_modules node_modules/@lumeweb/kernel-ipfs-client/node_modules && npm run compile && cpy \"assets/*\" dist" }, "author": "David Vorick", "license": "MIT", @@ -38,6 +38,7 @@ "webextension-polyfill": "^0.9.0" }, "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/tld-enum": "https://github.com/LumeWeb/list-of-top-level-domains.git", "ejs": "^3.1.8", diff --git a/src/contentProviders/ipfsProvider.ts b/src/contentProviders/ipfsProvider.ts index 4ac2f8c..3ab5a66 100644 --- a/src/contentProviders/ipfsProvider.ts +++ b/src/contentProviders/ipfsProvider.ts @@ -7,7 +7,7 @@ import { OnRequestDetailsType, StreamFilter, } from "../types.js"; -import { requestProxies } from "../util.js"; +import { getRelayProxies } from "../util.js"; import browser from "@lumeweb/webextension-polyfill"; import { ipfsPath, ipnsPath, path } from "is-ipfs"; import { @@ -105,7 +105,7 @@ export default class IpfsProvider extends BaseProvider { } async handleProxy(details: OnRequestDetailsType): Promise { - return requestProxies; + return getRelayProxies(); } async handleReqHeaders( diff --git a/src/contentProviders/skynetProvider.ts b/src/contentProviders/skynetProvider.ts index f776b1e..ed82b85 100644 --- a/src/contentProviders/skynetProvider.ts +++ b/src/contentProviders/skynetProvider.ts @@ -7,7 +7,7 @@ import { OnRequestDetailsType, } from "../types.js"; import { validSkylink } from "libskynet"; -import { downloadSkylink, requestProxies } from "../util.js"; +import { downloadSkylink, getRelayProxies } from "../util.js"; import browser from "@lumeweb/webextension-polyfill"; export default class SkynetProvider extends BaseProvider { @@ -24,7 +24,7 @@ export default class SkynetProvider extends BaseProvider { } async handleProxy(details: OnRequestDetailsType): Promise { - return requestProxies; + return getRelayProxies(); } async handleRequest( diff --git a/src/util.ts b/src/util.ts index 564052e..c8ce0c8 100644 --- a/src/util.ts +++ b/src/util.ts @@ -9,6 +9,9 @@ import { validSkylink, verifyDownloadResponse, } from "libskynet"; +import { DHT } from "@lumeweb/kernel-dht-client"; + +const relayDht = new DHT(); export function isIp(ip: string) { return /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test( @@ -26,6 +29,17 @@ export function normalizeDomain(domain: string): string { return domain.replace(/^\.+|\.+$/g, "").replace(/^\/+|\/+$/g, ""); } +export async function getRelayProxies() { + let relays: string[] = await relayDht.getRelayServers(); + let proxies = [{ type: "http", host: "localhost", port: 25252 }]; + + for (const relay of relays) { + proxies.push({ type: "http", host: new URL(relay).hostname, port: 25252 }); + } + + return proxies; +} + export const requestProxies = [ { type: "http", host: "localhost", port: 25252 }, { type: "http", host: "web3portal.com", port: 80 }, diff --git a/yarn.lock b/yarn.lock index bd28689..ec9331b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -52,9 +52,19 @@ resolved "https://registry.yarnpkg.com/@humanwhocodes/object-schema/-/object-schema-1.2.1.tgz#b520529ec21d8e5945a1851dfd1c32e94e39ff45" integrity sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA== +"@lumeweb/kernel-dht-client@https://github.com/LumeWeb/kernel-dht-client.git": + version "0.1.0" + resolved "https://github.com/LumeWeb/kernel-dht-client.git#4799cf4a989afd2ef63a38abd5fbff250576885b" + dependencies: + buffer "^6.0.3" + events "^3.3.0" + libkernel "^0.1.43" + libkmodule "^0.2.44" + libskynet "^0.0.62" + "@lumeweb/kernel-dns-client@https://github.com/LumeWeb/kernel-dns-client.git": version "0.1.0" - resolved "https://github.com/LumeWeb/kernel-dns-client.git#ac9cc4cf0dc58a5047a7b0c3bcc70705f51ae65f" + resolved "https://github.com/LumeWeb/kernel-dns-client.git#1ba24bee76ab7f746252a21fc48d34044924feb8" dependencies: buffer "^6.0.3" libkernel "^0.1.43"