From 2e639eb92a7b49b8ca6fe836e74d051ac027e93c Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 7 Mar 2023 03:24:15 -0500 Subject: [PATCH] *Move socket creation outside channel onopen --- src/peer.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/peer.ts b/src/peer.ts index 6a2fad7..e0c829a 100644 --- a/src/peer.ts +++ b/src/peer.ts @@ -95,12 +95,19 @@ export default class Peer { } async init() { - const write = async (data: any, cb: Function) => { - pipe.send(data); - await this._onsend?.(data); - cb(); - }; const self = this; + this._socket = new Socket({ + remoteAddress: self._peer.rawStream.remoteHost, + remotePort: self._peer.rawStream.remotePort, + remotePublicKey: self._peer.remotePublicKey, + async write(data: any, cb: Function) { + pipe.send(data); + await self._onsend?.(data); + cb(); + }, + emulateWebsocket: self._emulateWebsocket, + }); + this._channel = this._muxer.createChannel({ protocol: this._proxy.protocol, async onopen(m: any) { @@ -111,13 +118,7 @@ export default class Peer { if (m instanceof Uint8Array) { m = Buffer.from(m); } - self._socket = new Socket({ - remoteAddress: self._peer.rawStream.remoteHost, - remotePort: self._peer.rawStream.remotePort, - remotePublicKey: self._peer.remotePublicKey, - write, - emulateWebsocket: self._emulateWebsocket, - }); + self._socket.on("end", () => this._channel.close()); let ret = await self._onopen?.(self._socket, m); if (!ret || (ret && ret.connect === false)) {