2022-07-20 06:06:55 +00:00
|
|
|
// @ts-ignore
|
2022-07-20 05:59:16 +00:00
|
|
|
import DhtNode from "@hyperswarm/dht-relay";
|
2022-07-20 06:06:55 +00:00
|
|
|
// @ts-ignore
|
2022-06-27 22:22:53 +00:00
|
|
|
import Stream from "@hyperswarm/dht-relay/ws";
|
2022-07-20 06:06:55 +00:00
|
|
|
// @ts-ignore
|
2022-06-27 22:22:53 +00:00
|
|
|
import { Buffer } from "buffer";
|
2022-07-20 06:06:55 +00:00
|
|
|
// @ts-ignore
|
2022-07-26 23:28:53 +00:00
|
|
|
// @ts-ignore
|
2022-06-27 22:22:53 +00:00
|
|
|
import { blake2b } from "libskynet";
|
2022-07-20 06:06:55 +00:00
|
|
|
// @ts-ignore
|
2022-07-20 05:59:16 +00:00
|
|
|
import { registryRead } from "libkmodule";
|
|
|
|
import { unpack } from "msgpackr";
|
2022-07-26 23:28:53 +00:00
|
|
|
import randomNumber from "random-number-csprng";
|
2022-07-20 06:06:55 +00:00
|
|
|
const REGISTRY_DHT_KEY = "lumeweb-dht-node";
|
|
|
|
export default class DHT {
|
|
|
|
_options;
|
2022-07-26 23:28:53 +00:00
|
|
|
_relays = new Map();
|
|
|
|
_activeRelays = new Map();
|
|
|
|
_maxConnections = 10;
|
|
|
|
_inited = false;
|
2022-07-20 06:06:55 +00:00
|
|
|
constructor(opts = {}) {
|
|
|
|
// @ts-ignore
|
2022-07-21 18:58:32 +00:00
|
|
|
opts.custodial = false;
|
2022-07-20 06:06:55 +00:00
|
|
|
this._options = opts;
|
2022-06-27 22:22:53 +00:00
|
|
|
}
|
2022-07-20 06:06:55 +00:00
|
|
|
ready() {
|
2022-07-26 23:28:53 +00:00
|
|
|
this._inited = true;
|
|
|
|
return this.fillConnections();
|
2022-06-27 22:22:53 +00:00
|
|
|
}
|
2022-07-20 06:06:55 +00:00
|
|
|
get relays() {
|
2022-07-26 23:28:53 +00:00
|
|
|
return [...this._relays.keys()];
|
2022-06-27 22:22:53 +00:00
|
|
|
}
|
2022-07-20 06:06:55 +00:00
|
|
|
async addRelay(pubkey) {
|
|
|
|
let entry = await registryRead(Uint8Array.from(Buffer.from(pubkey, "hex")), hashDataKey(REGISTRY_DHT_KEY));
|
|
|
|
if (entry[1] || !entry[0]?.exists) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let host;
|
|
|
|
try {
|
|
|
|
host = unpack(entry[0].entryData);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const [domain, port] = host.split(":");
|
|
|
|
if (isNaN(parseInt(port))) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-26 23:28:53 +00:00
|
|
|
this._relays[pubkey] = `wss://${domain}:${port}/`;
|
|
|
|
if (this._inited) {
|
|
|
|
await this.fillConnections();
|
|
|
|
}
|
2022-07-20 06:06:55 +00:00
|
|
|
return true;
|
2022-06-27 22:22:53 +00:00
|
|
|
}
|
2022-07-20 06:06:55 +00:00
|
|
|
removeRelay(pubkey) {
|
2022-07-26 23:28:53 +00:00
|
|
|
if (!this._relays.has(pubkey)) {
|
2022-07-20 06:06:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-07-26 23:28:53 +00:00
|
|
|
if (this._activeRelays.has(pubkey)) {
|
|
|
|
this._activeRelays.get(pubkey).destroy();
|
|
|
|
this._activeRelays.delete(pubkey);
|
|
|
|
}
|
|
|
|
this._relays.delete(pubkey);
|
2022-07-20 06:06:55 +00:00
|
|
|
return true;
|
2022-06-27 22:22:53 +00:00
|
|
|
}
|
2022-07-20 06:06:55 +00:00
|
|
|
clearRelays() {
|
2022-07-26 23:28:53 +00:00
|
|
|
[...this._relays.keys()].forEach(this.removeRelay);
|
2022-07-20 06:06:55 +00:00
|
|
|
}
|
|
|
|
async isServerAvailable(connection) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
const ws = new WebSocket(connection);
|
|
|
|
ws.addEventListener("open", () => {
|
|
|
|
ws.close();
|
|
|
|
resolve(true);
|
|
|
|
});
|
|
|
|
ws.addEventListener("error", () => {
|
|
|
|
resolve(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
async connect(pubkey, options = {}) {
|
2022-07-26 23:28:53 +00:00
|
|
|
if (this._activeRelays.size === 0) {
|
2022-07-20 06:06:55 +00:00
|
|
|
throw new Error("Failed to find an available relay");
|
|
|
|
}
|
2022-07-26 23:28:53 +00:00
|
|
|
const node = this._activeRelays.get([...this._activeRelays.keys()][await randomNumber(0, this._activeRelays.size - 1)]);
|
2022-07-20 06:06:55 +00:00
|
|
|
return node.connect(pubkey, options);
|
|
|
|
}
|
2022-07-26 23:28:53 +00:00
|
|
|
async fillConnections() {
|
|
|
|
let available = [...this._relays.keys()].filter(x => [...this._activeRelays.keys()].includes(x));
|
|
|
|
let relayPromises = [];
|
|
|
|
while (this._activeRelays.size <= Math.min(this._maxConnections, available.length + this._activeRelays.size)) {
|
|
|
|
const relayIndex = await randomNumber(0, available.length - 1);
|
|
|
|
const connection = available[relayIndex];
|
|
|
|
if (!this.isServerAvailable(connection)) {
|
|
|
|
continue;
|
2022-07-20 06:06:55 +00:00
|
|
|
}
|
2022-07-26 23:28:53 +00:00
|
|
|
const node = new DhtNode(new Stream(true, new WebSocket(this._activeRelays.get(connection))), this._options);
|
|
|
|
this._activeRelays.set(available[relayIndex], node);
|
|
|
|
relayPromises.push(node.ready());
|
2022-07-20 06:06:55 +00:00
|
|
|
}
|
2022-07-26 23:28:53 +00:00
|
|
|
return Promise.allSettled(relayPromises);
|
2022-06-27 22:22:53 +00:00
|
|
|
}
|
2022-07-20 06:06:55 +00:00
|
|
|
}
|
|
|
|
export function hashDataKey(dataKey) {
|
|
|
|
return blake2b(encodeUtf8String(dataKey));
|
2022-06-27 22:22:53 +00:00
|
|
|
}
|
|
|
|
function encodeUtf8String(str) {
|
2022-07-20 06:06:55 +00:00
|
|
|
const byteArray = stringToUint8ArrayUtf8(str);
|
|
|
|
const encoded = new Uint8Array(8 + byteArray.length);
|
|
|
|
encoded.set(encodeNumber(byteArray.length));
|
|
|
|
encoded.set(byteArray, 8);
|
|
|
|
return encoded;
|
2022-06-27 22:22:53 +00:00
|
|
|
}
|
|
|
|
function stringToUint8ArrayUtf8(str) {
|
2022-07-20 06:06:55 +00:00
|
|
|
return Uint8Array.from(Buffer.from(str, "utf-8"));
|
2022-06-27 22:22:53 +00:00
|
|
|
}
|
|
|
|
function encodeNumber(num) {
|
2022-07-20 06:06:55 +00:00
|
|
|
const encoded = new Uint8Array(8);
|
|
|
|
for (let index = 0; index < encoded.length; index++) {
|
|
|
|
encoded[index] = num & 0xff;
|
|
|
|
num = num >> 8;
|
|
|
|
}
|
|
|
|
return encoded;
|
2022-06-27 22:22:53 +00:00
|
|
|
}
|