-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource-parser.js
2671 lines (2483 loc) · 126 KB
/
resource-parser.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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
☑️ 资源解析器 ©𝐒𝐡𝐚𝐰𝐧 ⟦2021-03-28 18:20⟧
----------------------------------------------------------
🛠 发现 𝐁𝐔𝐆 请反馈: @Shawn_KOP_bot
⛳️ 关注 🆃🅶 相关频道: https://t.me/QuanX_API
🗣 🆃🄷🄰🄽🄺🅂 🆃🄾 @Jamie CHIEN, @M**F**, @c0lada, @Peng-YM
🤖 主要功能:
❶ 将其它格式的服务器订阅解析成 𝐐𝐮𝐚𝐧𝐭𝐮𝐦𝐮𝐥𝐭 𝐗 格式
☑︎ 支持 𝗩𝗺𝗲𝘀𝘀/𝗦𝗦(𝗥/𝗗)/𝗛𝗧𝗧𝗣(𝗦)/𝗧𝗿𝗼𝗷𝗮𝗻/𝗤𝘂𝗮𝗻𝘁𝘂𝗺𝘂𝗹𝘁(𝗫)/𝗦𝘂𝗿𝗴𝗲/𝐂𝐥𝐚𝐬𝐡/𝐒𝐡𝐚𝐝𝐨𝐰𝐫𝐨𝐜𝐤𝐞𝐭/𝐋𝐨𝐨𝐧 格式
☑︎ 提供说明 1⃣️ 中的可选个性化参数(筛选、重命名 等)
❷ 𝗿𝗲𝘄𝗿𝗶𝘁𝗲(重写) & 𝗳𝗶𝗹𝘁𝗲𝗿(分流) 的 转换 & 筛选
☑︎ 用于禁用远程引用中某(几)项 𝗿𝗲𝘄𝗿𝗶𝘁𝗲/𝗵𝗼𝘀𝘁𝗻𝗮𝗺𝗲/𝗳𝗶𝗹𝘁𝗲𝗿
☑︎ 𝐒𝐮𝐫𝐠𝐞/𝐂𝐥𝐚𝐬𝐡 类型规则 𝗹𝗶𝘀𝘁 与 模块 𝐦𝐨𝐝𝐮𝐥𝐞 的解析使用
----------------------------------------------------------
0️⃣ ⟦原始链接⟧ 后加 "#" 使用, 不同参数用 "&" 连接:
⚠️ ☞ https://mysub.com#emoji=1&tfo=1&in=香港+台湾
❖ 本地资源片段引用, 请将参数 "#𝗶𝗻=𝘅𝘅𝘅." 填入文件第 ① 行 ❖
❖ 🚦 支持中文, "操作" 以下特殊字符时请先替换 🚦
∎ "+"⇒"%2B", 空格⇒"%20", "@"⇒"%40", "&"⇒"%26", "."⇒"\."
1️⃣ ⟦𝐬𝐞𝐫𝐯𝐞𝐫 节点⟧ ➠ 参数说明:
⦿ info=1, 开启通知提示机场 ✈️ 流量信息(如有提供);
⦿ emoji=1(国行设备用2)/-1, 添加/删除节点名内地区旗帜;
⦿ udp=1/-1, tfo=1/-1, 分别强制开启(关闭) 𝐮𝐝𝐩-𝐫𝐞𝐥𝐚𝐲/𝐟𝐚𝐬𝐭-𝐨𝐩𝐞𝐧;
⦿ tls13=1, cert=1, 分别开启 𝐭𝐥𝐬1.3 及 𝐭𝐥𝐬 证书验证(默认关闭);
⦿ in, out, regex 分别为 保留、删除、正则筛选 节点;
❖ in, out 中多参数(逻辑"或")用 "+", 逻辑"与"用 "." 表示;
❖ in/out/regex 均对节点的完整信息进行匹配(类型、端口、加密等);
❖ 示范: "in=香港.0\.2倍率+台湾&out=香港%20BGP®ex=(?i)iplc"
⦿ rename 重命名, "旧名@新名", "前缀@", "@后缀", 用 "+" 连接多个参数;
❖ 删除字段: "字段1.字段2☠️", 想删除 "." 时用 "\." 替代
❖ 示范: "rename=香港@𝐇𝐊+[𝐒𝐒]@+@[1𝐗]+流量.0\.2☠️"
❖ 默认 emoji 先生效, 如想调换顺序, 请用 𝗿𝗿𝗻𝗮𝗺𝗲 参数
❖ $type0/1/2/3/4/5 占位符,将节点类型(ss/ssr/vmess 等)作为可操作参数,如
∎ rename=@|$type2
∎ 样式分别为 "𝐬𝐬","𝐒𝐒","🅢🅢","🆂🆂","ⓢⓢ","🅂🅂"
❖ $emoji1/2 占位符,将节点地区emoji(🇭🇰 🇯🇵 等)作为可操作参数,如
∎ rename=@「$emoji1」
⦿ suffix=-1/1 将节点类型做为前缀/后缀 添加在节点名中, 如 「𝗌𝗌」 「𝖵𝗆𝖾𝗌𝗌」
⦿ ptn=1-6, 分别将节点名中的英文替换成花样字 ⇒ 🅰/🄰/𝐀/𝗮/𝔸/𝕒
⦿ delreg, 利用正则表达式来删除 "节点名" 中的字段(⚠️ 慎用)
⦿ replace 参数, 正则替换节点中内容, 可用于重命名/更改加密方式等
❖ replace𝗲=regex1@𝘀𝘁𝗿1+regex2@𝘀𝘁𝗿2
❖ replace=𝗿𝗲𝗴𝗲𝘅1@ 则等效于 𝗱𝗲𝗹𝗿𝗲𝗴 参数
⦿ sort=1/-1/x/指定规则, 分别按节点名 正/逆/随机/指定规则 排序
❖ 指定规则是正则表达式或简单关键词, 用"<" 或 ">" 连接
❖ sort=🇭🇰>🇸🇬>🇯🇵>🇺🇸 , 靠前排序
❖ sort=IEPL<IPLC<BGP , 靠后排序
⦿ del=0, 当有重名节点时, 用此参数保留重名节点
⦿ ⟦进阶参数⟧: 𝘀𝗳𝗶𝗹𝘁𝗲𝗿/𝘀𝗿𝗲𝗻𝗮𝗺𝗲, 传入一段 base64 编码的脚本, 可用于更为复杂的[过滤/重命名] 需求
❖ 说明: https://github.com/KOP-XIAO/QuantumultX/pull/9
2⃣️ ⟦𝐫𝐞𝐰𝐫𝐢𝐭𝐞 重写⟧/⟦𝐟𝐢𝐥𝐭𝐞𝐫 分流⟧ ➠ 参数说明:
⦿ in, out, 根据关键词 保留/禁用 相关分流、重写规则;
⦿ inhn, outhn, “保留/删除”主机名(𝒉𝒐𝒔𝒕𝒏𝒂𝒎𝒆);
❖ 示范: 禁用 "淘宝比价" 及 "weibo" 的 js 同主机名
𝐡𝐭𝐭𝐩𝐬://𝐦𝐲𝐥𝐢𝐬𝐭#𝒐𝒖𝒕=tb_price.js+wb_ad.js&outhn=weibo
⦿ regex, 正则筛选, 请自行折腾正则表达式;
❖ 可与 in(hn)/out(hn) 一起使用,in(hn)/out(hn) 会优先执行;
❖ 对 𝒉𝒐𝒔𝒕𝒏𝒂𝒎𝒆 & 𝐫𝐞𝐰𝐫𝐢𝐭𝐞/𝐟𝐢𝐥𝐭𝐞𝐫 同时生效(⚠️ 慎用)
⦿ policy 参数, 用于直接指定策略组,或为 𝐒𝐮𝐫𝐠𝐞 类型 𝗿𝘂𝗹𝗲-𝘀𝗲𝘁 生成策略组(默认"𝐒𝐡𝐚𝐰𝐧"策略组);
⦿ replace 参数, 正则替换 𝐟𝐢𝐥𝐭𝐞𝐫/𝐫𝐞𝐰𝐫𝐢𝐭𝐞 内容, regex@newregex;
❖ 将淘宝比价中脚本替换成 lite 版本, tiktok 中 JP 换成 KR
∎ replace=(price)(.*)@$1_lite$2+jp@kr
⦿ dst=rewrite/filter,分别为将 𝐦𝐨𝐝𝐮𝐥𝐞&𝗿𝘂𝗹𝗲-𝘀𝗲𝘁 转换成 重写/分流;
❖ ⚠️ 默认将 𝐦𝐨𝐝𝐮𝐥𝐞 转换到重写, 𝗿𝘂𝗹𝗲-𝘀𝗲𝘁 转成分流
❖ ⚠️ 把 𝗿𝘂𝗹𝗲-𝘀𝗲𝘁 中 𝐮𝐫𝐥-𝐫𝐞𝐠𝐞𝐱 转成重写时, 必须要加 dst=rewrite;
❖ ⚠️ 把 𝐦𝐨𝐝𝐮𝐥𝐞 中的分流规则转换时, 必须要加 dst=filter
⦿ cdn=1, 将 github 脚本的地址转换成免翻墙cdn.jsdelivr.net
3⃣️ 其他参数
⦿ 通知参数 ntf=0/1, 用于 关闭/打开 资源解析器的提示通知
❖ 𝗿𝗲𝘄𝗿𝗶𝘁𝗲/𝗳𝗶𝗹𝘁𝗲𝗿 默认“开启”通知提示, 以防规则误删除
❖ 𝘀𝗲𝗿𝘃𝗲𝗿 资源解析则默认”关闭“通知提示
⦿ 类型参数 type=domain-set/rule/module/list/nodes
❖ 当解析器未能正确识别类型时, 可尝试使用此参数强制指定
⦿ 隐藏参数 hide=1, 隐藏筛除的分流/重写,默认方式为禁用
----------------------------------------------------------
*/
/**
* 使用说明,
0️⃣ 在QuantumultX 配置文件中[general] 部分,加入
resource_parser_url = https://raw.githubusercontent.com/KOP-XIAO/QuantumultX/master/Scripts/resource-parser.js
⚠️⚠️如提示"没有自定义解析器",请长按右下角图标后点击左侧刷新按钮,更新资源,后台退出 app,直到出现解析器说明
1️⃣ 假设原始订阅连接为: https://raw.githubusercontent.com/crossutility/Quantumult-X/master/server-complete.txt ,
2️⃣ 假设你想要保留的参数为 in=tls+ss, 想要过滤的参数为 out=http+2, 请注意下面订阅链接后一定要加 ”#“ 符号
3️⃣ 则填入 Quanx 节点引用的的总链接为 https://raw.githubusercontent.com/crossutility/Quantumult-X/master/server-complete.txt#in=tls+ss&out=http+2
4️⃣ 填入上述链接, 并打开的资源解析器开关
------------------------------
*/
//beginning 解析器正常使用,調試註釋此部分
let [link0, content0, subinfo] = [$resource.link, $resource.content, $resource.info]
const subtag = $resource.tag != undefined ? $resource.tag : "";
////// 非 raw 链接的沙雕情形
content0 = content0.indexOf("DOCTYPE html") != -1 && link0.indexOf("github.com") != -1 ? ToRaw(content0) : content0 ;
//ends 正常使用部分,調試註釋此部分
var para = /^(http|https)\:\/\//.test(link0) ? link0 : content0.split("\n")[0];
var para1 = para.slice(para.indexOf("#") + 1).replace(/\$type/g,"node_type_para_prefix").replace(/\$emoji/g,"node_emoji_flag_prefix") //防止参数中其它位置也存在"#"
var mark0 = para.indexOf("#") != -1 ? true : false; //是否有參數需要解析
var Pinfo = mark0 && para1.indexOf("info=") != -1 ? para1.split("info=")[1].split("&")[0] : 0;
var ntf_flow = 0;
//常用量
const Base64 = new Base64Code();
const escapeRegExp = str => str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); //处理特殊符号以便正则匹配使用
var link1 = link0.split("#")[0]
const qxpng = "https://raw.githubusercontent.com/crossutility/Quantumult-X/master/quantumult-x.png" // server sub-info link
const subinfo_link = { "open-url": "https://t.me/QuanX_API", "media-url": "https://shrtm.nu/ebAr" };
const subinfo_link1 = { "open-url": link1, "media-url": "https://shrtm.nu/uo13" } // server sub-info link(fake-nodes)
const rwrite_link = { "open-url": link1, "media-url": "https://shrtm.nu/x3o2" } // rewrite filter link
const rwhost_link = { "open-url": link1, "media-url": "https://shrtm.nu/0n5J" } // hostname filter link
const rule_link = { "open-url": link1, "media-url": "https://shrtm.nu/cpHD" } // rule filter link
const nan_link = { "open-url": link1, "media-url": qxpng } // nan error link
const bug_link = { "open-url": "https://t.me/Shawn_KOP_bot", "media-url": "https://shrtm.nu/obcB" } // bug link
const sub_link = { "open-url": link1, "media-url": "https://shrtm.nu/ebAr" } // server link
SubFlow() //流量通知
// 参数获取
var Pin0 = mark0 && para1.indexOf("in=") != -1 ? (para1.split("in=")[1].split("&")[0].split("+")).map(decodeURIComponent) : null;
var Pout0 = mark0 && para1.indexOf("out=") != -1 ? (para1.split("out=")[1].split("&")[0].split("+")).map(decodeURIComponent) : null;
var Psfilter = mark0 && para1.indexOf("sfilter=") != -1 ? Base64.decode(para1.split("sfilter=")[1].split("&")[0]) : null; // script filter
var Preg = mark0 && para1.indexOf("regex=") != -1 ? decodeURIComponent(para1.split("regex=")[1].split("&")[0]) : null; //server正则过滤参数
var Pregdel = mark0 && para1.indexOf("delreg=") != -1 ? decodeURIComponent(para1.split("delreg=")[1].split("&")[0]) : null; // 正则删除参数
var Phin0 = mark0 && para1.indexOf("inhn=") != -1 ? (para1.split("inhn=")[1].split("&")[0].split("+")).map(decodeURIComponent) : null; //hostname
var Phout0 = mark0 && para1.indexOf("outhn=") != -1 ? (para1.split("outhn=")[1].split("&")[0].split("+")).map(decodeURIComponent) : null; //hostname
var Preplace = mark0 && para1.indexOf("replace=") != -1 ? para1.split("replace=")[1].split("&")[0] : null; //filter/rewrite 正则替换
var Pemoji = mark0 && para1.indexOf("emoji=") != -1 ? para1.split("emoji=")[1].split("&")[0] : null;
var Pudp0 = mark0 && para1.indexOf("udp=") != -1 ? para1.split("udp=")[1].split("&")[0] : 0;
var Ptfo0 = mark0 && para1.indexOf("tfo=") != -1 ? para1.split("tfo=")[1].split("&")[0] : 0;
var Prname = mark0 && para1.indexOf("rename=") != -1 ? para1.split("rename=")[1].split("&")[0].split("+") : null;
var Psrename = mark0 && para1.indexOf("srename=") != -1 ? Base64.decode(para1.split("srename=")[1].split("&")[0]) : null; // script rename
var Prrname = mark0 && para1.indexOf("rrname=") != -1 ? para1.split("rrname=")[1].split("&")[0].split("+") : null;
var Psuffix = mark0 && para1.indexOf("suffix=") != -1 ? para1.split("suffix=")[1].split("&")[0] : 0;
var Ppolicy = mark0 && para1.indexOf("policy=") != -1 ? decodeURIComponent(para1.split("policy=")[1].split("&")[0]) : "Shawn";
var Pcert0 = mark0 && para1.indexOf("cert=") != -1 ? para1.split("cert=")[1].split("&")[0] : 0;
var Psort0 = mark0 && para1.indexOf("sort=") != -1 ? para1.split("sort=")[1].split("&")[0] : 0;
var PsortX = mark0 && para1.indexOf("sortx=") != -1 ? para1.split("sortx=")[1].split("&")[0] : 0;
var PTls13 = mark0 && para1.indexOf("tls13=") != -1 ? para1.split("tls13=")[1].split("&")[0] : 0;
var Pntf0 = mark0 && para1.indexOf("ntf=") != -1 ? para1.split("ntf=")[1].split("&")[0] : 2;
var Phide = mark0 && para1.indexOf("hide=") != -1 ? para1.split("hide=")[1].split("&")[0] : 0;
var Pb64 = mark0 && para1.indexOf("b64=") != -1 ? para1.split("b64=")[1].split("&")[0] : 0;
var emojino = [" 0️⃣ ", " 1⃣️ ", " 2⃣️ ", " 3⃣️ ", " 4⃣️ ", " 5⃣️ ", " 6⃣️ ", " 7⃣️ ", " 8⃣️ ", " 9⃣️ ", " 🔟 "]
var pfi = Pin0 ? "in=" + Pin0.join(", ") + ", " : ""
var pfo = Pout0 ? "out=" + Pout0.join(", ") : ""
var pfihn = Phin0 ? "inhn=" + Phin0.join(", ") + ", " : ""
var pfohn = Phout0 ? "outhn=" + Phout0.join(", ") : ""
var Pcnt = para1.indexOf("cnt=") != -1 ? para1.split("cnt=")[1].split("&")[0] : 0;
var Pcap = para1.indexOf("cap=") != -1 ? para1.split("cap=")[1].split("&")[0] : "";
var Pptn = para1.indexOf("ptn=") != -1 ? para1.split("ptn=")[1].split("&")[0] : "";
var Pcdn = para1.indexOf("cdn=") != -1 ? para1.split("cdn=")[1].split("&")[0] : "";
let [flow, exptime, errornode, total] = "";
var Pdel = mark0 && para1.indexOf("del=") != -1 ? para1.split("del=")[1].split("&")[0] : 1; //删除重复节点
var typeU = para1.indexOf("type=") != -1 ? para1.split("type=")[1].split("&")[0] : "";
//花漾字 pattern
var pat=[]
pat[0] = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","k","r","s","t","u","v","w","x","y","z"]
pat[1] = ["🅰","🅱","🅲","🅳","🅴","🅵","🅶","🅷","🅸","🅹","🅺","🅻","🅼","🅽","🅾","🅿","🅺","🆁","🆂","🆃","🆄","🆅","🆆","🆇","🆈","🆉"]
pat[2] = ["🄰","🄱","🄲","🄳","🄴","🄵","🄶","🄷","🄸","🄹","🄺","🄻","🄼","🄽","🄾","🄿","🄺","🅁","🅂","🅃","🅄","🅅","🅆","🅇","🅈","🅉"]
pat[3] = ["𝐀","𝐁","𝐂","𝐃","𝐄","𝐅","𝐆","𝐇","𝐈","𝐉","𝐊","𝐋","𝐌","𝐍","𝐎","𝐏","𝐊","𝐑","𝐒","𝐓","𝐔","𝐕","𝐖","𝐗","𝐘","𝐙"]
pat[4] = ["𝗮","𝗯","𝗰","𝗱","𝗲","𝗳","𝗴","𝗵","𝗶","𝗷","𝗸","𝗹","𝗺","𝗻","𝗼","𝗽","𝗸","𝗿","𝘀","𝐭","𝘂","𝘃","𝘄","𝘅","𝘆","𝘇"]
pat[5] = ["𝔸","𝔹","ℂ","𝔻","𝔼","𝔽","𝔾","ℍ","𝕀","𝕁","𝕂","𝕃","𝕄","ℕ","𝕆","ℙ","𝕂","ℝ","𝕊","𝕋","𝕌","𝕍","𝕎","𝕏","𝕐","ℤ"]
pat[6] = ["𝕒","𝕓","𝕔","𝕕","𝕖","𝕗","𝕘","𝕙","𝕚","𝕛","𝕜","𝕝","𝕞","𝕟","𝕠","𝕡","𝕜","𝕣","𝕤","𝕥","𝕦","𝕧","𝕨","𝕩","𝕪","𝕫"]
var type0=""
//flag=1,2,3分别为 server、rewrite、rule 类型
var flag = 1
function Parser() {
try {
type0 = Type_Check(content0); // 类型判断
//$notify(type0,"hh")
total = ResourceParse();
} catch (err) {
$notify("❌ 解析出现错误", "⚠️ 请点击发送链接反馈", err, bug_link);
}
//$notify("","",total)
$done({ content: total });
}
if (typeof($resource)!=="undefined") {
Parser()
$done({ content: total })
}
/**
# 以下为具体的 function
*/
function ParseUnkown(cnt){
try {
cnt = JSON.parse(cnt)
if(cnt) {
$notify("⚠️ 链接返回内容并非有效订阅"+ "⟦" + subtag + "⟧","⁉️ 请自行检查原始链接,返回内容 👇️👇️",JSON.stringify(cnt), bug_link)
}
} catch(err) {
$notify("😭 未能识别该订阅格式: " + "⟦" + subtag + "⟧", "⚠️ 将直接导入Quantumult X \n 如认为是 BUG, 请点通知跳转反馈", "链接返回内容:\n"+cnt, bug_link);
}
}
function ResourceParse() {
//预处理,分流/重写等处理完成
if (type0 == "Subs-B64Encode") {
total = Subs2QX(Base64.decode(content0), Pudp0, Ptfo0, Pcert0, PTls13);
} else if (type0 == "Subs") {
total = Subs2QX(content0, Pudp0, Ptfo0, Pcert0, PTls13);
} else if (type0 == "QuanX" || type0 == "Clash" || type0 == "Surge") {
total = Subs2QX(isQuanX(content0).join("\n"), Pudp0, Ptfo0, Pcert0, PTls13);
} else if (type0 == "sgmodule") { // surge module 模块/含 url-regex 的 rule-set
flag = 2
total = SGMD2QX(content0) // 转换
total = Rewrite_Filter(total, Pin0, Pout0,Preg); // 筛选过滤
if (Preplace) { total = ReplaceReg(total, Preplace) }
if (Pcdn) {total = CDN(total)
} else { total = total.join("\n")}
} else if (type0 == "rewrite") { // rewrite 类型
flag = 2;
total = Rewrite_Filter(isQuanXRewrite(content0.split("\n")), Pin0, Pout0,Preg);
if (Preplace) { total = ReplaceReg(total, Preplace) }
if (Pcdn) {total = CDN(total)
} else {total = total.join("\n")}
} else if (type0 == "Rule") { // rule 类型, 已处理完毕
flag = 3;
total = Rule_Handle(content0.split("\n"), Pout0, Pin0).filter(Boolean);
if (Preg && total.length!=0) { // 正则筛选规则 filter
total = total.map(Regex).filter(Boolean).join("\n")
RegCheck(total, "分流引用", Preg)}
if (Preplace) { total = ReplaceReg(total, Preplace) }
total = total.join("\n")
} else if (content0.trim() == "") {
$notify("‼️ 引用" + "⟦" + subtag + "⟧" + " 返回內容为空", "⁉️ 点通知跳转以确认链接是否失效", para.split("#")[0], nan_link);
flag = 0;
} else if (type0 == "unknown") {
ParseUnkown(content0)
flag = -1;
}
//开始处理
if (flag == 1) { //server 类型统一处理
total = total.filter(Boolean)
if (Pinfo == 1 && ntf_flow == 0) { //假节点类型的流量通知
flowcheck(total)
}
if (Pin0 || Pout0) { total = Filter(total, Pin0, Pout0) } // in & out
if (Preg) { total = total.map(Regex).filter(Boolean) // regex
RegCheck(total, "节点订阅", Preg)}
if (Psfilter) { total = FilterScript(total, Psfilter) }
if (Prrname) {
Prn = Prrname;
total = total.map(Rename);
}
if (Pemoji) { total = emoji_handle(total, Pemoji); }
if (Pregdel) {
delreg = Pregdel
total = total.map(DelReg)
}
if (Preplace) { // server 类型也可用 replace 参数进行重命名操作
total = ReplaceReg(total, Preplace)
}
if (Prname) {
Prn = Prname;
total = total.map(Rename);
}
if (Psrename) { total = RenameScript(total, Psrename) }
if (Psort0) {
total = QXSort(total, Psort0);
}
if (total.length > 0){
if (Psuffix==1 || Psuffix==-1) {total = Psuffix == 1? total.map(type_suffix):total.map(type_prefix)
}
total = total.map(type_handle).map(emoji_prefix_handle)
total = TagCheck_QX(total).join("\n") //节点名检查
if (Pcnt == 1) {$notify("解析后最终返回内容" , "节点数量: " +total.split("\n").length, total)}
total = Base64.encode(total) //强制节点类型 base64 加密后再导入 Quantumult X
$done({ content: total });
} else {
$notify("❓❓ 友情提示", "⚠️⚠️ 解析后无有效内容", "🚥🚥 请自行检查相关参数, 或者点击通知跳转反馈", bug_link)
total = errornode
$done({ content: errornode })
}
} else if (flag == 0){ //空/错误类型
total = errornode
$done({ content: errornode })
} else if (flag == -1){ //未知类型
total = content0
$done({ content: content0 })
}
return total
}
//响应头流量处理部分
function SubFlow() {
if (Pinfo == 1 && subinfo) {
var sinfo = subinfo.replace(/ /g, "").toLowerCase();
var total = "总流量: " + (parseFloat(sinfo.split("total=")[1].split(",")[0]) / (1024 ** 3)).toFixed(2) + "GB";
var usd = "已用流量: " + ((parseFloat(sinfo.indexOf("upload")!=-1?sinfo.split("upload=")[1].split(",")[0]:"0") + parseFloat(sinfo.split("download=")[1].split(",")[0])) / (1024 ** 3)).toFixed(2) + "GB"
var left = "剩余流量: " + ((parseFloat(sinfo.split("total=")[1].split(",")[0]) / (1024 ** 3)) - ((parseFloat(sinfo.indexOf("upload")!=-1?sinfo.split("upload=")[1].split(",")[0]:"0") + parseFloat(sinfo.split("download=")[1].split(",")[0])) / (1024 ** 3))).toFixed(2) + "GB"
if (sinfo.indexOf("expire=") != -1) {
var epr = new Date(parseFloat(sinfo.split("expire=")[1].split(",")[0]) * 1000);
var year = epr.getFullYear(); // 获取完整的年份(4位,1970)
var mth = epr.getMonth() + 1 < 10 ? '0' + (epr.getMonth() + 1) : (epr.getMonth() + 1); // 获取月份(0-11,0代表1月,用的时候记得加上1)
var day = epr.getDate() < 10 ? "0" + (epr.getDate()) : epr.getDate();
epr = "过期时间: " + year + "-" + mth + "-" + day
} else {
epr = ""; //"过期时间: ✈️ 未提供該信息" //没过期时间的显示订阅链接
}
var message = total + "\n" + usd + ", " + left;
ntf_flow = 1;
$notify("流量信息: ⟦" + subtag + "⟧", epr, message, subinfo_link)
}
// } else if (Pinfo ==1){
// $notify("流量信息: ⟦" + subtag + "⟧", "", "⚠️ 该订阅链接未返回任何流量信息", subinfo_link)
// }
}
//flowcheck-fake-server
function flowcheck(cnt) {
for (var i = 0; i < cnt.length; i++) {
var item = cnt[i];
var nl = item.slice(item.indexOf("tag"))
var nm = nl.slice(nl.indexOf("=") + 1)
if (item.indexOf("剩余流量") != -1) {
flow = nm
} else if (item.indexOf("期时间") != -1) {
exptime = nm
}
}
flow = flow? flow:"⚠️ 该订阅未返回任何流量信息"
exptime = exptime? exptime:"⚠️ 该订阅未返回套餐时间信息"
if (flow != "") { $notify("流量信息: ⟦" + subtag + "⟧", flow, exptime, subinfo_link1) }
}
// regex 后的检查
function RegCheck(total, typen, regpara) {
if(total.length == 0){
$notify("‼️ " + typen + " ➟ " + "⟦" + subtag + "⟧", "⛔️ 筛选正则: regex=" + regpara, "⚠️ 筛选后剩余项为 0️⃣ , 请检查正则参数及原始链接", nan_link)
}else if((typen != "节点订阅" && Pntf0 !=0) || (typen == "节点订阅" && Pntf0 ==1)){
var nolist = total.length <= 10 ? emojino[total.length] : total.length
$notify("🤖 " + typen + " ➟ " + "⟦" + subtag + "⟧", "⛔️ 筛选正则: regex=" + regpara, "⚠️ 筛选后剩余以下" + nolist + "个匹配项 \\n ⨷ " + total.join("\n ⨷ "), sub_link)
}
}
//判断订阅类型
function Type_Check(subs) {
var type = "unknown"
var RuleK = ["host,", "-suffix,", "domain,", "-keyword,", "ip-cidr,", "ip-cidr6,", "geoip,", "user-agent,", "ip6-cidr,"];
var DomainK = ["domain-set,"]
var QuanXK = ["shadowsocks=", "trojan=", "vmess=", "http="];
var SurgeK = ["=ss,", "=vmess,", "=trojan,", "=http,", "=custom,", "=https,", "=shadowsocks", "=shadowsocksr"];
var ClashK = ["proxies:"]
var SubK = ["dm1lc3M", "c3NyOi8v", "CnNzOi8", "dHJvamFu", "c3M6Ly", "c3NkOi8v", "c2hhZG93",,"aHR0c"];
var RewriteK = [" url "]
var SubK2 = ["ss://", "vmess://", "ssr://", "trojan://", "ssd://", "https://"];
var ModuleK = ["[Script]", "[Rule]", "[URL Rewrite]", "[Map Local]", "[MITM]", "\nhttp-r"]
var html = "DOCTYPE html"
var subi = subs.replace(/ /g, "")
const RuleCheck = (item) => subi.toLowerCase().indexOf(item) != -1;
const NodeCheck = (item) => subi.toLowerCase().indexOf(item.toLowerCase()) != -1;
const RewriteCheck = (item) => subs.indexOf(item) != -1;
var subsn = subs.split("\n")
if (subs.indexOf(html) != -1 && link0.indexOf("github.com" == -1)) {
$notify("‼️ 该链接返回内容有误", "⁉️ 点通知跳转以确认链接是否失效", link0, nan_link);
type = "web";
} else if (typeU == "nodes") {
type = "Subs-B64Encode"
} else if (ClashK.some(NodeCheck) || typeU == "clash"){ // Clash 类型节点转换
type = "Clash";
content0 = Clash2QX(subs)
} else if ((/^hostname\s*\=|pattern\=/.test(subi) || RewriteK.some(RewriteCheck)) && !/\[(Proxy|filter_local)\]/.test(subs) && para1.indexOf("dst=filter")==-1 && subi.indexOf("securehostname") == -1 && !/module|nodes/.test(typeU)) {
type = "rewrite" //Quantumult X 类型 rewrite/ Surge Script/
} else if ( (((ModuleK.some(RewriteCheck) || para1.indexOf("dst=rewrite") != -1) && (para1.indexOf("dst=filter") == -1) && subs.indexOf("[Proxy]") == -1) || typeU == "module") && typeU != "nodes") { // Surge 类型 module /rule-set(含url-regex) 类型
type = "sgmodule"
} else if (((RuleK.some(RuleCheck) && subs.indexOf(html) == -1 && !/\[(Proxy|server_local)\]/.test(subs)) || typeU == "rule" || para1.indexOf("dst=filter")!=-1) && typeU != "nodes") {
type = "Rule";
} else if ((DomainK.some(RuleCheck) || typeU == "domain-set") && subs.indexOf("[Proxy]") == -1 && typeU != "nodes") {
type = "Rule";
content0 = Domain2Rule(content0) // 转换 domain-set
} else if (subsn.length >= 1 && SubK2.some(NodeCheck) && !/\[(Proxy|filter_local)\]/.test(subs)) { //未b64加密的多行URI 组合订阅
type = "Subs"
} else if (SubK.some(NodeCheck)) { //b64加密的订阅类型
type = "Subs-B64Encode"
} else if ((subi.indexOf("tag=") != -1 && QuanXK.some(NodeCheck) && !/\[(Proxy|filter_local)\]/.test(subs)) || typeU =="list") {
type = "Subs" // QuanX list
} else if (subs.indexOf("[Proxy]") != -1) {
type = "Surge"; // Surge Profiles
content0 = Surge2QX(content0).join("\n");
} else if ((SurgeK.some(NodeCheck) && !/\[(Proxy|filter_local)\]/.test(subs)) || typeU == "list") {
type = "Subs" // Surge proxy list
} else if (subs.indexOf("[server_local]") != -1) {
//type = "QuanX" // QuanX Profile
type = "Subs"
} else if (content0.indexOf("server") !=-1 && content0.indexOf("server_port") !=-1) { //SIP008
//type = "QuanX"
type = "Subs"
content0 = SIP2QuanX(content0)
}
// 用于通知判断类型,debug
if(typeU == "X"){
$notify(type,"",content0)
}
return type
}
// 检查节点名字(重复以及空名)等QuanX 不允许的情形,以及多个空格等“不规范”方式
function TagCheck_QX(content) {
console.log(content)
var Olist = content.map(item =>item.trim().replace(/\s{2,}/g," "))
//$notify("","",Olist)
var [Nlist, nmlist] = [ [], [] ]
var [nulllist,duplist] = [ [], [] ]; //记录空名字节点&重名节点
var no = 0;
for (var i = 0; i < Olist.length; i++) {
var item = Olist[i] ? Olist[i] : ""
if (item.replace(/ /gm, "").indexOf("tag=") != -1) {
var nl = item.slice(item.indexOf("tag"))
var nm = nl.slice(nl.indexOf("=") + 1)
if (nm == "") { //空名字
nm = " [" + item.split("=")[0] + "] " + item.split("=")[1].split(",")[0].split(":")[0]
item = item.split("tag")[0] + "tag=" + nm.replace("shadowsocks", "ss")
nulllist.push(nm.replace("shadowsocks", "ss"))
}
var ni = 0
while (nmlist.indexOf(nm) != -1) { //重名情形
nm = ni <= 10 ? nm.split(" ⌘")[0] + " ⌘" + emojino[ni] : nm.split(" ⌘")[0] + " ⌘" + ni
item = Pdel == 0 ? item.split("tag")[0] + "tag=" + nm : ""
ni = ni + 1
}
if (ni != 0) { duplist.push(nm) }
nmlist.push(nm)
if (Pcap) {
item = Capitalize(item,Pcap)
console.log(item)
}
if (Pptn) {
item = Pattern(item,Pptn)
console.log(item)
}
ni = 0
if (item) {
Nlist.push(item)
}
}// if "tag="
} // for
if (nulllist.length >= 1) {
no = nulllist.length <= 10 ? emojino[nulllist.length] : nulllist.length;
$notify("⚠️ 引用" + "⟦" + subtag + "⟧" + " 内有" + no + "个空节点名 ", "✅ 已将节点“类型+IP”设为节点名", " ⨁ " + nulllist.join("\n ⨁ "), nan_link)
}
if (duplist.length >= 1) {
no = duplist.length <= 10 ? emojino[duplist.length] : duplist.length;
if (Pdel==0){
$notify("⚠️ 引用" + "⟦" + subtag + "⟧" + " 内有" + no + "个名字重复的节点 ", "✅ 已添加⌘符号作为区分:", " ⨁ " + duplist.join("\n ⨁ "), nan_link)
} else {
$notify("⚠️ 引用" + "⟦" + subtag + "⟧" + " 内有" + no + "个名字重复的节点 ", "❌️ 已全部删除处理,如需保留请添加参数 del=0:", " ⨁ " + duplist.join("\n ⨁ "), nan_link)
}
}
return Nlist
}
function PatternN(cnt, para) {
for (var i=0;i<26;i++) {
cnt = cnt.toLowerCase()
cnt = cnt.replace(new RegExp(pat[0][i], "gmi"),pat[para][i])
}
console.log(cnt)
return cnt
}
function Pattern(cnt,para) {
if (para != "") {
cnt = cnt.split("tag=")[0] +"tag="+ PatternN(cnt.split("tag=")[1],para)
}
return cnt
}
//大小写
function Capitalize(cnt,para) {
if (para == 1) {
cnt = cnt.split("tag=")[0] +"tag="+ cnt.split("tag=")[1].toUpperCase()
} else if (para == -1) {
cnt = cnt.split("tag=")[0] +"tag="+ cnt.split("tag=")[1].toLowerCase()
} else if (para == 0) {
cnt =cnt.split("tag=")[0] +"tag="+titleCase(cnt.split("tag=")[1])
}
return cnt
}
function titleCase(str) {
var newStr = str.split(" ");
for(var i = 0; i<newStr.length; i++){
newStr[i] = newStr[i].slice(0,1).toUpperCase() + newStr[i].slice(1).toLowerCase();
} return newStr.join(" ");
}
// 类型前缀/后缀
function type_prefix(item) {
if(item.trim()!="") {
typefix = {"shadowsocks":"「𝐬𝐬」","vmess":"「𝐯𝐦𝐞𝐬𝐬」","trojan":"「𝐭𝐫𝐨𝐣𝐚𝐧」","http":"「𝐡𝐭𝐭𝐩」"}
typefix["shadowsocks"]=item.indexOf("ssr-protocol")!=-1? "「𝐬𝐬𝐫」" : "「𝐬𝐬」"
tp = typefix[item.split("=")[0].trim()]
return [[item.split("tag=")[0]+
"tag=", tp, item.split("tag=")[1]].join(" ")].join(" ")
}
}
function type_suffix(item) {
if(item.trim()!=""){
typefix={"shadowsocks":"「𝐬𝐬」","vmess":"「𝐯𝐦𝐞𝐬𝐬」","trojan":"「𝐭𝐫𝐨𝐣𝐚𝐧」","http":"「𝐡𝐭𝐭𝐩」"}
typefix["shadowsocks"]=item.indexOf("ssr-protocol")!=-1? "「𝐬𝐬𝐫」" : "「𝐬𝐬」"
tp = typefix[item.split("=")[0].trim()]
return [item, tp].join(" ")
}
}
//获取类型
function getnode_type(item,ind) {
if(item.trim()!="" && item.indexOf("tag=")!=-1) {
ind = !/^(0|1|2|3|4|5)$/.test(ind) ? 6 : ind
typefix = {"shadowsocks":["𝐬𝐬","𝐒𝐒","🅢🅢","🆂🆂","ⓢⓢ","🅂🅂","SS"],"shadowsocksr":["𝐬𝐬𝐫","𝐒𝐒𝐑","🅢🅢🅡","🆂🆂🆁","ⓢⓢⓡ","🅂🅂🅁","SSR"],"vmess":["𝐯𝐦𝐞𝐬𝐬","𝐕𝐌𝐄𝐒𝐒","🅥🅜🅔🅢🅢","🆅🅼🅴🆂🆂","ⓥⓜⓔⓢⓢ","🅅🄼🄴🅂🅂","VMESS"],"trojan":["𝐭𝐫𝐨𝐣𝐚𝐧","𝐓𝐑𝐎𝐉𝐀𝐍","🅣🅡🅞🅙🅐🅝","🆃🆁🅾🅹🅰🅽","ⓣⓡⓞⓙⓐⓝ","🅃🅁🄾🄹🄰🄽","TROJAN"],"http":["𝐡𝐭𝐭𝐩","𝐇𝐓𝐓𝐏","🅗🅣🅣🅟","🅷🆃🆃🅿","ⓗⓣⓣⓟ","🄷🅃🅃🄿","HTTP"]}
typefix["shadowsocks"]=item.indexOf("ssr-protocol")!=-1? typefix["shadowsocksr"] : typefix["shadowsocks"]
tp = typefix[item.split("=")[0].trim()][ind]
return tp
}
}
// 操作節點類型佔位符
function type_handle(item) {
if(item.indexOf("node_type_para_prefix")!=-1) {
item = item.replace(/node_type_para_prefix(\d{0,1})/g,getnode_type(item,item.split("node_type_para_prefix")[1][0]))
}
return item
}
// 操作emoji占位符
function emoji_prefix_handle(item) {
if(item.indexOf("node_emoji_flag_prefix")!=-1) {
item = item.replace(/node_emoji_flag_prefix\d{0,1}/g,getnode_emoji(item,item.split("node_emoji_flag_prefix")[1][0]))
//console.log(item)
}
return item
}
// 获取emoji
function getnode_emoji(item,ind){
ind = !/^(1|2)$/.test(ind) ? 2 : ind
if(item.indexOf("tag=")!=-1) {
return get_emoji(ind,item.split("tag=")[1])[1]
}
}
// 用于单条 URI 的 tag 参数, 直接指定节点名
function URI_TAG(cnt0,tag0) {
cnt0 = cnt0.split("tag=")[0] + "tag=" + tag0
return cnt0
}
// 用于某些奇葩用户不使用 raw 链接的问题
function rawtest(cnt) {
var Preg0 = RegExp(".*js-file-line\".*?\<\/td\>", "i")
if (Preg0.test(cnt)) {
return cnt.replace(/(.*js-file-line\"\>)(.*?)(\<\/td\>)/g,"$2").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").trim()
}
}
function ToRaw(cnt) {
cnt = cnt.split("\n").map(rawtest).filter(Boolean).join("\n")
var rawlink = link0.replace("github.com","raw.githubusercontent.com").replace("/blob","")
if (cnt) {
$notify( "⚠️⚠️ 将尝试解析该资源" + "⟦" + subtag + "⟧" , "🚥 请正确使用GitHub的 raw 链接" , "❌ 你的链接:"+link0+"\n✅ 正确链接:"+rawlink, {"open-url":rawlink})
} else if(content0.indexOf("gridcell")!=-1) {
$notify( "⚠️⚠️ 解析该资源" + " ⟦" + subtag + "⟧ 失败" , "🚥 你的链接似乎是目录,而不是文件" , "❌ 你的链接:"+link0, {"open-url":link0})
}
return cnt
}
function CDN(cnt) {
console.log("CDN start")
cnt = cnt.join("\n").replace(/https:\/\/raw.githubusercontent.com\/(.*?)\/(.*?)\/(.*)/gmi,"https://cdn.jsdelivr.net/gh/$1/$2@$3")
return cnt
}
//url-regex 转换成 Quantumult X
function URX2QX(subs) {
var nrw = []
var rw = ""
subs = subs.split("\n")
var NoteK = ["//", "#", ";"]; //排除注释项
for (var i = 0; i < subs.length; i++) {
const notecheck = (item) => subs[i].indexOf(item) == 0
if (!NoteK.some(notecheck)) {
if (subs[i].slice(0, 9) == "URL-REGEX") { // regex 类型
rw = subs[i].replace(/ /g, "").split(",REJECT")[0].split("GEX,")[1] + " url " + "reject-200"
nrw.push(rw)
} else if (subs[i].indexOf("data=") != -1 && subs.indexOf("[Map Local]") != -1){ // Map Local 类型
rw = subs[i].replace(/ /g, "").split("data=")[0] + " url " + "reject-dict"
nrw.push(rw)
}
}
}
return nrw
}
//script&rewrite 转换成 Quantumult X
function SCP2QX(subs) {
var nrw = []
var rw = ""
subs = subs.split("\n").map(x => x.trim().replace(/\s+/g," "))
for (var i = 0; i < subs.length; i++) {
if (subs[i].slice(0, 8) == "hostname") {
hn = subs[i].replace(/\%.*\%/g, "")
nrw.push(hn)
}
var SC = ["type=", ".js", "pattern=", "script-path="]
var NoteK = ["//", "#", ";"]; //排除注释项
const sccheck = (item) => subs[i].indexOf(item) != -1
const notecheck = (item) => subs[i].indexOf(item) == 0
if (!NoteK.some(notecheck)){
if (SC.every(sccheck)) { // surge js 新格式
ptn = subs[i].replace(/\s/gi,"").split("pattern=")[1].split(",")[0]
js = subs[i].replace(/\s/gi,"").split("script-path=")[1].split(",")[0]
type = subs[i].replace(/\s/gi,"").split("type=")[1].split(",")[0].trim()
subsi = subs[i].replace(/ /g,"").replace(/\=true/g,"=1")
if (type == "http-response" && subsi.indexOf("requires-body=1") != -1) {
type = "script-response-body "
} else if (type == "http-response" && subsi.indexOf("requires-body=1") == -1) {
type = "script-response-header "
} else if (type == "http-request" && subsi.indexOf("requires-body=1") != -1) {
type = "script-request-body "
} else if (type == "http-request" && subsi.indexOf("requires-body=1") == -1) {
type = "script-request-header "
} else {type = "" }
if (type != "") {
rw = ptn + " url " + type + js
nrw.push(rw)
}
} else if (subs[i].indexOf(" 302") != -1 || subs[i].indexOf(" 307") != -1) { //rewrite 302&307 复写
//tpe = subs[i].indexOf(" 302") != -1? "302":"307"
rw = subs[i].split(" ")[0] + " url " + subs[i].split(" ")[2] + " " + subs[i].split(" ")[1].trim()
//if(rw.indexOf("307")!=-1) {$notify("XX",subs[i],rw.split(" "))}
nrw.push(rw)
} else if(subs[i].split(" ")[2] == "header") { // rewrite header 类型
var pget = subs[i].split(" ")[0].split(".com")[1]
var pgetn = subs[i].split(" ")[1].split(".com")[1]
rw = subs[i].split(" ")[0] + " url request-header ^GET " + pget +"(.+\\r\\n)Host:.+(\\r\\n) request-header GET " + pgetn + "$1Host: " + subs[i].split(" ")[1].split("://")[1].split(".com")[0] + ".com$2"
nrw.push(rw)
} else if(subs[i].split(" ")[1] == "header-replace") { // rewrite header-replace 类型
console.log(subs[i])
var pget = subs[i].split("header-replace")[1].split(":")[0].trim()
var pgetn = subs[i].split("header-replace")[1].trim()
rw = subs[i].split(" ")[0] + " url request-header " +"(.+\\r\\n)"+pget+":.+(\\r\\n) request-header " + "$1" + pgetn + "$2"
nrw.push(rw)
} else if(subs[i].indexOf(" - reject") != -1) { // rewrite reject 类型
rw = subs[i].split(" ")[0] + " url reject-200"
nrw.push(rw)
} else if (subs[i].indexOf("script-path") != -1) { //surge js 旧写法
type = subs[i].replace(/\s+/g," ").split(" ")[0]
js = subs[i].split("script-path")[1].split("=")[1].split(",")[0]
ptn = subs[i].replace(/\s+/g," ").split(" ")[1]
subsi = subs[i].replace(/ /g,"").replace(/\=true/g,"=1")
if (type == "http-response" && subsi.indexOf("requires-body=1") != -1) {
type = "script-response-body "
} else if (type == "http-response" && subsi.indexOf("requires-body=1") == -1) {
type = "script-response-header "
} else if (type == "http-request" && subsi.indexOf("requires-body=1") != -1) {
type = "script-request-body "
} else if (type == "http-request" && subsi.indexOf("requires-body=1") == -1) {
type = "script-request-header "
} else {type = "" }
if (type != "") {
rw = ptn + " url " + type + js
nrw.push(rw)
}
}
}
}
return nrw
}
// 如果 URL-Regex 跟 rewrite/script 都需要
function SGMD2QX(subs) {
var nrw0 = URX2QX(subs)
var nrw1 = SCP2QX(subs)
var nrwt = [...nrw0, ...nrw1]
return nrwt
}
//Rewrite过滤,使用+连接多个关键词(逻辑"或"):in 为保留,out 为排除
function Rewrite_Filter(subs, Pin, Pout,Preg) {
var Nlist = [];
var noteK = ["//", "#", ";"];
var hnc = 0;
var dwrite = []
var hostname = ""
for (var i = 0; i < subs.length; i++) {
subi = subs[i].trim();
var subii = subi.replace(/ /g, "")
if (subi != "" && (subi.indexOf(" url ")!=-1 || /^hostname\=/.test(subii))) {
const notecheck = (item) => subi.indexOf(item) == 0
if (noteK.some(notecheck)) { // 注释项跳过
continue;
} else if (hnc == 0 && subii.indexOf("hostname=") == 0) { //hostname 部分
hostname = (Phin0 || Phout0 || Preg) ? HostNamecheck(subi, Phin0, Phout0) : subi;//hostname 部分
} else if (subii.indexOf("hostname=") != 0) { //rewrite 部分
var inflag = Rcheck(subi, Pin);
var outflag = Rcheck(subi, Pout);
if (outflag == 1 || inflag == 0) {
dwrite.push(subi.replace(" url "," - ")); //out 命中
} else if (outflag == 0 && inflag != 0) { //out 未命中 && in 未排除
Nlist.push(subi);
} else if (outflag == 2 && inflag != 0) { //无 out 参数 && in 未排除
Nlist.push(subi);
}
}
}
}
if (Pntf0 != 0) {
nowrite = dwrite.length <= 10 ? emojino[dwrite.length] : dwrite.length
no1write = Nlist.length <= 10 ? emojino[Nlist.length] : Nlist.length
if (Pin0 && no1write != " 0️⃣ ") { //有 in 参数就通知保留项目
$notify("🤖 " + "重写引用 ➟ " + "⟦" + subtag + "⟧", "⛔️ 筛选参数: " + pfi + pfo, "☠️ 重写 rewrite 中保留以下" + no1write + "个匹配项:" + "\n ⨷ " + Nlist.join("\n ⨷ "), rwrite_link)
} else if (dwrite.length > 0) {
$notify("🤖 " + "重写引用 ➟ " + "⟦" + subtag + "⟧", "⛔️ 筛选参数: " + pfi + pfo, "☠️ 重写 rewrite 中已禁用以下" + nowrite + "个匹配项:" + "\n ⨷ " + dwrite.join("\n ⨷ "), rwrite_link)
}
}
if (Nlist.length == 0) { $notify("🤖 " + "重写引用 ➟ " + "⟦" + subtag + "⟧", "⛔️ 筛选参数: " + pfi + pfo, "⚠️ 筛选后剩余rewrite规则数为 0️⃣ 条, 请检查参数及原始链接", nan_link) }
if(Preg){ Nlist = Nlist.map(Regex).filter(Boolean) // regex to filter rewrites
RegCheck(Nlist, "重写引用", Preg) }
if (hostname != "") { Nlist.push(hostname) }
Nlist =Phide ==1? Nlist : [...dwrite,...Nlist]
return Nlist
}
// 主机名处理
function HostNamecheck(content, parain, paraout) {
var hname = content.replace(/ /g, "").split("=")[1].split(",");
var nname = [];
var dname = []; //删除项
for (var i = 0; i < hname.length; i++) {
dd = hname[i]
const excludehn = (item) => dd.indexOf(item) != -1;
if (paraout && paraout != "") { //存在 out 参数时
if (!paraout.some(excludehn)) { //out 未命中🎯️
if (parain && parain != "") {
if (parain.some(excludehn)) { //Pin 命中🎯️
nname.push(hname[i])
} else {
dname.push(hname[i])
} //Pin 未命中🎯️的记录
} else { nname.push(hname[i]) } //无in 参数
} else { dname.push(hname[i]) } //out 参数命中
} else if (parain && parain != "") { //不存在 out,但有 in 参数时
if (parain.some(excludehn)) { //Pin 命中🎯️
nname.push(hname[i])
} else { dname.push(hname[i]) }
} else {
nname.push(hname[i])
}
} //for j
if (Pntf0 != 0) {
if (paraout || parain) {
var noname = dname.length <= 10 ? emojino[dname.length] : dname.length
var no1name = nname.length <= 10 ? emojino[nname.length] : nname.length
if (parain && no1name != " 0️⃣ ") {
$notify("🤖 " + "重写引用 ➟ " + "⟦" + subtag + "⟧", "⛔️ 筛选参数: " + pfihn + pfohn, "☠️ 主机名 hostname 中已保留以下" + no1name + "个匹配项:" + "\n ⨷ " + nname.join(","), rwhost_link)
} else if (dname.length > 0) {
$notify("🤖 " + "重写引用 ➟ " + "⟦" + subtag + "⟧", "⛔️ 筛选参数: " + pfihn + pfohn, "☠️ 主机名 hostname 中已删除以下" + noname + "个匹配项:" + "\n ⨷ " + dname.join(","), rwhost_link)
}
}
}
if (nname.length == 0) {
$notify("🤖 " + "重写引用 ➟ " + "⟦" + subtag + "⟧", "⛔️ 筛选参数: " + pfihn + pfohn, "⚠️ 主机名 hostname 中剩余 0️⃣ 项, 请检查参数及原始链接", nan_link)
}
if(Preg){ nname = nname.map(Regex).filter(Boolean)
RegCheck(nname, "主机名hostname", Preg) }
hname = "hostname=" + nname.join(", ");
return hname
}
//Rewrite 筛选的函数
function Rcheck(content, param) {
name = content.toUpperCase()
if (param) {
var flag = 0; //没命中
const checkpara = (item) => name.indexOf(item.toUpperCase()) != -1;
if (param.some(checkpara)) {
flag = 1 //命中
}
return flag
} else { //if param
return 2
} //无参数
}
//分流规则转换及过滤,可用于 surge 及 quanx 的 rule-list
function Rule_Handle(subs, Pout, Pin) {
cnt = subs //.split("\n");
Tin = Pin; //保留参数
Tout = Pout; //过滤参数
ply = Ppolicy; //策略组
var nlist = []
var RuleK = ["//", "#", ";"];
var RuleK2 = ["host,", "-suffix,", "domain,", "-keyword,", "ip-cidr,", "ip-cidr6,", "geoip,", "user-agent,", "ip6-cidr,"];
if (Tout != "" && Tout != null) { // 有 out 参数时
var dlist = [];
for (var i = 0; i < cnt.length; i++) {
cc = cnt[i].replace(/^\s*\-\s/g,"").trim()
const exclude = (item) => cc.indexOf(item) != -1; // 删除项
const RuleCheck = (item) => cc.indexOf(item) != -1; //无视注释行
if (Tout.some(exclude) && !RuleK.some(RuleCheck)) {
dlist.push(Rule_Policy("-" + cc))
} else if (!RuleK.some(RuleCheck) && cc) { //if Pout.some, 不操作注释项
dd = Rule_Policy(cc);
if (Tin != "" && Tin != null) {
const include = (item) => dd.indexOf(item) != -1; // 保留项
if (Tin.some(include)) {
nlist.push(dd);
}
} else {
nlist.push(dd);
}
} //else if cc
}//for cnt
var no = dlist.length <= 10 ? emojino[dlist.length] : dlist.length
if (dlist.length > 0) {
if (Pntf0 != 0) { $notify("🤖 " + "分流引用 ➟ " + "⟦" + subtag + "⟧", "⛔️ 禁用: " + Tout, "☠️ 已禁用以下" + no + "条匹配规则:" + "\n ⨷ " + dlist.join("\n ⨷ "), rule_link) }
} else { $notify("🤖 " + "分流引用 ➟ " + "⟦" + subtag + "⟧", "⛔️ 禁用: " + Tout, "⚠️ 未发现任何匹配项, 请检查参数或原始链接", nan_link) }
if (Tin != "" && Tin != null) { //有 in 跟 out 参数时
if (nlist.length > 0) {
var noin0 = nlist.length <= 10 ? emojino[nlist.length] : nlist.length
if (Pntf0 != 0) {
$notify("🤖 " + "分流引用 ➟ " + "⟦" + subtag + "⟧", "✅ 保留:" + Tin, "🎯 已保留以下 " + noin0 + "条匹配规则:" + "\n ⨁ " + nlist.join("\n ⨁ "), rule_link)
}
} else {
$notify("🤖 " + "分流引用 ➟ " + "⟦" + subtag + "⟧", "✅ 保留:" + Tin + ",⛔️ 禁用: " + Tout, "⚠️ 筛选后剩余规则数为 0️⃣ 条, 请检查参数及原始链接", nan_link)
}
} else {// if Tin (No Tin)
if (nlist.length == 0) {
$notify("🤖 " + "分流引用 ➟ " + "⟦" + subtag + "⟧", "⛔️ 禁用: " + Tout, "⚠️ 筛选后剩余规则数为 0️⃣ 条, 请检查参数及原始链接", nan_link)
}
}
nlist =Phide ==1? nlist : [...dlist,...nlist]
return nlist;
} else if (Tin != "" && Tin != null) { //if Tout
var dlist = [];
for (var i = 0; i < cnt.length; i++) {
cc = cnt[i].replace(/^\s*\-\s/g,"").trim()
const RuleCheck = (item) => cc.indexOf(item) != -1; //无视注释行
if (!RuleK.some(RuleCheck) && cc) { //if Pout.some, 不操作注释项
dd = Rule_Policy(cc);
const include = (item) => dd.indexOf(item) != -1; // 保留项
if (Tin.some(include)) {
nlist.push(dd);
} else { dlist.push("-" + dd) }
}
} // for cnt
if (nlist.length > 0) {
var noin = nlist.length <= 10 ? emojino[nlist.length] : nlist.length
if (Pntf0 != 0) {
$notify("🤖 " + "分流引用 ➟ " + "⟦" + subtag + "⟧", "✅ 保留:" + Tin, "🎯 已保留以下 " + noin + "条匹配规则:" + "\n ⨁ " + nlist.join("\n ⨁ "), rule_link)
}
} else { $notify("🤖 " + "分流引用 ➟ " + "⟦" + subtag + "⟧", "✅ 保留:" + Tin, "⚠️ 筛选后剩余规则数为 0️⃣ 条, 请检查参数及原始链接", nan_link) }
nlist =Phide ==1? nlist : [...dlist,...nlist]
return nlist;
} else { //if Tin
return cnt.map(Rule_Policy)
}
}
function Rule_Policy(content) { //增加、替换 policy
var cnt = content.replace(/^\s*\-\s/g,"").replace(/REJECT-TINYGIF/gi,"reject").trim().split(",");
var RuleK = ["//", "#", ";"];
var RuleK1 = ["host", "domain", "ip-cidr", "geoip", "user-agent", "ip6-cidr"];
const RuleCheck = (item) => cnt[0].trim().toLowerCase().indexOf(item) == 0; //无视注释行
const RuleCheck1 = (item) => cnt[0].trim().toLowerCase().indexOf(item) != -1; //无视 quanx 不支持的规则类别
if (RuleK1.some(RuleCheck1)) {
if (cnt.length == 3 && cnt.indexOf("no-resolve") == -1) {
ply0 = Ppolicy != "Shawn" ? Ppolicy : cnt[2]
nn = cnt[0] + ", " + cnt[1] + ", " + ply0
} else if (cnt.length == 2) { //Surge rule-set
ply0 = Ppolicy != "Shawn" ? Ppolicy : "Shawn"
nn = cnt[0] + ", " + cnt[1] + ", " + ply0
} else if (cnt.length == 3 && cnt[2].indexOf("no-resolve") != -1) {
ply0 = Ppolicy != "Shawn" ? Ppolicy : "Shawn"
nn = cnt[0] + ", " + cnt[1] + ", " + ply0 + ", " + cnt[2]
} else if (cnt.length == 4 && cnt[3].indexOf("no-resolve") != -1) {
ply0 = Ppolicy != "Shawn" ? Ppolicy : cnt[2]
nn = cnt[0] + ", " + cnt[1] + ", " + ply0 + ", " + cnt[3]
} else if (!RuleK.some(RuleCheck) && content) {
//$notify("未能解析" + "⟦" + subtag + "⟧" + "其中部分规则:", content, nan_link);
return ""
} else { return "" }
if (cnt[0].indexOf("URL-REGEX") != -1 || cnt[0].indexOf("PROCESS") != -1) {
nn = ""
} else { nn = nn.replace("IP-CIDR6", "ip6-cidr") }
return nn
} else { return "" }//if RuleK1 check
}
// Domain-Set
function Domain2Rule(content) {
var cnt = content.split("\n");
var RuleK = ["//", "#", ";"]
var nlist = []
for (var i = 0; i< cnt.length; i++) {
cc = cnt[i].trim();
const RuleCheck = (item) => cc.indexOf(item) != -1; //无视注释行
if(!RuleK.some(RuleCheck) && cc) {
if (cc[0] == "."){
nlist.push("host-suffix, " + cc.slice(1 , cc.length) )
} else {
nlist.push("host, " + cc )
}
}
}
return nlist.join("\n")
}
// 正则替换 filter/rewrite 的部分
// 用途:如 tiktok 换区: JP -> KR ,如淘宝比价脚本 -> lite 横幅通知版本
function ReplaceReg(cnt, para) {
var cnt0 = cnt//.join("\n")
//$notify("0","",cnt0)
var pp = para.replace(/\\\@/g,"atsymbol").replace(/\\\+/g,"plussymbol").split("+");
for (var i = 0; i < pp.length; i++) {
var p1 = decodeURIComponent(pp[i].split("@")[0]).replace(/atsymbol/g,"\@").replace(/plussymbol/g,"\\\+");
var p2 = decodeURIComponent(pp[i].split("@")[1]).replace(/atsymbol/g,"@").replace(/plussymbol/g,"+");
p1 = new RegExp(p1, "gmi");
cnt0 = cnt0.map(item => item.replace(p1, p2));
//$notify(p1,p2,cnt0)
}
//$notify("1","",cnt0)
return cnt0//.split("\n")
}
//混合订阅类型,用于未整体进行 base64 encode 的类型
function Subs2QX(subs, Pudp, Ptfo, Pcert, Ptls13) {
var list0 = subs.split("\n");
var QuanXK = ["shadowsocks=", "trojan=", "vmess=", "http="];
var SurgeK = ["=ss,", "=vmess,", "=trojan,", "=http,", "=custom,"];
var LoonK = ["=shadowsocks", "=shadowsocksr"]
var QXlist = [];
var failedList = [];
for (var i = 0; i < list0.length; i++) {
var node = ""
if (list0[i].trim().length > 3 && !/\;|\/|\#/.test(list0[i][0])) {
var type = list0[i].split("://")[0].trim()
var listi = list0[i].replace(/ /g, "")
var tag0 = list0[i].indexOf("tag=")!=-1 ? list0[i].split(/\&*(emoji|udp|tf0|cert|rename|replace)\=/)[0].split("tag=")[1] : ""
list0[i] = (type == "vmess" || type=="ssr") ? list0[i].split("#")[0] : list0[i]
const NodeCheck = (item) => listi.toLowerCase().indexOf(item) != -1;
try {
if (type == "vmess" && list0[i].indexOf("remarks=") == -1) {
var bnode = Base64.decode(list0[i].split("vmess://")[1])
if (bnode.indexOf("over-tls=") == -1) { //v2rayN
node = V2QX(list0[i], Pudp, Ptfo, Pcert, Ptls13)
} else { //quantumult 类型
node = VQ2QX(list0[i], Pudp, Ptfo, Pcert, Ptls13)
}
node = tag0 != "" ? URI_TAG(node, tag0) : node
} else if (type == "vmess" && list0[i].indexOf("remarks=") != -1) { //shadowrocket 类型
node = VR2QX(list0[i], Pudp, Ptfo, Pcert, Ptls13)
node = tag0 != "" ? URI_TAG(node, tag0) : node
} else if (type == "ssr") {
node = SSR2QX(list0[i], Pudp, Ptfo)
node = tag0 != "" ? URI_TAG(node, tag0) : node
} else if (type == "ss") {
node = SS2QX(list0[i], Pudp, Ptfo)
node = tag0 != "" ? URI_TAG(node, tag0) : node
} else if (type == "ssd") {
node = SSD2QX(list0[i], Pudp, Ptfo)
} else if (type == "trojan") {
node = TJ2QX(list0[i], Pudp, Ptfo, Pcert, Ptls13)
node = tag0 != "" ? URI_TAG(node, tag0) : node
} else if (type == "https" && list0[i].indexOf(",") == -1) {
if (listi.indexOf("@") != -1) {
node = HPS2QX(list0[i], Ptfo, Pcert, Ptls13)
node = tag0 != "" ? URI_TAG(node, tag0) : node
} else {
var listh = Base64.decode(listi.split("https://")[1].split("#")[0])
listh = "https://" + listh + "#" + listi.split("https://")[1].split("#")[1]
node = HPS2QX(listh, Ptfo, Pcert, Ptls13)