|
|
|
@ -245,47 +245,33 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Enable websockets
|
|
|
|
|
const wss = new plugins.ws.WebSocketServer({ server: this.httpsServer });
|
|
|
|
|
wss.on('connection', (ws: plugins.wsDefault, reqArg: plugins.http.IncomingMessage) => {
|
|
|
|
|
console.log(`got connection for wsc for https://${reqArg.headers.host}${reqArg.url}`);
|
|
|
|
|
let wscConnectedDeferred: plugins.smartpromise.Deferred<plugins.wsDefault>;
|
|
|
|
|
const wsServer = new plugins.ws.WebSocketServer({ server: this.httpsServer });
|
|
|
|
|
wsServer.on('connection', async (wsIncoming: plugins.wsDefault, reqArg: plugins.http.IncomingMessage) => {
|
|
|
|
|
console.log(`wss: got connection for wsc for https://${reqArg.headers.host}${reqArg.url}`);
|
|
|
|
|
|
|
|
|
|
let wsOutgoing: plugins.wsDefault;
|
|
|
|
|
try {
|
|
|
|
|
wsOutgoing = new plugins.wsDefault(`ws://${this.router.routeReq(reqArg).destinationIp}:${this.router.routeReq(reqArg).destinationPort}${reqArg.url}`);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
wsIncoming.terminate();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ws.on('message', async (message) => {
|
|
|
|
|
if (!wscConnectedDeferred) {
|
|
|
|
|
wscConnectedDeferred = plugins.smartpromise.defer();
|
|
|
|
|
let wsc;
|
|
|
|
|
try {
|
|
|
|
|
wsc = new plugins.wsDefault(`wss://${this.router.routeReq(reqArg).destinationIp}:${this.router.routeReq(reqArg).destinationPort}${reqArg.url}`);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
ws.terminate();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wsc.on('open', () => {
|
|
|
|
|
wscConnectedDeferred.resolve(wsc);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
const wsc = await wscConnectedDeferred.promise;
|
|
|
|
|
wsc.emit('message', message);
|
|
|
|
|
wsc.on('message', (message) => {
|
|
|
|
|
ws.emit('message', message);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// handle closing
|
|
|
|
|
const cleanUp = () => {
|
|
|
|
|
ws.terminate();
|
|
|
|
|
wsc.terminate();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ws.on('close', (message) => {
|
|
|
|
|
cleanUp();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
wsc.on('close', (message) => {
|
|
|
|
|
cleanUp();
|
|
|
|
|
});
|
|
|
|
|
wsIncoming.on("message", (message, isBinary) => {
|
|
|
|
|
console.log("client to upstream", message);
|
|
|
|
|
wsOutgoing.send(message, { binary: isBinary });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
wsOutgoing.on("message", (message, isBinary) => {
|
|
|
|
|
console.log("upstream to client", message);
|
|
|
|
|
wsIncoming.send(message, { binary: isBinary });
|
|
|
|
|
});
|
|
|
|
|
wsIncoming.on("error", () => wsOutgoing.terminate());
|
|
|
|
|
wsOutgoing.on("error", () => wsIncoming.terminate());
|
|
|
|
|
wsIncoming.on("close", () => wsOutgoing.terminate());
|
|
|
|
|
wsOutgoing.on("close", () => wsIncoming.terminate());
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
this.httpsServer.keepAliveTimeout = 600 * 1000;
|
|
|
|
|
this.httpsServer.headersTimeout = 600 * 1000;
|
|
|
|
|