forked from DreamLab-AI/VisionClaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps-proxy.js
More file actions
151 lines (131 loc) · 4.65 KB
/
https-proxy.js
File metadata and controls
151 lines (131 loc) · 4.65 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env node
/**
* HTTPS to HTTP Bridge Proxy
* Bridges https://localhost:<HTTPS_PORT> -> http://<HOST_IP>:<TARGET_PORT>
* Solves cross-origin security issues for local development
*
* Environment Variables:
* HOST_IP - Target host IP (default: gateway IP or 192.168.0.51)
* HTTPS_PORT - Local HTTPS port to listen on (default: 3001)
* TARGET_PORT - Remote HTTP port to proxy to (default: 3001)
* CERT_DIR - Directory containing server.key and server.crt
*/
const https = require('https');
const http = require('http');
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
// Try to detect gateway IP if HOST_IP not set
function detectGatewayIP() {
try {
const result = execSync("ip route | grep default | awk '{print $3}'", { encoding: 'utf8' });
return result.trim() || '192.168.0.51';
} catch {
return '192.168.0.51';
}
}
const HOST_IP = process.env.HOST_IP || detectGatewayIP();
const HTTPS_PORT = parseInt(process.env.HTTPS_PORT || '3001', 10);
const TARGET_PORT = parseInt(process.env.TARGET_PORT || '3001', 10);
const CERT_DIR = process.env.CERT_DIR || __dirname;
const keyPath = path.join(CERT_DIR, 'server.key');
const certPath = path.join(CERT_DIR, 'server.crt');
// Generate self-signed certificate if not exists
function ensureCertificates() {
if (!fs.existsSync(keyPath) || !fs.existsSync(certPath)) {
console.log('Generating self-signed certificate...');
try {
execSync(`openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout "${keyPath}" -out "${certPath}" -subj "/CN=localhost"`, {
stdio: 'inherit'
});
console.log('Certificate generated successfully');
} catch (err) {
console.error('Failed to generate certificate:', err.message);
process.exit(1);
}
}
}
ensureCertificates();
const options = {
key: fs.readFileSync(keyPath),
cert: fs.readFileSync(certPath)
};
const server = https.createServer(options, (req, res) => {
const startTime = Date.now();
const proxyOptions = {
hostname: HOST_IP,
port: TARGET_PORT,
path: req.url,
method: req.method,
headers: {
...req.headers,
host: `${HOST_IP}:${TARGET_PORT}`,
'x-forwarded-proto': 'https',
'x-forwarded-host': `localhost:${HTTPS_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, PATCH, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
res.setHeader('Access-Control-Allow-Credentials', 'true');
// Copy response headers (except CORS-related which we override)
Object.keys(proxyRes.headers).forEach(key => {
if (!key.toLowerCase().startsWith('access-control-')) {
res.setHeader(key, proxyRes.headers[key]);
}
});
res.writeHead(proxyRes.statusCode);
proxyRes.pipe(res);
proxyRes.on('end', () => {
const duration = Date.now() - startTime;
console.log(`${req.method} ${req.url} -> ${proxyRes.statusCode} (${duration}ms)`);
});
});
proxyReq.on('error', (err) => {
console.error(`Proxy error for ${req.url}: ${err.message}`);
res.writeHead(502, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
error: 'Bad Gateway',
message: err.message,
target: `http://${HOST_IP}:${TARGET_PORT}`
}));
});
// Handle preflight OPTIONS requests
if (req.method === 'OPTIONS') {
res.writeHead(204, {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, X-Requested-With',
'Access-Control-Max-Age': '86400'
});
res.end();
return;
}
req.pipe(proxyReq);
});
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.error(`Port ${HTTPS_PORT} is already in use`);
process.exit(1);
}
console.error('Server error:', err);
});
server.listen(HTTPS_PORT, '0.0.0.0', () => {
console.log('='.repeat(60));
console.log('HTTPS Bridge Proxy Started');
console.log('='.repeat(60));
console.log(` Local: https://localhost:${HTTPS_PORT}`);
console.log(` Target: http://${HOST_IP}:${TARGET_PORT}`);
console.log('='.repeat(60));
});
// Graceful shutdown
process.on('SIGTERM', () => {
console.log('Shutting down HTTPS bridge...');
server.close(() => process.exit(0));
});
process.on('SIGINT', () => {
console.log('\nShutting down HTTPS bridge...');
server.close(() => process.exit(0));
});