forked from DreamLab-AI/origin-logseq-AR
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathhttps-proxy.js
More file actions
executable file
·66 lines (55 loc) · 2.04 KB
/
https-proxy.js
File metadata and controls
executable file
·66 lines (55 loc) · 2.04 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
#!/usr/bin/env node
/**
* HTTPS to HTTP Bridge Proxy
* Bridges https://localhost:3001 -> http://<HOST_IP>:3001
* Solves cross-origin security issues for local development
*/
const https = require('https');
const http = require('http');
const fs = require('fs');
const path = require('path');
const HOST_IP = process.env.HOST_IP || '192.168.0.51';
const HTTPS_PORT = parseInt(process.env.HTTPS_PORT || '3001', 10);
const TARGET_PORT = parseInt(process.env.TARGET_PORT || '3001', 10);
const certDir = process.env.CERT_DIR || __dirname;
const keyPath = path.join(certDir, 'server.key');
const certPath = path.join(certDir, 'server.crt');
if (!fs.existsSync(keyPath) || !fs.existsSync(certPath)) {
console.error('Certificate files not found. Run:');
console.error(` openssl req -x509 -nodes -days 365 -newkey rsa:2048 \\`);
console.error(` -keyout ${keyPath} -out ${certPath} -subj "/CN=localhost"`);
process.exit(1);
}
const options = {
key: fs.readFileSync(keyPath),
cert: fs.readFileSync(certPath)
};
const server = https.createServer(options, (req, res) => {
const proxyOptions = {
hostname: HOST_IP,
port: TARGET_PORT,
path: req.url,
method: req.method,
headers: {
...req.headers,
host: `${HOST_IP}:${TARGET_PORT}`
}
};
const proxyReq = http.request(proxyOptions, (proxyRes) => {
// Add CORS headers for browser compatibility
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res);
});
proxyReq.on('error', (err) => {
console.error(`Proxy error: ${err.message}`);
res.writeHead(502, { 'Content-Type': 'text/plain' });
res.end(`Bad Gateway: ${err.message}`);
});
req.pipe(proxyReq);
});
server.listen(HTTPS_PORT, '0.0.0.0', () => {
console.log(`HTTPS Bridge running: https://localhost:${HTTPS_PORT} -> http://${HOST_IP}:${TARGET_PORT}`);
});