-
Notifications
You must be signed in to change notification settings - Fork 308
/
index.js
98 lines (90 loc) · 2.61 KB
/
index.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const { CONFIG, CONFIG_Port } = require('./config')
const webstser = require('./webmodule')
webstser()
const aProxyOptions = {
port: CONFIG_Port.proxy,
rule: require('./script/rule.js'),
webInterface: {
enable: true, // 是否打开代理请求查看端口
webPort: CONFIG_Port.webif
},
wsIntercept: Boolean(CONFIG.anyproxy.wsIntercept)
}
const { eproxy, wsSer, logger, message, checkupdate } = require('./utils')
const clog = new logger({ head: 'elecV2P', level: 'debug' })
// anyproxy 临时设置
CONFIG_Port.anyproxy = { ...CONFIG.anyproxy }
if (process.env.PROXYEN) {
CONFIG_Port.anyproxy.enable = true
}
let eProxy = null
if (CONFIG_Port.anyproxy.enable === false) {
clog.info('anyproxy not enabled yet')
} else {
eProxy = new eproxy(aProxyOptions)
eProxy.start()
}
// websocket 快速打开/关闭 anyproxy
wsSer.recv.eproxy = (op = '')=>{
if (typeof op === 'object') {
if (op.port > 0) {
CONFIG_Port.anyproxy.port = op.port
aProxyOptions.port = op.port
}
if (op.webPort > 0) {
CONFIG_Port.anyproxy.webPort = op.webPort
aProxyOptions.webInterface.webPort = op.webPort
}
op = op.op || (op.enable ? 'start' : 'close')
}
switch(op) {
case 'new':
case 'start':
case 'enable':
if (eProxy === null) {
eProxy = new eproxy(aProxyOptions)
eProxy.start()
message.success(`anyproxy started on port ${CONFIG_Port.anyproxy.port}`)
} else {
clog.info('anyproxy already started')
message.error('anyproxy already started')
}
CONFIG_Port.anyproxy.enable = true
break
case 'delete':
case 'close':
case 'disable':
if (eProxy) {
eProxy.close()
message.success('anyproxy closed')
eProxy = null
} else {
clog.info('anyproxy already closed')
message.error('anyproxy already closed')
}
CONFIG_Port.anyproxy.enable = false
break
case 'restart':
if (eProxy) {
eProxy.close()
eProxy = null
}
eProxy = new eproxy(aProxyOptions)
eProxy.start()
message.success(`anyproxy restart on port ${CONFIG_Port.anyproxy.port}`)
CONFIG_Port.anyproxy.enable = true
break
default:{
clog.error('unknow operation on anyproxy:', op)
message.error('unknow operation on anyproxy')
}
}
}
// checkupdate
if (CONFIG.init?.checkupdate !== false) {
checkupdate().then(body=>{
if (body.updateversion) {
clog.notify(`elecV2P v${body.updateversion} is available`)
}
})
}