-
Notifications
You must be signed in to change notification settings - Fork 0
/
caiyun_vip.js
633 lines (604 loc) · 20.5 KB
/
caiyun_vip.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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/**
* [MITM]
* hostname = biz.caiyunapp.com
*
* [Script]
* 彩云天气_SVIP = type=http-response,requires-body=1,max-size=0,pattern=https?:\/\/biz\.caiyunapp\.com\/(membership_rights|v2\/user),script-path=https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/script/caiyun/caiyun_svip.js
*
*/
const SCRIPT_NAME = '彩云天气';
const USER_REGEX = /https?:\/\/biz\.caiyunapp\.com\/v2\/user/;
const RIGHTS_REGEX = /https?:\/\/biz\.caiyunapp\.com\/membership_rights/;
const RESULT = {
is_vip: true,
vip_type: "s",
svip_expired_at: 1882066669.9452950954,
}
const RESULT_WT = {
vip: {
enable: true,
svip_expired_at: 1882066669.9452950954
}
}
const RIGHTS = {
"result": [
{
"name": "\u514d\u5e7f\u544a",
"enabled": true,
"vip_icon": "https://cdn.caiyunapp.com/ad/img/membership_rights/vip-ads-free.png",
"vip": true,
"svip": true,
"_id": "5ee5eb091d28d2634a2ee08f",
"svip_icon": "https://cdn.caiyunapp.com/ad/img/membership_rights/svip-ads-free.png"
},
{
"name": "\u591a\u5730\u5929\u6c14\u63a8\u9001",
"enabled": true,
"vip_icon": "https://cdn.caiyunapp.com/ad/img/membership_rights/vip-custom-push.png",
"vip": true,
"svip": true,
"_id": "5ee5eb091d28d2634a2ee090",
"svip_icon": "https://cdn.caiyunapp.com/ad/img/membership_rights/svip-custom-push.png"
},
{
"name": "\u964d\u6c34\u63d0\u9192",
"enabled": true,
"vip_icon": "https://cdn.caiyunapp.com/ad/img/membership_rights/vip-rain-notification.png",
"vip": true,
"svip": true,
"_id": "5ee5eb091d28d2634a2ee091",
"svip_icon": "https://cdn.caiyunapp.com/ad/img/membership_rights/svip-rain-notification.png"
},
{
"name": "\u536b\u661f\u4e91\u56fe",
"enabled": true,
"vip_icon": null,
"vip": false,
"svip": true,
"_id": "5ee5eb091d28d2634a2ee092",
"svip_icon": "https://cdn.caiyunapp.com/ad/img/membership_rights/svip-satellite-clouds.png"
},
{
"name": "\u4e91\u91cf",
"enabled": true,
"vip_icon": null,
"vip": false,
"svip": true,
"_id": "5ee5eb091d28d2634a2ee093",
"svip_icon": "https://cdn.caiyunapp.com/ad/img/membership_rights/svip-cloud-cover.png"
},
{
"name": "\u6c14\u6e29\u9884\u62a5",
"enabled": true,
"vip_icon": null,
"vip": false,
"svip": true,
"_id": "5ee5eb091d28d2634a2ee094",
"svip_icon": "https://cdn.caiyunapp.com/ad/img/membership_rights/svip-tmp-forecast.png"
},
{
"name": "\u9732\u70b9\u6e29\u5ea6\u9884\u62a5",
"enabled": true,
"vip_icon": null,
"vip": false,
"svip": true,
"_id": "5ee5eb091d28d2634a2ee095",
"svip_icon": "https://cdn.caiyunapp.com/ad/img/membership_rights/svip-dew-point-tmp-forecast.png"
},
{
"name": "\u77ed\u6ce2\u8f90\u5c04\u901a\u91cf",
"enabled": true,
"vip_icon": null,
"vip": false,
"svip": true,
"_id": "5ee5eb091d28d2634a2ee096",
"svip_icon": "https://cdn.caiyunapp.com/ad/img/membership_rights/svip-short-wave-radiation.png"
},
{
"name": "\u6c14\u538b",
"enabled": true,
"vip_icon": null,
"vip": false,
"svip": true,
"_id": "5ee5eb091d28d2634a2ee097",
"svip_icon": "https://cdn.caiyunapp.com/ad/img/membership_rights/svip-pressure.png"
},
{
"name": "\u80fd\u89c1\u5ea6",
"enabled": true,
"vip_icon": null,
"vip": false,
"svip": true,
"_id": "5ee5eb091d28d2634a2ee098",
"svip_icon": "https://cdn.caiyunapp.com/ad/img/membership_rights/svip-visibility.png"
},
{
"name": "\u6e7f\u5ea6\u9884\u62a5",
"enabled": true,
"vip_icon": null,
"vip": false,
"svip": true,
"_id": "5ee5eb091d28d2634a2ee099",
"svip_icon": "https://cdn.caiyunapp.com/ad/img/membership_rights/svip-humidity-forecast.png"
},
{
"name": "2\u5929\u964d\u96e8\u9884\u62a5\u56fe",
"enabled": true,
"vip_icon": null,
"vip": false,
"svip": true,
"_id": "5ee5eb091d28d2634a2ee09a",
"svip_icon": "https://cdn.caiyunapp.com/ad/img/membership_rights/svip-rain-forecast.png"
}
],
"rc": 0
}
let magicJS = MagicJS(SCRIPT_NAME);
function Main(){
if (magicJS.isResponse){
if (USER_REGEX.test(magicJS.request.url)){
try{
let obj = JSON.parse(magicJS.response.body);
Object.assign(obj['result'], RESULT)
Object.assign(obj['result']['wt'], RESULT_WT)
let body = JSON.stringify(obj);
magicJS.done({body});
}
catch(err){
magicJS.log(`解锁SVIP失败,异常信息${err}`);
magicJS.done();
}
}
if (RIGHTS_REGEX.test(magicJS.request.url)){
let body = JSON.stringify(RIGHTS);
magicJS.done({body});
}
}
}
Main();
function MagicJS(scriptName='MagicJS', logLevel='INFO'){
return new class{
constructor(){
this.scriptName = scriptName;
this.logLevel = this.getLogLevels(logLevel.toUpperCase());
this.node = {'request': undefined, 'fs': undefined, 'data': {}};
if (this.isNode){
this.node.fs = require('fs');
this.node.request = require('request');
try{
this.node.fs.accessSync('./magic.json');
}
catch(err){
this.logError(err);
this.node.fs.writeFileSync('./magic.json', '{}')
}
this.node.data = require('./magic.json');
}
if (this.isJSBox){
if (!$file.exists('drive://MagicJS')){
$file.mkdir('drive://MagicJS');
}
if (!$file.exists('drive://MagicJS/magic.json')){
$file.write({
data: $data({string: '{}'}),
path: 'drive://MagicJS/magic.json'
})
}
}
}
get version() { return 'v2.1.4' };
get isSurge() { return typeof $httpClient !== 'undefined' && !this.isLoon };
get isQuanX() { return typeof $task !== 'undefined' };
get isLoon() { return typeof $loon !== 'undefined' };
get isJSBox() { return typeof $drive !== 'undefined'};
get isNode() { return typeof module !== 'undefined' && !this.isJSBox };
get isRequest() { return (typeof $request !== 'undefined') && (typeof $response === 'undefined')}
get isResponse() { return typeof $response !== 'undefined' }
get request() { return (typeof $request !== 'undefined') ? $request : undefined }
get response() {
if (typeof $response !== 'undefined'){
if ($response.hasOwnProperty('status')) $response['statusCode'] = $response['status']
if ($response.hasOwnProperty('statusCode')) $response['status'] = $response['statusCode']
return $response;
}
else{
return undefined;
}
}
get logLevels(){
return {
DEBUG: 4,
INFO: 3,
WARNING: 2,
ERROR: 1,
CRITICAL: 0
};
}
getLogLevels(level){
try{
if (this.isNumber(level)){
return level;
}
else{
let levelNum = this.logLevels[level];
if (typeof levelNum === 'undefined'){
this.logError(`获取MagicJS日志级别错误,已强制设置为DEBUG级别。传入日志级别:${level}。`)
return this.logLevels.DEBUG;
}
else{
return levelNum;
}
}
}
catch(err){
this.logError(`获取MagicJS日志级别错误,已强制设置为DEBUG级别。传入日志级别:${level},异常信息:${err}。`)
return this.logLevels.DEBUG;
}
}
read(key, session=''){
let val = '';
// 读取原始数据
if (this.isSurge || this.isLoon) {
val = $persistentStore.read(key);
}
else if (this.isQuanX) {
val = $prefs.valueForKey(key);
}
else if (this.isNode){
val = this.node.data;
}
else if (this.isJSBox){
val = $file.read('drive://MagicJS/magic.json').string;
}
try {
// Node 和 JSBox数据处理
if (this.isNode) val = val[key]
if (this.isJSBox) val = JSON.parse(val)[key];
// 带Session的情况
if (!!session){
if(typeof val === 'string') val = JSON.parse(val);
val = !!val && typeof val === 'object' ? val[session]: null;
}
}
catch (err){
this.logError(`raise exception: ${err}`);
val = !!session? {} : null;
this.del(key);
}
if (typeof val === 'undefined') val = null;
try {if(!!val && typeof val === 'string') val = JSON.parse(val)} catch(err) {}
this.logDebug(`read data [${key}]${!!session? `[${session}]`: ''}(${typeof val})\n${JSON.stringify(val)}`);
return val;
};
write(key, val, session=''){
let data = !!session ? {} : '';
// 读取原先存储的JSON格式数据
if (!!session && (this.isSurge || this.isLoon)) {
data = $persistentStore.read(key);
}
else if (!!session && this.isQuanX) {
data = $prefs.valueForKey(key);
}
else if (this.isNode){
data = this.node.data;
}
else if (this.isJSBox){
data = JSON.parse($file.read('drive://MagicJS/magic.json').string);
}
if (!!session){
// 有Session,要求所有数据都是Object
try {
if (typeof data === 'string') data = JSON.parse(data)
data = typeof data === 'object' && !!data ? data : {};
}
catch(err){
this.logError(`raise exception: ${err}`);
this.del(key);
data = {};
};
if (this.isJSBox || this.isNode){
// 构造数据
if (!data.hasOwnProperty(key) || typeof data[key] != 'object'){
data[key] = {};
}
if (!data[key].hasOwnProperty(session)){
data[key][session] = null;
}
// 写入或删除数据
if (typeof val === 'undefined'){
delete data[key][session];
}
else{
data[key][session] = val;
}
}
else {
// 写入或删除数据
if (typeof val === 'undefined'){
delete data[session];
}
else{
data[session] = val;
}
}
}
// 没有Session时
else{
if (this.isNode || this.isJSBox){
// 删除数据
if (typeof val === 'undefined'){
delete data[key];
}
else{
data[key] = val;
}
}
else{
// 删除数据
if (typeof val === 'undefined'){
data = null;
}
else{
data = val;
}
}
}
// 数据回写
if (typeof data === 'object') data = JSON.stringify(data);
if (this.isSurge || this.isLoon) {
$persistentStore.write(data, key);
}
else if (this.isQuanX) {
$prefs.setValueForKey(data, key);
}
else if (this.isNode){
this.node.fs.writeFileSync('./magic.json', data)
}
else if (this.isJSBox){
$file.write({data: $data({string: data}), path: 'drive://MagicJS/magic.json'});
}
this.logDebug(`write data [${key}]${!!session? `[${session}]`: ''}(${typeof val})\n${JSON.stringify(val)}`);
};
del(key, session=''){
this.logDebug(`delete key [${key}]${!!session ? `[${session}]`:''}`);
this.write(key, undefined, session);
}
/**
* iOS系统通知
* @param {*} title 通知标题
* @param {*} subTitle 通知副标题
* @param {*} body 通知内容
* @param {*} options 通知选项,目前支持传入超链接或Object
* Surge不支持通知选项,Loon仅支持打开URL,QuantumultX支持打开URL和多媒体通知
* options "applestore://" 打开Apple Store
* options "https://www.apple.com.cn/" 打开Apple.com.cn
* options {'open-url': 'https://www.apple.com.cn/'} 打开Apple.com.cn
* options {'open-url': 'https://www.apple.com.cn/', 'media-url': 'https://raw.githubusercontent.com/Orz-3/mini/master/Apple.png'} 打开Apple.com.cn,显示一个苹果Logo
*/
notify(title=this.scriptName, subTitle='', body='', options=''){
let convertOptions = (_options) =>{
let newOptions = '';
if (typeof _options === 'string'){
if (this.isLoon) newOptions = _options;
else if (this.isQuanX) newOptions = {'open-url': _options};
}
else if (typeof _options === 'object'){
if (this.isLoon) newOptions = !!_options['open-url'] ? _options['open-url'] : '';
else if (this.isQuanX) newOptions = !!_options['open-url'] || !!_options['media-url'] ? _options : {};
}
return newOptions;
}
options = convertOptions(options);
// 支持单个参数通知
if (arguments.length == 1){
title = this.scriptName;
subTitle = '',
body = arguments[0];
}
if (this.isSurge){
$notification.post(title, subTitle, body);
}
else if (this.isLoon){
// 2020.08.11 Loon2.1.3(194)TF 如果不加这个log,在跑测试用例连续6次通知,会漏掉一些通知,已反馈给作者。
this.logInfo(`title: ${title}, subTitle:${subTitle}, body:${body}, options:${options}`);
if (!!options) $notification.post(title, subTitle, body, options);
else $notification.post(title, subTitle, body);
}
else if (this.isQuanX) {
$notify(title, subTitle, body, options);
}
else if (this.isNode) {
this.log(`${title} ${subTitle}\n${body}`);
}
else if (this.isJSBox){
let push = {
title: title,
body: !!subTitle ? `${subTitle}\n${body}` : body,
}
$push.schedule(push);
}
}
log(msg, level="INFO"){
if (this.logLevel >= this.getLogLevels(level.toUpperCase())) console.log(`[${level}] [${this.scriptName}]\n${msg}\n`)
}
logDebug(msg){
this.log(msg, "DEBUG");
}
logInfo(msg){
this.log(msg, "INFO");
}
logWarning(msg){
this.log(msg, "WARNING");
}
logError(msg){
this.log(msg, "ERROR");
}
get(options, callback){
let _options = typeof options === 'object'? Object.assign({}, options): options;
this.logDebug(`http get: ${JSON.stringify(_options)}`);
if (this.isSurge || this.isLoon) {
$httpClient.get(_options, callback);
}
else if (this.isQuanX) {
if (typeof _options === 'string') _options = { url: _options }
_options['method'] = 'GET'
$task.fetch(_options).then(
resp => {
resp['status'] = resp.statusCode
callback(null, resp, resp.body)
},
reason => callback(reason.error, null, null),
)
}
else if(this.isNode){
return this.node.request.get(_options, callback);
}
else if(this.isJSBox){
_options = typeof _options === 'string'? {'url': _options} :_options;
options['header'] = _options['headers'];
delete _options['headers']
_options['handler'] = (resp)=>{
let err = resp.error? JSON.stringify(resp.error) : undefined;
let data = typeof resp.data === 'object' ? JSON.stringify(resp.data) : resp.data;
callback(err, resp.response, data);
}
$http.get(_options);
}
}
post(options, callback){
let _options = typeof options === 'object'? Object.assign({}, options): options;
this.logDebug(`http post: ${JSON.stringify(_options)}`);
if (this.isSurge || this.isLoon) {
$httpClient.post(_options, callback);
}
else if (this.isQuanX) {
if (typeof _options === 'string') _options = { url: _options }
if (_options.hasOwnProperty('body') && typeof _options['body'] !== 'string') _options['body'] = JSON.stringify(_options['body']);
_options['method'] = 'POST'
$task.fetch(_options).then(
resp => {
resp['status'] = resp.statusCode
callback(null, resp, resp.body)
},
reason => {callback(reason.error, null, null)}
)
}
else if(this.isNode){
if (typeof _options.body === 'object') _options.body = JSON.stringify(_options.body);
return this.node.request.post(_options, callback);
}
else if(this.isJSBox){
_options = typeof _options === 'string'? {'url': _options} : _options;
_options['header'] = _options['headers'];
delete _options['headers']
_options['handler'] = (resp)=>{
let err = resp.error? JSON.stringify(resp.error) : undefined;
let data = typeof resp.data === 'object' ? JSON.stringify(resp.data) : resp.data;
callback(err, resp.response, data);
}
$http.post(_options);
}
}
done(value = {}){
if (typeof $done !== 'undefined'){
$done(value);
}
}
isToday(day){
if (day == null){
return false;
}
else{
let today = new Date();
if (typeof day == 'string'){
day = new Date(day);
}
if (today.getFullYear() == day.getFullYear() && today.getMonth() == day.getMonth() && today.getDay() == day.getDay()){
return true;
}
else{
return false;
}
}
}
isNumber(val) {
return parseFloat(val).toString() === "NaN"? false: true;
}
/**
* 对await执行中出现的异常进行捕获并返回,避免写过多的try catch语句
* @param {*} promise Promise 对象
* @param {*} defaultValue 出现异常时返回的默认值
* @returns 返回两个值,第一个值为异常,第二个值为执行结果
*/
attempt(promise, defaultValue=null){ return promise.then((args)=>{return [null, args]}).catch(ex=>{this.log('raise exception:' + ex); return [ex, defaultValue]})};
/**
* 重试方法
* @param {*} fn 需要重试的函数
* @param {number} [retries=5] 重试次数
* @param {number} [interval=0] 每次重试间隔
* @param {function} [callback=null] 函数没有异常时的回调,会将函数执行结果result传入callback,根据result的值进行判断,如果需要再次重试,在callback中throw一个异常,适用于函数本身没有异常但仍需重试的情况。
* @returns 返回一个Promise对象
*/
retry(fn, retries=5, interval=0, callback=null) {
return (...args)=>{
return new Promise((resolve, reject) =>{
function _retry(...args){
Promise.resolve().then(()=>fn.apply(this,args)).then(
result => {
if (typeof callback === 'function'){
Promise.resolve().then(()=>callback(result)).then(()=>{resolve(result)}).catch(ex=>{
if (retries >= 1 && interval > 0){
setTimeout(() => _retry.apply(this, args), interval);
}
else if (retries >= 1) {
_retry.apply(this, args);
}
else{
reject(ex);
}
retries --;
});
}
else{
resolve(result);
}
}
).catch(ex=>{
if (retries >= 1 && interval > 0){
setTimeout(() => _retry.apply(this, args), interval);
}
else if (retries >= 1) {
_retry.apply(this, args);
}
else{
reject(ex);
}
retries --;
})
}
_retry.apply(this, args);
});
};
}
formatTime(time, fmt="yyyy-MM-dd hh:mm:ss") {
var o = {
"M+": time.getMonth() + 1,
"d+": time.getDate(),
"h+": time.getHours(),
"m+": time.getMinutes(),
"s+": time.getSeconds(),
"q+": Math.floor((time.getMonth() + 3) / 3),
"S": time.getMilliseconds()
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (time.getFullYear() + "").substr(4 - RegExp.$1.length));
for (let k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
};
now(){
return this.formatTime(new Date(), "yyyy-MM-dd hh:mm:ss");
}
sleep(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
}(scriptName);
}