-
Notifications
You must be signed in to change notification settings - Fork 0
/
napcat copy.sh
1001 lines (855 loc) · 30.5 KB
/
napcat copy.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/sh
clear
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m'
CURRENT_DIR=$(
cd "$(dirname "$0")" || exit
pwd
)
echo -e '\033[35m'
cat << EOF
███╗ ██╗ █████╗ ██████╗ ██████╗ █████╗ ████████╗
████╗ ██║██╔══██╗██╔══██╗██╔════╝██╔══██╗╚══██╔══╝
██╔██╗ ██║███████║██████╔╝██║ ███████║ ██║
██║╚██╗██║██╔══██║██╔═══╝ ██║ ██╔══██║ ██║
██║ ╚████║██║ ██║██║ ╚██████╗██║ ██║ ██║
╚═╝ ╚═══╝╚═╝ ╚═╝╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═╝
EOF
echo -e '\033[0m'
echo "当前目录为 $CURRENT_DIR"
function log() {
message="[Napcat Log]: $1 "
case "$1" in
*"失败"*|*"错误"*|*"请使用 root 或 sudo 权限运行此脚本"*|*"暂不支持的系统架构,请参阅官方文档,选择受支持的系统。"*)
echo -e "${RED}${message}${NC}" 2>&1 | tee -a "${CURRENT_DIR}"/install.log
;;
*"成功"*)
echo -e "${GREEN}${message}${NC}" 2>&1 | tee -a "${CURRENT_DIR}"/install.log
;;
*"忽略"*|*"跳过"*)
echo -e "${YELLOW}${message}${NC}" 2>&1 | tee -a "${CURRENT_DIR}"/install.log
;;
*)
echo -e "${BLUE}${message}${NC}" 2>&1 | tee -a "${CURRENT_DIR}"/install.log
;;
esac
}
function Check_System() {
osCheck=$(uname -m)
if [[ $osCheck == "x86_64" || $osCheck == "amd64" ]]; then
os="x86_64"
elif [[ $osCheck == "aarch64" || $osCheck == "arm64" ]]; then
os="aarch64"
else
log "暂不支持的系统架构,请参阅官方文档,选择受支持的系统。"
exit 1
fi
if ! command -v apt &> /dev/null; then
log "未检测到 apt 包管理器,请参阅官方文档,选择受支持的系统。"
exit 1
fi
}
function Check_Root() {
if [[ $EUID -ne 0 ]]; then
log "请使用 root 或 sudo 权限运行此脚本"
exit 1
fi
}
function Docker_Network_Test() {
docker_target_proxy=""
docker_proxy_arr=("docker.1panel.dev" "dockerpull.com" "docker.rainbond.cc" "dockerproxy.cn" "docker.agsvpt.work" "docker.agsv.top" "docker.registry.cyou")
for docker_proxy in "${docker_proxy_arr[@]}"; do
docker_status=$(curl -o /dev/null -s -w "%{http_code}" --max-time 10 "$docker_proxy/")
if [ $docker_status -eq 200 ] || [ $docker_status -eq 301 ]; then
docker_target_proxy="$docker_proxy"
log "将使用Docker代理: $docker_proxy"
break
else
log "无可用代理, 将直接连接Docker..."
fi
done
}
function Github_Network_Test() {
github_target_proxy=""
github_proxy_arr=( "https://ghp.ci" "https://github.moeyy.xyz" "https://mirror.ghproxy.com" "https://gh-proxy.com" "https://x.haod.me")
for github_proxy in "${github_proxy_arr[@]}"; do
github_status=$(curl -o /dev/null -s -w "%{http_code}" --max-time 10 "$github_proxy/")
if [ $github_status -eq 200 ]; then
github_target_proxy="$github_proxy"
log "将使用Github代理: $github_proxy"
break
else
log "无可用代理, 将直接连接Github..."
fi
done
}
function Change_Repo() {
change_repo_url="https://linuxmirrors.cn/main.sh"
if curl -s --head "$change_repo_url" | grep "200 OK" > /dev/null; then
change_repo_url=${change_repo_url}
else
Github_Network_Test
change_repo_url=${github_target_proxy:+${github_target_proxy}/}https://raw.githubusercontent.com/SuperManito/LinuxMirrors/main/ChangeMirrors.sh
fi
curl -sSL ${change_repo_url} -o ChangeMirrors.sh
chmod +x ChangeMirrors.sh
log "(1)手动换源"
log "(2)一键换源(清华源)"
while true; do
read -p "请输入数字选择您需要进行的操作:" repochoice
case "$repochoice" in
1)
bash ChangeMirrors.sh
break
;;
2)
bash ChangeMirrors.sh --source mirrors.tuna.tsinghua.edu.cn --protocol http --use-intranet-source false --install-epel false --close-firewall true --backup true --upgrade-software true --clean-cache true --ignore-backup-tips
break
;;
*)
log "错误的选项,请重新输入。"
continue
;;
esac
done
}
function Install_Fonts() {
log "apt install fonts-wqy-zenhei fonts-wqy-microhei -y > /dev/null 2>&1"
apt install fonts-wqy-zenhei fonts-wqy-microhei -y > /dev/null 2>&1
log "fc-cache -fv > /dev/null 2>&1"
fc-cache -fv > /dev/null 2>&1
log "中文字体安装完成,若不生效请重启服务器"
}
function Check_Docker() {
while true; do
if command -v docker >/dev/null 2>&1; then
docker_version=$(docker --version | awk '{print $3}' | sed 's/,//g')
log "检测到 Docker 已安装,当前版本为 $docker_version"
read -p "是否强制重新安装(回车默认跳过) (Y/n): " reinstalldocker
if [[ "$reinstalldocker" == "N" || "$reinstalldocker" == "n" ]]; then
log "正在强制重新安装 Docker..."
Install_Docker
break
fi
if systemctl is-active --quiet docker; then
log "Docker 已在运行, 不需要重启"
break
else
log "启动 Docker"
systemctl start docker 2>&1 | tee -a "${CURRENT_DIR}"/install.log
break
fi
else
log "未检测到 Docker, 正在安装"
Install_Docker
break
fi
done
}
function Install_Docker() {
while true; do
log "请选择 Docker 安装方式:"
log "1. 在线安装"
log "2. 离线安装"
read -p "请输入选择 (回车默认在线安装): " choiceway
choiceway=${choiceway:-1}
if [ "$choiceway" = "1" ]; then
log "... 在线安装 Docker"
dockerinstallation_url="https://linuxmirrors.cn/docker.sh"
if curl -s --head "$dockerinstallation_url" | grep "200 OK" > /dev/null; then
dockerinstallation_url=${dockerinstallation_url}
else
Github_Network_Test
dockerinstallation_url=${github_target_proxy:+${github_target_proxy}/}https://raw.githubusercontent.com/SuperManito/LinuxMirrors/main/DockerInstallation.sh
fi
curl -sSL ${dockerinstallation_url} -o DockerInstallation.sh
chmod +x DockerInstallation.sh
log "(1)手动安装"
log "(2)一键安装(清华源)"
while true; do
read -p "请输入数字选择您需要进行的操作:" dockerchoice
case "$dockerchoice" in
1)
bash DockerInstallation.sh
break
;;
2)
bash DockerInstallation.sh --source mirrors.tuna.tsinghua.edu.cn/docker-ce --source-registry registry.hub.docker.com --install-latested true --ignore-backup-tips
break
;;
*)
log "错误的选项,请重新输入。"
continue
;;
esac
done
elif [ "$choiceway" = "2" ]; then
log "... 离线安装 Docker"
Github_Network_Test
curl -O ${github_target_proxy:+${github_target_proxy}/}https://github.com/Fahaxikiii/napcat-scripts/releases/download/docker/docker.tar.gz
tar zxvf docker.tar.gz
rm -rf docker.tar.gz
chmod +x docker/bin/*
cp docker/bin/* /usr/bin/
cp docker/service/docker.service /etc/systemd/system/
chmod 754 /etc/systemd/system/docker.service
mkdir -p /etc/docker/
cp docker/conf/daemon.json /etc/docker/daemon.json
rm -rf docker
log "... 启动 docker"
systemctl enable docker; systemctl daemon-reload; systemctl start docker 2>&1 | tee -a "${CURRENT_DIR}"/install.log
else
log "无效的选择,请重新输入"
continue
fi
if command -v docker >/dev/null 2>&1; then
docker_new_version=$(docker --version | awk '{print $3}' | sed 's/,//g')
log "Docker 已安装,版本为 $docker_new_version"
break
else
log "Docker 安装更新失败"
read -p "是否重试安装 Docker? 请尝试另一种方法(Y/n): " retry
if [ "$retry" = "Y" ] || [ "$retry" = "y" ] || [ -z "$retry" ]; then
log "重试安装 Docker..."
continue
else
log "退出安装"
exit 1
fi
fi
done
}
function Check_Docker-Compose() {
Check_System
Github_Network_Test
REQUIRED_Docker_Compose_Version="2.26.1"
#docker+compose_version=$(curl "https://api.github.com/repos/docker/compose/releases/latest" | jq -r '.tag_name')
docker_compose_url=${github_target_proxy:+${github_target_proxy}/}https://github.com/Fahaxikiii/napcat-scripts/releases/download/docker-compose/docker-compose-linux-$os
if command -v docker-compose >/dev/null 2>&1; then
INSTALLED_Docker_Compose_Version=$(docker-compose --version | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+' | sed 's/^v//')
if [ "$(printf '%s\n' "$REQUIRED_Docker_Compose_Version" "$INSTALLED_Docker_Compose_Version" | sort -V | head -n1)" = "$REQUIRED_Docker_Compose_Version" ]; then
log "检测到 docker-compose 已安装, 跳过安装步骤"
else
log " docker-compose 版本过低, 开始升级"
apt autoremove docker-compose -y > /dev/null 2>&1
rm -rf $(which docker-compose) > /dev/null 2>&1
curl -L "$docker_compose_url" -o /usr/bin/docker-compose
if [ -f /usr/bin/docker-compose ]; then
chmod +x /usr/bin/docker-compose
log "docker-compose 成功安装。"
else
log "文件下载失败,请检查网络连接。"
exit 1
fi
fi
else
log "检测到 docker-compose 没有安装, 开始安装"
curl -L "$docker_compose_url" -o /usr/bin/docker-compose
if [ -f "/usr/bin/docker-compose" ]; then
chmod +x /usr/bin/docker-compose
log "docker-compose 成功安装。"
else
log "文件下载失败,请检查网络连接。"
exit 1
fi
fi
}
function Set_Container_Name() {
DEFAULT_CONTAINER_NAME="napcat"
while true; do
read -p "设置容器名称(默认为$DEFAULT_CONTAINER_NAME): " CONTAINER_NAME
if [[ "$CONTAINER_NAME" == "" ]];then
CONTAINER_NAME=$DEFAULT_CONTAINER_NAME
fi
log "您设置的容器名称为: $CONTAINER_NAME"
break
done
}
function Set_QQ_Account() {
DEFAULT_QQ_ACCOUNT="123456789"
while true; do
read -p "设置 机器人QQ号(默认为$DEFAULT_QQ_ACCOUNT): " QQ_ACCOUNT
if [[ "$QQ_ACCOUNT" == "" ]];then
QQ_ACCOUNT=$DEFAULT_QQ_ACCOUNT
fi
if [[ ! "$QQ_ACCOUNT" =~ ^[0-9]{1,30}$ ]]; then
log "错误: QQ号仅支持数字,长度 1-30 位"
continue
fi
log "您设置的机器人QQ号为: $QQ_ACCOUNT"
break
done
}
function Set_Config_Path() {
log "当前目录为 $CURRENT_DIR"
DEFAULT_CONFIG_PATH="/opt/napcat"
while true; do
read -p "设置 NAPCAT 安装目录(默认为$DEFAULT_CONFIG_PATH): " CONFIG_PATH
if [[ "$CONFIG_PATH" != "" ]]; then
if [[ "$CONFIG_PATH" != /* ]]; then
log "请输入目录的完整路径"
continue
elif [[ ! -d $CONFIG_PATH ]]; then
mkdir -p "$CONFIG_PATH"
mkdir -p "$CONFIG_PATH/QQ"
mkdir -p "$CONFIG_PATH/config"
mkdir -p "$CONFIG_PATH/logs"
Create_Napcat
log "您设置的安装路径为: $CONFIG_PATH"
log "您设置的QQ持久化数据路径为: $CONFIG_PATH/QQ"
log "您设置的NapCat配置文件路径为: $CONFIG_PATH/config"
log "您设置的NapCat日志输出路径为: $CONFIG_PATH/logs"
break
else
Create_Napcat
log "您设置的安装路径为: $CONFIG_PATH"
log "您设置的QQ持久化数据路径为: $CONFIG_PATH/QQ"
log "您设置的NapCat配置文件路径为: $CONFIG_PATH/config"
log "您设置的NapCat日志输出路径为: $CONFIG_PATH/logs"
break
fi
else
CONFIG_PATH=$DEFAULT_CONFIG_PATH
mkdir -p "$CONFIG_PATH"
mkdir -p "$CONFIG_PATH/QQ"
mkdir -p "$CONFIG_PATH/config"
mkdir -p "$CONFIG_PATH/logs"
Create_Napcat
log "您设置的安装路径为: $CONFIG_PATH"
log "您设置的QQ持久化数据路径为: $CONFIG_PATH/QQ"
log "您设置的NapCat配置文件路径为: $CONFIG_PATH/config"
log "您设置的NapCat日志输出路径为: $CONFIG_PATH/logs"
break
fi
done
}
function Set_Bot_Path() {
while true; do
read -p "是否添加机器人目录(方便ffmpeg转码及发送本地文件) (Y/n): " addbot
if [ "$addbot" = "Y" ] || [ "$addbot" = "y" ] || [ -z "$addbot" ]; then
Add_Bot_Path
break
else
log "跳过添加,继续下一步"
break
fi
done
}
function Add_Bot_Path() {
while true; do
log "回车也可跳过添加,继续下一步"
read -p "请输入机器人安装目录(如/root/zhenxun_bot): " BOT_PATH
if [[ "$BOT_PATH" == "" ]]; then
log "目录为空,跳过添加,继续下一步。"
BOT_PATH=$BOT_PATH
break
elif [[ "$BOT_PATH" != /* ]]; then
log "请输入目录的完整路径"
continue
elif [[ ! -d $BOT_PATH ]]; then
log "目录不存在,请重新添加"
continue
fi
BOT_PATH=$BOT_PATH
break
done
}
function Set_Webui_Host() {
DEFAULT_WEBUI_HOST="0.0.0.0"
while true; do
read -p "设置WEBUI主机(默认为$DEFAULT_WEBUI_HOST): " WEBUI_HOST
if [[ "$WEBUI_HOST" == "" ]];then
WEBUI_HOST=$DEFAULT_WEBUI_HOST
fi
if [[ "$WEBUI_HOST" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
WEBUI_HOST=$WEBUI_HOST
log "您设置的WEBUI主机为: $WEBUI_HOST"
break
elif [[ "$WEBUI_HOST" =~ ^\[?([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\]?$ || "$WEBUI_HOST" == "::" ]]; then
WEBUI_HOST=[$WEBUI_HOST]
log "您设置的WEBUI主机为: $WEBUI_HOST"
break
else
log "错误: 输入的主机必须是ipv4或ipv6地址"
continue
fi
log "您设置的WEBUI主机为: $WEBUI_HOST"
break
done
}
function Set_Webui_Port() {
DEFAULT_WEBUI_PORT="6099"
while true; do
read -p "设置 WEBUI 端口(默认为$DEFAULT_WEBUI_PORT): " WEBUI_PORT
if [[ "$WEBUI_PORT" == "" ]];then
WEBUI_PORT=$DEFAULT_WEBUI_PORT
fi
if ! [[ "$WEBUI_PORT" =~ ^[1-9][0-9]{0,4}$ && "$WEBUI_PORT" -le 65535 ]]; then
log "错误: 输入的端口号必须在 1 到 65535 之间"
continue
fi
if command -v ss >/dev/null 2>&1; then
if ss -tlun | grep -q ":$WEBUI_PORT " >/dev/null 2>&1; then
log "端口$WEBUI_PORT被占用, 请重新输入..."
continue
fi
elif command -v netstat >/dev/null 2>&1; then
if netstat -tlun | grep -q ":$WEBUI_PORT " >/dev/null 2>&1; then
log "端口$WEBUI_PORT被占用, 请重新输入..."
continue
fi
fi
log "您设置的WEBUI端口为: $WEBUI_PORT"
break
done
}
function Set_Webui_Token() {
DEFAULT_WEBUI_TOKEN="napcat"
while true; do
read -p "设置WEBUI密钥(默认为$DEFAULT_WEBUI_TOKEN): " WEBUI_TOKEN
if [[ "$WEBUI_TOKEN" == "" ]];then
WEBUI_TOKEN=$DEFAULT_WEBUI_TOKEN
fi
log "您设置的WEBUI密钥为: $WEBUI_TOKEN"
break
done
}
function Set_Webui_Login_Rate() {
DEFAULT_WEBUI_LOGIN_RATE="3"
while true; do
read -p "设置WEBUI登录次数(默认为$DEFAULT_WEBUI_LOGIN_RATE): " WEBUI_LOGIN_RATE
if [[ "$WEBUI_LOGIN_RATE" == "" ]];then
WEBUI_LOGIN_RATE=$DEFAULT_WEBUI_LOGIN_RATE
fi
if [[ ! "$WEBUI_LOGIN_RATE" =~ ^[0-9]{1,30}$ ]]; then
log "错误: WEBUI登录次数仅支持数字,长度 1-30 位"
continue
fi
log "您设置的WEBUI登录次数为: $WEBUI_LOGIN_RATE"
break
done
}
function Create_Webui_Config() {
WEBUI_PATH="$CONFIG_PATH/config/webui.json"
touch "$WEBUI_PATH"
cat <<EOF > "$WEBUI_PATH"
{
"host": "$WEBUI_HOST",
"port": $WEBUI_PORT,
"prefix": "",
"token": "$WEBUI_TOKEN",
"loginRate": $WEBUI_LOGIN_RATE
}
EOF
}
function Set_Mac_Address() {
DEFAULT_MAC_ADDRESS=$(ip addr show $(ip route | awk '/default/ {print $5}') | grep link/ether | awk '{print $2}')
while true; do
read -p "设置 MAC_ADDRESS(默认为$DEFAULT_MAC_ADDRESS): " MAC_ADDRESS
if [[ "$MAC_ADDRESS" == "" ]];then
MAC_ADDRESS=$DEFAULT_MAC_ADDRESS
fi
if [[ ! "$MAC_ADDRESS" =~ ^([0-9a-fA-F]{2}(:|-)){5}[0-9a-fA-F]{2}$ ]]; then
log "错误: MAC_ADDRESS格式错误"
continue
fi
log "您设置的MAC_ADDRESS为: $MAC_ADDRESS"
break
done
}
function Set_Napcat_UID() {
DEFAULT_NAPCAT_UID="0"
while true; do
read -p "设置Napcat UID(默认为$DEFAULT_NAPCAT_UID): " NAPCAT_UID
if [[ "$NAPCAT_UID" == "" ]];then
NAPCAT_UID=$DEFAULT_NAPCAT_UID
fi
if [[ ! "$NAPCAT_UID" =~ ^[0-9]{1,30}$ ]]; then
log "错误: Napcat UID仅支持数字,长度 1-30 位"
continue
fi
log "您设置的Napcat UID为: $NAPCAT_UID"
break
done
}
function Set_Napcat_GID() {
DEFAULT_NAPCAT_GID="0"
while true; do
read -p "设置Napcat GID(默认为$DEFAULT_NAPCAT_GID): " NAPCAT_GID
if [[ "$NAPCAT_GID" == "" ]];then
NAPCAT_GID=$DEFAULT_NAPCAT_GID
fi
if [[ ! "$NAPCAT_GID" =~ ^[0-9]{1,30}$ ]]; then
log "错误: Napcat GID仅支持数字,长度 1-30 位"
continue
fi
log "您设置的Napcat GID为: $NAPCAT_GID"
break
done
}
function Get_Ip() {
#active_interface=$(ip route get 8.8.8.8 | awk 'NR==1 {print $5}')
active_interface=$(ip route | awk '/default/ {print $5}')
PUBLIC_IPV4=$(curl -s 4.ipw.cn)
PUBLIC_IPV6=$(curl -s 6.ipw.cn)
if [[ -z $active_interface ]]; then
LOCAL_IP="127.0.0.1"
else
LOCAL_IP=$(ip -4 addr show dev "$active_interface" | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
fi
if [[ -z "$PUBLIC_IPV4" ]]; then
PUBLIC_IPV4="N/A"
fi
if echo "$PUBLIC_IPV4" | grep -q ":"; then
PUBLIC_IPV4=${PUBLIC_IPV4}
fi
if [[ -z "$PUBLIC_IPV6" ]]; then
PUBLIC_IPV6="N/A"
fi
if echo "$PUBLIC_IPV6" | grep -q ":"; then
PUBLIC_IPV6=[${PUBLIC_IPV6}]
fi
}
function Confirm_Napcat() {
log "(1)您设置的容器名称为: $CONTAINER_NAME"
log "(2)您设置的机器人QQ号为: $QQ_ACCOUNT"
log "(3)您设置的QQ持久化数据路径为: $CONFIG_PATH/QQ"
log "(3)您设置的NapCat配置文件路径为: $CONFIG_PATH/config"
log "(3)您设置的NapCat日志输出路径为: $CONFIG_PATH/logs"
log "(4)您设置的WEBUI主机为: $WEBUI_HOST"
log "(5)您设置的WEBUI端口为: $WEBUI_PORT"
log "(6)您设置的WEBUI密钥为: $WEBUI_TOKEN"
log "(7)您设置的WEBUI登录次数为: $WEBUI_LOGIN_RATE"
log "(8)您设置的MAC_ADDRESS为: $MAC_ADDRESS"
log "(9)您设置的Napcat UID为: $NAPCAT_UID"
log "(10)您设置的Napcat GID为: $NAPCAT_GID"
if [[ "$BOT_PATH" != "" ]]; then
log "(11)您设置的机器人目录为: $BOT_PATH"
else
log "(11)您设置的机器人目录为空"
fi
while true; do
log "您想要继续修改数据还是继续下一步?直接回车进行安装"
read -p "请输入数字选择您需要修改的数据:" choice
case "$choice" in
1)
Set_Container_Name
continue
;;
2)
Set_QQ_Account
continue
;;
3)
Set_Config_Path
continue
;;
4)
Set_Webui_Host
continue
;;
5)
Set_Webui_Port
continue
;;
6)
Set_Webui_Token
continue
;;
7)
Set_Webui_Login_Rate
continue
;;
8)
Set_Mac_Address
continue
;;
9)
Set_Napcat_UID
continue
;;
10)
Set_Napcat_GID
continue
;;
11)
Set_Bot_Path
continue
;;
"")
break
;;
*)
log "错误的选项,请重新输入。"
continue
;;
esac
done
log "开始安装,请您耐心等待。"
Create_Napcat_ENV
Create_Napcat_CMD
Install_Napcat
}
function Create_Napcat_ENV() {
Docker_Network_Test
cat <<EOF > "${CONFIG_PATH}/.env"
CONTAINER_NAME=${CONTAINER_NAME}
MAC_ADDRESS=${MAC_ADDRESS}
NAPCAT_UID=${NAPCAT_UID}
NAPCAT_GID=${NAPCAT_GID}
QQ_ACCOUNT=${QQ_ACCOUNT}
CONFIG_PATH=${CONFIG_PATH}
docker_target_proxy=${docker_target_proxy}
BOT_PATH=$BOT_PATH
EOF
}
function Create_Napcat() {
cat <<EOF > "${CONFIG_PATH}/docker-compose.yml"
services:
napcat:
container_name: \${CONTAINER_NAME}
restart: always
network_mode: "host"
mac_address: "\${MAC_ADDRESS}"
environment:
- "TZ=Asia/Shanghai"
- "NAPCAT_UID=\${NAPCAT_UID}"
- "NAPCAT_GID=\${NAPCAT_GID}"
- "ACCOUNT=\${QQ_ACCOUNT}"
volumes:
- "\${CONFIG_PATH}/QQ:/app/.config/QQ"
- "\${CONFIG_PATH}/config:/app/napcat/config"
- "\${CONFIG_PATH}/logs:/app/napcat/logs"
image: \${docker_target_proxy:+\${docker_target_proxy}/}mlikiowa/napcat-docker:latest
EOF
}
function Check_Docker-Compose_File() {
while true; do
if [ -f "docker-compose.yml" ]; then
break
else
log "当前目录下docker-compose.yml文件不存在, 操作失败"
exit 1
fi
done
}
function Install_Napcat() {
docker-compose -f "${CONFIG_PATH}/docker-compose.yml" --env-file "${CONFIG_PATH}/.env" up -d
Show_Result
}
function Create_Napcat_CMD() {
mkdir -vp /usr/local/bin
cat <<EOF > "/usr/local/bin/${CONTAINER_NAME}"
if [ -n "\$1" ]; then
case "\$1" in
status)
docker ps -a | grep "$CONTAINER_NAME"
;;
start)
docker-compose -f "${CONFIG_PATH}/docker-compose.yml" --env-file "${CONFIG_PATH}/.env" up -d
;;
stop)
docker-compose -f "${CONFIG_PATH}/docker-compose.yml" stop
;;
restart)
docker-compose -f "${CONFIG_PATH}/docker-compose.yml" restart
;;
update)
docker-compose -f "${CONFIG_PATH}/docker-compose.yml" stop
docker-compose -f "${CONFIG_PATH}/docker-compose.yml" pull
docker-compose -f "${CONFIG_PATH}/docker-compose.yml" up -d
;;
rebuild)
echo "咕咕咕"
;;
uninstall)
docker-compose -f "${CONFIG_PATH}/docker-compose.yml" down
;;
info)
cat "${CONFIG_PATH}/info.log"
;;
log)
docker logs -fn"\${2:-100}" "$CONTAINER_NAME"
;;
help)
echo "Usage: ${CONTAINER_NAME} {status|start|stop|restart|log|update|help}"
echo " status 查看 ${CONTAINER_NAME} 容器运行状态"
echo " start 启动 ${CONTAINER_NAME} 容器"
echo " stop 停止 ${CONTAINER_NAME} 容器"
echo " restart 重启 ${CONTAINER_NAME} 容器"
echo " update 更新 ${CONTAINER_NAME} 容器"
echo " rebuild 重建 ${CONTAINER_NAME} 容器"
echo " uninstall 卸载 ${CONTAINER_NAME} 容器"
echo " info 获取 ${CONTAINER_NAME} 容器信息"
echo " log 行数 查看 ${CONTAINER_NAME} 容器最后多少条(默认100)日志"
;;
*)
echo "不支持的参数,请使用 ${CONTAINER_NAME} help 获取帮助"
;;
esac
else
echo "请使用 ${CONTAINER_NAME} help 获取帮助"
fi
EOF
cp -f "/usr/local/bin/${CONTAINER_NAME}" "${CONFIG_PATH}/${CONTAINER_NAME}"
chmod 755 "/usr/local/bin/${CONTAINER_NAME}"
}
# function Reinstall_Napcat() {
# docker-compose -f "${CONFIG_PATH}/docker-compose.yml" stop
# Confirm_Napcat
# }
# function Stop_Napcat() {
# docker-compose -f "${CONFIG_PATH}/docker-compose.yml" stop
# log "停止成功"
# }
# function Restart_Napcat() {
# docker-compose -f "${CONFIG_PATH}/docker-compose.yml" restart
# log "重启成功"
# }
# function Update_Napcat() {
# docker-compose -f "${CONFIG_PATH}/docker-compose.yml" stop
# docker-compose -f "${CONFIG_PATH}/docker-compose.yml" pull
# docker-compose -f "${CONFIG_PATH}/docker-compose.yml" up -d
# log "更新成功"
# }
function Show_Result() {
log ""
log "=================感谢您的耐心等待,安装已经完成=================="
log ""
log "Napcat 容器已启动,容器名称: $CONTAINER_NAME"
log ""
log "请用浏览器访问WEBUI:"
log "外网地址: http://$PUBLIC_IPV4:$WEBUI_PORT/webui"
log "外网地址: http://$PUBLIC_IPV6:$WEBUI_PORT/webui"
log "内网地址: http://127.0.0.1:$WEBUI_PORT/webui"
log "内网地址: http://$LOCAL_IP:$WEBUI_PORT/webui"
log "访问密钥: $WEBUI_TOKEN"
log ""
log "项目官网: https://napneko.github.io/zh-CN"
log "项目文档: https://napneko.github.io/zh-CN/guide/getting-started"
log "代码仓库: https://github.com/NapNeko/NapCatQQ"
log ""
log "如果使用的是云服务器,请至安全组开放 $WEBUI_PORT 端口"
log ""
log "请使用 ${CONTAINER_NAME} help 获取帮助"
log ""
log "================================================================"
cat <<EOF > "${CONFIG_PATH}/info.log"
请用浏览器访问WEBUI:
外网地址: http://$PUBLIC_IPV4:$WEBUI_PORT/webui
外网地址: http://$PUBLIC_IPV6:$WEBUI_PORT/webui
内网地址: http://127.0.0.1:$WEBUI_PORT/webui
内网地址: http://$LOCAL_IP:$WEBUI_PORT/webui
访问密钥: $WEBUI_TOKEN
EOF
}
function Main() {
Check_System
Check_Root
log "(1)安装NapCat"
# log "(2)升级NapCat"
# log "(3)重启NapCat"
# log "(4)停止NapCat"
# log "(5)重建NapCat(咕咕咕)"
log "(6)更换系统软件源"
log "(7)安装升级docker"
log "(8)重新查看安装信息"
log "(9)退出脚本"
while true; do
read -p "请输入数字选择您需要进行的操作:" mainchoice
case "$mainchoice" in
1)
Check_Docker
Check_Docker-Compose
Set_Container_Name
Set_QQ_Account
Set_Config_Path
Set_Bot_Path
Set_Webui_Host
Set_Webui_Port
Set_Webui_Token
Set_Webui_Login_Rate
Create_Webui_Config
Set_Mac_Address
Set_Napcat_UID
Set_Napcat_GID
Get_Ip
clear
Confirm_Napcat
break
;;
2)
Check_Docker-Compose_File
Update_Napcat
continue
;;
3)
Check_Docker-Compose_File
Restart_Napcat
continue
;;
4)
Check_Docker-Compose_File
Stop_Napcat
continue
;;
5)
Check_Docker-Compose_File
Reinstall_Napcat
continue
;;
6)
Change_Repo
continue
;;
7)
Check_Docker
continue
;;
8)
Show_Result
break
;;
9)
log "欢迎您的使用"
break
;;
*)
log "错误的选项,请重新输入。"
continue
;;
esac
done
}
while [[ $# -gt 0 ]]; do
case $1 in
--install)
Check_Docker
Check_Docker-Compose
Set_Container_Name
Set_QQ_Account
Set_Config_Path
Set_Bot_Path
Set_Webui_Host
Set_Webui_Port
Set_Webui_Token
Set_Webui_Login_Rate
Create_Webui_Config
Set_Mac_Address
Set_Napcat_UID
Set_Napcat_GID
Get_Ip
clear
Confirm_Napcat
exit 0
;;
*)
echo "错误,请使用--install"
exit 1
;;
esac
done