*prettier

This commit is contained in:
Derrick Hammer 2022-07-26 21:39:32 -04:00
parent c7b64064ad
commit 2e71082a59
1 changed files with 130 additions and 116 deletions

View File

@ -5,166 +5,180 @@ import Stream from "@hyperswarm/dht-relay/ws";
// @ts-ignore // @ts-ignore
import createRoundRobin from "@derhuerst/round-robin-scheduler"; import createRoundRobin from "@derhuerst/round-robin-scheduler";
// @ts-ignore // @ts-ignore
import {Buffer} from "buffer"; import { Buffer } from "buffer";
// @ts-ignore // @ts-ignore
// @ts-ignore // @ts-ignore
import {blake2b, errTuple} from "libskynet"; import { blake2b, errTuple } from "libskynet";
// @ts-ignore // @ts-ignore
import {registryRead} from "libkmodule"; import { registryRead } from "libkmodule";
import {unpack} from "msgpackr"; import { unpack } from "msgpackr";
import randomNumber from "random-number-csprng"; import randomNumber from "random-number-csprng";
const REGISTRY_DHT_KEY = "lumeweb-dht-node"; const REGISTRY_DHT_KEY = "lumeweb-dht-node";
export default class DHT { export default class DHT {
private _options: any; private _options: any;
private _relays: Map<string, string> = new Map(); private _relays: Map<string, string> = new Map();
private _activeRelays: Map<string, typeof DhtNode> = new Map(); private _activeRelays: Map<string, typeof DhtNode> = new Map();
private _maxConnections = 10; private _maxConnections = 10;
private _inited = false; private _inited = false;
constructor(opts = {}) { constructor(opts = {}) {
// @ts-ignore // @ts-ignore
opts.custodial = false; opts.custodial = false;
this._options = opts; this._options = opts;
}
ready(): Promise<void> {
if (this._inited) {
return Promise.resolve();
} }
ready(): Promise<void> { this._inited = true;
if (this._inited) { return this.fillConnections();
return Promise.resolve(); }
}
this._inited = true; get relays(): string[] {
return this.fillConnections(); return [...this._relays.keys()];
}
public async addRelay(pubkey: string): Promise<boolean> {
let entry: errTuple = await registryRead(
Uint8Array.from(Buffer.from(pubkey, "hex")),
hashDataKey(REGISTRY_DHT_KEY)
);
if (entry[1] || !entry[0]?.exists) {
return false;
} }
get relays(): string[] { let host;
return [...this._relays.keys()];
try {
host = unpack(entry[0].entryData);
} catch (e) {
return false;
} }
public async addRelay(pubkey: string): Promise<boolean> { const [domain, port] = host.split(":");
let entry: errTuple = await registryRead(
Uint8Array.from(Buffer.from(pubkey, "hex")),
hashDataKey(REGISTRY_DHT_KEY)
);
if (entry[1] || !entry[0]?.exists) { if (isNaN(parseInt(port))) {
return false; 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;
}
this._relays.set(pubkey, `wss://${domain}:${port}/`);
if (this._inited) {
await this.fillConnections();
}
return true;
} }
public removeRelay(pubkey: string): boolean { this._relays.set(pubkey, `wss://${domain}:${port}/`);
if (!this._relays.has(pubkey)) {
return false;
}
if (this._inited) {
if (this._activeRelays.has(pubkey)) { await this.fillConnections();
this._activeRelays.get(pubkey).destroy();
this._activeRelays.delete(pubkey)
}
this._relays.delete(pubkey)
return true;
} }
public clearRelays(): void { return true;
[...this._relays.keys()].forEach(this.removeRelay); }
public removeRelay(pubkey: string): boolean {
if (!this._relays.has(pubkey)) {
return false;
} }
private async isServerAvailable(connection: string): Promise<boolean> { if (this._activeRelays.has(pubkey)) {
return new Promise<boolean>((resolve) => { this._activeRelays.get(pubkey).destroy();
const ws = new WebSocket(connection); this._activeRelays.delete(pubkey);
ws.addEventListener("open", () => {
ws.close();
resolve(true);
});
ws.addEventListener("error", () => {
resolve(false);
});
});
} }
async connect(pubkey: string, options = {}): Promise<DhtNode> { this._relays.delete(pubkey);
if (this._activeRelays.size === 0) {
throw new Error("Failed to find an available relay");
}
const node = this._activeRelays.get([...this._activeRelays.keys()][await randomNumber(0, this._activeRelays.size - 1)]); return true;
}
return node.connect(pubkey, options) 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> {
if (this._activeRelays.size === 0) {
throw new Error("Failed to find an available relay");
} }
private async fillConnections(): Promise<any> { const node = this._activeRelays.get(
let available = [...this._relays.keys()].filter(x => [...this._activeRelays.keys()].includes(x)); [...this._activeRelays.keys()][
let relayPromises = []; await randomNumber(0, this._activeRelays.size - 1)
if (0 > available.length) { ]
return; );
}
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]; return node.connect(pubkey, options);
}
if (!this.isServerAvailable(connection)) { private async fillConnections(): Promise<any> {
continue; let available = [...this._relays.keys()].filter((x) =>
} [...this._activeRelays.keys()].includes(x)
);
const node = new DhtNode(new Stream(true, new WebSocket(this._activeRelays.get(connection) as string)), this._options); let relayPromises = [];
this._activeRelays.set(available[relayIndex], node); if (0 > available.length) {
return;
relayPromises.push(node.ready());
}
return Promise.allSettled(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;
}
const node = new DhtNode(
new Stream(
true,
new WebSocket(this._activeRelays.get(connection) as string)
),
this._options
);
this._activeRelays.set(available[relayIndex], node);
relayPromises.push(node.ready());
}
return Promise.allSettled(relayPromises);
}
} }
export function hashDataKey(dataKey: string): Uint8Array { export function hashDataKey(dataKey: string): Uint8Array {
return blake2b(encodeUtf8String(dataKey)); return blake2b(encodeUtf8String(dataKey));
} }
function encodeUtf8String(str: string): Uint8Array { function encodeUtf8String(str: string): Uint8Array {
const byteArray = stringToUint8ArrayUtf8(str); const byteArray = stringToUint8ArrayUtf8(str);
const encoded = new Uint8Array(8 + byteArray.length); const encoded = new Uint8Array(8 + byteArray.length);
encoded.set(encodeNumber(byteArray.length)); encoded.set(encodeNumber(byteArray.length));
encoded.set(byteArray, 8); encoded.set(byteArray, 8);
return encoded; return encoded;
} }
function stringToUint8ArrayUtf8(str: string): Uint8Array { function stringToUint8ArrayUtf8(str: string): Uint8Array {
return Uint8Array.from(Buffer.from(str, "utf-8")); return Uint8Array.from(Buffer.from(str, "utf-8"));
} }
function encodeNumber(num: number): Uint8Array { function encodeNumber(num: number): Uint8Array {
const encoded = new Uint8Array(8); const encoded = new Uint8Array(8);
for (let index = 0; index < encoded.length; index++) { for (let index = 0; index < encoded.length; index++) {
encoded[index] = num & 0xff; encoded[index] = num & 0xff;
num = num >> 8; num = num >> 8;
} }
return encoded; return encoded;
} }