-
Notifications
You must be signed in to change notification settings - Fork 1
/
bandwagon.js
330 lines (308 loc) · 11.5 KB
/
bandwagon.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
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
/**
* 搬瓦工查询流量,需要手动填入veid 和 api_key
* 测试可用 quantumultx Loon JSBox
* [task_local]
* 30 8 * * * bandwagon.js
*/
// 修改veid和api_key
let config = {
veid: ***, // 填你的veid
api_key: "***"
};
const $tool = tool();
check_flow();
$tool.done();
// 格式化时间
function formatTime(date) {
var Y = date.getFullYear() + '-';
var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
// var D = date.getDate() + ' ';
var D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' ';
var h = date.getHours() + ':';
var m = date.getMinutes() + ':';
var s = date.getSeconds();
return Y+M+D+h+m+s;
}
// 前一个月时间
function preMonthTime(resetTime) {
var date = new Date(resetTime);
date.setMonth(date.getMonth() - 1);
return date;
}
// 当前月份天数
function monthDays(currentTime) {
var date = new Date(currentTime)
//将当前月份加1,下移到下一个月
date.setMonth(date.getMonth()+1);
date.setDate(0);
return date.getDate();
}
// 计算当前时间 间隔
function intervalTimes(startTime, endTime) {
var stime = Date.parse(startTime);
var etime = Date.parse(endTime);
// 两个时间戳相差的毫秒数
var usedTime = etime - stime;
// 计算相差的天数
var days = Math.floor(usedTime / (24 * 3600 * 1000));
// 计算天数后剩余的毫秒数
var leave1 = usedTime % (24 * 3600 * 1000);
// 计算出小时数
var hours = Math.floor(leave1 / (3600 * 1000));
// 计算小时数后剩余的毫秒数
var leave2 = leave1 % (3600 * 1000);
// 计算相差分钟数
var minutes = Math.floor(leave2 / (60 * 1000));
var time = days + "天" + hours + "时" + minutes + "分";
return time;
}
// 剩余平均每天可用流量
function averageFlow(startTime, endTime, flow) {
var stime = Date.parse(startTime);
var etime = Date.parse(endTime);
// 两个时间戳相差的毫秒数
var usedTime = etime - stime;
var dayFlow = flow / 1073741824 / usedTime * 1000 * 3600 * 24;
return dayFlow.toFixed(3)
}
function parse_flow(data) {
var currentTime = new Date();
var resetTime = new Date(data.data_next_reset * 1000);
var startTime = preMonthTime(resetTime);
var use_plan = (data.data_counter/1073741824).toFixed(2) + "/" + data.plan_monthly_data/1073741824 + "G";
var percent = (data.data_counter * 100 / data.plan_monthly_data).toFixed(2) + "%";
var ip = data.ip_addresses[0];
var reset_date = formatTime(resetTime);
var start_date = formatTime(startTime);
var days = monthDays(startTime);
var residue = ((data.plan_monthly_data - data.data_counter)/1073741824).toFixed(2) + "G"
var subTitle = "已用: "+ percent + ", " + use_plan + " 剩余: "+ residue;
var usedTimes = intervalTimes(startTime, currentTime);
var residueTimes = intervalTimes(currentTime, resetTime);
var residueFlow = averageFlow(currentTime, resetTime, data.plan_monthly_data - data.data_counter);
var usedFlow = averageFlow(startTime, currentTime, data.data_counter);
var msg1 = "已用: " + usedTimes + ", 平均每天: " + usedFlow + "GB";
var msg2 = "剩余: " + residueTimes + ", 剩余每天: " + residueFlow + "GB";
var msg3 = "本月: " + days + "天, 重置: " + reset_date;
var message = msg1 + "\n" + msg2 + "\n" + msg3;
if (Number(usedFlow) > Number(residueFlow) ){
$tool.notify("⚠️搬瓦工VPS流量超速", subTitle, message);
$tool.log(subTitle);
$tool.log(message);
$done();}
else {
/* $tool.notify("搬瓦工VPS流量正常", subTitle, message);*/
$tool.log(subTitle);
$tool.log(message);
$done(); }
}
function check_flow() {
var request = {
url: "https://api.64clouds.com/v1/getServiceInfo?veid=" + config.veid + "&api_key="+ config.api_key,
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_1_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 - mmbWebBrowse - ios"
}
};
$tool.get(request, function (error, response, body) {
// response.status response.statusCode response.headers
if (!error) {
if (typeof response.statusCode == "undefined" || response.statusCode == 200) {
parse_flow(JSON.parse(body));
}
} else {
$tool.notify("搬瓦工流量查询", "接口查询错误", error);
$tool.log(error)
}
})
}
// https://github.com/yichahucha/surge/blob/master/tool.js
// 基于上面修改,在此注明出处
function tool() {
const isSurge = typeof $httpClient != "undefined";
const isQuanX = typeof $task != "undefined";
const isJSBox = typeof $app != "undefined" && $app.info.bundleID == "app.cyan.jsbox"
const isNode = typeof require == "function" && !isJSBox;
const isResponse = typeof $response != "undefined";
const isRequest = typeof $request != "undefined";
const isRe = isRequest || isResponse;
const nodeStore = "prefs.json";
const nodeRequest = (() => {
if (isNode) {
const request = require('request');
return ({request})
} else {
return (null)
}
})();
const notify = (title, subtitle, message) => {
if (isQuanX) $notify(title, subtitle, message);
if (isSurge) $notification.post(title, subtitle, message);
if (isNode) log(JSON.stringify({title, subtitle, message}));
if (isJSBox) $push.schedule({title: title, body: subtitle + "\n" + message})
};
const write = (value, key) => {
if (isQuanX) return $prefs.setValueForKey(value, key);
if (isSurge) return $persistentStore.write(value, key);
if (isJSBox) return $prefs.set(key, value);
if (isNode) {
try {
var fs = require("fs");
if (!fs.existsSync(nodeStore)) {
fs.writeFileSync(nodeStore, JSON.stringify({}));
}
var data = JSON.parse(fs.readFileSync(nodeStore));
data[key] = value;
fs.writeFileSync(nodeStore, JSON.stringify(data))
return true;
} catch (error) {
log(error);
}
return false;
}
};
const read = (key) => {
if (isQuanX) return $prefs.valueForKey(key);
if (isSurge) return $persistentStore.read(key);
if (isJSBox) return $prefs.get(key);
if (isNode) {
try {
var fs = require("fs");
var data = JSON.parse(fs.readFileSync(nodeStore));
if (typeof data[key] != "undefined") {
return data[key];
}
} catch (error) {
log(error);
}
return "";
}
};
const adapterStatus = (response) => {
if (response) {
if (response.status) {
response["statusCode"] = response.status;
} else if (response.statusCode) {
response["status"] = response.statusCode;
}
}
return response
};
const get = (options, callback) => {
if (isQuanX) {
if (typeof options == "string") options = {url: options}
options["method"] = "GET";
$task.fetch(options).then(response => {
callback(null, adapterStatus(response), response.body)
}, reason => callback(reason.error, null, null))
}
if (isSurge) $httpClient.get(options, (error, response, body) => {
callback(error, adapterStatus(response), body)
});
if (isNode && nodeRequest) {
nodeRequest.request(options, (error, response, body) => {
callback(error, adapterStatus(response), body)
})
}
if (isJSBox) {
if (typeof options == "string") options = {url: options};
options["header"] = options["headers"];
delete options["headers"];
options["handler"] = function (resp) {
let error = resp.error;
if (error) error = JSON.stringify(resp.error)
let body = resp.data;
if (typeof body == "object") body = JSON.stringify(resp.data);
callback(error, adapterStatus(resp.response), body)
};
$http.get(options);
}
};
const post = (options, callback) => {
if (isQuanX) {
if (typeof options == "string") options = {url: options}
options["method"] = "POST";
$task.fetch(options).then(response => {
callback(null, adapterStatus(response), response.body)
}, reason => callback(reason.error, null, null))
}
if (isSurge) {
$httpClient.post(options, (error, response, body) => {
callback(error, adapterStatus(response), body)
})
}
if (isNode && nodeRequest) {
nodeRequest.request.post(options, (error, response, body) => {
callback(error, adapterStatus(response), body)
})
}
if (isJSBox) {
if (typeof options == "string") options = {url: options};
options["header"] = options["headers"];
delete options["headers"];
options["handler"] = function (resp) {
let error = resp.error;
if (error) error = JSON.stringify(resp.error)
let body = resp.data;
if (typeof body == "object") body = JSON.stringify(resp.data);
callback(error, adapterStatus(resp.response), body)
};
$http.post(options);
}
};
const log = (message) => console.log(message);
const done = (value = {}) => {
if (isQuanX) isRe ? $done(value) : "";
if (isSurge) isRe ? $done(value) : $done()
};
const timeout = (handler, timeout = 0) => {
if (typeof setTimeout != "undefined") {
return setTimeout(handler, timeout);
}
return (handler, timeout = 0)=>{return handler()};
};
const httpRequest = async (resq, delay = 0, statusCode = 200) => {
return new Promise(resolve => {
timeout(() => {
var adapterClient = get;
if (typeof resq.method != "undefined") {
if (resq.method == "POST") {
adapterClient = post
}
delete resq.method;
}
adapterClient(resq, function (error, response, body) {
try {
if (!error) {
if (typeof response.statusCode == "undefined" || response.statusCode == statusCode) {
resolve(JSON.parse(body));
}
} else {
notify('', 'httpRequest', error);
resolve("");
}
} catch (e) {
notify('', 'httpRequest catch', e);
resolve("");
}
});
}, parseInt(delay))
});
};
return {
isQuanX,
isSurge,
isJSBox,
isRequest,
isResponse,
isRe,
notify,
write,
read,
get,
post,
httpRequest,
log,
done
}
}