48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
const https = require('https');
|
|
const httpProxy = require('http-proxy');
|
|
const fs = require('fs');
|
|
|
|
const certPath = process.argv[2];
|
|
const keyPath = process.argv[3];
|
|
const targetHost = process.argv[4] || 'localhost';
|
|
const targetPort = parseInt(process.argv[5] || '3000', 10);
|
|
const PORT = parseInt(process.argv[6] || '3443', 10);
|
|
const HOST = process.argv[7] || '0.0.0.0';
|
|
|
|
const proxy = httpProxy.createProxyServer({
|
|
target: { host: targetHost, port: targetPort, protocol: 'http:' },
|
|
ws: true,
|
|
changeOrigin: true,
|
|
proxyTimeout: 60000,
|
|
timeout: 60000,
|
|
});
|
|
|
|
proxy.on('error', (err, req, res) => {
|
|
if (res && res.writeHead) {
|
|
try { res.writeHead(502).end(); } catch {}
|
|
}
|
|
});
|
|
|
|
const server = https.createServer({
|
|
cert: fs.readFileSync(certPath),
|
|
key: fs.readFileSync(keyPath),
|
|
}, (req, res) => {
|
|
proxy.web(req, res);
|
|
});
|
|
|
|
server.on('upgrade', (req, socket, head) => {
|
|
proxy.ws(req, socket, head);
|
|
});
|
|
|
|
server.listen(PORT, HOST, () => {
|
|
const ifaces = require('os').networkInterfaces();
|
|
let ip = 'localhost';
|
|
for (const name of Object.keys(ifaces)) {
|
|
for (const iface of ifaces[name]) {
|
|
if (iface.family === 'IPv4' && !iface.internal) { ip = iface.address; break; }
|
|
}
|
|
if (ip !== 'localhost') break;
|
|
}
|
|
console.log(`HTTPS proxy: https://${ip}:${PORT}`);
|
|
});
|