libhyperproxy/dist/peer.js

108 lines
3.5 KiB
JavaScript
Raw Normal View History

2023-01-12 17:50:38 +00:00
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const socket_js_1 = __importDefault(require("./socket.js"));
2023-02-06 10:51:41 +00:00
const buffer_1 = require("buffer");
2023-01-12 17:50:38 +00:00
class Peer {
_proxy;
_peer;
_muxer;
_onopen;
_onreceive;
_onsend;
_onclose;
2023-03-03 10:22:55 +00:00
_onchannel;
2023-02-26 03:16:59 +00:00
_emulateWebsocket;
2023-03-15 12:14:29 +00:00
_createDefaultMessage;
constructor({ proxy, peer, muxer, onopen, onreceive, onsend, onclose, onchannel, emulateWebsocket = false, createDefaultMessage = true, }) {
2023-01-12 17:50:38 +00:00
this._proxy = proxy;
this._peer = peer;
this._muxer = muxer;
2023-03-02 10:37:30 +00:00
this._onopen = onopen?.bind(undefined, this);
this._onreceive = onreceive?.bind(undefined, this);
this._onsend = onsend?.bind(undefined, this);
this._onclose = onclose?.bind(undefined, this);
2023-03-03 10:22:55 +00:00
this._onchannel = onchannel?.bind(undefined, this);
2023-02-26 03:16:59 +00:00
this._emulateWebsocket = emulateWebsocket;
2023-03-15 12:14:29 +00:00
this._createDefaultMessage = createDefaultMessage;
2023-01-12 17:50:38 +00:00
}
2023-03-05 08:00:28 +00:00
_socket;
get socket() {
return this._socket;
}
2023-03-02 10:41:30 +00:00
_channel;
get channel() {
return this._channel;
}
2023-01-12 17:50:38 +00:00
async init() {
const self = this;
2023-03-15 12:14:29 +00:00
let pipe;
2023-04-09 16:15:38 +00:00
const raw = await maybeGetAsyncProperty(self._peer.rawStream);
2023-03-07 08:25:02 +00:00
this._socket = new socket_js_1.default({
2023-04-09 16:15:38 +00:00
remoteAddress: raw.remoteHost,
remotePort: raw.remotePort,
2023-04-09 16:24:05 +00:00
remotePublicKey: await maybeGetAsyncProperty(self._peer.remotePublicKey),
2023-03-07 08:25:02 +00:00
async write(data, cb) {
2023-03-15 12:14:29 +00:00
if (pipe) {
pipe.send(data);
}
2023-03-07 08:25:02 +00:00
await self._onsend?.(data);
cb();
},
emulateWebsocket: self._emulateWebsocket,
});
2023-04-09 00:56:45 +00:00
this._channel = await this._muxer.createChannel({
2023-01-12 17:50:38 +00:00
protocol: this._proxy.protocol,
async onopen(m) {
if (!m) {
2023-02-06 10:51:41 +00:00
m = buffer_1.Buffer.from([]);
}
if (m instanceof Uint8Array) {
m = buffer_1.Buffer.from(m);
2023-01-12 17:50:38 +00:00
}
2023-03-02 10:26:40 +00:00
self._socket.on("end", () => this._channel.close());
2023-01-12 17:50:38 +00:00
let ret = await self._onopen?.(self._socket, m);
if (!ret || (ret && ret.connect === false)) {
// @ts-ignore
self._socket.emit("connect");
}
self._socket.emit("data", m);
},
async onclose() {
2023-02-16 06:02:20 +00:00
self._socket?.destroy();
2023-01-12 17:50:38 +00:00
await self._onclose?.(self._socket);
},
});
2023-03-15 12:14:29 +00:00
if (this._createDefaultMessage) {
2023-04-09 00:56:45 +00:00
pipe = await this._channel.addMessage({
2023-03-15 12:14:29 +00:00
async onmessage(m) {
if (m instanceof Uint8Array) {
m = buffer_1.Buffer.from(m);
}
self._socket.emit("data", m);
await self._onreceive?.(m);
},
});
}
2023-03-03 10:22:55 +00:00
await this._onchannel?.(this._channel);
2023-03-02 10:26:40 +00:00
await this._channel.open();
2023-01-12 17:50:38 +00:00
}
}
exports.default = Peer;
2023-04-09 16:15:38 +00:00
async function maybeGetAsyncProperty(object) {
if (typeof object === "function") {
object = object();
}
if (isPromise(object)) {
object = await object;
}
return object;
}
function isPromise(obj) {
return (!!obj &&
(typeof obj === "object" || typeof obj === "function") &&
typeof obj.then === "function");
}