2023-01-12 17:40:28 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import Protomux from "protomux";
|
|
|
|
// @ts-ignore
|
|
|
|
import { SPVNode } from "hsd/lib/node";
|
|
|
|
// @ts-ignore
|
|
|
|
import { Peer as HNSPeer } from "hsd/lib/net";
|
|
|
|
// @ts-ignore
|
|
|
|
import sodium from "sodium-universal";
|
|
|
|
import b4a from "b4a";
|
|
|
|
// @ts-ignore
|
|
|
|
import c from "compact-encoding";
|
2023-02-18 00:29:13 +00:00
|
|
|
|
2023-01-12 17:40:28 +00:00
|
|
|
const PROTOCOL = "lumeweb.proxy.handshake";
|
|
|
|
|
|
|
|
import {
|
|
|
|
Proxy,
|
|
|
|
createSocket,
|
|
|
|
createServer,
|
|
|
|
Socket,
|
|
|
|
} from "@lumeweb/libhyperproxy";
|
|
|
|
|
|
|
|
export default class HandshakeProxy extends Proxy {
|
2023-02-18 00:28:33 +00:00
|
|
|
private _join: boolean;
|
2023-02-18 00:29:13 +00:00
|
|
|
|
2023-02-18 00:28:33 +00:00
|
|
|
constructor({
|
|
|
|
swarm,
|
|
|
|
listen = false,
|
|
|
|
join = false,
|
|
|
|
}: {
|
|
|
|
swarm: any;
|
|
|
|
listen?: boolean;
|
|
|
|
join?: boolean;
|
|
|
|
}) {
|
2023-01-12 17:40:28 +00:00
|
|
|
super({
|
|
|
|
swarm,
|
|
|
|
listen,
|
|
|
|
autostart: true,
|
|
|
|
protocol: PROTOCOL,
|
|
|
|
async onopen(socket: Socket) {
|
|
|
|
const peer = HNSPeer.fromInbound(self._node.pool.options, socket);
|
|
|
|
peer.connected = false;
|
|
|
|
peer.outbound = true;
|
|
|
|
|
|
|
|
if (
|
|
|
|
self._node.pool.peers.map.has(peer.hostname()) ||
|
|
|
|
self._node.pool.peers.ids.has(peer.id)
|
|
|
|
) {
|
|
|
|
return { connect: false };
|
|
|
|
}
|
|
|
|
|
|
|
|
self._node.pool.bindPeer(peer);
|
|
|
|
const open = peer.tryOpen();
|
|
|
|
// @ts-ignore
|
|
|
|
socket.emit("connect");
|
|
|
|
await open;
|
|
|
|
self._node.pool.peers.add(peer);
|
|
|
|
self._node.pool.connectedGroups.add(peer.address.getGroup());
|
|
|
|
self._node.pool.emit("peer", peer);
|
|
|
|
return { connect: false };
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const self = this;
|
2023-02-18 00:28:33 +00:00
|
|
|
this._join = join;
|
2023-01-12 17:40:28 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 20:50:15 +00:00
|
|
|
private _node?: any;
|
|
|
|
|
|
|
|
get node(): any {
|
|
|
|
return this._node;
|
|
|
|
}
|
|
|
|
|
2023-01-12 17:40:28 +00:00
|
|
|
protected async _init() {
|
|
|
|
this._node = new SPVNode({
|
|
|
|
config: false,
|
|
|
|
argv: false,
|
|
|
|
env: false,
|
|
|
|
noDns: true,
|
2023-02-15 01:10:01 +00:00
|
|
|
memory: false,
|
2023-01-12 17:40:28 +00:00
|
|
|
logFile: false,
|
|
|
|
logConsole: true,
|
|
|
|
logLevel: "info",
|
|
|
|
workers: true,
|
|
|
|
network: "main",
|
|
|
|
createServer,
|
|
|
|
createSocket,
|
|
|
|
});
|
|
|
|
|
2023-02-18 00:28:33 +00:00
|
|
|
if (this?._node?.http?.http?.listen) {
|
|
|
|
this._node.http.http.listen = (
|
|
|
|
port: number,
|
|
|
|
host: string,
|
|
|
|
cb: Function
|
|
|
|
) => cb();
|
2023-02-01 17:57:11 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 17:40:28 +00:00
|
|
|
await this._node.open();
|
|
|
|
this._node.pool.connected = true;
|
|
|
|
this._node.startSync();
|
|
|
|
|
2023-02-18 00:28:33 +00:00
|
|
|
if (this._join) {
|
|
|
|
const topic = b4a.from(PROTOCOL);
|
|
|
|
const topicHash = b4a.allocUnsafe(32);
|
|
|
|
sodium.crypto_generichash(topicHash, topic);
|
|
|
|
this.swarm.join(topicHash);
|
|
|
|
}
|
2023-01-12 17:40:28 +00:00
|
|
|
}
|
|
|
|
}
|