-
Notifications
You must be signed in to change notification settings - Fork 946
/
start.sh
executable file
·2632 lines (2332 loc) · 60.4 KB
/
start.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
#!/bin/bash
RED='\033[0;91m'
GREEN='\033[0;92m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;96m'
WHITE='\033[0;37m'
RESET='\033[0m'
yellow() {
echo -e "${YELLOW}$1${RESET}"
}
green() {
echo -e "${GREEN}$1${RESET}"
}
red() {
echo -e "${RED}$1${RESET}"
}
installpath="$HOME"
if [[ -e "$installpath/serv00-play" ]]; then
source ${installpath}/serv00-play/utils.sh
fi
PS3="请选择(输入0退出): "
install(){
cd ${installpath}
if [ -d serv00-play ]; then
cd "serv00-play"
git stash
if git pull; then
echo "更新完毕"
#重新给各个脚本赋权限
chmod +x ./start.sh
chmod +x ./keepalive.sh
chmod +x ${installpath}/serv00-play/singbox/start.sh
chmod +x ${installpath}/serv00-play/singbox/killsing-box.sh
chmod +x ${installpath}/serv00-play/ssl/cronSSL.sh
red "请重新启动脚本!"
exit 0
fi
fi
cd ${installpath}
echo "正在安装..."
if ! git clone https://github.com/frankiejun/serv00-play.git; then
echo -e "${RED}安装失败!${RESET}"
exit 1;
fi
echo -e "${YELLOW}安装成功${RESET}"
}
showSingBoxInfo(){
cd ${installpath}/serv00-play/singbox
if [ ! -f singbox.json ]; then
red "配置文件不存在,请先行配置!"
return
fi
if [ ! -e list ]; then
red "请先运行sing-box"
fi
config="singbox.json"
type=$(jq -r ".TYPE" $config)
chmod +x ./start.sh && ./start.sh $type list
}
chooseSingbox(){
echo "保活sing-box中哪个项目: "
echo " 1.hy2/vmess+ws/socks5 "
echo " 2.argo+vmess "
echo " 3.all "
read -p "请选择:" input
if [ "$input" = "1" ]; then
item+=("hy2/vmess+ws")
elif [ "$input" = "2" ]; then
item+=("vmess")
elif [ "$input" = "3" ]; then
item+=("hy2/vmess+ws")
item+=("vmess")
else
red "无效选择!"
return 1
fi
}
setConfig(){
cd ${installpath}/serv00-play/
if [ -f config.json ]; then
echo "目前已有配置:"
config_content=$(cat config.json)
echo $config_content
read -p "是否修改? [y/n] [y]:" input
input=${input:-y}
if [ "$input" != "y" ]; then
return
fi
fi
createConfigFile
}
createConfigFile(){
echo "选择你要保活的项目(可多选,用空格分隔):"
echo "1. sun-panel "
echo "2. sing-box(包含hy2,vmess,socks5) "
echo "3. 哪吒探针 "
echo "4. mtproto代理"
echo "5. alist"
echo "6. webssh"
echo "88. 暂停所有保活功能"
echo "99. 复通所有保活功能(之前有配置的情况下)"
item=()
read -p "请选择: " choices
choices=($choices)
if [[ "${choices[@]}" =~ "88" && ${#choices[@]} -gt 1 ]]; then
red "选择出现了矛盾项,请重新选择!"
return 1
fi
#过滤重复
choices=($(echo "${choices[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
# 根据选择来询问对应的配置
for choice in "${choices[@]}"; do
case "$choice" in
1)
item+=("sun-panel")
;;
2)
if ! chooseSingbox; then
return
fi
;;
3)
item+=("nezha-agent")
;;
4)
item+=("mtg")
;;
5)
item+=("alist")
;;
6)
item+=("webssh")
;;
88)
delCron
backupConfig "config.json"
green "设置完毕!"
return 0
;;
99)
if [[ ! -e config.bak ]]; then
red "之前未有配置,未能复通!"
return 1
fi
restoreConfig "config.bak"
tm=$(jq -r ".chktime" config.json)
addCron $tm
green "设置完毕!"
return 0
;;
*)
echo "无效选择"
return 1
;;
esac
done
json_content="{\n"
json_content+=" \"item\": [\n"
for item in "${item[@]}"; do
json_content+=" \"$item\","
done
# 删除最后一个逗号并换行
json_content="${json_content%,}\n"
json_content+=" ],\n"
if [ "$num" = "4" ]; then
json_content+=" \"chktime\": \"null\""
json_content+="}\n"
printf "$json_content" > ./config.json
echo -e "${YELLOW} 设置完成! ${RESET} "
delCron
return
fi
read -p "配置保活检查的时间间隔(单位分钟,默认5分钟):" tm
tm=${tm:-"5"}
json_content+=" \"chktime\": \"$tm\","
read -p "是否需要配置消息推送? [y/n] [n]:" input
input=${input:-n}
if [ "${input}" == "y" ]; then
json_content+="\n"
echo "选择要推送的app:"
echo "1) Telegram "
echo "2) 微信 "
echo "3) 以上皆是"
read -p "请选择:" sendtype
if [ "$sendtype" == "1" ]; then
writeTG
elif [ "$sendtype" == "2" ]; then
writeWX
elif [ "$sendtype" == "3" ]; then
writeTG
writeWX
else
echo "无效选择"
return
fi
else
sendtype=${sendtype:-"null"}
fi
json_content+="\n \"sendtype\": $sendtype \n"
json_content+="}\n"
# 使用 printf 生成文件
printf "$json_content" > ./config.json
addCron $tm
chmod +x ${installpath}/serv00-play/keepalive.sh
echo -e "${YELLOW} 设置完成! ${RESET} "
}
backupConfig(){
local filename=$1
if [[ -e "$filename" ]]; then
if [[ "$filename" =~ ".json" ]]; then
local basename=${filename%.json}
mv $filename $basename.bak
fi
fi
}
restoreConfig(){
local filename=$1
if [[ -e "$filename" ]]; then
if [[ "$filename" =~ ".bak" ]]; then
local basename=${filename%.bak}
mv $filename $basename.json
fi
fi
}
make_vmess_config() {
cat >tempvmess.json <<EOF
{
"tag": "vmess-ws-in",
"type": "vmess",
"listen": "::",
"listen_port": $vmport,
"users": [
{
"uuid": "$uuid"
}
],
"transport": {
"type": "ws",
"path": "/$wspath",
"early_data_header_name": "Sec-WebSocket-Protocol"
}
}
EOF
}
make_hy2_config() {
cat > temphy2.json <<EOF
{
"tag": "hysteria-in",
"type": "hysteria2",
"listen": "$hy2_ip",
"listen_port": $hy2_port,
"users": [
{
"password": "$uuid"
}
],
"masquerade": "https://www.bing.com",
"tls": {
"enabled": true,
"alpn": [
"h3"
],
"certificate_path": "cert.pem",
"key_path": "private.key"
}
}
EOF
}
make_socks5_config(){
cat > tmpsocks5.json <<EOF
{
"type": "socks",
"tag": "socks-in",
"listen": "::",
"listen_port": $socks5_port,
"users": [
{
"username": "$username",
"password": "$password"
}
]
}
EOF
}
generate_config() {
comma=""
comma0=""
if [[ ! -e "private.key" || ! -e "cert.pem" ]]; then
openssl ecparam -genkey -name prime256v1 -out "private.key"
openssl req -new -x509 -days 3650 -key "private.key" -out "cert.pem" -subj "/CN=www.bing.com"
fi
if [[ "$type" == "1.1" || "$type" == "1.2" ]]; then
make_vmess_config
elif [ "$type" = "2" ]; then
make_hy2_config
elif [ "$type" = "1.3" ]; then
make_socks5_config
elif [[ "$type" =~ ^(2.4|2.5)$ ]]; then
make_vmess_config
comma0=","
make_socks5_config
elif [[ "$type" =~ ^(3.1|3.2)$ ]]; then
make_vmess_config
comma=","
make_hy2_config
elif [[ "$type" == "3.3" ]]; then
make_hy2_config
make_socks5_config
comma0=","
else
make_socks5_config
make_vmess_config
make_hy2_config
comma=","
comma0=","
fi
cat >config.json <<EOF
{
"log": {
"disabled": true,
"level": "info",
"timestamp": true
},
"dns": {
"servers": [
{
"tag": "google",
"address": "tls://8.8.8.8",
"strategy": "ipv4_only",
"detour": "direct"
}
],
"rules": [
{
"rule_set": [
"geosite-category-ads-all"
],
"server": "block"
}
],
"final": "google",
"strategy": "",
"disable_cache": false,
"disable_expire": false
},
"inbounds": [
$([[ "$type" =~ ^(1.3|3.3|2.4|2.5|3.3|4.4|4.5)$ ]] && cat tmpsocks5.json)
$comma0
$([[ "$type" == "1.1" || "$type" == "1.2" || "$type" =~ ^(2.4|2.5|3.1|3.2|4.4|4.5)$ ]] && cat tempvmess.json)
$comma
$([[ "$type" == "2" || "$type" =~ ^(3|4)\.[0-9]+$ ]] && cat temphy2.json)
],
"outbounds": [
{
"type": "direct",
"tag": "direct"
},
{
"type": "block",
"tag": "block"
},
{
"type": "dns",
"tag": "dns-out"
}
],
"route": {
"rules": [
{
"protocol": "dns",
"outbound": "dns-out"
},
{
"ip_is_private": true,
"outbound": "direct"
},
{
"rule_set": [
"geosite-category-ads-all"
],
"outbound": "block"
}
],
"rule_set": [
{
"tag": "geosite-category-ads-all",
"type": "remote",
"format": "binary",
"url": "https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-category-ads-all.srs",
"download_detour": "direct"
}
],
"final": "direct"
}
}
EOF
rm -rf tempvmess.json temphy2.json tmpsocks5.json
}
configSingBox(){
if ! checkInstalled "serv00-play"; then
return 1
fi
cd ${installpath}/serv00-play/singbox
loadPort
if [ -e singbox.json ]; then
red "目前已有配置如下:"
cat singbox.json
read -p "$(echo -e "${RED}继续配置将会覆盖原有配置:[y/n] [n]${RESET}") " input
input=${input:-n}
if [ "$input" != "y" ]; then
return 1
fi
fi
echo "选择你要配置的项目(可多选,用空格分隔):"
echo "1. vmess"
echo "2. hy2"
echo "3. socks5"
echo "4. all"
read -p "请选择: " choices
choices=($choices)
if [[ "${choices[@]}" =~ "4" ]]; then
choices=("4")
fi
#过滤重复
choices=($(echo "${choices[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
type="0"
# 根据选择来询问对应的配置
for choice in "${choices[@]}"; do
case "$choice" in
1)
echo "请选择协议(2选1):"
echo "1. argo+vmess"
echo "2. vmess+ws "
read -p "请选择:" co
if [[ "$co" != "1" && "$co" != "2" ]]; then
echo "无效输入!"
return 1
fi
if [[ "$co" == "1" ]]; then
type=$(echo "$type + 1.1" | bc)
randomPort tcp vmess
if [[ -n "$port" ]]; then
vmport="$port"
fi
read -p "请输入WSPATH,默认是[serv00]: " wspath
read -p "请输入ARGO隧道token: " token
read -p "请输入ARGO隧道的域名: " domain
else
type=$(echo "$type + 1.2" | bc)
randomPort tcp vmess
if [[ -n "$port" ]]; then
vmport="$port"
else
red "未输入端口号"
return 1
fi
read -p "请输入WSPATH,默认是[serv00]: " wspath
read -p "请输入优选域名:" goodDomain
fi
;;
2)
type=$(echo "$type + 2" | bc)
randomPort udp hy2
if [[ -n "$port" ]]; then
hy2_port="$port"
else
red "未输入端口号"
return 1
fi
echo "自动选择未封ip..."
hy2_ip=$(get_ip)
if [[ -n "$hy2_ip" ]]; then
green "选中未封ip为 $hy2_ip"
else
red "未能找到未封IP,保持默认值!"
fi
;;
3)
type=$(echo "$type + 1.3" | bc)
randomPort tcp socks5
if [[ -n "$port" ]]; then
socks5_port="$port"
else
red "未输入端口号"
return 1
fi
read -p "请输入socks5用户名:" username
read -p "请输入socks5密码:" password
;;
4)
echo "请选择协议(2选1):"
echo "1. argo+vmess"
echo "2. vmess+ws "
read -p "请选择:" co
if [[ "$co" != "1" && "$co" != "2" ]]; then
echo "无效输入!"
return 1
fi
if [[ "$co" == "1" ]]; then
type="3.1"
randomPort tcp vmess
if [[ -n "$port" ]]; then
vmport="$port"
else
red "未输入端口号"
return 1
fi
read -p "请输入WSPATH,默认是[serv00]: " wspath
read -p "请输入ARGO隧道token: " token
read -p "请输入ARGO隧道的域名: " domain
else
type="3.2"
randomPort tcp vmess
if [[ -n "$port" ]]; then
vmport="$port"
else
red "未输入端口号"
return 1
fi
read -p "请输入WSPATH,默认是[serv00]: " wspath
read -p "请输入优选域名:" goodDomain
fi
# 配置 hy2
randomPort udp hy2
if [[ -n "$port" ]]; then
hy2_port="$port"
else
red "未输入端口号"
return 1
fi
echo "自动选择未封ip..."
hy2_ip=$(get_ip)
if [[ -n "$hy2_ip" ]]; then
green "选中未封ip为 $hy2_ip"
else
red "未能找到未封IP,保持默认值!"
fi
#配置socks5
type=$(echo "$type + 1.3" | bc)
randomPort tcp socks5
if [[ -n "$port" ]]; then
socks5_port="$port"
else
red "未输入端口号"
return 1
fi
read -p "请输入socks5用户名:" username
read -p "请输入socks5密码:" password
;;
*)
echo "无效的选择: $choice"
;;
esac
done
if [[ "$type" != "1.3" ]]; then
wspath=${wspath:-serv00}
read -p "是否自动分配UUID? [y/n] [y]:" input
input=${input:-y}
if [[ "$input" == "y" ]]; then
uuid=$(uuidgen -r)
else
read -p "请输入UUID:" uuid
fi
fi
cat > singbox.json <<EOF
{
"TYPE": "$type",
"VMPORT": ${vmport:-null},
"HY2IP": "${hy2_ip:-'::'}",
"HY2PORT": ${hy2_port:-null},
"UUID": "$uuid",
"WSPATH": "${wspath}",
"ARGO_AUTH": "${token:-null}",
"ARGO_DOMAIN": "${domain:-null}",
"GOOD_DOMAIN": "${goodDomain:-null}",
"SOCKS5_PORT": "${socks5_port:-null}",
"SOCKS5_USER": "${username:-null}",
"SOCKS5_PASS": "${password:-null}"
}
EOF
generate_config
yellow "sing-box配置完毕!"
}
startSingBox(){
cd ${installpath}/serv00-play/singbox
if [[ ! -e "singbox.json" ]]; then
red "请先进行配置!"
return 1
fi
# if [[ ! -e ${installpath}/serv00-play/singbox/serv00sb ]] || [[ ! -e ${installpath}/serv00-play/singbox/cloudflared ]]; then
# read -p "请输入使用密码:" password
# fi
if ! checkDownload "serv00sb"; then
return
fi
if ! checkDownload "cloudflared"; then
return
fi
if checkSingboxAlive; then
red "sing-box 已在运行,请勿重复操作!"
exit 1
else
chmod 755 ./killsing-box.sh
./killsing-box.sh
fi
if chmod +x start.sh && ! ./start.sh; then
red "sing-box启动失败!"
exit 1
fi
yellow "启动成功!"
}
stopSingBox(){
cd ${installpath}/serv00-play/singbox
if [ -f killsing-box.sh ]; then
chmod 755 ./killsing-box.sh
./killsing-box.sh
else
echo "请先安装serv00-play!!!"
return
fi
echo "已停掉sing-box!"
}
killUserProc(){
local user=$(whoami)
pkill -kill -u $user
}
ImageRecovery(){
cd ${installpath}/backups/local
# 定义一个关联数组
declare -A snapshot_paths
# 遍历每个符号链接,并将文件夹名称及真实路径保存到数组中
while read -r line; do
# 提取文件夹名称和对应的真实路径
folder=$(echo "$line" | awk '{print $9}')
real_path=$(echo "$line" | awk '{print $11}')
# 将文件夹名称和真实路径存入数组
snapshot_paths["$folder"]="$real_path"
done < <(ls -trl | grep -F "lrwxr")
size=${#snapshot_paths[@]}
sorted_keys=($(echo "${!snapshot_paths[@]}" | tr ' ' '\n' | sort -r))
if [ $size -eq 0 ]; then
echo "未有备份快照!"
return
fi
echo "选择你需要恢复的内容:"
echo "1. 完整快照恢复 "
echo "2. 恢复某个文件或目录"
read -p "请选择:" input
if [ "$input" = "1" ]; then
local i=1
declare -a folders
for folder in "${sorted_keys[@]}"; do
echo "${i}. ${folder} "
i=$((i+1))
done
retries=3
while [ $retries -gt 0 ]; do
read -p "请选择恢复到哪一天(序号)?" input
# 检查输入是否有效
if [[ $input =~ ^[0-9]+$ ]] && [ "$input" -gt 0 ] && [ "$input" -le $size ]; then
# 输入有效,退出循环
targetFolder="${sorted_keys[@]:$input-1:1}"
echo "你选择的恢复日期是:${targetFolder}"
break
else
# 输入无效,减少重试次数
retries=$((retries-1))
echo "输入有误,请重新输入!你还有 $retries 次机会。"
fi
if [ $retries -eq 0 ]; then
echo "输入错误次数过多,操作已取消。"
return
fi
done
killUserProc
srcpath=${snapshot_paths["${targetFolder}"]}
#echo "srcpath:$srcpath"
rm -rf ~/* > /dev/null 2>&1
rsync -a $srcpath/ ~/ 2>/dev/null
yellow "快照恢复完成!"
return
elif [ "$input" = "2" ]; then
declare -A foundArr
read -p "输入你要恢复到文件或目录:" infile
for folder in "${!snapshot_paths[@]}"; do
path="${snapshot_paths[$folder]}"
results=$(find "${path}" -name "$infile" 2>/dev/null)
# echo "111results:|$results|"
if [[ -n "$results" ]]; then
#echo "put |$results| to folder:$folder"
foundArr["$folder"]="$results"
fi
done
local i=1
sortedFoundArr=($(echo "${!foundArr[@]}" | tr ' ' '\n' | sort -r))
declare -A indexPathArr
for folder in "${sortedFoundArr[@]}"; do
echo "$i. $folder:"
results="${foundArr[${folder}]}"
IFS=$'\n' read -r -d '' -a paths <<< "$results"
local j=1
for path in "${paths[@]}"; do
indexPathArr["$i"."$j"]="$path"
echo " $j. $path"
j=$((j+1))
done
i=$((i+1))
done
while [ true ]; do
read -p "输入要恢复的文件序号,格式:日期序号.文件序号, 多个以逗号分隔.(如输入 1.2,3.2)[按enter返回]:" input
regex='^([0-9]+\.[0-9]+)(,[0-9]+\.[0-9]+)*$'
if [ -z "$input" ]; then
return
fi
if [[ "$input" =~ $regex ]]; then
declare -a pairNos
declare -a fileNos
IFS=',' read -r -a pairNos <<< "$input"
echo "请选择文件恢复的目标路径:"
echo "1.原路返回 "
echo "2.${installpath}/restore "
read -p "请选择:" targetDir
if [[ "$targetDir" != "1" ]] && [[ "$targetDir" != "2" ]];then
red "无效输入!"
return
fi
for pairNo in "${pairNos[@]}"; do
srcpath="${indexPathArr[$pairNo]}"
if [ "$targetDir" = "1" ]; then
local user=$(whoami)
targetPath=${srcpath#*${user}}
if [ -d $srcpath ]; then
targetPath=${targetPath%/*}
fi
echo "cp -r $srcpath $HOME/$targetPath"
cp -r ${srcpath} $HOME/${targetPath}
elif [ "$targetDir" = "2" ]; then
targetPath="${installpath}/restore"
if [ ! -e "$targetPath" ]; then
mkdir -p "$targetPath"
fi
cp -r $srcpath $targetPath/
fi
done
green "完成文件恢复"
else
red "输入格式不对,请重新输入!"
fi
done
fi
}
uninstall(){
read -p "确定卸载吗? [y/n] [n]:" input
input=${input:-n}
if [ "$input" == "y" ]; then
delCron
stopSingBox
cd $HOME
rm -rf serv00-play
echo "bye!"
fi
}
InitServer(){
read -p "$(red "将初始化帐号系统,要继续?[y/n] [n]:")" input
input=${input:-n}
read -p "是否保留用户配置?[y/n] [y]:" saveProfile
saveProfile=${saveProfile:-y}
if [[ "$input" == "y" ]] || [[ "$input" == "Y" ]]; then
cleanCron
green "清理进程中..."
killUserProc
green "清理磁盘中..."
if [[ "$saveProfile" = "y" ]] || [[ "$saveProfile" = "Y" ]]; then
rm -rf ~/* 2>/dev/null
else
rm -rf ~/* ~/.* 2>/dev/null
fi
cleanPort
yellow "初始化完毕"
exit 0
fi
}
manageNeZhaAgent(){
if ! checkInstalled "serv00-play"; then
return 1
fi
while true; do
yellow "-------------------------"
echo "探针管理:"
echo "1.安装探针"
echo "2.升级探针"
echo "3.启动/重启探针"
echo "4.停止探针"
echo "9.返回主菜单"
echo "0.退出脚本"
yellow "-------------------------"
read -p "请选择:" choice
case $choice in
1)
installNeZhaAgent
;;
2)
updateAgent
;;
3)
startAgent
exit 0;
;;
4)
stopNeZhaAgent
;;
9)
break
;;
0) exit 0
;;
*)
echo "无效选项,请重试"
;;
esac
done
showMenu
}
updateAgent(){
red "暂不提供在线升级, 只适配哪吒面板v0版本系列。"
return 1
exepath="${installpath}/serv00-play/nezha/nezha-agent"
if [ ! -e "$exepath" ]; then
red "未安装探针,请先安装!!!"
return
fi
local workedir="${installpath}/serv00-play/nezha"
cd $workedir
local_version="v"$(./nezha-agent -v)
latest_version=$(curl -sL https://github.com/nezhahq/agent/releases/latest | sed -n 's/.*tag\/\(v[0-9.]*\).*/\1/p' | head -1)
if [[ "$local_version" != "$latest_version" ]]; then
echo "发现新版本: $latest_version,当前版本: $local_version。正在更新..."
download_url="https://github.com/nezhahq/agent/releases/download/$latest_version/nezha-agent_freebsd_amd64.zip"
local filezip="nezha-agent_latest.zip"
curl -sL -o "$filezip" "$download_url"
if [[ ! -e "$filezip" || -n $(file "$filezip" | grep "text") ]]; then
echo "下载探针文件失败!"
return
fi
local agent_runing=0
if checknezhaAgentAlive; then
stopNeZhaAgent
agent_runing=1
fi
unzip -o $filezip -d .
chmod +x ./nezha-agent
if [ $agent_runing -eq 1 ]; then
startAgent
fi
rm -rf $filezip
green "更新完成!新版本: $latest_version"
else
echo "已经是最新版本: $local_version"
fi
if [[ $agent_runing -eq 1 ]]; then
exit 0;
fi
}
startAgent(){
local workedir="${installpath}/serv00-play/nezha"
if [ ! -e "${workedir}" ]; then
red "未安装探针,请先安装!!!"
return
fi
cd $workedir
local configfile="./nezha.json"
if [ ! -e "$configfile" ]; then
red "未安装探针,请先安装!!!"
return
fi
nezha_domain=$(jq -r ".nezha_domain" $configfile)
nezha_port=$(jq -r ".nezha_port" $configfile)
nezha_pwd=$(jq -r ".nezha_pwd" $configfile)
tls=$(jq -r ".tls" $configfile)
if checknezhaAgentAlive; then
stopNeZhaAgent
fi
local args="--report-delay 4 --disable-auto-update --disable-force-update "
if [[ "$tls" == "y" ]]; then
args="${args} --tls "
fi
#echo "./nezha-agent ${args} -s ${nezha_domain}:${nezha_port} -p ${nezha_pwd}"
nohup ./nezha-agent ${args} -s ${nezha_domain}:${nezha_port} -p ${nezha_pwd} >/dev/null 2>&1 &
if checknezhaAgentAlive; then
green "启动成功!"
else
red "启动失败!"
fi
#即便使用nohup放后台,此处如果使用ctrl+c退出脚本,nezha-agent进程也会退出。非常奇葩,因此startAgent后只能exit退出脚本,避免用户使用ctrl+c退出。
}