|
|
|
@ -129,16 +129,20 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
|
|
|
|
originResponse.writeHead(statusArg, messageArg);
|
|
|
|
|
originResponse.end(messageArg);
|
|
|
|
|
if (originRequest.socket !== originResponse.socket) {
|
|
|
|
|
console.log('hey, something is strange.')
|
|
|
|
|
console.log('hey, something is strange.');
|
|
|
|
|
}
|
|
|
|
|
originResponse.destroy();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
console.log(`got request: ${originRequest.headers.host}${plugins.url.parse(originRequest.url).path}`);
|
|
|
|
|
console.log(
|
|
|
|
|
`got request: ${originRequest.headers.host}${plugins.url.parse(originRequest.url).path}`
|
|
|
|
|
);
|
|
|
|
|
const destinationConfig = this.router.routeReq(originRequest);
|
|
|
|
|
|
|
|
|
|
if (!destinationConfig) {
|
|
|
|
|
console.log(`${originRequest.headers.host} can't be routed properly. Terminating request.`);
|
|
|
|
|
console.log(
|
|
|
|
|
`${originRequest.headers.host} can't be routed properly. Terminating request.`
|
|
|
|
|
);
|
|
|
|
|
endOriginReqRes();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -189,7 +193,7 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
|
|
|
|
headers: {
|
|
|
|
|
...originRequest.headers,
|
|
|
|
|
'X-Forwarded-Host': originRequest.headers.host,
|
|
|
|
|
'X-Forwarded-Proto': 'https'
|
|
|
|
|
'X-Forwarded-Proto': 'https',
|
|
|
|
|
},
|
|
|
|
|
keepAlive: true,
|
|
|
|
|
},
|
|
|
|
@ -213,7 +217,7 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
|
|
|
|
});
|
|
|
|
|
proxyRequest.on('error', () => {
|
|
|
|
|
endOriginReqRes();
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
originResponse.statusCode = proxyResponse.statusCode;
|
|
|
|
@ -238,58 +242,65 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
|
|
|
|
});
|
|
|
|
|
proxyResponse.on('timeout', () => {
|
|
|
|
|
originResponse.end();
|
|
|
|
|
originResponse.destroy()
|
|
|
|
|
originResponse.destroy();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Enable websockets
|
|
|
|
|
const wsServer = new plugins.ws.WebSocketServer({ server: this.httpsServer });
|
|
|
|
|
wsServer.on('connection', async (wsIncoming: plugins.wsDefault, reqArg: plugins.http.IncomingMessage) => {
|
|
|
|
|
console.log(`wss proxy: got connection for wsc for https://${reqArg.headers.host}${reqArg.url}`);
|
|
|
|
|
|
|
|
|
|
let wsOutgoing: 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 proxy: got connection for wsc for https://${reqArg.headers.host}${reqArg.url}`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const outGoingDeferred = plugins.smartpromise.defer();
|
|
|
|
|
let wsOutgoing: plugins.wsDefault;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
wsOutgoing = new plugins.wsDefault(`ws://${this.router.routeReq(reqArg).destinationIp}:${this.router.routeReq(reqArg).destinationPort}${reqArg.url}`);
|
|
|
|
|
console.log('wss proxy: initiated outgoing proxy');
|
|
|
|
|
wsOutgoing.on('open', async () => {
|
|
|
|
|
outGoingDeferred.resolve();
|
|
|
|
|
})
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
wsIncoming.terminate();
|
|
|
|
|
return;
|
|
|
|
|
const outGoingDeferred = plugins.smartpromise.defer();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
wsOutgoing = new plugins.wsDefault(
|
|
|
|
|
`ws://${this.router.routeReq(reqArg).destinationIp}:${
|
|
|
|
|
this.router.routeReq(reqArg).destinationPort
|
|
|
|
|
}${reqArg.url}`
|
|
|
|
|
);
|
|
|
|
|
console.log('wss proxy: initiated outgoing proxy');
|
|
|
|
|
wsOutgoing.on('open', async () => {
|
|
|
|
|
outGoingDeferred.resolve();
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
wsIncoming.terminate();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wsIncoming.on('message', async (message, isBinary) => {
|
|
|
|
|
await outGoingDeferred.promise;
|
|
|
|
|
// console.log("client to upstream", message);
|
|
|
|
|
wsOutgoing.send(message, { binary: isBinary });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
wsOutgoing.on('message', async (message, isBinary) => {
|
|
|
|
|
// console.log("upstream to client", message);
|
|
|
|
|
wsIncoming.send(message, { binary: isBinary });
|
|
|
|
|
});
|
|
|
|
|
const terminateWsOutgoing = () => {
|
|
|
|
|
wsOutgoing.terminate();
|
|
|
|
|
console.log('terminated outgoing ws.');
|
|
|
|
|
};
|
|
|
|
|
wsIncoming.on('error', () => terminateWsOutgoing());
|
|
|
|
|
wsIncoming.on('close', () => terminateWsOutgoing());
|
|
|
|
|
|
|
|
|
|
const terminateWsIncoming = () => {
|
|
|
|
|
wsIncoming.terminate();
|
|
|
|
|
console.log('terminated incoming ws.');
|
|
|
|
|
};
|
|
|
|
|
wsOutgoing.on('error', () => terminateWsIncoming());
|
|
|
|
|
wsOutgoing.on('close', () => terminateWsIncoming());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wsIncoming.on("message", async (message, isBinary) => {
|
|
|
|
|
await outGoingDeferred.promise;
|
|
|
|
|
// console.log("client to upstream", message);
|
|
|
|
|
wsOutgoing.send(message, { binary: isBinary });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
wsOutgoing.on("message", async (message, isBinary) => {
|
|
|
|
|
// console.log("upstream to client", message);
|
|
|
|
|
wsIncoming.send(message, { binary: isBinary });
|
|
|
|
|
});
|
|
|
|
|
const terminateWsOutgoing = () => {
|
|
|
|
|
wsOutgoing.terminate();
|
|
|
|
|
console.log('terminated outgoing ws.');
|
|
|
|
|
}
|
|
|
|
|
wsIncoming.on("error", () => terminateWsOutgoing());
|
|
|
|
|
wsIncoming.on("close", () => terminateWsOutgoing());
|
|
|
|
|
|
|
|
|
|
const terminateWsIncoming = () => {
|
|
|
|
|
wsIncoming.terminate();
|
|
|
|
|
console.log('terminated incoming ws.');
|
|
|
|
|
}
|
|
|
|
|
wsOutgoing.on("error", () => terminateWsIncoming());
|
|
|
|
|
wsOutgoing.on("close", () => terminateWsIncoming());
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
);
|
|
|
|
|
this.httpsServer.keepAliveTimeout = 600 * 1000;
|
|
|
|
|
this.httpsServer.headersTimeout = 600 * 1000;
|
|
|
|
|
|
|
|
|
@ -314,7 +325,7 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
|
|
|
|
});
|
|
|
|
|
connection.on('timeout', () => {
|
|
|
|
|
cleanupConnection();
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.httpsServer.listen(this.options.port);
|
|
|
|
|