-
-
Notifications
You must be signed in to change notification settings - Fork 365
/
ecs.sh
5149 lines (4988 loc) · 211 KB
/
ecs.sh
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
# by spiritlhl
# from https://github.com/spiritLHLS/ecs
cd /root >/dev/null 2>&1
myvar=$(pwd)
ver="2024.11.08"
# =============== 默认输入设置 ===============
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
PLAIN="\033[0m"
SAVE_CURSOR="\033[s"
RESTORE_CURSOR="\033[u"
HIDE_CURSOR="\033[?25l"
SHOW_CURSOR="\033[?25h"
_red() { echo -e "\033[31m\033[01m$@\033[0m"; }
_green() { echo -e "\033[32m\033[01m$@\033[0m"; }
_yellow() { echo -e "\033[33m\033[01m$@\033[0m"; }
_blue() { echo -e "\033[36m\033[01m$@\033[0m"; }
reading() { read -rp "$(_green "$1")" "$2"; }
utf8_locale=$(locale -a 2>/dev/null | grep -i -m 1 -E "UTF-8|utf8")
if [[ -z "$utf8_locale" ]]; then
_yellow "No UTF-8 locale found"
else
export LC_ALL="$utf8_locale"
export LANG="$utf8_locale"
export LANGUAGE="$utf8_locale"
_green "Locale set to $utf8_locale"
fi
menu_mode=true
en_status=false
swhc_mode=true
test_base_status=false
test_cpu_type=""
test_disk_type=""
test_network_type=""
build_text_status=true
multidisk_status=false
target_ipv4=""
route_location=""
enable_speedtest=true
main_menu_option=0
sub_menu_option=0
sub_of_sub_menu_option=0
break_status=true
m_params=()
# 解析命令行选项
while [ "$#" -gt 0 ]; do
case "$1" in
-m)
# 处理 -m 选项,关闭菜单模式
menu_mode=false
shift # 移动到下一个参数
while [ "$#" -gt 0 ] && [[ "$1" != -* ]]; do
m_params+=("$1")
shift
done
;;
-i)
# 处理 -i 选项,获取IPv4地址
target_ipv4="$2"
swhc_mode=false
shift 2
;;
-r)
# 处理 -r 选项,选择测试回程路由的目标地址 (三网)
route_location="$2"
shift 2
;;
-en)
# 处理 -en 选项,选择使用英文显示
en_status=true
shift
;;
-base)
# 处理 -base 选项,选择仅测试系统信息
menu_mode=false
test_base_status=true
shift
;;
-ctype)
# 处理 -ctype 选项,选择测试cpu使用的方式
test_cpu_type="$2"
shift 2
;;
-dtype)
# 处理 -dtype 选项,选择测试磁盘使用的方式
test_disk_type="$2"
shift 2
;;
-mdisk)
# 处理 -mdisk 选项,选择测试多个挂载盘,且含系统盘
multidisk_status=true
shift
;;
-stype)
# 处理 -stype 选项,选择测试网速的数据来源,不指定时默认优先使用.net数据
test_network_type="$2"
shift 2
;;
-bansp)
# 处理 -bansp 选项,禁用测速
enable_speedtest=false
shift
;;
-banup)
# 处理 -banup 选项,禁用分享链接生成
build_text_status=false
shift
;;
-h)
if [ "$en_status" = true ]; then
echo "Executed using parameter mode:"
echo "-m Mandatory, Specify the options in the original menu, support up to three levels of selection"
echo " For example, executing bash ecs.sh -m 5 1 1 will select the script execution for sub-option 1 under option 1 of option 5 of the main menu."
echo " Can specify only 1~3 parameter by default, e.g. -m 1 or -m 1 0 or -m 1 0 0"
echo "-en Optional, Can specify which language is used to display the test, unspecified Chinese is used."
echo "-i Optional, Can specify the target IPV4 address in the backhaul routing test."
echo "-base Optional, Only basic system information is tested, not CPU, hard disk, streaming, backhaul routing, etc."
echo "-ctype Optional, Can specify the way to test the cpu, optional gb4 gb5 gb6 corresponds to geekbench version 4, 5, 6 respectively."
echo "-dtype Optional, Can specify the program to test the IO of the hard disk, you can choose dd or fio, the former test is fast and the latter test is slow."
echo "-mdisk Optional, Can specify to test the IO of multiple mounted disks."
echo "-bansp Optional, Can specify not to run speedtest."
echo "-banup Optional, Can specify to force not to generate the sharing link."
else
echo "使用参数模式执行:"
echo "-m 必填项,指定原本menu中的选项,最多支持三层选择"
echo " 例如执行 bash ecs.sh -m 5 1 1 将选择主菜单第5选项下的第1选项下的子选项1的脚本执行"
echo " (可缺省仅指定一个参数,如 -m 1 仅指定执行融合怪完全体,执行 -m 1 0 以及 -m 1 0 0 都是指定执行融合怪完全体)"
echo "-en 可选项,可指定测试时使用的是哪种语言进行展示,该指令指定为使用英语,未指定时使用中文"
echo "-i 可选项,可指定回程路由测试中的目标IPV4地址,可通过 ip.sb ipinfo.io 等网站获取本地IPV4地址后指定"
echo "-r 可选项,可指定回程路由测试中的三网IPV4地址,可选 b g s c 分别对应 北京、广州、上海、成都 的三网地址,如 -r g 指定测试广州地址"
echo " 可指定仅测试IPV6三网,可选 b6 g6 s6 分别对应 北京、广州、上海 的三网的IPV6地址,如 -r b6 指定测试北京IPV6地址"
echo "-base 可选项,仅测试基础的系统信息,不测试CPU、硬盘、流媒体、回程路由等内容"
echo "-ctype 可选项,可指定通过何种方式测试cpu,可选 gb4 gb5 gb6 分别对应geekbench的4、5、6版本,无该指令则默认使用sysbench测试"
echo "-dtype 可选项,可指定测试硬盘IO的程序,可选 dd 或 fio 前者测试快后者测试慢,无该指令则默认为都使用进行测试"
echo "-mdisk 可选项,可指定测试多个挂载盘的IO,注意这也会测试系统盘且仅使用fio测试"
echo "-stype 可选项,可指定测试时使用的是什么平台的测速节点,可选 .cn .com 分别对应 speedtest.cn speedtest.com 数据"
echo "-bansp 可选项,可指定强制不测试网速,无该指令则默认测试网速"
echo "-banup 可选项,可指定强制不生成分享链接,无该指令则默认生成分享链接"
fi
exit 1
;;
*)
echo "未知的选项: $1"
exit 1
;;
esac
done
if [ -n "$target_ipv4" ]; then
if [ "$en_status" = true ]; then
test_area_local=("Yor local public IPV4 address")
test_ip_local=("$target_ipv4")
else
test_area_local=("你本地的IPV4地址")
test_ip_local=("$target_ipv4")
fi
fi
# 在menu_mode为false时才打印信息
if [ "$menu_mode" = false ]; then
if [ "$en_status" = true ]; then
_blue "Parameter is detected, use parameter mode, read the parameter as follows, display for 4 seconds"
else
_blue "检测到参数,使用参数模式,读取参数如下,显示4秒"
fi
echo "menu_mode: $menu_mode"
echo "test_base_status: $test_base_status"
echo "target_ipv4: $target_ipv4"
echo "route_location: $route_location"
echo "test_cpu_type: $test_cpu_type"
echo "test_disk_type: $test_disk_type"
echo "multidisk_status: $multidisk_status"
echo "enable_speedtest: $enable_speedtest"
echo "build_text_status: $build_text_status"
# 读取 -m 选项后的参数
main_menu_option=${m_params[0]:-0}
sub_menu_option=${m_params[1]:-0}
sub_of_sub_menu_option=${m_params[2]:-0}
echo "main_menu_option: $main_menu_option"
echo "sub_menu_option: $sub_menu_option"
echo "sub_of_sub_menu_option: $sub_of_sub_menu_option"
sleep 4
fi
# =============== 自定义基础参数 ==============
if [ "$en_status" = true ]; then
changeLog="VPS Fusion Monster Test From Multi-script"
else
changeLog="VPS融合怪测试(集百家之长)"
fi
http_short_url=""
https_short_url=""
TEMP_DIR='/tmp/ecs'
PROGRESS_DIR="/tmp/progress"
rm -rf "$PROGRESS_DIR"
mkdir -p "$PROGRESS_DIR"
PID_FILE="/tmp/pids.txt"
rm -rf "$PID_FILE"
temp_file_apt_fix="${TEMP_DIR}/apt_fix.txt"
WorkDir="/tmp/.LemonBench"
test_area_g=("广州电信" "广州联通" "广州移动")
test_ip_g=("58.60.188.222" "210.21.196.6" "120.196.165.24")
test_area_s=("上海电信" "上海联通" "上海移动")
test_ip_s=("202.96.209.133" "210.22.97.1" "211.136.112.200")
test_area_b=("北京电信" "北京联通" "北京移动")
test_ip_b=("219.141.140.10", "202.106.195.68", "221.179.155.161")
test_area_c=("成都电信" "成都联通" "成都移动")
test_ip_c=("61.139.2.69" "119.6.6.6" "211.137.96.205")
test_area_g6=("广州电信" "广州联通" "广州移动")
test_ip_g6=("240e:97c:2f:3000::44" "2408:8756:f50:1001::c" "2409:8c54:871:1001::12")
test_area_s6=("上海电信" "上海联通" "上海移动")
test_ip_s6=("240e:e1:aa00:4000::24" "2408:80f1:21:5003::a" "2409:8c1e:75b0:3003::26")
test_area_b6=("北京电信" "北京联通" "北京移动")
test_ip_b6=("2400:89c0:1053:3::69" "2400:89c0:1013:3::54" "2409:8c00:8421:1303::55")
BrowserUA="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36"
Speedtest_Go_version="1.6.12"
# =============== 基础信息设置 ===============
REGEX=("debian|astra" "ubuntu" "centos|red hat|kernel|oracle linux|alma|rocky" "'amazon linux'" "fedora" "arch" "freebsd")
RELEASE=("Debian" "Ubuntu" "CentOS" "CentOS" "Fedora" "Arch" "FreeBSD")
PACKAGE_UPDATE=("! apt-get update && apt-get --fix-broken install -y && apt-get update" "apt-get update" "yum -y update" "yum -y update" "yum -y update" "pacman -Sy" "pkg update")
PACKAGE_INSTALL=("apt-get -y install" "apt-get -y install" "yum -y install" "yum -y install" "yum -y install" "pacman -Sy --noconfirm --needed" "pkg install -y")
PACKAGE_REMOVE=("apt-get -y remove" "apt-get -y remove" "yum -y remove" "yum -y remove" "yum -y remove" "pacman -Rsc --noconfirm" "pkg delete")
PACKAGE_UNINSTALL=("apt-get -y autoremove" "apt-get -y autoremove" "yum -y autoremove" "yum -y autoremove" "yum -y autoremove" "" "pkg autoremove")
CMD=("$(grep -i pretty_name /etc/os-release 2>/dev/null | cut -d \" -f2)" "$(hostnamectl 2>/dev/null | grep -i system | cut -d : -f2)" "$(lsb_release -sd 2>/dev/null)" "$(grep -i description /etc/lsb-release 2>/dev/null | cut -d \" -f2)" "$(grep . /etc/redhat-release 2>/dev/null)" "$(grep . /etc/issue 2>/dev/null | cut -d \\ -f1 | sed '/^[ ]*$/d')" "$(grep -i pretty_name /etc/os-release 2>/dev/null | cut -d \" -f2)" "$(uname -s)")
SYS="${CMD[0]}"
[[ -n $SYS ]] || exit 1
for ((int = 0; int < ${#REGEX[@]}; int++)); do
if [[ $(echo "$SYS" | tr '[:upper:]' '[:lower:]') =~ ${REGEX[int]} ]]; then
SYSTEM="${RELEASE[int]}"
[[ -n $SYSTEM ]] && break
fi
done
# =================== 其他脚本相关设置 ===================
export DEBIAN_FRONTEND=noninteractive
rm -rf test_result.txt >/dev/null 2>&1
if [ ! -d "/tmp" ]; then
mkdir /tmp
fi
usage_timeout=true
DISPLAY_RUNNING=1
# =============== 脚本退出执行相关函数 部分 ===============
trap _exit INT QUIT TERM
_exit() {
# 终止信号捕获 - ctrl+c
echo -e "\n${Msg_Error}Exiting ..."
if [ "$en_status" = true ]; then
_red "An exit operation is detected and the script terminates!"
else
_red "检测到退出操作,脚本终止!"
fi
global_exit_action
rm_script
exit 1
}
global_startup_init_action() {
# 清理残留, 为新一次的运行做好准备
echo -e "${Msg_Info}Initializing Running Enviorment, Please wait ..."
rm -rf "$WorkDir"
rm -rf /.tmp_LBench/
mkdir "$WorkDir"/
echo -e "${Msg_Info}Checking Dependency ..."
BenchFunc_Systeminfo_GetSysteminfo
echo -e "${Msg_Info}Starting Test ..."
}
global_exit_action() {
reset_default_sysctl >/dev/null 2>&1
echo -en "$SHOW_CURSOR"
if [ "$build_text_status" = true ]; then
build_text
if [ -n "$https_short_url" ] || [ -n "$http_short_url" ]; then
if [ "$en_status" = true ]; then
_green " ShortLink:"
else
_green " 短链:"
fi
if [ -n "$https_short_url" ]; then
_blue " $https_short_url"
fi
if [ -n "$http_short_url" ]; then
_blue " $http_short_url"
fi
fi
fi
rm -rf ${TEMP_DIR}
rm -rf ${WorkDir}/
rm -rf /.tmp_LBench/
rm -rf *00_00
}
_exists() {
# 查询对应变量或组件是否存在
local cmd="$1"
if eval type type >/dev/null 2>&1; then
eval type "$cmd" >/dev/null 2>&1
elif command >/dev/null 2>&1; then
command -v "$cmd" >/dev/null 2>&1
else
which "$cmd" >/dev/null 2>&1
fi
local rt=$?
return ${rt}
}
reset_default_sysctl() {
# 还原系统原有的设置
if [ -f /etc/security/limits.conf ]; then
cp /etc/security/limits.conf.backup /etc/security/limits.conf
rm /etc/security/limits.conf.backup
fi
if which systemctl >/dev/null 2>&1; then
if [ -f "$sysctl_conf" ]; then
cp "$sysctl_conf_backup" "$sysctl_conf"
check_and_cat_file "$sysctl_default" >>"$sysctl_conf"
$sysctl_path -p 2>/dev/null
cp "$sysctl_conf_backup" "$sysctl_conf"
rm "$sysctl_conf_backup"
rm "$sysctl_default"
fi
$sysctl_path -p 2>/dev/null
fi
}
next() {
echo -en "\r"
[ "${Var_OSRelease}" = "freebsd" ] && printf "%-72s\n" "-" | tr ' ' '-' && return
printf "%-72s\n" "-" | sed 's/\s/-/g'
}
# =============== 组件预安装及文件预下载 部分 ===============
checkver() {
check_cdn_file
running_version=$(sed -n '7s/ver="\(.*\)"/\1/p' "$0")
curl -L "${cdn_success_url}https://raw.githubusercontent.com/spiritLHLS/ecs/main/ecs.sh" -o ecs1.sh && chmod 777 ecs1.sh
downloaded_version=$(sed -n '7s/ver="\(.*\)"/\1/p' ecs1.sh)
if [ "$running_version" != "$downloaded_version" ]; then
if [ "$en_status" = true ]; then
_yellow "Upgrade script from $ver to $downloaded_version"
else
_yellow "更新脚本从 $ver 到 $downloaded_version"
fi
mv ecs1.sh "$0"
./ecs.sh
else
if [ "$en_status" = true ]; then
_green "This script is the lastes version."
else
_green "本脚本已是最新脚本无需更新"
fi
rm -rf ecs1.sh*
fi
}
check_root() {
local root_status=true
[[ $EUID -ne 0 ]] && root_status=false
if [ "$en_status" = true ] && [ "$root_status" = false ]; then
echo -e "${RED}Please use root user to run this script!${PLAIN}" && exit 1
elif [ "$root_status" = false ]; then
echo -e "${RED}请使用 root 用户运行本脚本!${PLAIN}" && exit 1
fi
}
check_update() {
_yellow "Updating package management sources"
if command -v apt-get >/dev/null 2>&1; then
apt_update_output=$(apt-get update 2>&1)
echo "$apt_update_output" >"$temp_file_apt_fix"
if grep -q 'NO_PUBKEY' "$temp_file_apt_fix"; then
public_keys=$(grep -oE 'NO_PUBKEY [0-9A-F]+' "$temp_file_apt_fix" | awk '{ print $2 }')
joined_keys=$(echo "$public_keys" | paste -sd " ")
_yellow "No Public Keys: ${joined_keys}"
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ${joined_keys}
apt-get update
if [ $? -eq 0 ]; then
_green "Fixed"
fi
fi
rm "$temp_file_apt_fix"
else
${PACKAGE_UPDATE[int]}
fi
}
check_sudo() {
_yellow "checking sudo"
if ! command -v sudo >/dev/null 2>&1; then
_yellow "Installing sudo"
${PACKAGE_INSTALL[int]} sudo >/dev/null 2>&1
fi
}
check_curl() {
if ! which curl >/dev/null; then
_yellow "Installing curl"
${PACKAGE_INSTALL[int]} curl
fi
if [ $? -ne 0 ]; then
apt-get -f install >/dev/null 2>&1
${PACKAGE_INSTALL[int]} curl
fi
}
check_wget() {
if ! which wget >/dev/null; then
_yellow "Installing wget"
${PACKAGE_INSTALL[int]} wget
fi
}
check_free() {
[ "${Var_OSRelease}" = "freebsd" ] && return
if ! command -v free >/dev/null 2>&1; then
_yellow "Installing procps"
${PACKAGE_INSTALL[int]} procps
fi
}
check_lsb_release() {
[ "${Var_OSRelease}" = "freebsd" ] && return
if ! command -v lsb_release >/dev/null 2>&1; then
_yellow "Installing lsb-release"
${PACKAGE_INSTALL[int]} lsb-release
fi
}
check_timeout() {
if command -v timeout >/dev/null 2>&1; then
usage_timeout=true
else
usage_timeout=false
fi
}
check_lscpu() {
if ! command -v lscpu >/dev/null 2>&1; then
_yellow "Installing lscpu"
${PACKAGE_INSTALL[int]} lscpu
fi
}
check_unzip() {
if ! command -v unzip >/dev/null 2>&1; then
_yellow "Installing unzip"
${PACKAGE_INSTALL[int]} unzip
fi
}
check_ip() {
if ! command -v ip >/dev/null 2>&1; then
_yellow "Installing iproute2 to use ip command"
${PACKAGE_INSTALL[int]} iproute2
fi
if ! command -v ifconfig >/dev/null 2>&1; then
_yellow "Installing net-tools to use ifconfig command"
${PACKAGE_INSTALL[int]} net-tools
fi
}
check_ping() {
_yellow "checking ping"
if ! which ping >/dev/null; then
_yellow "Installing ping"
${PACKAGE_INSTALL[int]} iputils-ping >/dev/null 2>&1
${PACKAGE_INSTALL[int]} ping >/dev/null 2>&1
fi
}
check_nc() {
_yellow "checking nc"
if ! command -v nc >/dev/null; then
_yellow "Installing nc"
if command -v apt >/dev/null; then
${PACKAGE_INSTALL[int]} netcat >/dev/null 2>&1
else
${PACKAGE_INSTALL[int]} nc >/dev/null 2>&1
fi
fi
}
check_tar() {
_yellow "checking tar"
if ! command -v tar &>/dev/null; then
_yellow "Installing tar"
${PACKAGE_INSTALL[int]} tar
fi
if [ $? -ne 0 ]; then
apt-get -f install >/dev/null 2>&1
${PACKAGE_INSTALL[int]} tar >/dev/null 2>&1
fi
}
check_lsof() {
_yellow "checking lsof"
if ! command -v lsof &>/dev/null; then
_yellow "Installing lsof"
${PACKAGE_INSTALL[int]} lsof
fi
if [ $? -ne 0 ]; then
apt-get -f install >/dev/null 2>&1
${PACKAGE_INSTALL[int]} lsof >/dev/null 2>&1
fi
}
check_haveged() {
[ "${Var_OSRelease}" = "freebsd" ] && return
_yellow "checking haveged"
if ! command -v haveged >/dev/null 2>&1; then
${PACKAGE_INSTALL[int]} haveged >/dev/null 2>&1
fi
if which systemctl >/dev/null 2>&1; then
systemctl disable --now haveged
systemctl enable --now haveged
else
service haveged stop
service haveged start
fi
}
check_dnsutils() {
_yellow "Installing dnsutils"
if [ "${Var_OSRelease}" == "centos" ]; then
yum -y install dnsutils >/dev/null 2>&1
yum -y install bind-utils >/dev/null 2>&1
elif [ "${Var_OSRelease}" == "arch" ]; then
pacman -S --noconfirm --needed bind >/dev/null 2>&1
else
${PACKAGE_INSTALL[int]} dnsutils >/dev/null 2>&1
fi
}
checkpip() {
[ "${Var_OSRelease}" = "freebsd" ] && curl -L https://bootstrap.pypa.io/get-pip.py -o get-pip.py && chmod +x get-pip.py && python3 get-pip.py && rm -rf get-pip.py && return
local pvr="$1"
local pip_version=$(pip --version 2>&1)
if [[ $? -eq 0 && $pip_version != *"command not found"* ]]; then
_blue "$pip_version"
else
_yellow "installing python${pvr}-pip"
${PACKAGE_INSTALL[int]} python${pvr}-pip
pip_version=$(pip --version 2>&1)
if [[ $? -eq 0 ]]; then
_blue "$pip_version"
else
_red "python${pvr}-pip installation failed, please install it manually"
return
fi
fi
}
check_and_cat_file() {
local file="$1"
# 检测文件是否存在
if [[ -f "$file" ]]; then
# 判断文件内容是否为空或只包含空行
if [[ -s "$file" ]] && [[ "$(grep -vE '^\s*$' "$file")" ]]; then
:
else
truncate -s 0 "$file"
fi
else
truncate -s 0 "$file"
fi
# 检测文件内容是否包含"error",如果包含则不打印文件内容
if grep -q "error" "$file"; then
return
fi
cat "$file"
}
# 移动光标并清除行
move_and_clear() {
local line=$1
echo -en "\033[${line};0H\033[K"
}
# 显示进度条
display_progress() {
local use_tput=false
if command -v tput > /dev/null 2>&1; then
use_tput=true
fi
local progress_height=$((${#dfiles[@]} + 2)) # 进度显示所需的行数
# 保存光标位置并隐藏光标
echo -en "$SAVE_CURSOR$HIDE_CURSOR"
while [ $DISPLAY_RUNNING -eq 1 ]; do
# 将光标移动到保存的位置
echo -en "$RESTORE_CURSOR"
if [ "$en_status" = true ]; then
echo "Download progress:"
else
echo "下载进度:"
fi
local all_completed=true
for dfile in "${dfiles[@]}"; do
if [ -f "$PROGRESS_DIR/$dfile" ]; then
local percentage=$(cat "$PROGRESS_DIR/$dfile")
if [[ "$percentage" =~ ^[0-9]+$ ]]; then
percentage=$((percentage > 100 ? 100 : percentage))
printf "%-20s [%-50s] %3d%%\n" "$dfile" "$(printf '#%.0s' $(seq 1 $((percentage / 2))))" "$percentage"
if [ "$percentage" -lt 100 ]; then
all_completed=false
fi
else
printf "%-20s [%-50s] ???\n" "$dfile" ""
all_completed=false
fi
else
printf "%-20s [%-50s] ???\n" "$dfile" ""
all_completed=false
fi
done
if [ "$all_completed" = true ]; then
break
fi
sleep 3.5
done
# 显示光标
echo -en "$SHOW_CURSOR"
echo ""
}
# 开始整体并发下载并显示进度条
start_downloads() {
local dfiles=("$@") # 接收文件列表作为参数
# 初始化进度
for dfile in "${dfiles[@]}"; do
echo "0" > "$PROGRESS_DIR/$dfile"
done
# 获取当前光标位置
local current_line=$(tput lines)
# 启动后台进程来更新显示
display_progress $current_line &
local display_pid=$!
# 并发下载并跟踪PID
for dfile in "${dfiles[@]}"; do
main_download "$dfile" &
echo $! >> "$PID_FILE"
done
wait
# 停止显示进程
DISPLAY_RUNNING=0
}
download_file() {
local url=$1
local output=$2
local progress_file=$3
# 获取文件总大小
local total_size=$(curl -sIkL "$url" | grep -i Content-Length | awk '{print $2}' | tr -d '\r')
if [ -z "$total_size" ] || [ "$total_size" -eq 0 ]; then
echo "无法获取 $url 的文件大小,将使用 0 作为默认值。" >&2
total_size=0
fi
# 连续检测到下载完成的次数
local complete_count=0
# 连续检测到下载失败的次数
local download_failed=0
while true; do
if ! curl -Lk "$url" -o "$output" 2>&1 | \
while true; do
if [ -f "$output" ]; then
sleep 1
local current_size=$(stat -f%z "$output" 2>/dev/null || stat -c%s "$output" 2>/dev/null)
if [ "$total_size" -gt 0 ]; then
local progress=$((current_size * 100 / total_size))
else
local progress=0
fi
echo "$progress" > "$progress_file"
sleep 1
# 检查是否下载完成
if [ "$current_size" -ge "$total_size" ]; then
complete_count=$((complete_count + 1))
# 只有连续3次检测到下载完成才退出循环
if [ "$complete_count" -ge 3 ]; then
break 2 # 退出外层循环
fi
else
complete_count=0 # 如果不完整,重置计数器
fi
fi
done; then
complete_count=0
download_failed=$((download_failed + 1))
if [ "$download_failed" -ge 2 ]; then
echo "curl 和 wget 下载都失败,退出下载。" >&2
return 1 # 返回错误码
fi
echo "curl 下载失败,切换到 wget 下载。" >&2
wget -O "$output" "$url" 2>&1 | \
while true; do
if [ -f "$output" ]; then
sleep 1
local current_size=$(stat -f%z "$output" 2>/dev/null || stat -c%s "$output" 2>/dev/null)
if [ "$total_size" -gt 0 ]; then
local progress=$((current_size * 100 / total_size))
else
local progress=0
fi
echo "$progress" > "$progress_file"
sleep 1
# 检查是否下载完成
if [ "$current_size" -ge "$total_size" ]; then
complete_count=$((complete_count + 1))
# 只有连续3次检测到下载完成才退出循环
if [ "$complete_count" -ge 3 ]; then
break 2 # 退出外层循环
fi
else
complete_count=0 # 如果不完整,重置计数器
fi
fi
done
else
break # curl 下载成功,退出外层循环
fi
done
# 确保最终进度被写入
if [ -f "$output" ]; then
local final_size=$(stat -f%z "$output" 2>/dev/null || stat -c%s "$output" 2>/dev/null)
if [ "$total_size" -gt 0 ]; then
local final_progress=$((final_size * 100 / total_size))
else
local final_progress=0
fi
echo "$final_progress" > "$progress_file"
fi
# 如果下载失败两次则返回错误码
[ "$download_failed" -ge 2 ] && return 1 || return 0
}
main_download() {
local file=$1
case $file in
sysbench)
local url="${cdn_success_url}https://github.com/akopytov/sysbench/archive/1.0.20.zip"
local output="$TEMP_DIR/sysbench.zip"
download_file "$url" "$output" "$PROGRESS_DIR/$file"
chmod +x "$output"
unzip "$output" -d ${TEMP_DIR}
echo "100" > "$PROGRESS_DIR/$file"
;;
CommonMediaTests)
local url="${cdn_success_url}https://github.com/oneclickvirt/CommonMediaTests/releases/download/output/${CommonMediaTests_FILE}"
local output="$TEMP_DIR/CommonMediaTests"
download_file "$url" "$output" "$PROGRESS_DIR/$file"
chmod +x "$output"
echo "100" > "$PROGRESS_DIR/$file"
;;
media_lmc_check)
local url="${cdn_success_url}https://raw.githubusercontent.com/lmc999/RegionRestrictionCheck/main/check.sh"
local output="$TEMP_DIR/media_lmc_check.sh"
download_file "$url" "$output" "$PROGRESS_DIR/$file"
chmod 777 "$output"
old_url="https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fcheck.unclock.media&count_bg=%2379C83D&title_bg=%23555555&icon=&icon_color=%23E7E7E7&title=visit&edge_flat=false"
new_url="https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2Foneclickvirt%2FUnlockTests&count_bg=%2323E01C&title_bg=%23555555&icon=sonarcloud.svg&icon_color=%23E7E7E7&title=hits&edge_flat=false"
sed -i "s|$old_url|$new_url|g" "$output"
echo "100" > "$PROGRESS_DIR/$file"
;;
# besttrace)
# local url="${cdn_success_url}https://raw.githubusercontent.com/spiritLHLS/ecs/main/archive/besttrace/2021/${BESTTRACE_FILE}"
# local output="$TEMP_DIR/$BESTTRACE_FILE"
# download_file "$url" "$output" "$PROGRESS_DIR/$file"
# chmod +x "$output"
# echo "100" > "$PROGRESS_DIR/$file"
# ;;
nexttrace)
NEXTTRACE_VERSION=$(curl -m 6 -sSL "https://api.github.com/repos/nxtrace/Ntrace-core/releases/latest" | awk -F \" '/tag_name/{print $4}')
if [ -z "$NEXTTRACE_VERSION" ]; then
NEXTTRACE_VERSION=$(curl -m 6 -sSL "https://fd.spiritlhl.top/https://api.github.com/repos/nxtrace/Ntrace-core/releases/latest" | awk -F \" '/tag_name/{print $4}')
fi
if [ -z "$NEXTTRACE_VERSION" ]; then
NEXTTRACE_VERSION=$(curl -m 6 -sSL "https://githubapi.spiritlhl.top/repos/nxtrace/Ntrace-core/releases/latest" | awk -F \" '/tag_name/{print $4}')
fi
local url="${cdn_success_url}https://github.com/nxtrace/Ntrace-core/releases/download/${NEXTTRACE_VERSION}/${NEXTTRACE_FILE}"
local output="$TEMP_DIR/$NEXTTRACE_FILE"
download_file "$url" "$output" "$PROGRESS_DIR/$file"
chmod +x "$output"
echo "100" > "$PROGRESS_DIR/$file"
;;
backtrace)
local url="${cdn_success_url}https://github.com/oneclickvirt/backtrace/releases/download/output/$BACKTRACE_FILE"
local output="$TEMP_DIR/backtrace"
download_file "$url" "$output" "$PROGRESS_DIR/$file"
echo "100" > "$PROGRESS_DIR/$file"
;;
gostun)
local url="${cdn_success_url}https://github.com/oneclickvirt/gostun/releases/download/output/$GOSTUN_FILE"
local output="$TEMP_DIR/gostun"
download_file "$url" "$output" "$PROGRESS_DIR/$file"
echo "100" > "$PROGRESS_DIR/$file"
;;
securityCheck)
local url="${cdn_success_url}https://github.com/oneclickvirt/securityCheck/releases/download/output/$SecurityCheck_FILE"
local output="$TEMP_DIR/securityCheck"
download_file "$url" "$output" "$PROGRESS_DIR/$file"
echo "100" > "$PROGRESS_DIR/$file"
;;
portchecker)
local url="${cdn_success_url}https://github.com/oneclickvirt/portchecker/releases/download/output/$PortChecker_FILE"
local output="$TEMP_DIR/pck"
download_file "$url" "$output" "$PROGRESS_DIR/$file"
echo "100" > "$PROGRESS_DIR/$file"
;;
yabs)
local url="${cdn_success_url}https://raw.githubusercontent.com/masonr/yet-another-bench-script/master/yabs.sh"
local output="$TEMP_DIR/yabs.sh"
download_file "$url" "$output" "$PROGRESS_DIR/$file"
chmod +x "$output"
sed -i '/# gather basic system information (inc. CPU, AES-NI\/virt status, RAM + swap + disk size)/,/^echo -e "IPv4\/IPv6 : $ONLINE"/d' "$output"
echo "100" > "$PROGRESS_DIR/$file"
;;
ecsspeed_ping)
local url="${cdn_success_url}https://raw.githubusercontent.com/spiritLHLS/ecsspeed/main/script/ecsspeed-ping.sh"
local output="$TEMP_DIR/ecsspeed-ping.sh"
download_file "$url" "$output" "$PROGRESS_DIR/$file"
chmod +x "$output"
echo "100" > "$PROGRESS_DIR/$file"
;;
*)
echo "Invalid file: $file"
echo "0" > "$PROGRESS_DIR/$file"
;;
esac
}
# =============== 其他相关信息查询 部分 ===============
declare -A sysctl_vars=(
["fs.file-max"]=1024000
["net.core.rmem_max"]=134217728
["net.core.wmem_max"]=134217728
["net.core.netdev_max_backlog"]=250000
["net.core.somaxconn"]=1024000
["net.ipv4.conf.all.rp_filter"]=0
["net.ipv4.conf.default.rp_filter"]=0
["net.ipv4.conf.lo.arp_announce"]=2
["net.ipv4.conf.all.arp_announce"]=2
["net.ipv4.conf.default.arp_announce"]=2
["net.ipv4.ip_forward"]=1
["net.ipv4.ip_local_port_range"]="1024 65535"
["net.ipv4.neigh.default.gc_stale_time"]=120
["net.ipv4.tcp_syncookies"]=1
["net.ipv4.tcp_tw_reuse"]=1
["net.ipv4.tcp_low_latency"]=1
["net.ipv4.tcp_fin_timeout"]=10
["net.ipv4.tcp_window_scaling"]=1
["net.ipv4.tcp_keepalive_time"]=10
["net.ipv4.tcp_timestamps"]=0
["net.ipv4.tcp_sack"]=1
["net.ipv4.tcp_fack"]=1
["net.ipv4.tcp_syn_retries"]=3
["net.ipv4.tcp_synack_retries"]=3
["net.ipv4.tcp_max_syn_backlog"]=16384
["net.ipv4.tcp_max_tw_buckets"]=8192
["net.ipv4.tcp_fastopen"]=3
["net.ipv4.tcp_mtu_probing"]=1
["net.ipv4.tcp_rmem"]="8192 262144 536870912"
["net.ipv4.tcp_wmem"]="4096 16384 536870912"
["net.ipv4.tcp_adv_win_scale"]=-2
["net.ipv4.tcp_collapse_max_bytes"]=6291456
["net.ipv4.tcp_notsent_lowat"]=131072
["net.ipv4.udp_rmem_min"]=16384
["net.ipv4.udp_wmem_min"]=16384
["net.ipv6.conf.all.forwarding"]=1
["net.ipv6.conf.default.forwarding"]=1
["net.nf_conntrack_max"]=25000000
["net.netfilter.nf_conntrack_max"]=25000000
["net.netfilter.nf_conntrack_tcp_timeout_time_wait"]=30
["net.netfilter.nf_conntrack_tcp_timeout_established"]=180
["net.netfilter.nf_conntrack_tcp_timeout_close_wait"]=30
["net.netfilter.nf_conntrack_tcp_timeout_fin_wait"]=30
)
sysctl_conf="/etc/sysctl.conf"
sysctl_conf_backup="/etc/sysctl.conf.backup"
sysctl_default="${TEMP_DIR}/sysctl_backup.txt"
sysctl_path=$(which sysctl)
variable_exists() {
local variable="$1"
grep -q "^$variable=" "$sysctl_conf"
}
optimized_kernel() {
_yellow "optimizing resource limits"
if [ -f /etc/security/limits.conf ]; then
cp /etc/security/limits.conf /etc/security/limits.conf.backup
cat >/etc/security/limits.conf <<EOF
* soft nofile 512000
* hard nofile 512000
* soft nproc 512000
* hard nproc 512000
root soft nofile 512000
root hard nofile 512000
root soft nproc 512000
root hard nproc 512000
EOF
fi
if which systemctl >/dev/null 2>&1; then
_yellow "optimizing sysctl configuration"
declare -A default_values
if [ -f "$sysctl_conf" ]; then
if [ ! -f "$sysctl_conf_backup" ]; then
cp "$sysctl_conf" "$sysctl_conf_backup"
fi
while IFS= read -r line; do
variable="${line%%=*}"
variable="${variable%%[[:space:]]*}"
default_value="${line#*=}"
default_values["$variable"]="$default_value"
done < <($sysctl_path -a)
echo "" >"$sysctl_default"
for variable in "${!sysctl_vars[@]}"; do
value="${sysctl_vars[$variable]}"
if variable_exists "$variable"; then
sed -i "s/^$variable=.*/$variable=$value/" "$sysctl_conf"
else
echo "$variable=$value" >>"$sysctl_conf"
default_value="${default_values[$variable]}"
echo "$variable=$default_value" >>"$sysctl_default"
fi
done
$sysctl_path -p 2>/dev/null
fi
fi
}
check_cdn() {
local o_url=$1
for cdn_url in "${cdn_urls[@]}"; do
if curl -sL -k "$cdn_url$o_url" --max-time 6 | grep -q "success" >/dev/null 2>&1; then
export cdn_success_url="$cdn_url"
return
fi
sleep 0.5
done
export cdn_success_url=""
}
check_cdn_file() {
check_cdn "https://raw.githubusercontent.com/spiritLHLS/ecs/main/back/test"
if [ -n "$cdn_success_url" ]; then
_yellow "CDN available, using CDN"
else
_yellow "No CDN available, no use CDN"
fi
}
check_time_zone() {
_yellow "adjusting the time"
if command -v ntpd >/dev/null 2>&1; then
if which systemctl >/dev/null 2>&1; then
systemctl stop chronyd
systemctl stop ntpd
else
service chronyd stop
service ntpd stop
fi
if lsof -i:123 | grep -q "ntpd"; then
echo "Port 123 is already in use. Skipping ntpd command."
else
# 最多对准时长进行60秒,避免对准时间这个过程耗时过长
if [ "$usage_timeout" = true ]; then
timeout 60s ntpd -gq
else
ntpd -gq
fi
if which systemctl >/dev/null 2>&1; then
systemctl start ntpd
else
service ntpd start
fi
fi
sleep 0.5
return
fi
if ! command -v chronyd >/dev/null 2>&1; then
${PACKAGE_INSTALL[int]} chrony >/dev/null 2>&1
fi
if which systemctl >/dev/null 2>&1; then
systemctl stop chronyd
chronyd -q -t 30
systemctl start chronyd
else
service chronyd stop
chronyd -q -t 30
service chronyd start
fi
sleep 0.5
}