-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps-proxy.js
More file actions
71 lines (63 loc) · 2.13 KB
/
https-proxy.js
File metadata and controls
71 lines (63 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const fs = require('fs');
const https = require('https');
const http = require('http');
const { exec } = require('child_process');
// Generate self-signed cert if not exists
if (!fs.existsSync('key.pem') || !fs.existsSync('cert.pem')) {
console.log('Generating self-signed certificate...');
exec('openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/CN=localhost"', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
startProxy();
});
} else {
startProxy();
}
function startProxy() {
const options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
// Proxy for Web (8443 -> 8083)
https.createServer(options, (req, res) => {
const proxyReq = http.request({
hostname: 'localhost',
port: 8083,
path: req.url,
method: req.method,
headers: req.headers
}, (proxyRes) => {
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res);
});
req.pipe(proxyReq);
proxyReq.on('error', (e) => {
console.error(`Web proxy error: ${e.message}`);
res.end();
});
}).listen(8443, '0.0.0.0', () => {
console.log('HTTPS Web Proxy listening on https://0.0.0.0:8443 (proxies to :8083)');
});
// Proxy for API (3443 -> 3005)
https.createServer(options, (req, res) => {
const proxyReq = http.request({
hostname: 'localhost',
port: 3005,
path: req.url,
method: req.method,
headers: req.headers
}, (proxyRes) => {
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res);
});
req.pipe(proxyReq);
proxyReq.on('error', (e) => {
console.error(`API proxy error: ${e.message}`);
res.end();
});
}).listen(3443, '0.0.0.0', () => {
console.log('HTTPS API Proxy listening on https://0.0.0.0:3443 (proxies to :3005)');
});
}