2022-07-21 17:03:18 +00:00
|
|
|
// @ts-ignore
|
2023-02-01 12:50:45 +00:00
|
|
|
import Hyperswarm from "@lumeweb/hyperswarm-web";
|
2022-07-21 17:03:18 +00:00
|
|
|
import type { ActiveQuery } from "libkmodule";
|
2022-08-03 16:01:11 +00:00
|
|
|
import { addHandler, getSeed, handleMessage } from "libkmodule";
|
|
|
|
import { handlePresentSeed as handlePresentSeedModule } from "libkmodule/dist/seed.js";
|
2022-08-13 19:11:18 +00:00
|
|
|
import type { Buffer } from "buffer";
|
2023-02-01 12:50:45 +00:00
|
|
|
import * as ed from "@noble/ed25519";
|
|
|
|
import b4a from "b4a";
|
2023-02-06 17:31:07 +00:00
|
|
|
import { pubKeyToIpv6 } from "./addr.js";
|
2023-03-19 19:16:51 +00:00
|
|
|
import { EventEmitter2 as EventEmitter } from "eventemitter2";
|
|
|
|
import { logErr } from "libkmodule/dist";
|
2022-07-21 17:03:18 +00:00
|
|
|
|
2023-03-19 11:19:19 +00:00
|
|
|
const MAX_PEER_LISTENERS = 20;
|
|
|
|
|
2023-02-06 17:31:07 +00:00
|
|
|
interface SwarmConnection {
|
2023-02-01 12:50:45 +00:00
|
|
|
swarm: number;
|
2022-08-03 16:01:11 +00:00
|
|
|
conn: any;
|
|
|
|
}
|
|
|
|
|
2023-03-19 19:16:51 +00:00
|
|
|
interface SwarmEvents {
|
|
|
|
swarm: number;
|
|
|
|
events: EventEmitter;
|
|
|
|
}
|
|
|
|
|
2023-02-06 17:31:07 +00:00
|
|
|
const connections = new Map<number, SwarmConnection>();
|
2023-02-01 12:50:45 +00:00
|
|
|
const swarmInstances = new Map<number, Hyperswarm>();
|
2023-03-19 19:16:51 +00:00
|
|
|
const swarmEvents = new Map<number, SwarmEvents>();
|
2022-07-21 17:03:18 +00:00
|
|
|
|
2023-02-01 12:50:45 +00:00
|
|
|
let defaultSwarm: Hyperswarm;
|
2022-08-31 19:35:24 +00:00
|
|
|
|
|
|
|
let moduleReadyResolve: Function;
|
|
|
|
let moduleReady: Promise<void> = new Promise((resolve) => {
|
|
|
|
moduleReadyResolve = resolve;
|
|
|
|
});
|
2022-07-21 17:03:18 +00:00
|
|
|
|
|
|
|
onmessage = handleMessage;
|
2023-02-01 12:50:45 +00:00
|
|
|
function idFactory(start = 1) {
|
2022-08-31 19:35:24 +00:00
|
|
|
let id = start;
|
|
|
|
|
|
|
|
return function nextId() {
|
|
|
|
const nextId = id;
|
2023-02-01 12:50:45 +00:00
|
|
|
id += 1;
|
2022-08-31 19:35:24 +00:00
|
|
|
return nextId;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-01 12:50:45 +00:00
|
|
|
const getSwarmId = idFactory();
|
|
|
|
const getSocketId = idFactory();
|
2022-07-21 17:03:18 +00:00
|
|
|
|
|
|
|
addHandler("presentSeed", handlePresentSeed);
|
2023-02-06 17:31:07 +00:00
|
|
|
addHandler("join", handleJoin);
|
2023-02-01 12:50:45 +00:00
|
|
|
addHandler("getPeerByPubkey", handleGetPeerByPubkey);
|
2023-02-06 17:31:07 +00:00
|
|
|
|
2022-07-21 17:03:18 +00:00
|
|
|
addHandler("addRelay", handleAddRelay);
|
|
|
|
addHandler("removeRelay", handleRemoveRelay);
|
|
|
|
addHandler("clearRelays", handleClearRelays);
|
2022-08-14 10:49:24 +00:00
|
|
|
addHandler("getRelays", handleGetRelays);
|
2023-02-01 17:07:46 +00:00
|
|
|
addHandler("init", handleInit);
|
2022-07-21 17:03:18 +00:00
|
|
|
addHandler("ready", handleReady);
|
2023-02-06 17:31:07 +00:00
|
|
|
addHandler("listenConnections", handleListenConnections, {
|
|
|
|
receiveUpdates: true,
|
|
|
|
});
|
|
|
|
addHandler("socketGetInfo", handleGetSocketInfo);
|
2022-07-21 17:03:18 +00:00
|
|
|
|
2023-02-06 17:31:07 +00:00
|
|
|
addHandler("socketExists", handleSocketExists);
|
|
|
|
addHandler("socketListenEvent", handleSocketListenEvent, {
|
|
|
|
receiveUpdates: true,
|
|
|
|
});
|
|
|
|
addHandler("socketWrite", handleWriteSocketEvent);
|
|
|
|
addHandler("socketClose", handleCloseSocketEvent);
|
2022-08-03 16:01:11 +00:00
|
|
|
async function handlePresentSeed(aq: ActiveQuery) {
|
2023-02-06 17:31:07 +00:00
|
|
|
const pubkey = await ed.getPublicKey(aq.callerInput.rootKey);
|
2023-02-01 12:50:45 +00:00
|
|
|
handlePresentSeedModule({
|
|
|
|
callerInput: {
|
|
|
|
seed: {
|
|
|
|
publicKey: await ed.getPublicKey(aq.callerInput.rootKey),
|
2023-02-06 17:31:07 +00:00
|
|
|
secretKey: b4a.concat([aq.callerInput.rootKey, pubkey]),
|
2023-02-01 12:50:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
} as ActiveQuery);
|
|
|
|
|
|
|
|
if (!defaultSwarm) {
|
|
|
|
defaultSwarm = swarmInstances.get(await createSwarm()) as Hyperswarm;
|
2022-08-03 16:01:11 +00:00
|
|
|
}
|
2022-08-31 19:35:24 +00:00
|
|
|
moduleReadyResolve();
|
2022-08-03 16:01:11 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 12:50:45 +00:00
|
|
|
async function createSwarm(): Promise<number> {
|
|
|
|
const swarmInstance = new Hyperswarm({ keyPair: await getSeed() });
|
|
|
|
const id = getSwarmId();
|
|
|
|
swarmInstances.set(id, swarmInstance);
|
2022-08-03 16:01:11 +00:00
|
|
|
|
2023-02-18 03:37:05 +00:00
|
|
|
swarmInstance.onSelf("init", () => {
|
2023-03-19 19:16:51 +00:00
|
|
|
const swarmInstanceEvents = new EventEmitter();
|
|
|
|
swarmInstanceEvents.setMaxListeners(MAX_PEER_LISTENERS);
|
|
|
|
swarmEvents.set(id, { swarm: id, events: swarmInstanceEvents });
|
2023-02-18 03:37:05 +00:00
|
|
|
swarmInstance.on("connection", (peer: any) => {
|
|
|
|
const socketId = getSocketId();
|
2023-03-19 19:16:51 +00:00
|
|
|
connections.set(socketId, {
|
|
|
|
swarm: id,
|
|
|
|
conn: peer,
|
|
|
|
});
|
2023-02-18 03:37:05 +00:00
|
|
|
|
2023-03-19 19:16:51 +00:00
|
|
|
peer.once("close", () => {
|
2023-02-18 03:37:05 +00:00
|
|
|
connections.delete(socketId);
|
|
|
|
});
|
2023-03-19 19:16:51 +00:00
|
|
|
|
|
|
|
swarmInstanceEvents.emit("connection", peer);
|
2022-08-03 16:01:11 +00:00
|
|
|
});
|
2022-07-21 17:03:18 +00:00
|
|
|
});
|
|
|
|
|
2023-03-19 19:16:51 +00:00
|
|
|
swarmInstance.onSelf("close", (...args) => {
|
|
|
|
swarmEvents.get(id)?.events.emit("close", ...args);
|
|
|
|
swarmEvents.get(id)?.events.removeAllListeners();
|
|
|
|
swarmEvents.delete(id);
|
|
|
|
});
|
|
|
|
|
2023-02-01 12:50:45 +00:00
|
|
|
return id;
|
2022-07-21 17:03:18 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 17:31:07 +00:00
|
|
|
function handleSocketListenEvent(aq: ActiveQuery) {
|
2022-07-21 17:03:18 +00:00
|
|
|
const { event = null } = aq.callerInput;
|
|
|
|
|
2022-08-03 16:01:11 +00:00
|
|
|
const socket = validateConnection(aq);
|
|
|
|
|
|
|
|
if (!socket) {
|
2022-07-21 17:03:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!event) {
|
|
|
|
aq.reject("Invalid event");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-19 12:11:54 +00:00
|
|
|
let responded = false;
|
|
|
|
const respond = () => {
|
|
|
|
if (responded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
responded = true;
|
|
|
|
aq.respond();
|
|
|
|
};
|
|
|
|
|
2022-07-21 17:03:18 +00:00
|
|
|
const cb = (data: Buffer) => {
|
|
|
|
aq.sendUpdate(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
socket.on(event, cb);
|
2023-03-18 18:41:20 +00:00
|
|
|
socket.once("close", () => {
|
2022-09-19 12:11:54 +00:00
|
|
|
socket.off(event, cb);
|
|
|
|
respond();
|
2022-07-21 17:03:18 +00:00
|
|
|
});
|
|
|
|
|
2023-02-06 17:31:07 +00:00
|
|
|
aq.setReceiveUpdate?.(() => {
|
|
|
|
socket.off(event, cb);
|
|
|
|
respond();
|
2022-07-21 17:03:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-13 19:12:49 +00:00
|
|
|
async function handleSocketExists(aq: ActiveQuery) {
|
|
|
|
const { id = null } = aq.callerInput;
|
|
|
|
|
2023-02-01 12:50:45 +00:00
|
|
|
aq.respond(connections.has(Number(id)));
|
2022-08-13 19:12:49 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 17:03:18 +00:00
|
|
|
function handleCloseSocketEvent(aq: ActiveQuery) {
|
2022-08-03 16:01:11 +00:00
|
|
|
const socket = validateConnection(aq);
|
2022-07-21 17:03:18 +00:00
|
|
|
|
2022-08-03 16:01:11 +00:00
|
|
|
if (!socket) {
|
2022-07-21 17:03:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-03 16:01:11 +00:00
|
|
|
socket.end();
|
2022-07-21 17:03:18 +00:00
|
|
|
|
|
|
|
aq.respond();
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleWriteSocketEvent(aq: ActiveQuery) {
|
2022-08-03 16:01:11 +00:00
|
|
|
const socket = validateConnection(aq);
|
2022-07-21 17:03:18 +00:00
|
|
|
|
2022-08-03 16:01:11 +00:00
|
|
|
if (!socket) {
|
2022-07-21 17:03:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const { message = null } = aq.callerInput;
|
|
|
|
|
|
|
|
if (!message) {
|
|
|
|
aq.reject("empty message");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-03 16:01:11 +00:00
|
|
|
socket.write(message);
|
2022-07-21 17:03:18 +00:00
|
|
|
|
|
|
|
aq.respond();
|
|
|
|
}
|
|
|
|
|
2022-08-03 16:01:11 +00:00
|
|
|
function validateConnection(aq: ActiveQuery): any | boolean {
|
2022-07-21 17:03:18 +00:00
|
|
|
const { id = null } = aq.callerInput;
|
|
|
|
|
2023-02-01 12:50:45 +00:00
|
|
|
if (!id || !connections.has(id)) {
|
2022-07-21 17:03:18 +00:00
|
|
|
aq.reject("Invalid connection id");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-01 12:50:45 +00:00
|
|
|
return connections.get(id)?.conn;
|
2022-08-03 16:01:11 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 12:50:45 +00:00
|
|
|
async function getSwarm(aq: ActiveQuery): Promise<Hyperswarm> {
|
2022-08-31 19:35:24 +00:00
|
|
|
await moduleReady;
|
2023-02-01 12:50:45 +00:00
|
|
|
let swarm;
|
2022-08-31 19:35:24 +00:00
|
|
|
if ("callerInput" in aq && aq.callerInput) {
|
2023-02-01 12:50:45 +00:00
|
|
|
swarm = aq.callerInput.swarm ?? null;
|
2022-08-03 16:01:11 +00:00
|
|
|
|
2023-02-01 12:50:45 +00:00
|
|
|
if (swarm && !swarmInstances.has(swarm)) {
|
|
|
|
const error = "Invalid swarm id";
|
2022-08-31 19:35:24 +00:00
|
|
|
aq.reject(error);
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
2022-08-03 16:01:11 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 12:50:45 +00:00
|
|
|
if (!swarm) {
|
|
|
|
return defaultSwarm;
|
2022-08-03 16:01:11 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 12:50:45 +00:00
|
|
|
return swarmInstances.get(swarm) as Hyperswarm;
|
2022-07-21 17:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function handleAddRelay(aq: ActiveQuery) {
|
|
|
|
const { pubkey = null } = aq.callerInput;
|
|
|
|
|
|
|
|
if (!pubkey) {
|
|
|
|
aq.reject("invalid pubkey");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-01 13:18:31 +00:00
|
|
|
const swarm = await getSwarm(aq);
|
2022-08-31 19:35:24 +00:00
|
|
|
|
2023-02-01 13:18:31 +00:00
|
|
|
aq.respond(await swarm.addRelay(pubkey));
|
2022-07-21 17:03:18 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 19:35:24 +00:00
|
|
|
async function handleRemoveRelay(aq: ActiveQuery) {
|
2022-07-21 17:03:18 +00:00
|
|
|
const { pubkey = null } = aq.callerInput;
|
|
|
|
|
|
|
|
if (!pubkey) {
|
|
|
|
aq.reject("invalid pubkey");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-01 13:18:31 +00:00
|
|
|
const swarm = await getSwarm(aq);
|
2022-08-31 19:35:24 +00:00
|
|
|
|
2023-02-01 13:18:31 +00:00
|
|
|
aq.respond(swarm.removeRelay(pubkey));
|
2022-07-21 17:03:18 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 19:35:24 +00:00
|
|
|
async function handleClearRelays(aq: ActiveQuery) {
|
2023-02-01 13:18:31 +00:00
|
|
|
const swarm = await getSwarm(aq);
|
2022-08-03 16:01:11 +00:00
|
|
|
|
2023-02-01 13:18:31 +00:00
|
|
|
swarm.clearRelays();
|
2022-07-21 17:03:18 +00:00
|
|
|
|
|
|
|
aq.respond();
|
|
|
|
}
|
|
|
|
|
2022-08-31 19:35:24 +00:00
|
|
|
async function handleGetRelays(aq: ActiveQuery) {
|
2023-02-01 12:50:45 +00:00
|
|
|
aq.respond(await (await getSwarm(aq)).relays);
|
2022-08-14 10:49:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 17:31:07 +00:00
|
|
|
async function handleJoin(aq: ActiveQuery) {
|
2023-02-01 12:50:45 +00:00
|
|
|
const { topic = null } = aq.callerInput;
|
|
|
|
|
|
|
|
const swarm = await getSwarm(aq);
|
|
|
|
|
|
|
|
if (!topic) {
|
|
|
|
aq.reject("invalid topic");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!b4a.isBuffer(topic)) {
|
|
|
|
aq.reject("topic must be a buffer");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// @ts-ignore
|
2023-02-06 17:31:07 +00:00
|
|
|
swarm.join(topic, { server: false });
|
2022-07-21 17:03:18 +00:00
|
|
|
aq.respond();
|
|
|
|
}
|
2023-02-01 12:50:45 +00:00
|
|
|
async function handleGetPeerByPubkey(aq: ActiveQuery) {
|
|
|
|
const { pubkey = null } = aq.callerInput;
|
2022-08-03 16:01:11 +00:00
|
|
|
|
2023-02-01 12:50:45 +00:00
|
|
|
const swarm = await getSwarm(aq);
|
2022-08-03 16:01:11 +00:00
|
|
|
|
2023-02-01 12:50:45 +00:00
|
|
|
if (!pubkey) {
|
|
|
|
aq.reject("invalid topic");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!b4a.isBuffer(pubkey)) {
|
|
|
|
aq.reject("pubkey must be a buffer");
|
|
|
|
return;
|
|
|
|
}
|
2022-08-03 16:01:11 +00:00
|
|
|
|
2023-02-01 12:50:45 +00:00
|
|
|
// @ts-ignore
|
|
|
|
if (!swarm._allConnections.has(pubkey)) {
|
|
|
|
aq.reject("peer does not exist");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
const peer = swarm._allConnections.get(pubkey);
|
|
|
|
|
2023-02-06 17:31:07 +00:00
|
|
|
aq.respond(getSwarmToSocketConnectionId(peer));
|
2022-08-03 16:01:11 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:07:46 +00:00
|
|
|
async function handleInit(aq: ActiveQuery) {
|
2023-02-01 12:50:45 +00:00
|
|
|
const swarm = await getSwarm(aq);
|
2023-02-01 17:07:46 +00:00
|
|
|
try {
|
|
|
|
await swarm.init();
|
|
|
|
} catch (e) {
|
|
|
|
aq.reject((e as Error).message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-01 12:50:45 +00:00
|
|
|
aq.respond();
|
2022-08-03 16:01:11 +00:00
|
|
|
}
|
2023-02-01 17:07:46 +00:00
|
|
|
async function handleReady(aq: ActiveQuery) {
|
|
|
|
const swarm = await getSwarm(aq);
|
|
|
|
|
2023-02-17 13:10:14 +00:00
|
|
|
if (swarm.activeRelay && swarm.ready) {
|
2023-02-01 17:07:46 +00:00
|
|
|
aq.respond();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
swarm.once("ready", () => {
|
|
|
|
aq.respond();
|
|
|
|
});
|
|
|
|
}
|
2023-02-06 17:31:07 +00:00
|
|
|
async function handleListenConnections(aq: ActiveQuery) {
|
|
|
|
const swarm = await getSwarm(aq);
|
2023-04-04 12:07:02 +00:00
|
|
|
const swarmId = getSwarmToSwarmId(swarm);
|
2023-02-06 17:31:07 +00:00
|
|
|
|
|
|
|
const listener = (peer: any) => {
|
|
|
|
aq.sendUpdate(getSwarmToSocketConnectionId(peer));
|
|
|
|
};
|
|
|
|
|
2023-04-04 12:07:02 +00:00
|
|
|
const swarmEvent = swarmEvents.get(swarmId as number)?.events;
|
2023-03-19 19:16:51 +00:00
|
|
|
|
|
|
|
if (!swarmEvent) {
|
|
|
|
logErr("swarm event object is missing");
|
|
|
|
}
|
|
|
|
|
|
|
|
swarmEvent?.on("connection", listener);
|
2023-02-06 17:31:07 +00:00
|
|
|
|
|
|
|
aq.setReceiveUpdate?.(() => {
|
2023-03-19 19:16:51 +00:00
|
|
|
swarmEvent?.off("connection", listener);
|
2023-02-06 17:31:07 +00:00
|
|
|
aq.respond();
|
|
|
|
});
|
2023-02-17 02:40:40 +00:00
|
|
|
|
2023-02-18 00:16:22 +00:00
|
|
|
const closeCb = () => {
|
2023-03-19 19:16:51 +00:00
|
|
|
swarmEvent?.off("connection", listener);
|
|
|
|
swarmEvent?.emit("close");
|
2023-02-17 02:40:40 +00:00
|
|
|
aq.respond();
|
2023-02-18 00:16:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const hookClose = () => {
|
2023-03-29 18:48:15 +00:00
|
|
|
swarmEvent?.once("close", closeCb);
|
2023-02-18 00:16:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (swarm.activeRelay) {
|
|
|
|
hookClose();
|
|
|
|
return;
|
|
|
|
}
|
2023-03-19 19:16:51 +00:00
|
|
|
swarm.onceSelf("ready", hookClose);
|
2023-04-04 12:07:02 +00:00
|
|
|
|
|
|
|
for (const conn of connections) {
|
|
|
|
if (conn[1].swarm === swarmId) {
|
|
|
|
listener(conn[1].conn);
|
|
|
|
}
|
|
|
|
}
|
2023-02-06 17:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function handleGetSocketInfo(aq: ActiveQuery) {
|
|
|
|
const socket = validateConnection(aq);
|
|
|
|
|
|
|
|
if (!socket) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aq.respond({
|
|
|
|
remotePublicKey: socket.remotePublicKey,
|
|
|
|
publicKey: socket.publicKey,
|
|
|
|
rawStream: {
|
|
|
|
remoteHost: pubKeyToIpv6(socket.remotePublicKey),
|
|
|
|
remotePort: 0,
|
|
|
|
remoteFamily: "IPv6",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSwarmToSocketConnectionId(socket: any) {
|
|
|
|
for (const conn of connections) {
|
|
|
|
if (conn[1].conn === socket) {
|
|
|
|
return conn[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2023-03-19 19:16:51 +00:00
|
|
|
function getSwarmToSwarmId(swarm: any) {
|
|
|
|
for (const swarmInstance of swarmInstances) {
|
|
|
|
if (swarmInstance[1] === swarm) {
|
|
|
|
return swarmInstance[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|