From 45ce56b118d67c9fb4362f4ad6674f898b83cafb Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Fri, 6 Jan 2023 11:57:11 +0100 Subject: [PATCH] fix(core): update --- ts/00_commitinfo_data.ts | 2 +- ts/smartproxy.classes.networkproxy.ts | 61 ++++++++++----------------- 2 files changed, 24 insertions(+), 39 deletions(-) diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 9d03282..7646a21 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@pushrocks/smartproxy', - version: '3.0.52', + version: '3.0.53', description: 'a proxy for handling high workloads of proxying' } diff --git a/ts/smartproxy.classes.networkproxy.ts b/ts/smartproxy.classes.networkproxy.ts index 234b94c..4c3f518 100644 --- a/ts/smartproxy.classes.networkproxy.ts +++ b/ts/smartproxy.classes.networkproxy.ts @@ -246,47 +246,32 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g= // Enable websockets const wss = new plugins.ws.WebSocketServer({ server: this.httpsServer }); - wss.on('connection', (ws: plugins.wsDefault, reqArg: plugins.http.IncomingMessage) => { + wss.on('connection', async (ws: plugins.wsDefault, reqArg: plugins.http.IncomingMessage) => { console.log(`wss: got connection for wsc for https://${reqArg.headers.host}${reqArg.url}`); - let wscConnectedDeferred: plugins.smartpromise.Deferred; + + let wsc: plugins.wsDefault; + try { + wsc = new plugins.wsDefault(`ws://${this.router.routeReq(reqArg).destinationIp}:${this.router.routeReq(reqArg).destinationPort}${reqArg.url}`); + } catch (err) { + console.log(err); + ws.terminate(); + return; + } - ws.on('message', async (message) => { - if (!wscConnectedDeferred) { - wscConnectedDeferred = plugins.smartpromise.defer(); - let wsc: plugins.wsDefault; - try { - wsc = new plugins.wsDefault(`ws://${this.router.routeReq(reqArg).destinationIp}:${this.router.routeReq(reqArg).destinationPort}${reqArg.url}`, { - headers: reqArg.headers - }); - } catch (err) { - console.log(err); - ws.terminate(); - return; - } - wsc.on('open', () => { - console.log('wsc opened.'); - wscConnectedDeferred.resolve(wsc); - }); - wsc.on('message', (message) => { - ws.send(message); - }); - } - const wsc = await wscConnectedDeferred.promise; - wsc.send(message); - // handle closing - const cleanUp = () => { - ws.terminate(); - wsc.terminate(); - }; - - ws.on('close', (message) => { - cleanUp(); - }); - - wsc.on('close', (message) => { - cleanUp(); - }); + ws.on("message", (message, isBinary) => { + console.log("client to upstream", message); + wsc.send(message, { binary: isBinary }); }); + + wsc.on("message", (message, isBinary) => { + console.log("upstream to client", message); + ws.send(message, { binary: isBinary }); + }); + ws.on("error", () => wsc.terminate()); + wsc.on("error", () => ws.terminate()); + ws.on("close", () => wsc.terminate()); + wsc.on("close", () => ws.terminate()); + }); this.httpsServer.keepAliveTimeout = 600 * 1000; this.httpsServer.headersTimeout = 600 * 1000;