2022-06-27 22:21:31 +00:00
|
|
|
// @ts-ignore
|
2022-07-20 05:55:44 +00:00
|
|
|
import DhtNode from "@hyperswarm/dht-relay";
|
2022-06-27 22:21:31 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import Stream from "@hyperswarm/dht-relay/ws";
|
|
|
|
// @ts-ignore
|
|
|
|
import createRoundRobin from "@derhuerst/round-robin-scheduler";
|
2022-07-20 05:55:44 +00:00
|
|
|
// @ts-ignore
|
|
|
|
// @ts-ignore
|
2022-08-14 00:02:51 +00:00
|
|
|
import { blake2b, errTuple, hexToBuf } from "libskynet";
|
2022-07-20 05:55:44 +00:00
|
|
|
// @ts-ignore
|
2022-07-27 01:39:32 +00:00
|
|
|
import { registryRead } from "libkmodule";
|
2022-07-20 05:55:44 +00:00
|
|
|
|
2022-07-27 01:39:32 +00:00
|
|
|
import { unpack } from "msgpackr";
|
2022-07-26 23:27:59 +00:00
|
|
|
import randomNumber from "random-number-csprng";
|
2022-06-27 22:21:31 +00:00
|
|
|
|
2022-07-20 05:55:44 +00:00
|
|
|
const REGISTRY_DHT_KEY = "lumeweb-dht-node";
|
2022-06-27 22:21:31 +00:00
|
|
|
|
|
|
|
export default class DHT {
|
2022-07-27 01:39:32 +00:00
|
|
|
private _options: any;
|
|
|
|
private _relays: Map<string, string> = new Map();
|
|
|
|
private _activeRelays: Map<string, typeof DhtNode> = new Map();
|
|
|
|
private _maxConnections = 10;
|
|
|
|
private _inited = false;
|
|
|
|
|
|
|
|
constructor(opts = {}) {
|
|
|
|
// @ts-ignore
|
|
|
|
opts.custodial = false;
|
|
|
|
this._options = opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
ready(): Promise<void> {
|
|
|
|
if (this._inited) {
|
|
|
|
return Promise.resolve();
|
2022-07-20 05:55:44 +00:00
|
|
|
}
|
2022-06-27 22:21:31 +00:00
|
|
|
|
2022-07-27 01:39:32 +00:00
|
|
|
this._inited = true;
|
|
|
|
return this.fillConnections();
|
|
|
|
}
|
2022-07-27 01:38:05 +00:00
|
|
|
|
2022-07-27 01:39:32 +00:00
|
|
|
get relays(): string[] {
|
|
|
|
return [...this._relays.keys()];
|
|
|
|
}
|
2022-06-27 22:21:31 +00:00
|
|
|
|
2022-07-27 01:39:32 +00:00
|
|
|
public async addRelay(pubkey: string): Promise<boolean> {
|
|
|
|
let entry: errTuple = await registryRead(
|
2022-08-14 00:02:51 +00:00
|
|
|
hexToBuf(pubkey).shift() as Uint8Array,
|
2022-07-27 01:39:32 +00:00
|
|
|
hashDataKey(REGISTRY_DHT_KEY)
|
|
|
|
);
|
2022-06-27 22:21:31 +00:00
|
|
|
|
2022-07-27 01:39:32 +00:00
|
|
|
if (entry[1] || !entry[0]?.exists) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-20 05:55:44 +00:00
|
|
|
|
2022-07-27 01:39:32 +00:00
|
|
|
let host;
|
2022-06-27 22:21:31 +00:00
|
|
|
|
2022-07-27 01:39:32 +00:00
|
|
|
try {
|
|
|
|
host = unpack(entry[0].entryData);
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-20 05:55:44 +00:00
|
|
|
|
2022-07-27 01:39:32 +00:00
|
|
|
const [domain, port] = host.split(":");
|
2022-07-20 05:55:44 +00:00
|
|
|
|
2022-07-27 01:39:32 +00:00
|
|
|
if (isNaN(parseInt(port))) {
|
|
|
|
return false;
|
2022-06-27 22:21:31 +00:00
|
|
|
}
|
|
|
|
|
2022-07-27 01:39:32 +00:00
|
|
|
this._relays.set(pubkey, `wss://${domain}:${port}/`);
|
2022-07-20 05:55:44 +00:00
|
|
|
|
2022-07-27 01:39:32 +00:00
|
|
|
if (this._inited) {
|
|
|
|
await this.fillConnections();
|
|
|
|
}
|
2022-07-26 23:27:59 +00:00
|
|
|
|
2022-07-27 01:39:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-07-20 05:55:44 +00:00
|
|
|
|
2022-07-27 01:39:32 +00:00
|
|
|
public removeRelay(pubkey: string): boolean {
|
|
|
|
if (!this._relays.has(pubkey)) {
|
|
|
|
return false;
|
2022-06-27 22:21:31 +00:00
|
|
|
}
|
|
|
|
|
2022-07-27 01:39:32 +00:00
|
|
|
if (this._activeRelays.has(pubkey)) {
|
|
|
|
this._activeRelays.get(pubkey).destroy();
|
|
|
|
this._activeRelays.delete(pubkey);
|
2022-07-20 05:55:44 +00:00
|
|
|
}
|
2022-06-27 22:21:31 +00:00
|
|
|
|
2022-07-27 01:39:32 +00:00
|
|
|
this._relays.delete(pubkey);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public clearRelays(): void {
|
|
|
|
[...this._relays.keys()].forEach(this.removeRelay);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async isServerAvailable(connection: string): Promise<boolean> {
|
|
|
|
return new Promise<boolean>((resolve) => {
|
|
|
|
const ws = new WebSocket(connection);
|
|
|
|
ws.addEventListener("open", () => {
|
|
|
|
ws.close();
|
|
|
|
resolve(true);
|
|
|
|
});
|
|
|
|
ws.addEventListener("error", () => {
|
|
|
|
resolve(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async connect(pubkey: string, options = {}): Promise<DhtNode> {
|
2022-08-14 00:04:35 +00:00
|
|
|
if (this._activeRelays.size === 0) {
|
|
|
|
await this.fillConnections();
|
|
|
|
}
|
2022-07-27 01:39:32 +00:00
|
|
|
if (this._activeRelays.size === 0) {
|
|
|
|
throw new Error("Failed to find an available relay");
|
2022-06-27 22:21:31 +00:00
|
|
|
}
|
|
|
|
|
2022-07-27 04:37:30 +00:00
|
|
|
let index = 0;
|
|
|
|
|
|
|
|
if (this._activeRelays.size > 1) {
|
|
|
|
index = await randomNumber(0, this._activeRelays.size - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const node = this._activeRelays.get([...this._activeRelays.keys()][index]);
|
2022-07-27 01:39:32 +00:00
|
|
|
|
|
|
|
return node.connect(pubkey, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async fillConnections(): Promise<any> {
|
2022-07-27 03:20:34 +00:00
|
|
|
let available: string[] = [];
|
|
|
|
|
|
|
|
const updateAvailable = () => {
|
|
|
|
available = [...this._relays.keys()].filter(
|
|
|
|
(x) => ![...this._activeRelays.keys()].includes(x)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
updateAvailable();
|
|
|
|
|
2022-07-27 01:39:32 +00:00
|
|
|
let relayPromises = [];
|
|
|
|
while (
|
|
|
|
this._activeRelays.size <=
|
2022-07-27 03:20:34 +00:00
|
|
|
Math.min(this._maxConnections, available.length)
|
2022-07-27 01:39:32 +00:00
|
|
|
) {
|
2022-07-27 03:28:12 +00:00
|
|
|
if (0 === available.length) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-07-27 03:59:45 +00:00
|
|
|
let relayIndex = 0;
|
|
|
|
|
|
|
|
if (available.length > 1) {
|
|
|
|
relayIndex = await randomNumber(0, available.length - 1);
|
|
|
|
}
|
2022-07-27 01:39:32 +00:00
|
|
|
|
2022-08-14 00:04:35 +00:00
|
|
|
const pubkey = available[relayIndex];
|
2022-07-27 02:58:24 +00:00
|
|
|
const connection = this._relays.get(available[relayIndex]) as string;
|
2022-07-27 01:39:32 +00:00
|
|
|
|
2022-07-27 04:05:28 +00:00
|
|
|
if (!(await this.isServerAvailable(connection))) {
|
2022-07-27 04:12:41 +00:00
|
|
|
available.splice(relayIndex, 1);
|
|
|
|
this.removeRelay(available[relayIndex]);
|
2022-07-27 01:39:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const node = new DhtNode(
|
2022-07-27 02:56:49 +00:00
|
|
|
new Stream(true, new WebSocket(connection)),
|
2022-07-27 01:39:32 +00:00
|
|
|
this._options
|
|
|
|
);
|
|
|
|
this._activeRelays.set(available[relayIndex], node);
|
2022-07-27 03:28:12 +00:00
|
|
|
updateAvailable();
|
2022-07-27 01:39:32 +00:00
|
|
|
|
|
|
|
relayPromises.push(node.ready());
|
2022-08-14 00:04:35 +00:00
|
|
|
|
|
|
|
node._protocol._stream.on("close", () => {
|
|
|
|
this._activeRelays.delete(pubkey);
|
|
|
|
});
|
2022-07-20 05:55:44 +00:00
|
|
|
}
|
2022-07-27 01:39:32 +00:00
|
|
|
|
|
|
|
return Promise.allSettled(relayPromises);
|
|
|
|
}
|
2022-06-27 22:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function hashDataKey(dataKey: string): Uint8Array {
|
2022-07-27 01:39:32 +00:00
|
|
|
return blake2b(encodeUtf8String(dataKey));
|
2022-06-27 22:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function encodeUtf8String(str: string): Uint8Array {
|
2022-07-27 01:39:32 +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:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function stringToUint8ArrayUtf8(str: string): Uint8Array {
|
2022-08-14 00:02:51 +00:00
|
|
|
return new TextEncoder().encode(str);
|
2022-06-27 22:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function encodeNumber(num: number): Uint8Array {
|
2022-07-27 01:39:32 +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:21:31 +00:00
|
|
|
}
|