-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathSpeedSwitcher.js
More file actions
373 lines (308 loc) · 8.51 KB
/
SpeedSwitcher.js
File metadata and controls
373 lines (308 loc) · 8.51 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
const config = getBoxjs();
const body = {
mock_type: "generic",
timeout: config.TIMEOUT ?? 380,
script_text: `(${main.toString()})('${JSON.stringify(config)}')`,
};
const opts = [
"POST",
`/v1/scripting/evaluate`,
body,
]
monitorDownloadSpeed(opts);
function main(config) {
// 读取上次更新时间的缓存信息
const CACHE = JSON.parse($persistentStore.read("last_update_time") || "{}");
// 设定一些默认基础配置已供调试
const {
// 监控的服务器主机地址
HOST = "iosapps.itunes.apple.com",
// 设置监测的最小速度要求(单位MB/S)
MINSPEED = 12,
// 设置监控时间(单位秒
TOTALTIME = 15,
// 设置查询间隔(单位秒)
INTERVAL = 1,
// 设置更新时间 (单位自定义: s ms min h)
INITTIEME = tomilli("30min"),
// 设置监测的策略组名称
GROUP = "Download",
//debug开关
isDebug = true,
} = new Proxy(JSON.parse(config),{
get(...args){
const value = Reflect.get(...args)
if (args[1] === "INITTIME") {
return tomilli(value)
}
if(args[1] === "GROUP" && !value) {
throw new Error("未填写策略组")
}
return value;
}
})
// 创建一个调试器实例,带有debug开关
const DEBUG = LogLevelPrefixes(isDebug,"debug");
//初始化DECISIONS和GROUPS变量
let {
DECISIONS, //当前策略
GROUPS, //当前策略组
} = extractGroupDecisions();
(async () => {
CACHE[HOST] ||= {};
if (CACHE[HOST].switch) return $done();
CACHE[HOST].switch = 1;
$persistentStore.write(JSON.stringify(CACHE), "last_update_time");
init();
await setIntervalTaskRunner();
})()
.catch(handleError)
.finally(() => $done());
function setIntervalTaskRunner() {
const totalTimeMs = TOTALTIME * 1000;
const intervalMs = INTERVAL * 1000;
let elapsed = 0;
return new Promise((_, reject) => {
const intervalId = setInterval(async () => {
const { isConnected, speed } = await getSpeed();
DEBUG.json({
连通性: isConnected,
当前策略: DECISIONS,
});
DEBUG.func(() => "速度: " + speed_unit(speed));
try { SpeedMeasurements().add(speed);
if (elapsed >= totalTimeMs) {
throw "速度未达标,切换策略";
}
elapsed += intervalMs;
if (!isConnected) {
await getNetworkOk();
}
if (speed >= MINSPEED * 1048576) {
throw "速度达标,结束脚本";
}
} catch (e) {
clearInterval(intervalId);
mixSpeed(SpeedMeasurements().averageSpeed);
reject(e);
}
}, intervalMs);
});
}
function init() {
const isRestarted = () => {
if (CACHE.isRestarted) {
CACHE.isRestarted = false;
return true;
}
}
const oldTime = CACHE.lastRouteSwitchTime ?? 0;
if (isRestarted() || Date.now() - oldTime > INITTIEME) {
DECISIONS = GROUPS[0];
$surge.setSelectGroupPolicy(GROUP, DECISIONS);
CACHE[HOST] = {};
return;
}
if (CACHE[HOST]?.max?.max_end) {
throw "已是最优策略,结束脚本";
}
}
function handleError(err) {
const msg = err.toString();
DEBUG(msg);
DEBUG.json(CACHE);
if (msg.includes("切换策略")) {
shouldSwitchStrategy();
}
CACHE[HOST].switch = 0;
$persistentStore.write(JSON.stringify(CACHE), "last_update_time");
}
function shouldSwitchStrategy() {
if (DECISIONS === GROUPS.at(-1)) {
CACHE[HOST].max.max_end = true;
const { max_policy } = CACHE[HOST].max;
if (max_policy === DECISIONS) return;
$surge.setSelectGroupPolicy(GROUP, max_policy);
} else {
$surge.setSelectGroupPolicy(
GROUP,
GROUPS[GROUPS.indexOf(DECISIONS) + 1]
);
}
CACHE.lastRouteSwitchTime = Date.now();
$notification.post(
`🎉 策略切换成功 监控时间${TOTALTIME}秒`,
`当前速度 ➟ ${speed_unit(
SpeedMeasurements().arr.at(-1)
)} ➟ ${MINSPEED} MB/S`,
`${HOST}平均 下载速度低余${MINSPEED} MB/S 已自动切换至${
extractGroupDecisions().DECISIONS
}策略`,
{ "auto-dismiss": 60 }
);
}
async function getNetworkOk() {
const { requests: data } = await httpAPI("v1/requests/recent");
const msg = data.find(({ URL }) => URL.startsWith(HOST));
if (msg?.remark === "Connection refused") {
throw "节点链接失败,切换策略";
}
throw "连接已断开,结束脚本";
}
async function getSpeed() {
const { requests: data } = await httpAPI("v1/requests/active");
let isConnected = false;
const speed = data.reduce((acc, obj) => {
if (obj.URL.startsWith(HOST)) {
isConnected = true;
acc += obj.inCurrentSpeed;
}
return acc;
}, 0);
return { isConnected, speed };
}
function extractGroupDecisions() {
const { groups, decisions } = $surge.selectGroupDetails();
return {
DECISIONS: decisions[GROUP],
GROUPS: groups[GROUP],
};
}
function speed_unit(speed) {
for (units of ["B/S", "KB/S", "MB/S", "GB/S", "TB/S"]) {
if (speed < 1000 || !(speed = parseFloat(speed / 1024)))
return `${(+speed).toFixed(2)} ${units}`;
}
}
function tomilli(String) {
if (!String) return void 0;
const obj = {
ms: 1,
s: 1000,
min: 60 * 1000,
h: 60 * 60 * 1000,
};
const [, num, unit] = String.match(/([\d\.]+)(ms|s|min|h)/);
return num * obj[unit];
}
function mixSpeed(speed) {
const max = CACHE[HOST]?.max || {};
max.max_speed ??= 0;
if (speed > max.max_speed)
CACHE[HOST].max = {
max_speed: speed,
max_policy: DECISIONS,
max_end: false,
};
}
function SpeedMeasurements() {
SpeedMeasurements.obj ||= {
arr: [],
add(num){
this.arr.push(num)
},
get averageSpeed() {
const filteredArr = this.arr.filter(
(element) => !isNaN(element)
);
return filteredArr.length
? filteredArr.reduce((acc, val) => acc + val) /
filteredArr.length
: 0;
},
};
return SpeedMeasurements.obj;
}
function LogLevelPrefixes(isEnabled, prefix) {
prefix = prefix ? `[${prefix.toUpperCase()}] ` : "";
const generateLogFunctions = isEnabled
? (formatter) => (...args) => console.log(prefix + args.map(formatter).join(" "))
: () => () => {};
const loggingFunctions = {
text: generateLogFunctions((arg) => arg ?? String(arg)),
json: generateLogFunctions((arg) => JSON.stringify(arg, null, 4)),
func: generateLogFunctions((fn) => fn()),
};
const enhancedLogMethod = (...msg) => loggingFunctions.text(...msg);
Object.assign(enhancedLogMethod, loggingFunctions);
return enhancedLogMethod;
}
function httpAPI(path, method = "GET", body = null) {
return new Promise((resolve) => {
$httpAPI(method, path, body, (result) => {
resolve(result);
});
});
}
}
function monitorDownloadSpeed(opts) {
// 读取上次更新时间的缓存信息
let CACHE = JSON.parse($persistentStore.read("last_update_time") || "{}");
CACHE[config.HOST] ||= {};
$httpAPI("GET", "/v1/events", null, ({ events }) => {
const { date: startTime } = events.at(-1);
const oldTime = CACHE.startTime;
if (!oldTime) {
saveUpdateTime(startTime);
} else if (oldTime !== startTime) {
CACHE = { isRestarted: true };
saveUpdateTime(startTime);
}
if (!CACHE[config.HOST]?.switch) {
//利用HTTP API运行
$httpAPI(...opts);
}
return $done({ matched: true });
});
//更新时间并保存
function saveUpdateTime(startTime) {
CACHE.startTime = startTime; $persistentStore.write(JSON.stringify(CACHE), "last_update_time");
}
}
function getBoxjs() {
const HOST = this.$request && $request.hostname;
if (!HOST) return {};
//获取本地存储
const {default_config,configList} = JSON.parse($persistentStore.read("Xiao_download") || "{}");
const result = filterObjectProperties(
default_config,
parseQueryString(configList),
)
return {
...result[0],
...(result[1][HOST] ?? {}),
...{HOST},
}
}
function filterObjectProperties(...objs) {
return objs.map(obj => {
if (!obj) return {};
const filteredObj = {};
Object.keys(obj).forEach(key => {
const value = obj[key];
if (value !== "" && value !== null && value !== void 0) {
filteredObj[key] = value;
}
});
return filteredObj;
});
}
function parseQueryString(querys) {
if (!querys) return;
const newObj = {};
querys.split("\n").forEach(query => {
const arrAy = query.split("&");
const index = arrAy.findIndex(q => q.includes("HOST="));
if (index === -1) return;
const host = arrAy.splice(index, 1)[0].split("=")[1];
const result = arrAy.reduce((acc, curr) => {
const [k, v] = curr.split("=");
acc[host][k] = v;
return acc;
}, {[host]:{}});
Object.assign(newObj, result);
});
return Object.keys(newObj).length
? newObj
: null;
}