-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodeInfo.js
100 lines (97 loc) · 2.53 KB
/
nodeInfo.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
99
100
async function operator(proxies = [], targetPlatform, env) {
let args = $arguments || {}
const {
parseFlowHeaders,
getFlowHeaders,
flowTransfer,
getRmainingDays
} = flowUtils
const sub = env.source[proxies[0].subName]
let subInfo
if (sub.source === 'local' && !['localFirst', 'remoteFirst'].includes(sub.mergeSources)) {
if (sub.subUserinfo) {
subInfo = sub.subUserinfo
}
} else {
let url = `${sub.url}`
.split(/[\r\n]+/)
.map(i => i.trim())
.filter(i => i.length)?.[0]
let urlArgs = {}
const rawArgs = url.split('#')
url = url.split('#')[0]
if (rawArgs.length > 1) {
try {
// 支持 `#${encodeURIComponent(JSON.stringify({arg1: "1"}))}`
urlArgs = JSON.parse(decodeURIComponent(rawArgs[1]))
} catch (e) {
for (const pair of rawArgs[1].split('&')) {
const key = pair.split('=')[0]
const value = pair.split('=')[1]
// 部分兼容之前的逻辑 const value = pair.split('=')[1] || true;
urlArgs[key] = value == null || value === '' ? true : decodeURIComponent(value)
}
}
}
args = {
...urlArgs,
...args
}
if (!args.noFlow) {
if (sub.subUserinfo) {
subInfo = sub.subUserinfo
} else {
subInfo = await getFlowHeaders(url)
}
}
}
if (subInfo) {
let {
expires,
total,
usage: {
upload,
download
},
} = parseFlowHeaders(subInfo)
if (args.hideExpire) {
expires = undefined
}
const date = expires ? new Date(expires * 1000).toLocaleDateString() : ''
let remainingDays
try {
remainingDays = getRmainingDays({
resetDay: args.resetDay,
startDate: args.startDate,
cycleDays: args.cycleDays,
})
} catch (e) {}
let show = upload + download
if (args.showRemaining) {
show = total - show
}
const showT = flowTransfer(Math.abs(show))
showT.value = show < 0 ? '-' + showT.value : showT.value
const totalT = flowTransfer(total)
let name = `${showT.value}${showT.unit}|${totalT.value}${totalT.unit}`
if (remainingDays) {
name = `${name} | ${remainingDays} 天`
}
if (date) {
name = `${name} | ${date}`
}
// 获取 proxies 的最后一项
const node = proxies[proxies.length - 1] || {
type: 'ss',
server: '1.0.0.1',
port: 80,
cipher: 'aes-128-gcm',
password: 'password',
}
proxies.unshift({
...node,
name,
})
}
return proxies
}