-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy.js
74 lines (67 loc) · 1.44 KB
/
proxy.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const http = require('http');
const Router = require('../lib');
const {
SPACE_NAME,
GROUP_NAME,
ENV_NAME,
NOHOST_RULE,
NOHOST_VALUE,
writeHead,
writeError,
// CLIENT_ID,
} = Router;
// router 会自动去重
const servers = [
{
host: '127.0.0.1',
port: 8080,
},
{
host: '127.0.0.1',
port: 8080,
},
];
const router = new Router(servers);
const getOptions = (req) => {
const { headers } = req;
const spaceName = 'imweb';
let groupName;
let envName;
let clientId;
if (headers.host === 'km.oa2.com') {
groupName = 'avenwu';
envName = '测试'; // 可选
} else if (req.headers.host !== 'km.oa.com') {
groupName = 'avenwu2';
envName = '测试2'; // 可选
if (req.headers.host === 'ke.qq.com') {
clientId = 'test';
}
}
return {
rules: 'file://{test.html} km.oa2.com www.test2.com',
values: { 'test.html': 'hell world.' },
spaceName,
groupName,
envName,
clientId,
// callback: console.log, // 可选
};
};
const server = http.createServer(async (req, res) => {
try {
const svrRes = await router.proxy(req, res, getOptions(req));
writeHead(res, svrRes);
svrRes.pipe(res);
} catch (err) {
writeError(res, err);
}
});
const handleSocket = async (req, socket) => {
router.proxy(req, socket, getOptions(req));
};
// TCP 请求
server.on('connect', handleSocket);
// WebSocket 请求
server.on('upgrade', handleSocket);
server.listen(5566);