-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcommon.sh
More file actions
3244 lines (2660 loc) · 76.9 KB
/
Copy pathcommon.sh
File metadata and controls
3244 lines (2660 loc) · 76.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
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
#!/usr/bin/env bash
: "${PROJECT_DIR:=}"
: "${INSTALL_SCOPE:=}"
: "${INSTALL_HOME:=}"
: "${RUNTIME_DIR:=}"
: "${BIN_DIR:=}"
: "${LOG_DIR:=}"
: "${CONFIG_DIR:=}"
: "${RESOURCE_DIR:=}"
DEFAULT_MIHOMO_VERSION="${MIHOMO_VERSION:-v1.19.23}"
DEFAULT_CLASH_VERSION="${CLASH_VERSION:-v1.18.0}"
DEFAULT_SUBCONVERTER_VERSION="${SUBCONVERTER_VERSION:-v0.9.9}"
DEFAULT_YQ_VERSION="${YQ_VERSION:-v4.52.4}"
log() { printf '%s\n' "$*"; }
info() { printf 'ℹ %s\n' "$*"; }
success() { printf '✔ %s\n' "$*"; }
warn() { printf '⚠ %s\n' "$*" >&2; }
error() { printf '✘ %s\n' "$*" >&2; }
die() { error "$*"; exit 1; }
ui_color() {
local color="$1"
local msg="$2"
if [[ ! "$color" =~ ^#[0-9a-fA-F]{6}$ ]]; then
printf '%s\n' "$msg"
return
fi
local hex="${color#\#}"
local r=$((16#${hex:0:2}))
local g=$((16#${hex:2:2}))
local b=$((16#${hex:4:2}))
local color_code="\033[38;2;${r};${g};${b}m"
local reset_code="\033[0m"
printf "%b%s%b\n" "$color_code" "$msg" "$reset_code"
}
die_usage() {
local message="$1"
local next_step="${2:-}"
ui_error "$message"
[ -n "${next_step:-}" ] && ui_next "$next_step" >&2
exit 1
}
die_state() {
local message="$1"
local next_step="${2:-}"
ui_error "$message"
[ -n "${next_step:-}" ] && ui_next "$next_step" >&2
exit 1
}
die_missing() {
local thing="$1"
local next_step="${2:-}"
ui_error "${thing}不存在或不可用"
[ -n "${next_step:-}" ] && ui_next "$next_step" >&2
exit 1
}
ui_blank() {
echo
}
ui_title() {
local text="$1"
echo
echo "$text"
echo
}
ui_section() {
local text="$1"
echo "【$text】"
}
ui_ok() {
printf '🐱 %s\n' "$*"
}
ui_info() {
printf 'ℹ️ %s\n' "$*"
}
ui_warn() {
printf '🚨 %s\n' "$*"
}
ui_error() {
printf '❗ %s\n' "$*" >&2
}
ui_next() {
printf '👉 %s\n' "$*"
}
ui_download() {
printf '⏳ %s\n' "$*"
}
ui_target() {
ui_color "#fd79a8" "🎯 $*" >&2
}
ui_kv() {
local icon="$1"
local key="$2"
local value="$3"
printf '%s %s:%s\n' "$icon" "$key" "$value"
}
ui_progress_line() {
local percent="$1"
local message="$2"
if [ -t 2 ]; then
printf '\r\033[K⏳ [%3s%%] %s' "$percent" "$message" >&2
else
printf '⏳ [%3s%%] %s\n' "$percent" "$message" >&2
fi
}
ui_progress_done() {
local message="${1:-完成}"
if [ -t 2 ]; then
printf '\r\033[K✅ [100%%] %s\n' "$message" >&2
else
printf '✅ [100%%] %s\n' "$message" >&2
fi
}
ui_progress_clear() {
if [ -t 2 ]; then
printf '\r\033[K' >&2
fi
}
install_phase_begin() {
local icon="$1"
local text="$2"
echo
printf '%s %s\n' "$icon" "$text"
}
install_phase_done() {
local text="$1"
printf '✔ %s\n' "$text"
}
install_arch_text() {
get_arch 2>/dev/null || echo "unknown"
}
install_state_text() {
local has_subscription install_ready runtime_ready controller_ready build_status
if install_has_subscription; then
has_subscription="true"
else
has_subscription="false"
fi
install_ready="$(read_runtime_event_value "RUNTIME_LAST_INSTALL_READY" 2>/dev/null || true)"
runtime_ready="$(install_verify_runtime_ready 2>/dev/null || true)"
controller_ready="$(install_verify_controller_ready 2>/dev/null || true)"
build_status="$(read_build_value "BUILD_LAST_STATUS" 2>/dev/null || true)"
if [ "${has_subscription:-false}" != "true" ]; then
echo "stopped"
return 0
fi
if [ "${build_status:-}" = "failed" ]; then
echo "broken"
return 0
fi
if [ "${install_ready:-false}" = "true" ] \
|| { [ "${runtime_ready:-false}" = "true" ] && [ "${controller_ready:-false}" = "true" ]; }; then
echo "ready"
return 0
fi
echo "verifying"
}
install_build_result_text() {
local command_ready config_ready build_status
command_ready="$(install_verify_command_ready 2>/dev/null || true)"
config_ready="$(install_verify_config_ready 2>/dev/null || true)"
build_status="$(read_build_value "BUILD_LAST_STATUS" 2>/dev/null || true)"
if [ "${build_status:-}" = "failed" ]; then
echo "failed"
return 0
fi
if [ "${command_ready:-false}" = "true" ] && { [ "${config_ready:-false}" = "true" ] || ! install_has_subscription; }; then
echo "success"
else
echo "pending"
fi
}
install_next_step_text() {
case "$(install_state_text)" in
ready)
echo "clashctl select"
;;
verifying)
if install_has_subscription; then
echo "clashon"
else
echo "clashctl add <订阅链接>"
fi
;;
broken)
echo "clashctl doctor"
;;
stopped)
echo "clashctl add <订阅链接>"
;;
*)
echo "clashctl status"
;;
esac
}
install_stage_title() {
local text="$1"
echo
echo "==> $text"
}
install_stage_done() {
local text="$1"
echo "✔ $text"
}
install_stage_skip() {
local text="$1"
echo "↷ $text"
}
init_project_context() {
PROJECT_DIR="$1"
CONFIG_DIR="$PROJECT_DIR/config"
RESOURCE_DIR="$PROJECT_DIR/resources"
}
is_wsl_env() {
grep -qiE 'microsoft|wsl' /proc/sys/kernel/osrelease 2>/dev/null
}
path_is_wsl_windows_mount() {
local path="$1"
path="$(readlink -f "$path" 2>/dev/null || printf '%s' "$path")"
case "$path" in
/mnt/[a-zA-Z]|/mnt/[a-zA-Z]/*)
return 0
;;
*)
return 1
;;
esac
}
ensure_project_not_wsl_windows_mount() {
if is_wsl_env && path_is_wsl_windows_mount "$PROJECT_DIR"; then
die_state \
"当前项目目录位于 WSL Windows 挂载盘:$PROJECT_DIR" \
"请将项目移动到 Linux 原生目录后重新安装,例如:cd ~ && git clone <repo> clash-for-linux && cd clash-for-linux && bash install.sh"
fi
}
load_env_if_exists() {
if [ -f "$PROJECT_DIR/.env" ]; then
set -a
# shellcheck disable=SC1090
source "$PROJECT_DIR/.env"
set +a
fi
normalize_env_compat
}
normalize_env_compat() {
if [ -n "${KERNEL_NAME:-}" ]; then
KERNEL_TYPE="$KERNEL_NAME"
fi
if [ -n "${CLASH_CONFIG_URL:-}" ]; then
CLASH_SUBSCRIPTION_URL="$CLASH_CONFIG_URL"
fi
if [ -n "${VERSION_MIHOMO:-}" ]; then
MIHOMO_VERSION="$VERSION_MIHOMO"
fi
if [ -n "${VERSION_YQ:-}" ]; then
YQ_VERSION="$VERSION_YQ"
fi
if [ -n "${VERSION_SUBCONVERTER:-}" ]; then
SUBCONVERTER_VERSION="$VERSION_SUBCONVERTER"
fi
if [ -n "${URL_CLASH_UI:-}" ]; then
CLASH_PUBLIC_UI_URL="$URL_CLASH_UI"
fi
if [ -n "${URL_GH_PROXY:-}" ]; then
CLASH_GH_PROXY="$URL_GH_PROXY"
fi
if [ -n "${CLASH_SUB_UA:-}" ]; then
CLASH_SUBSCRIPTION_UA="$CLASH_SUB_UA"
fi
# 旧默认值迁移:公网默认监听
if [ "${EXTERNAL_CONTROLLER:-}" = "127.0.0.1:9090" ]; then
EXTERNAL_CONTROLLER="0.0.0.0:9090"
fi
# 已废弃:active-only 主链不再消费该字段
unset BUILD_MIN_SUCCESS_SOURCES 2>/dev/null || true
return 0
}
migrate_env_legacy_compat_fields() {
local file="$1"
local tmp
[ -n "${file:-}" ] || return 0
[ -f "$file" ] || return 0
tmp="$(mktemp "${file}.tmp.XXXXXX")" || return 0
awk '
$0 ~ /^[[:space:]]*(export[[:space:]]+)?BUILD_MIN_SUCCESS_SOURCES=/ { next }
$0 ~ /^[[:space:]]*(export[[:space:]]+)?CLASH_SUBSCRIPTION_FORMAT=/ { next }
$0 ~ /^[[:space:]]*(export[[:space:]]+)?EXTERNAL_CONTROLLER="?127\.0\.0\.1:9090"?$/ {
print "export EXTERNAL_CONTROLLER=\"0.0.0.0:9090\""
next
}
{ print }
' "$file" > "$tmp" || {
rm -f "$tmp" 2>/dev/null || true
return 0
}
if cmp -s "$file" "$tmp"; then
rm -f "$tmp" 2>/dev/null || true
return 0
fi
if [ ! -w "$file" ]; then
rm -f "$tmp" 2>/dev/null || true
warn ".env 当前用户不可写,已跳过兼容迁移:$file"
return 0
fi
mv -f "$tmp" "$file"
}
github_proxy_prefix() {
local prefix="${CLASH_GH_PROXY:-}"
prefix="${prefix%/}"
echo "$prefix"
}
bundled_asset_enabled() {
case "${CLASH_BUNDLED_ASSET_ENABLED:-true}" in
true|1|yes|on) return 0 ;;
false|0|no|off) return 1 ;;
*) return 0 ;;
esac
}
bundled_asset_root() {
if [ -n "${CLASH_BUNDLED_ASSET_DIR:-}" ]; then
echo "$CLASH_BUNDLED_ASSET_DIR"
return 0
fi
[ -n "${RESOURCE_DIR:-}" ] || return 1
echo "$RESOURCE_DIR/bin"
}
copy_bundled_asset() {
local category="$1"
local version="$2"
local file="$3"
local out="$4"
local asset_name="${5:-$file}"
local root candidate
[ "$category" != "clash" ] || return 1
bundled_asset_enabled || return 1
root="$(bundled_asset_root 2>/dev/null || true)"
[ -n "${root:-}" ] || return 1
for candidate in \
"$root/$category/$file" \
"$root/$category/$version/$file" \
"$root/$file" \
"$RESOURCE_DIR/$category/$file"; do
[ -s "$candidate" ] || continue
mkdir -p "$(dirname "$out")"
if [ "$candidate" != "$out" ]; then
cp -f "$candidate" "$out"
fi
success "${asset_name} 已使用内置资源:$candidate"
return 0
done
return 1
}
download_connect_timeout() {
echo "${CLASH_DOWNLOAD_CONNECT_TIMEOUT:-8}"
}
download_probe_timeout() {
echo "${CLASH_DOWNLOAD_PROBE_TIMEOUT:-4}"
}
download_max_time() {
echo "${CLASH_DOWNLOAD_MAX_TIME:-1200}"
}
download_cache_enabled() {
case "${CLASH_DOWNLOAD_CACHE_ENABLED:-true}" in
true|1|yes|on) return 0 ;;
false|0|no|off) return 1 ;;
*) return 0 ;;
esac
}
download_fail_cooldown() {
echo "${CLASH_DOWNLOAD_FAIL_COOLDOWN:-1800}"
}
download_cache_dir() {
echo "$RUNTIME_DIR/cache/assets"
}
download_mirror_state_file() {
echo "$RUNTIME_DIR/cache/download-mirrors.env"
}
download_now_epoch() {
date +%s
}
download_hash_key() {
local text="$1"
if command -v sha256sum >/dev/null 2>&1; then
printf '%s' "$text" | sha256sum | awk '{print $1}'
return 0
fi
if command -v shasum >/dev/null 2>&1; then
printf '%s' "$text" | shasum -a 256 | awk '{print $1}'
return 0
fi
printf '%s' "$text" | cksum | awk '{print $1 "-" $2}'
}
download_cache_key() {
local url="$1"
download_hash_key "$url"
}
download_cache_file() {
local url="$1"
echo "$(download_cache_dir)/$(download_cache_key "$url").bin"
}
download_cache_meta_file() {
local url="$1"
echo "$(download_cache_dir)/$(download_cache_key "$url").meta"
}
download_cache_restore() {
local url="$1"
local out="$2"
local cache_file
download_cache_enabled || return 1
cache_file="$(download_cache_file "$url")"
[ -s "$cache_file" ] || return 1
mkdir -p "$(dirname "$out")"
cp -f "$cache_file" "$out"
return 0
}
download_cache_store() {
local url="$1"
local src="$2"
local source_url="${3:-}"
local cache_file meta_file
download_cache_enabled || return 0
[ -s "$src" ] || return 0
cache_file="$(download_cache_file "$url")"
meta_file="$(download_cache_meta_file "$url")"
mkdir -p "$(download_cache_dir)"
cp -f "$src" "$cache_file"
cat > "$meta_file" <<EOF
CACHE_URL="$url"
CACHE_SOURCE_URL="$source_url"
CACHE_TIME="$(now_datetime)"
EOF
}
github_url_is_mirrorable() {
case "$1" in
https://github.com/*|https://raw.githubusercontent.com/*)
return 0
;;
*)
return 1
;;
esac
}
default_github_mirror_pool() {
cat <<'EOF'
gh-proxy|https://gh-proxy.org|full
ghfast|https://ghfast.top|hostpath
ghproxy-net|https://ghproxy.net|hostpath
EOF
}
custom_github_mirror_pool() {
if [ -n "${CLASH_GH_PROXY_POOL:-}" ]; then
printf '%s\n' "$CLASH_GH_PROXY_POOL"
fi
}
normalize_github_mirror_entry() {
local entry="$1"
local label prefix mode
[ -n "${entry//[[:space:]]/}" ] || return 1
case "$entry" in
\#*) return 1 ;;
esac
if printf '%s' "$entry" | grep -Fq '|'; then
IFS='|' read -r label prefix mode <<EOF
$entry
EOF
label="${label:-mirror}"
prefix="${prefix:-}"
mode="${mode:-full}"
[ -n "${prefix:-}" ] || return 1
printf '%s|%s|%s\n' "$label" "${prefix%/}" "$mode"
return 0
fi
prefix="${entry%/}"
[ -n "${prefix:-}" ] || return 1
printf '%s|%s|full\n' "$prefix" "$prefix"
}
github_url_host() {
local url="$1"
local no_scheme
no_scheme="${url#https://}"
no_scheme="${no_scheme#http://}"
echo "${no_scheme%%/*}"
}
github_url_path() {
local url="$1"
local no_scheme
no_scheme="${url#https://}"
no_scheme="${no_scheme#http://}"
echo "${no_scheme#*/}"
}
build_github_mirror_url() {
local entry="$1"
local url="$2"
local label prefix mode host path
IFS='|' read -r label prefix mode <<EOF
$entry
EOF
case "${mode:-full}" in
full)
echo "${prefix%/}/${url}"
;;
hostpath)
host="$(github_url_host "$url")"
path="$(github_url_path "$url")"
echo "${prefix%/}/${host}/${path}"
;;
origin)
echo "$url"
;;
*)
echo "${prefix%/}/${url}"
;;
esac
}
github_mirror_candidate_entries() {
local url="$1"
local entry normalized
local seen=""
github_url_is_mirrorable "$url" || {
echo "origin||origin"
return 0
}
if [ -n "$(github_proxy_prefix)" ]; then
entry="custom|$(github_proxy_prefix)|full"
normalized="$(normalize_github_mirror_entry "$entry" 2>/dev/null || true)"
if [ -n "${normalized:-}" ]; then
printf '%s\n' "$normalized"
seen="${seen}${normalized}"$'\n'
fi
fi
if [ -n "${CLASH_GH_PROXY_POOL:-}" ]; then
while IFS= read -r entry; do
normalized="$(normalize_github_mirror_entry "$entry" 2>/dev/null || true)"
[ -n "${normalized:-}" ] || continue
if ! printf '%s' "$seen" | grep -Fxq "$normalized"; then
printf '%s\n' "$normalized"
seen="${seen}${normalized}"$'\n'
fi
done <<EOF
$(custom_github_mirror_pool)
EOF
else
while IFS= read -r entry; do
normalized="$(normalize_github_mirror_entry "$entry" 2>/dev/null || true)"
[ -n "${normalized:-}" ] || continue
if ! printf '%s' "$seen" | grep -Fxq "$normalized"; then
printf '%s\n' "$normalized"
seen="${seen}${normalized}"$'\n'
fi
done <<EOF
$(default_github_mirror_pool)
EOF
fi
echo "origin||origin"
}
download_mirror_state_key() {
local label="$1"
printf '%s' "$label" | tr '[:lower:]-./:' '[:upper:]_____'
}
read_download_mirror_state() {
local label="$1"
local field="$2"
local file key
file="$(download_mirror_state_file)"
[ -f "$file" ] || return 1
key="DOWNLOAD_MIRROR_$(download_mirror_state_key "$label")_${field}"
sed -nE "s/^[[:space:]]*${key}=['\"]?([^'\"]*)['\"]?$/\1/p" "$file" | head -n 1
}
write_download_mirror_state() {
local label="$1"
local field="$2"
local value="$3"
local file key
file="$(download_mirror_state_file)"
mkdir -p "$(dirname "$file")"
touch "$file"
key="DOWNLOAD_MIRROR_$(download_mirror_state_key "$label")_${field}"
if grep -qE "^[[:space:]]*${key}=" "$file"; then
awk -v k="$key" -v v="$value" '
$0 ~ "^[[:space:]]*" k "=" {
print k "=\"" v "\""
next
}
{ print }
' "$file" > "${file}.tmp" && mv "${file}.tmp" "$file"
else
printf '%s="%s"\n' "$key" "$value" >> "$file"
fi
}
record_download_mirror_success() {
local label="$1"
local candidate_url="${2:-}"
local now
now="$(download_now_epoch)"
write_download_mirror_state "$label" "LAST_SUCCESS_AT" "$now"
write_download_mirror_state "$label" "LAST_SUCCESS_URL" "$candidate_url"
write_download_mirror_state "$label" "FAIL_STREAK" "0"
}
record_download_mirror_failure() {
local label="$1"
local candidate_url="${2:-}"
local now fail_streak
now="$(download_now_epoch)"
fail_streak="$(read_download_mirror_state "$label" "FAIL_STREAK" 2>/dev/null || echo "0")"
case "$fail_streak" in
''|*[!0-9]*) fail_streak="0" ;;
esac
fail_streak=$((fail_streak + 1))
write_download_mirror_state "$label" "LAST_FAILURE_AT" "$now"
write_download_mirror_state "$label" "LAST_FAILURE_URL" "$candidate_url"
write_download_mirror_state "$label" "FAIL_STREAK" "$fail_streak"
}
download_mirror_recent_failure_active() {
local label="$1"
local fail_at success_at now cooldown delta
fail_at="$(read_download_mirror_state "$label" "LAST_FAILURE_AT" 2>/dev/null || true)"
success_at="$(read_download_mirror_state "$label" "LAST_SUCCESS_AT" 2>/dev/null || true)"
cooldown="$(download_fail_cooldown)"
now="$(download_now_epoch)"
case "$fail_at" in
''|*[!0-9]*) return 1 ;;
esac
case "$success_at" in
''|*[!0-9]*) success_at=0 ;;
esac
case "$cooldown" in
''|*[!0-9]*) cooldown=1800 ;;
esac
[ "$fail_at" -gt "$success_at" ] || return 1
delta=$((now - fail_at))
[ "$delta" -lt "$cooldown" ]
}
download_mirror_score() {
local entry="$1"
local label success_at fail_at fail_streak score now
label="${entry%%|*}"
now="$(download_now_epoch)"
success_at="$(read_download_mirror_state "$label" "LAST_SUCCESS_AT" 2>/dev/null || echo "0")"
fail_at="$(read_download_mirror_state "$label" "LAST_FAILURE_AT" 2>/dev/null || echo "0")"
fail_streak="$(read_download_mirror_state "$label" "FAIL_STREAK" 2>/dev/null || echo "0")"
score=0
case "$success_at" in ''|*[!0-9]*) success_at=0 ;; esac
case "$fail_at" in ''|*[!0-9]*) fail_at=0 ;; esac
case "$fail_streak" in ''|*[!0-9]*) fail_streak=0 ;; esac
if [ "$label" = "origin" ]; then
score=$((score + 5))
fi
if [ "$success_at" -gt 0 ]; then
score=$((score + 200))
score=$((score - ((now - success_at) / 3600)))
fi
if [ "$fail_at" -gt "$success_at" ]; then
score=$((score - 150))
fi
if [ "$fail_streak" -gt 0 ]; then
score=$((score - fail_streak * 20))
fi
if download_mirror_recent_failure_active "$label"; then
score=$((score - 1000))
fi
echo "$score"
}
github_mirror_candidate_entries_ordered() {
local url="$1"
local entry
while IFS= read -r entry; do
[ -n "${entry:-}" ] || continue
printf '%s|%s\n' "$(download_mirror_score "$entry")" "$entry"
done <<EOF
$(github_mirror_candidate_entries "$url")
EOF
}
curl_env_points_to_local_proxy() {
local value
for value in \
"${http_proxy:-}" "${https_proxy:-}" \
"${HTTP_PROXY:-}" "${HTTPS_PROXY:-}" \
"${all_proxy:-}" "${ALL_PROXY:-}"; do
[ -n "${value:-}" ] || continue
case "$value" in
http://127.0.0.1:*|https://127.0.0.1:*|socks5://127.0.0.1:*|socks5h://127.0.0.1:* \
|http://localhost:*|https://localhost:*|socks5://localhost:*|socks5h://localhost:* \
|http://[::1]:*|https://[::1]:*|socks5://[::1]:*|socks5h://[::1]:*)
return 0
;;
esac
done
return 1
}
curl_download() {
if curl_env_points_to_local_proxy; then
env \
-u http_proxy \
-u https_proxy \
-u HTTP_PROXY \
-u HTTPS_PROXY \
-u all_proxy \
-u ALL_PROXY \
curl "$@"
return $?
fi
curl "$@"
}
download_candidate_probe() {
local url="$1"
curl_download -fsSIL \
--location \
--connect-timeout "$(download_probe_timeout)" \
--max-time "$(download_probe_timeout)" \
"$url" >/dev/null 2>&1
}
download_candidate_fetch() {
local url="$1"
local out="$2"
local progress_arg="--progress-bar"
curl_download \
"$progress_arg" \
--show-error \
--fail \
--location \
--connect-timeout "$(download_connect_timeout)" \
--max-time "$(download_max_time)" \
--retry 1 \
--output "$out" \
"$url"
}
download_file() {
local url="$1"
local out="$2"
local asset_name="${3:-$(basename "$url")}"
local ordered_entries entry candidate_url label attempt_mode
local probed_any="false"
local tried_urls=""
local fetch_tmp
mkdir -p "$(dirname "$out")"
rm -f "$out" 2>/dev/null || true
fetch_tmp="$(mktemp)"
rm -f "$fetch_tmp" 2>/dev/null || true
if ! github_url_is_mirrorable "$url"; then
ui_download "正在下载:${asset_name}"
if download_candidate_fetch "$url" "$fetch_tmp" "$asset_name"; then
mv -f "$fetch_tmp" "$out"
download_cache_store "$url" "$out" "$url"
return 0
fi
rm -f "$fetch_tmp" 2>/dev/null || true
die_state "下载失败:${asset_name}" \
"请检查网络连通性,或在 .env 中配置下载源后重试;也可先执行 clashctl doctor"
fi
ordered_entries="$(
github_mirror_candidate_entries_ordered "$url" \
| sort -t'|' -k1,1nr \
| cut -d'|' -f2-
)"
for attempt_mode in probe blind; do
while IFS= read -r entry; do
[ -n "${entry:-}" ] || continue
candidate_url="$(build_github_mirror_url "$entry" "$url")"
[ -n "${candidate_url:-}" ] || continue
if printf '%s\n' "$tried_urls" | grep -Fxq "$candidate_url"; then
continue
fi
label="${entry%%|*}"
if [ "$attempt_mode" = "probe" ]; then
if download_mirror_recent_failure_active "$label"; then
continue
fi
if ! download_candidate_probe "$candidate_url"; then
continue
fi
probed_any="true"
else
[ "$probed_any" = "false" ] || continue
fi
if [ "$label" = "origin" ]; then
ui_download "正在下载:${asset_name}"
else
ui_download "正在下载:${asset_name} [${label}]"
fi
if download_candidate_fetch "$candidate_url" "$fetch_tmp" "$asset_name"; then
mv -f "$fetch_tmp" "$out"
download_cache_store "$url" "$out" "$candidate_url"
record_download_mirror_success "$label" "$candidate_url"
return 0
fi
record_download_mirror_failure "$label" "$candidate_url"
rm -f "$fetch_tmp" 2>/dev/null || true
fetch_tmp="$(mktemp)"
rm -f "$fetch_tmp" 2>/dev/null || true
tried_urls="${tried_urls}${candidate_url}"$'\n'
done <<EOF
$ordered_entries
EOF
done
rm -f "$fetch_tmp" 2>/dev/null || true
die_state "下载失败:${asset_name}" \
"请检查网络连通性,或在 .env 中配置下载源后重试;也可先执行 clashctl doctor"
}
download_text_tmp_file() {
mktemp
}
download_http_user_agent() {
echo "${1:-curl/8}"
}
download_http_fetch_to_file() {
local url="$1"
local out="$2"