-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy-server.js
56 lines (44 loc) · 1.56 KB
/
proxy-server.js
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
const http = require('http');
const httpProxy = require('http-proxy');
const fs = require('fs');
const proxy = httpProxy.createProxyServer();
const https = require('https');
const options = {
key: fs.readFileSync('/etc/letsencrypt/live/james016.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/james016.com/fullchain.pem')
};
// 添加错误处理程序
proxy.on('error', (err, req, socketOrRes) => {
console.error(`Error occurred while proxying request: ${err.message}`);
if (socketOrRes.writeHead) {
socketOrRes.writeHead(502, { 'Content-Type': 'text/plain' });
socketOrRes.end('An error occurred while proxying the request.');
} else {
socketOrRes.destroy();
}
});
const server = https.createServer(options, (req, res) => {
const url = req.url;
if (url.startsWith('/socket')) {
// 将请求代理到 localhost:10002
proxy.web(req, res, { target: 'http://localhost:10002' });
} else {
// 将请求代理到 localhost:10001
proxy.web(req, res, { target: 'http://localhost:10001' });
}
});
// 代理WebSocket连接
server.on('upgrade', (req, socket, head) => {
const url = req.url;
if (url.startsWith('/socket')) {
// 将WebSocket请求代理到 localhost:10002
proxy.ws(req, socket, head, { target: 'ws://localhost:10002' });
} else {
// 将WebSocket请求代理到 localhost:10001
proxy.ws(req, socket, head, { target: 'ws://localhost:10001' });
}
});
const port = 443;
server.listen(port, () => {
console.log('Proxy server is running on http://localhost:' + port);
});