2022-07-24 03:16:34 +00:00
|
|
|
//const require = createRequire(import.meta.url);
|
|
|
|
//import { createRequire } from "module";
|
2022-07-19 22:24:53 +00:00
|
|
|
|
2022-11-26 07:59:07 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import Hyperswarm from "hyperswarm";
|
2022-08-03 06:02:40 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import DHT from "@hyperswarm/dht";
|
2022-12-30 06:00:45 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import Protomux from "protomux";
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-11-26 07:59:07 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import sodium from "sodium-universal";
|
|
|
|
import b4a from "b4a";
|
2022-12-19 16:44:11 +00:00
|
|
|
import log from "../log.js";
|
2022-12-18 18:32:59 +00:00
|
|
|
import { getKeyPair } from "../lib/seed.js";
|
2023-01-06 00:44:43 +00:00
|
|
|
import { getPluginAPI } from "./plugin";
|
2022-11-26 07:59:07 +00:00
|
|
|
|
|
|
|
const LUMEWEB = b4a.from("lumeweb");
|
2022-12-04 17:03:09 +00:00
|
|
|
export const LUMEWEB_TOPIC_HASH = b4a.allocUnsafe(32);
|
|
|
|
sodium.crypto_generichash(LUMEWEB_TOPIC_HASH, LUMEWEB);
|
2022-11-26 07:59:07 +00:00
|
|
|
|
|
|
|
export type SecretStream = any;
|
|
|
|
|
|
|
|
let node: Hyperswarm;
|
2022-12-30 06:00:45 +00:00
|
|
|
let protocolManager: ProtocolManager;
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-11-26 22:13:02 +00:00
|
|
|
export async function start() {
|
2022-09-22 14:17:37 +00:00
|
|
|
const keyPair = getKeyPair();
|
2022-12-22 13:28:58 +00:00
|
|
|
const bootstrap = DHT.bootstrapper(49737, "0.0.0.0");
|
|
|
|
await bootstrap.ready();
|
|
|
|
|
2022-12-31 18:28:59 +00:00
|
|
|
const address = bootstrap.address();
|
2022-12-22 13:28:58 +00:00
|
|
|
node = new Hyperswarm({
|
|
|
|
keyPair,
|
2022-12-30 23:27:18 +00:00
|
|
|
dht: new DHT({
|
|
|
|
keyPair,
|
2022-12-31 18:28:59 +00:00
|
|
|
bootstrap: [{ host: address.host, port: address.port }].concat(
|
2022-12-30 23:27:18 +00:00
|
|
|
require("@hyperswarm/dht/lib/constants").BOOTSTRAP_NODES
|
|
|
|
),
|
|
|
|
}),
|
2022-12-22 13:28:58 +00:00
|
|
|
});
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-11-26 07:59:07 +00:00
|
|
|
// @ts-ignore
|
|
|
|
await node.dht.ready();
|
|
|
|
await node.listen();
|
2022-12-04 17:03:09 +00:00
|
|
|
node.join(LUMEWEB_TOPIC_HASH);
|
2022-07-19 22:24:53 +00:00
|
|
|
|
2023-01-06 00:44:43 +00:00
|
|
|
getPluginAPI().on("core.shutdown", async () => {
|
|
|
|
return bootstrap.destroy();
|
|
|
|
});
|
|
|
|
|
2022-12-06 22:03:30 +00:00
|
|
|
log.info(
|
2022-12-19 19:17:07 +00:00
|
|
|
"Relay Identity is %s",
|
|
|
|
b4a.from(getKeyPair().publicKey).toString("hex")
|
2022-12-06 22:03:30 +00:00
|
|
|
);
|
|
|
|
|
2022-07-19 22:31:15 +00:00
|
|
|
return node;
|
2022-06-27 17:53:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-26 22:13:02 +00:00
|
|
|
export function get(): Hyperswarm {
|
2022-07-19 22:31:15 +00:00
|
|
|
return node;
|
2022-06-27 17:53:00 +00:00
|
|
|
}
|
2022-12-30 06:00:45 +00:00
|
|
|
|
|
|
|
export class ProtocolManager {
|
|
|
|
private _protocols: Map<string, Function> = new Map<string, Function>();
|
|
|
|
private _swarm;
|
|
|
|
|
|
|
|
constructor(swarm: any) {
|
|
|
|
this._swarm = swarm;
|
|
|
|
|
|
|
|
this._swarm.on("connection", (peer: any) => {
|
|
|
|
for (const protocol of this._protocols) {
|
|
|
|
Protomux.from(peer).pair(
|
|
|
|
protocol[0],
|
|
|
|
this.handler.bind(this, protocol[0], peer)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private handler(protocol: string, peer: any) {
|
|
|
|
if (this._protocols.has(protocol)) {
|
|
|
|
this._protocols.get(protocol)?.(peer, Protomux.from(peer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public register(name: string, handler: Function): boolean {
|
|
|
|
if (this._protocols.has(name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._protocols.set(name, handler);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getProtocolManager(): ProtocolManager {
|
|
|
|
if (!protocolManager) {
|
|
|
|
protocolManager = new ProtocolManager(get());
|
|
|
|
}
|
|
|
|
|
|
|
|
return protocolManager;
|
|
|
|
}
|