-
Notifications
You must be signed in to change notification settings - Fork 2
/
blash.cmd
3303 lines (2588 loc) · 97.1 KB
/
blash.cmd
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
@REM author: wzdnzd
@REM date: 2022-08-24
@REM describe: network proxy controller for clash
@echo off & PUSHD %~DP0 & cd /d "%~dp0"
@REM change encoding
chcp 65001 >nul 2>nul
@REM https://blog.csdn.net/sanqima/article/details/37818115
setlocal enableDelayedExpansion
@REM output with color
call :setESC
@REM call workflow
goto :workflow
@REM ########################
@REM function define blow ###
@REM ########################
@REM process pipeline
:workflow
@REM batch file name
set "batname=%~nx0"
@REM microsoft terminal displays differently from cmd and powershell
@REM call :ismsterminal msterminal
set "msterminal=1"
@REM enable create shortcut
set "enableshortcut=1"
@REM enable download config from remote
set "enableremoteconf=1"
set "remoteurl="
@REM validate configuration files before starting
set "verifyconf=0"
@REM check and update wintun.dll
set "checkwintun=0"
@REM info color
set "infocolor=92"
set "warncolor=93"
if "!msterminal!" == "1" (
set "infocolor=95"
set "warncolor=97"
)
@REM print heart
set "customize=0"
set "drawheart=0"
@REM exit flag
set "shouldexit=0"
@REM init
set "initflag=0"
@REM configuration file name
set "configuration=config.yaml"
@REM subscription link
set "sublink="
set "isweblink=0"
@REM check
set "testflag=0"
@REM repair
set "repair=0"
@REM only reload
set "reloadonly=0"
@REM restart clash.exe
set "restartflag=0"
@REM close proxy
set "killflag=0"
@REM update
set "updateflag=0"
@REM purge
set "purgeflag=0"
@REM only update subscriptions and rulesets
set "quickflag=0"
@REM don't update subscription
set "exclude=0"
@REM use clash.meta
set "clashmeta=0"
@REM use clash.premium
set "clashpremium=0"
@REM alpha version allowed
set "alpha=0"
@REM simplified mode
set "brief=0"
@REM regenerates auto update script
set "regenerate=0"
@REM yacd dashboard, see https://github.com/MetaCubeX/Yacd-meta or https://github.com/haishanh/yacd
set "yacd=0"
@REM metacubexd, see https://github.com/MetaCubeX/metacubexd
set "metacubexd=0"
@REM run on background
set "asdaemon=0"
@REM show window
set "show=0"
@REM setting workspace
set "dest="
@REM network proxy registry configuration path
set "proxyregpath=HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
@REM autostart registry configuration path
set "autostartregpath=HKCU\Software\Microsoft\Windows\CurrentVersion\Run"
set "startupapproved=HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\Run"
@REM parse arguments
call :argsparse %*
@REM invalid arguments
if "!shouldexit!" == "1" exit /b 1
@REM regular file path
if "!dest!" == "" set "dest=%~dp0"
call :pathregular dest "!dest!"
@REM auto start vb script
set "startupvbs=!dest!\startup.vbs"
@REM auto update vb script
set "updatevbs=!dest!\update.vbs"
@REM draw a heart
if "!drawheart!"== "1" goto :printheart
@REM close network proxy
if "!killflag!" == "1" goto :closeproxy
@REM clean all setting
if "!purgeflag!" == "1" goto :purge
@REM prevent precheck if no action
if "!reloadonly!" == "0" if "!restartflag!" == "0" if "!repair!" == "0" if "!testflag!" == "0" if "!updateflag!" == "0" if "!initflag!" == "0" (
@REM @echo [%ESC%[91m错误%ESC%[0m] 必须包含 [%ESC%[!warncolor!m-f%ESC%[0m %ESC%[!warncolor!m-i%ESC%[0m %ESC%[!warncolor!m-k%ESC%[0m %ESC%[!warncolor!m-r%ESC%[0m %ESC%[!warncolor!m-t%ESC%[0m %ESC%[!warncolor!m-u%ESC%[0m] 中的一种操作
@REM @echo.
if "!shouldexit!" == "0" goto :usage
exit /b
)
@REM config file path
call :precheck configfile
if "!configfile!" == "" exit /b 1
@REM connectivity test
if "!testflag!" == "1" (
call :checkconnect available 1
exit /b
)
@REM reload config
if "!reloadonly!" == "1" goto :reload
@REM update
if "!restartflag!" == "1" goto :restartprogram
@REM check issues
if "!repair!" == "1" goto :resolveissues
@REM update
if "!updateflag!" == "1" goto :updateplugins
@REM init
if "!initflag!" == "1" goto :initialize
@REM unknown command
@REM if "!shouldexit!" == "0" goto :usage
exit /b
@REM check if the configuration file exists
:precheck <result>
set "%~1="
set "subfile=!temp!\clashsub.yaml"
@REM absolute path
call :pathconvert conflocation "!configuration!"
call :pathregular conflocation "!conflocation!"
if "!conflocation!" == "" (
@echo [%ESC%[91m错误%ESC%[0m] 配置文件路径%ESC%[91m无效%ESC%[0m
exit /b 1
)
@REM cannot contain whitespace in path
if "!conflocation!" NEQ "!conflocation: =!" (
@echo [%ESC%[91m错误%ESC%[0m] 无效的配置文件 "%ESC%[!warncolor!m!conflocation!%ESC%[0m", 路径不能包含%ESC%[!warncolor!m空格%ESC%[0m
exit /b 1
)
if "!isweblink!" == "1" (
if exist "!conflocation!" (
set "tips=[%ESC%[!warncolor!m警告%ESC%[0m] %ESC%[!warncolor!m已存在%ESC%[0m配置文件 "%ESC%[!warncolor!m!conflocation!%ESC%[0m" 会被%ESC%[91m覆盖%ESC%[0m,是否继续? (%ESC%[!warncolor!mY%ESC%[0m/%ESC%[!warncolor!mN%ESC%[0m) "
if "!msterminal!" == "1" (
choice /t 6 /d n /n /m "!tips!"
) else (
set /p "=!tips!" <nul
choice /t 6 /d n /n
)
if !errorlevel! == 2 exit /b 1
)
@REM try to download
del /f /q "!subfile!" >nul 2>nul
set "statuscode=000"
for /f %%a in ('curl --retry 3 --retry-max-time 30 -m 60 --connect-timeout 30 -L -s -o "!subfile!" -w "%%{http_code}" -H "User-Agent: Clash" "!sublink!"') do set "statuscode=%%a"
@REM download success
if "!statuscode!" == "200" (
set "filesize=0"
if exist "!subfile!" (for %%a in ("!subfile!") do set "filesize=%%~za")
if !filesize! GTR 64 (
@REM validate
set "content="
for /f "tokens=*" %%a in ('findstr /i /r /c:"^external-controller:[ ][ ]*.*:[0-9][0-9]*.*" !subfile!') do set "content=%%a"
if "!content!" == "" (
@echo [%ESC%[91m错误%ESC%[0m] 订阅 "%ESC%[!warncolor!m!sublink!%ESC%[0m" 无效,请检查确认
exit /b 1
)
del /f /q "!conflocation!" >nul 2>nul
call :splitpath filepath filename "!conflocation!"
call :makedirs success "!filepath!"
if "!success!" == "0" (
@echo [%ESC%[91m错误%ESC%[0m] 创建文件夹 "%ESC%[!warncolor!m!filepath!%ESC%[0m" %ESC%[91m失败%ESC%[0m,请确认路径是否合法
exit /b 1
)
move "!subfile!" "!conflocation!" >nul 2>nul
@echo [%ESC%[!infocolor!m信息%ESC%[0m] 订阅下载%ESC%[!infocolor!m成功%ESC%[0m
@REM 保存订阅链接
@echo !sublink! > "!filepath!\subscriptions.txt"
) else (
@REM output is empty
set "statuscode=000"
)
)
if "!statuscode!" NEQ "200" (
@echo [%ESC%[91m错误%ESC%[0m] 订阅下载%ESC%[91m失败%ESC%[0m, 请检查确认此订阅是否有效
exit /b 1
)
)
if not exist "!conflocation!" (
@echo [%ESC%[91m错误%ESC%[0m] 配置文件 "%ESC%[!warncolor!m!conflocation!%ESC%[0m" %ESC%[91m不存在%ESC%[0m
goto :eof
)
@REM validate
set "content="
for /f "tokens=1* delims=:" %%a in ('findstr /i /r /c:"^proxy-groups:[ ]*" "!conflocation!"') do set "content=%%a"
call :trim content "!content!"
if "!content!" NEQ "proxy-groups" (
@echo [%ESC%[91m错误%ESC%[0m] %ESC%[91m无效%ESC%[0m的配置文件 "%ESC%[!warncolor!m!conflocation!%ESC%[0m"
exit /b 1
)
set "%~1=!conflocation!"
goto :eof
@REM Initialize network proxy
:initialize
set "tips=[%ESC%[!warncolor!m提示%ESC%[0m] 网络代理程序将在目录 "%ESC%[!warncolor!m!dest!%ESC%[0m" 安装并运行,是否继续?(%ESC%[!warncolor!mY%ESC%[0m/%ESC%[!warncolor!mN%ESC%[0m) "
if "!msterminal!" == "1" (
choice /t 5 /d n /n /m "!tips!"
) else (
set /p "=!tips!" <nul
choice /t 5 /d n /n
)
if !errorlevel! == 2 exit /b 1
set "quickflag=0"
set "exclude=1"
call :updateplugins
goto :eof
@REM fix network issues
:resolveissues
@REM mandatory use of the stable version
set "alpha=0"
@echo [%ESC%[!infocolor!m信息%ESC%[0m] 开始检查并尝试修复网络代理,请稍等
@REM check status
call :checkconnect available 0
set "lazycheck=0"
if "!available!" == "1" (
set "tips=[%ESC%[!warncolor!m提示%ESC%[0m] 代理网络运行%ESC%[!infocolor!m正常%ESC%[0m,%ESC%[91m不存在%ESC%[0m问题,是否继续?(%ESC%[!warncolor!mY%ESC%[0m/%ESC%[!warncolor!mN%ESC%[0m) "
if "!msterminal!" == "1" (
choice /t 5 /d n /n /m "!tips!"
) else (
set /p "=!tips!" <nul
choice /t 5 /d n /n
)
if !errorlevel! == 2 exit /b 1
) else (
@REM running detect
call :isrunning status
if "!status!" == "0" (
call :checkwapper continue 1
if "!continue!" == "0" exit /b
) else set "lazycheck=1"
)
@REM O: Reload | R: Restart | U: Restore | N: Cancel
set "tips=[%ESC%[!warncolor!m提示%ESC%[0m] 按 %ESC%[!warncolor!mO%ESC%[0m %ESC%[!warncolor!m重载%ESC%[0m,按 %ESC%[!warncolor!mR%ESC%[0m %ESC%[!warncolor!m重启%ESC%[0m,按 %ESC%[!warncolor!mU%ESC%[0m %ESC%[!warncolor!m恢复%ESC%[0m至默认,按 %ESC%[!warncolor!mN%ESC%[0m %ESC%[!warncolor!m取消%ESC%[0m (%ESC%[!warncolor!mO%ESC%[0m/%ESC%[!warncolor!mR%ESC%[0m/%ESC%[!warncolor!mU%ESC%[0m/%ESC%[!warncolor!mN%ESC%[0m) "
if "!msterminal!" == "1" (
choice /t 6 /c ORUN /d R /n /m "!tips!"
) else (
set /p "=!tips!" <nul
choice /t 6 /c ORUN /d R /n
)
if !errorlevel! == 1 (
call :reload
) else if !errorlevel! == 2 (
call :restartprogram
) else if !errorlevel! == 3 (
@REM kill clash process
call :killprocesswrapper
@REM lazy check
if "!lazycheck!" == "1" (
call :checkwapper continue 0
if "!continue!" == "0" exit /b
)
@REM restore plugins
call :updateplugins
) else (
:: cancel
exit /b
)
for /l %%i in (1,1,5) do (
@REM recheck
call :checkconnect available 0
if "!available!" == "1" (
@echo [%ESC%[!infocolor!m信息%ESC%[0m] 问题修复%ESC%[!infocolor!m成功%ESC%[0m,网络代理可%ESC%[!infocolor!m正常%ESC%[0m使用
exit /b
) else (
@REM wait
timeout /t 1 /nobreak >nul 2>nul
)
)
@echo [%ESC%[91m错误%ESC%[0m] 问题修复%ESC%[91m失败%ESC%[0m, 网络代理仍%ESC%[91m无法%ESC%[0m使用, 请尝试其他方法
goto :eof
@REM check if the network is available
:checkwapper <result> <enable>
set "%~1=1"
call :trim loglevel "%~2"
if "!loglevel!" == "" set "loglevel=1"
call :isavailable available 0 "https://www.baidu.com" ""
if "!available!" == "0" (
@echo [%ESC%[91m错误%ESC%[0m] 网络%ESC%[91m不可用%ESC%[0m, 但代理程序%ESC%[91m并未运行%ESC%[0m,请检查你的%ESC%[!warncolor!m本地网络%ESC%[0m是否正常
@REM should terminate
set "%~1=0"
exit /b
)
if "!loglevel!" == "1" (
@echo [%ESC%[!warncolor!m提示%ESC%[0m] 网络代理%ESC%[91m没有开启%ESC%[0m, 推荐选择 %ESC%[!warncolor!mRestart%ESC%[0m 开启
)
goto :eof
@REM update workflow
:updateplugins
set "downloaded=0"
if "!quickflag!" == "1" (
call :quickupdate modified
if "!modified!" == "0" (exit /b 0) else (set "downloaded=1")
)
@REM run as admin
if "!asdaemon!" == "1" (
cacls "%SystemDrive%\System Volume Information" >nul 2>&1 || (start "" mshta vbscript:CreateObject^("Shell.Application"^).ShellExecute^("%~snx0"," %*","","runas",!show!^)^(window.close^)&exit /b)
)
@REM prepare all plugins
call :prepare changed 1 !downloaded!
@REM no new version found
if "!changed!" == "0" (
@echo [%ESC%[!infocolor!m信息%ESC%[0m] 当前已是最新版本,无需更新
) else (
@REM wait for overwrite files
timeout /t 1 /nobreak >nul 2>nul
)
@REM postclean
call :cleanworkspace "!temp!"
@REM startup
call :startclash
@REM regenerate auto update script
if "!regenerate!" == "1" call :generateupdatevbs
goto :eof
@REM parse and validate arguments
:argsparse
set result=false
if "%1" == "-a" set result=true
if "%1" == "--alpha" set result=true
if "!result!" == "true" (
set "alpha=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-b" set result=true
if "%1" == "--brief" set result=true
if "!result!" == "true" (
set "brief=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-c" set result=true
if "%1" == "--conf" set result=true
if "!result!" == "true" (
@REM validate argument
call :trim subscription "%~2"
if "!subscription!" == "" set result=false
if "!subscription:~0,2!" == "--" set result=false
if "!subscription:~0,1!" == "-" set result=false
if "!result!" == "false" (
@echo [%ESC%[91m错误%ESC%[0m] 如果指定参数 "%ESC%[!warncolor!m--conf%ESC%[0m" 或者 "%ESC%[!warncolor!m-c%ESC%[0m" 则必须提供有效的%ESC%[!warncolor!m配置文件%ESC%[0m或%ESC%[!warncolor!m订阅%ESC%[0m
@echo.
goto :usage
)
if "!subscription:~0,8!" == "https://" set "isweblink=1"
if "!subscription:~0,7!" == "http://" set "isweblink=1"
if "!isweblink!" == "1" (
set "invalid=0"
@REM include '"' see https://stackoverflow.com/questions/46238709/how-to-detect-if-input-is-quote
@echo !subscription! | findstr /i /r /c:"\"^" >nul && (set "invalid=1")
@REM contain whitespace
if "!subscription!" neq "!subscription: =!" set "invalid=1"
@REM match url
echo "!subscription!" | findstr /i /r /c:^"\"http.*://.*[a-zA-Z0-9][a-zA-Z0-9]*\"^" >nul 2>nul || (set "invalid=1")
if "!invalid!" == "1" (
set "shouldexit=1"
@echo [%ESC%[91m错误%ESC%[0m] 无效的订阅链接 "%ESC%[!warncolor!m!subscription!%ESC%[0m"
@echo.
goto :eof
)
set "sublink=!subscription!"
) else (
set "invalid=1"
if "!subscription:~-5!" == ".yaml" (set "invalid=0") else (
if "!subscription:~-4!" == ".yml" (set "invalid=0")
)
if "!invalid!" == "0" (
set "configuration=!subscription!"
) else (
set "shouldexit=1"
@echo [%ESC%[91m错误%ESC%[0m] 无效的配置文件 "%ESC%[!warncolor!m!subscription!%ESC%[0m",仅支持 "%ESC%[!warncolor!m.yaml%ESC%[0m" 和 "%ESC%[!warncolor!m.yml%ESC%[0m" 格式
@echo.
goto :eof
)
)
shift & shift & goto :argsparse
)
if "%1" == "-d" set result=true
if "%1" == "--daemon" set result=true
if "!result!" == "true" (
set "asdaemon=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-e" set result=true
if "%1" == "--exclude" set result=true
if "!result!" == "true" (
set "exclude=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-f" set result=true
if "%1" == "--fix" set result=true
if "!result!" == "true" (
set "repair=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-g" set result=true
if "%1" == "--generate" set result=true
if "!result!" == "true" (
set "regenerate=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-h" set result=true
if "%1" == "--help" set result=true
if "!result!" == "true" (
call :usage
)
if "%1" == "-i" set result=true
if "%1" == "--init" set result=true
if "!result!" == "true" (
set "initflag=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-k" set result=true
if "%1" == "--kill" set result=true
if "!result!" == "true" (
set "killflag=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-l" set result=true
if "%1" == "--love" set result=true
if "!result!" == "true" (
if "!customize!" == "1" (
set "drawheart=1"
set result=false
shift & goto :argsparse
) else (
@echo [%ESC%[91m错误%ESC%[0m] 未知参数:%ESC%[91m%1%ESC%[0m
@echo.
goto :usage
)
)
if "%1" == "-m" set result=true
if "%1" == "--meta" set result=true
if "!result!" == "true" (
set "clashmeta=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-n" set result=true
if "%1" == "--native" set result=true
if "!result!" == "true" (
set "clashpremium=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-o" set result=true
if "%1" == "--overload" set result=true
if "!result!" == "true" (
set "reloadonly=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-p" set result=true
if "%1" == "--purge" set result=true
if "!result!" == "true" (
set "purgeflag=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-q" set result=true
if "%1" == "--quick" set result=true
if "!result!" == "true" (
set "quickflag=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-r" set result=true
if "%1" == "--restart" set result=true
if "!result!" == "true" (
set "restartflag=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-s" set result=true
if "%1" == "--show" set result=true
if "!result!" == "true" (
set "show=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-t" set result=true
if "%1" == "--test" set result=true
if "!result!" == "true" (
set "testflag=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-u" set result=true
if "%1" == "--update" set result=true
if "!result!" == "true" (
set "updateflag=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-w" set result=true
if "%1" == "--workspace" set result=true
if "!result!" == "true" (
@REM validate argument
call :trim param "%~2"
if "!param!" == "" set result=false
if "!param:~0,2!" == "--" set result=false
if "!param:~0,1!" == "-" set result=false
if "!result!" == "false" (
@echo [%ESC%[91m错误%ESC%[0m] 无效的参数,如果指定 "%ESC%[!warncolor!m--workspace%ESC%[0m","%ESC%[!warncolor!m!param!%ESC%[0m",则需提供有效的路径
@echo.
goto :usage
)
call :pathconvert directory "!param!"
if not exist "!directory!" (
call :makedirs success "!directory!"
if "!success!" == "1" (rd "!directory!" /s /q >nul 2>nul) else (set "shouldexit=1")
)
if "!shouldexit!" == "1" (
@echo [%ESC%[91m错误%ESC%[0m] 参数 "%ESC%[!warncolor!m--workspace%ESC%[0m" 指定的文件夹路径 "%ESC%[!warncolor!m!directory!%ESC%[0m" %ESC%[91m无效%ESC%[0m
@echo.
goto :eof
)
set "dest=!directory!"
set result=false
shift & shift & goto :argsparse
)
if "%1" == "-x" set result=true
if "%1" == "--metacubexd" set result=true
if "!result!" == "true" (
set "yacd=0"
set "metacubexd=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-y" set result=true
if "%1" == "--yacd" set result=true
if "!result!" == "true" (
set "yacd=1"
set "metacubexd=0"
set result=false
shift & goto :argsparse
)
@REM will throw exception if this code not in here or delete it or merge with <if "%1" NEQ "">. why?
if "%1" == "" goto :eof
if "%1" NEQ "" (
call :trim syntax "%~1"
if "!syntax!" == "goto" (
call :trim funcname "%~2"
if "!funcname!" == "" (
@echo [%ESC%[91m错误%ESC%[0m] 无效的语法,调用 "%ESC%[!warncolor!mgoto%ESC%[0m" 时必须提供函数名
goto :usage
)
for /f "tokens=1-2,* delims= " %%a in ("%*") do set "params=%%c"
if "!params!" == "" (
call !funcname!
exit /b
) else (
call !funcname! !params!
exit /b
)
)
@echo [%ESC%[91m错误%ESC%[0m] 未知参数:%ESC%[91m%1%ESC%[0m
@echo.
goto :usage
)
goto :eof
@REM help
:usage
@echo 使用方法:!batname! [%ESC%[!warncolor!m功能选项%ESC%[0m] [%ESC%[!warncolor!m其他参数%ESC%[0m],支持 %ESC%[!warncolor!m-%ESC%[0m 和 %ESC%[!warncolor!m--%ESC%[0m 两种模式
@echo.
@echo 功能选项:
@REM @echo. if this line contains Chinese output, it will be garbled. Why? ? ? >_<
@echo -f, --fix 检查并尝试修复代理网络
@echo -h, --help 打印帮助信息
@echo -i, --init 利用 %ESC%[!warncolor!m--conf%ESC%[0m 提供的配置文件创建代理网络
@REM @echo. if this line contains Chinese output, it will be garbled. Why? ? ? >_<
@echo -k, --kill 退出网络代理程序
@REM @echo. if this line contains Chinese output, it will be garbled. Why? ? ? >_<
if "!customize!" == "1" (
@echo -l, --love 当然是大声告诉我宝我爱她啦🤪🤪🤪
)
@REM @echo. if this line contains Chinese output, it will be garbled. Why? ? ? >_<
@echo -o, --overload 重新加载配置文件
@echo -p, --purge 关闭系统代理并禁止程序开机自启,取消自动更新
@REM @echo. if this line contains Chinese output, it will be garbled. Why? ? ? >_<
@echo -r, --restart 重启网络代理程序
@echo -t, --test 测试代理网络是否可用
@echo -u, --update 更有所有组件,包括 clash.exe、订阅、代理规则以及 IP 地址数据库等
echo.
@echo 其他参数:
@REM @echo. if this line contains Chinese output, it will be garbled. Why? ? ? >_<
@echo -a, --alpha 是否允许使用预览版,默认为稳定版,搭配 %ESC%[!warncolor!m-i%ESC%[0m 或者 %ESC%[!warncolor!m-u%ESC%[0m 使用
@REM @echo. if this line contains Chinese output, it will be garbled. Why? ? ? >_<
@echo -b, --brief 精简模式运行,没有明确配置dashboard情况下,无法使用可视化页面
@REM @echo. if this line contains Chinese output, it will be garbled. Why? ? ? >_<
@echo -c, --conf 配置文件,支持本地配置文件和订阅链接,默认为当前目录下的 %ESC%[!warncolor!mconfig.yaml%ESC%[0m
@REM @echo. if this line contains Chinese output, it will be garbled. Why? ? ? >_<
@echo -d, --daemon 后台静默执行,禁止打印日志
@REM @echo. if this line contains Chinese output, it will be garbled. Why? ? ? >_<
@echo -e, --exclude 更新时跳过代理集中配置的订阅
@REM @echo. if this line contains Chinese output, it will be garbled. Why? ? ? >_<
@echo -g, --generate 重新生成自动检查更新的脚本,搭配 %ESC%[!warncolor!m-u%ESC%[0m 使用
@REM @echo. if this line contains Chinese output, it will be garbled. Why? ? ? >_<
@echo -m, --meta 如果配置兼容,使用 clash.meta 代替 clash.premium,搭配 %ESC%[!warncolor!m-i%ESC%[0m 或 %ESC%[!warncolor!m-u%ESC%[0m 使用
@REM @echo. if this line contains Chinese output, it will be garbled. Why? ? ? >_<
@echo -n, --native 强制使用 clash.premium,搭配 %ESC%[!warncolor!m-i%ESC%[0m 或 %ESC%[!warncolor!m-u%ESC%[0m 使用
@echo -q, --quick 仅更新新订阅和代理规则,搭配 %ESC%[!warncolor!m-u%ESC%[0m 使用
@REM @echo. if this line contains Chinese output, it will be garbled. Why? ? ? >_<
@echo -s, --show 新窗口中执行,默认为当前窗口
@REM @echo. if this line contains Chinese output, it will be garbled. Why? ? ? >_<
@echo -w, --workspace 代理程序运行路径,默认为当前脚本所在目录
@REM @echo. if this line contains Chinese output, it will be garbled. Why? ? ? >_<
@echo -x, --metacubexd 使用 %ESC%[!warncolor!mmetacubexd%ESC%[0m 控制面板,搭配 %ESC%[!warncolor!m-i%ESC%[0m 或 %ESC%[!warncolor!m-u%ESC%[0m 使用
@REM @echo. if this line contains Chinese output, it will be garbled. Why? ? ? >_<
@echo -y, --yacd 使用 %ESC%[!warncolor!myacd%ESC%[0m 控制面板,搭配 %ESC%[!warncolor!m-i%ESC%[0m 或 %ESC%[!warncolor!m-u%ESC%[0m 使用
@echo.
set "shouldexit=1"
goto :eof
@REM draw heart
:printheart
set "wthitespace="
@echo.
@echo !wthitespace! ********* *********
@echo !wthitespace! ***************** *****************
@echo !wthitespace! *****************************************
@echo !wthitespace! *******************************************
@echo !wthitespace!*********************************************
@echo !wthitespace!**********************************************
@echo !wthitespace!**********************************************
@echo !wthitespace!**********************************************
if "!msterminal!" == "1" (
@echo !wthitespace!*********** %ESC%[91m我的宝,我爱你 ♥♥♥%ESC%[0m *************
) else (
@echo !wthitespace!*********** %ESC%[91m我的宝,我爱你 ♥♥♥%ESC%[0m ***************
)
@echo !wthitespace!********** ***********
@echo !wthitespace! ******** %ESC%[91m因为有你,生活可爱了许多%ESC%[0m *********
@echo !wthitespace! *****************************************
@echo !wthitespace! ***************************************
@echo !wthitespace! *************************************
@echo !wthitespace! ***********************************
@echo !wthitespace! *********************************
@echo !wthitespace! *****************************
@echo !wthitespace! *************************
@echo !wthitespace! *********************
@echo !wthitespace! ***************
@echo !wthitespace! *********
@echo !wthitespace! ***
@echo !wthitespace! *
@echo.
exit /b
goto :eof
@REM confirm download url and filename according parameters
:versioned <geosite> <subfiles>
set "%~1=0"
set "content="
set "needgeosite=0"
@REM yacd dashboard
if "!metacubexd!" == "0" if "!dashboard!" NEQ "" if exist "!dashboard!\yacd.ico" (
set "yacd=1"
set "metacubexd=0"
)
@REM metacubexd dashboard
if "!yacd!" == "0" if "!dashboard!" NEQ "" if exist "!dashboard!\maskable-icon-512x512.png" (
set "yacd=0"
set "metacubexd=1"
)
@REM force use clash.premium
if "!clashpremium!" == "1" (
set "clashmeta=0"
goto :eof
)
for /f "tokens=*" %%i in ('findstr /i /r "GEOSITE,.*" "!configfile!"') do set "content=!content!;%%i"
call :searchrules notfound "!content!"
if "!notfound!" == "1" (
for /f "tokens=*" %%i in ('findstr /i /r "SUB-RULE,.* AND,.* OR,.* NOT,.* IN-TYPE,.*" "!configfile!"') do set "content=!content!;%%i"
call :searchrules notfound "!content!"
) else (
set "needgeosite=1"
)
@REM rulesets include GEOSITE, must be clash.meta
if "!notfound!" == "0" (set "clashmeta=1")
if "!clashmeta!" == "1" (
set "%~1=!needgeosite!"
set "clashpremium=0"
goto :eof
)
@REM clash.meta not support SCRIPT rule
set "content="
for /f "tokens=*" %%i in ('findstr /i /r "SCRIPT,.*" "!configfile!"') do set "content=!content!;%%i"
call :searchrules notfound "!content!"
@REM rulesets include SCRIPT, must be clash.premium
if "!notfound!" == "0" (
set "clashmeta=0"
set "clashpremium=1"
goto :eof
)
@REM include sniffer, must be clash.meta
for /f "tokens=1* delims=:" %%a in ('findstr /i /r /c:"sniffer:[ ]*" "!configfile!"') do (
call :trim sniffer %%a
if "!sniffer!" == "sniffer" (
set "clashmeta=1"
set "clashpremium=0"
goto :eof
)
)
@REM proxy-groups include exclude-filter, must be clash.meta
for /f "tokens=1* delims=:" %%a in ('findstr /i /r /c:"^[ ][ ]*exclude-filter:[ ][ ]*.*" "!configfile!"') do (
call :trim excludekey %%a
if /i "!excludekey:~0,1!" NEQ "#" (
set "clashmeta=1"
set "clashpremium=0"
goto :eof
)
)
@REM include vless or hysteria, must be clash.meta
call :trim subfiles "%~2"
set "subfiles=!configfile!,!subfiles!"
set "tempfile=!temp!\clashproxies.txt"
set "regex=^\s+(type:\s+(vless|hysteria)|client-fingerprint:\s+|flow:\s+xtls-).*"
del /f /q "!tempfile!" >nul 2>nul
for %%f in (!subfiles!) do (
if "%%f" NEQ "" if exist %%f (
call :findby "%%f" "!regex!" "!tempfile!" 1
if exist "!tempfile!" (
set "clashmeta=1"
set "clashpremium=0"
del /f /q "!tempfile!" >nul 2>nul
goto :eof
)
)
)
@REM proxy-groups include filter, must be clash.meta
@REM set "tempfile=!temp!\clashproxygroups.txt"
@REM set "regex=^\s+type:\s+(select|url-test|fallback|load-balance|relay).*"
@REM del /f /q "!tempfile!" >nul 2>nul
@REM call :findby "!configfile!" "!regex!" "!tempfile!" 10
@REM if exist "!tempfile!" (
@REM for /f "tokens=1* delims=:" %%a in ('findstr /i /r /c:"^[ ][ ]*filter:[ ][ ]*.*" "!tempfile!"') do (
@REM call :trim includekey %%a
@REM if /i "!includekey:~0,1!" NEQ "#" (
@REM set "clashmeta=1"
@REM set "clashpremium=0"
@REM del /f /q "!tempfile!" >nul 2>nul
@REM goto :eof
@REM )
@REM )
@REM del /f /q "!tempfile!" >nul 2>nul
@REM )
@REM old edittion
if exist "!dest!\clash.exe" ("!dest!\clash.exe" -v | findstr /i "Meta" >nul 2>nul && (
set "clashmeta=1"
set "clashpremium=0"
)
)
goto :eof
@REM quickly update subscriptions and rulesets
:quickupdate <edition>
set "%~1=0"
@REM configration
call :updateconfig 1
@REM subscriptions
if "!exclude!" == "0" call :updatesubs subfiles 1
@REM rulesets
call :updaterules 1
@REM detect new edition
set "clashedition=0"
if exist "!dest!\clash.exe" ("!dest!\clash.exe" -v | findstr /i "Meta" >nul 2>nul && (set "clashedition=1"))
call :versioned geositeneed !subfiles!
if "!clashedition!" NEQ "!clashmeta!" (
set "%~1=1"
if "!clashmeta!" == "1" (
set "oldedition=clash.premium"
set "newedition=clash.meta"
) else (
set "oldedition=clash.meta"
set "newedition=clash.premium"
)
@echo [%ESC%[!warncolor!m提示%ESC%[0m] 配置%ESC%[91m不兼容%ESC%[0m,代理程序需从 %ESC%[!warncolor!m!oldedition!%ESC%[0m 切换至 %ESC%[!warncolor!m!newedition!%ESC%[0m
goto :eof
)
@REM reload
if "!changed!" == "1" (goto :reload) else (goto :eof)
@REM check if special rules are included
:searchrules <notfound> <text>
set "%~1=1"
set "rulesets=%~2"
for /F "tokens=1* delims=;" %%f in ("!rulesets!") do (
:: set "rule=%%f"
call :trim rule "%%f"
if /i "!rule:~0,1!"=="-" (
set "%~1=0"