hypercore-proxy-handshake/dist/index.js

71 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-01-12 17:53:58 +00:00
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// @ts-ignore
const node_1 = require("hsd/lib/node");
// @ts-ignore
const net_1 = require("hsd/lib/net");
// @ts-ignore
const sodium_universal_1 = __importDefault(require("sodium-universal"));
const b4a_1 = __importDefault(require("b4a"));
const PROTOCOL = "lumeweb.proxy.handshake";
const libhyperproxy_1 = require("@lumeweb/libhyperproxy");
class HandshakeProxy extends libhyperproxy_1.Proxy {
constructor({ swarm, listen = false }) {
super({
swarm,
listen,
autostart: true,
protocol: PROTOCOL,
async onopen(socket) {
const peer = net_1.Peer.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;
}
async _init() {
this._node = new node_1.SPVNode({
config: false,
argv: false,
env: false,
noDns: true,
memory: true,
logFile: false,
logConsole: true,
logLevel: "info",
workers: true,
network: "main",
createServer: libhyperproxy_1.createServer,
createSocket: libhyperproxy_1.createSocket,
});
2023-02-01 17:57:30 +00:00
if (this?._node?.http?.http?.listen) {
this._node.http.http.listen = (port, host, cb) => cb();
}
2023-01-12 17:53:58 +00:00
await this._node.open();
this._node.pool.connected = true;
this._node.startSync();
const topic = b4a_1.default.from(PROTOCOL);
const topicHash = b4a_1.default.allocUnsafe(32);
sodium_universal_1.default.crypto_generichash(topicHash, topic);
this.swarm.join(topicHash);
}
}
exports.default = HandshakeProxy;