-
Notifications
You must be signed in to change notification settings - Fork 319
/
kejilion.sh
9374 lines (7896 loc) · 271 KB
/
kejilion.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
sh_v="3.4.3"
gl_hui='\e[37m'
gl_hong='\033[31m'
gl_lv='\033[32m'
gl_huang='\033[33m'
gl_lan='\033[34m'
gl_bai='\033[0m'
gl_zi='\033[35m'
gl_kjlan='\033[96m'
canshu="default"
permission_granted="false"
ENABLE_STATS="true"
quanju_canshu() {
if [ "$canshu" = "CN" ]; then
zhushi=0
gh_proxy="https://gh.kejilion.pro/"
elif [ "$canshu" = "V6" ]; then
zhushi=1
gh_proxy="https://gh.kejilion.pro/"
else
zhushi=1 # 0 表示执行,1 表示不执行
gh_proxy=""
fi
}
quanju_canshu
# 定义一个函数来执行命令
run_command() {
if [ "$zhushi" -eq 0 ]; then
"$@"
fi
}
canshu_v6() {
if grep -q '^canshu="V6"' /usr/local/bin/k > /dev/null 2>&1; then
sed -i 's/^canshu="default"/canshu="V6"/' ~/kejilion.sh
sed -i 's/^canshu="default"/canshu="V6"/' /usr/local/bin/k
fi
}
CheckFirstRun_true() {
if grep -q '^permission_granted="true"' /usr/local/bin/k > /dev/null 2>&1; then
sed -i 's/^permission_granted="false"/permission_granted="true"/' ~/kejilion.sh
sed -i 's/^permission_granted="false"/permission_granted="true"/' /usr/local/bin/k
fi
}
# 收集功能埋点信息的函数,记录当前脚本版本号,使用时间,系统版本,CPU架构,机器所在国家和用户使用的功能名称,绝对不涉及任何敏感信息,请放心!请相信我!
# 为什么要设计这个功能,目的更好的了解用户喜欢使用的功能,进一步优化功能推出更多符合用户需求的功能。
# 全文可搜搜 send_stats 函数调用位置,透明开源,如有顾虑可拒绝使用。
send_stats() {
if [ "$ENABLE_STATS" == "false" ]; then
return
fi
local country=$(curl -s ipinfo.io/country)
local os_info=$(grep PRETTY_NAME /etc/os-release | cut -d '=' -f2 | tr -d '"')
local cpu_arch=$(uname -m)
curl -s -X POST "https://api.kejilion.pro/api/log" \
-H "Content-Type: application/json" \
-d "{\"action\":\"$1\",\"timestamp\":\"$(date -u '+%Y-%m-%d %H:%M:%S')\",\"country\":\"$country\",\"os_info\":\"$os_info\",\"cpu_arch\":\"$cpu_arch\",\"version\":\"$sh_v\"}" &>/dev/null &
}
yinsiyuanquan2() {
if grep -q '^ENABLE_STATS="false"' /usr/local/bin/k > /dev/null 2>&1; then
sed -i 's/^ENABLE_STATS="true"/ENABLE_STATS="false"/' ~/kejilion.sh
sed -i 's/^ENABLE_STATS="true"/ENABLE_STATS="false"/' /usr/local/bin/k
fi
}
canshu_v6
CheckFirstRun_true
yinsiyuanquan2
cp -f ./kejilion.sh ~/kejilion.sh > /dev/null 2>&1
cp -f ~/kejilion.sh /usr/local/bin/k > /dev/null 2>&1
CheckFirstRun_false() {
if grep -q '^permission_granted="false"' /usr/local/bin/k > /dev/null 2>&1; then
UserLicenseAgreement
fi
}
# 提示用户同意条款
UserLicenseAgreement() {
clear
echo -e "${gl_kjlan}欢迎使用科技lion脚本工具箱${gl_bai}"
echo "首次使用脚本,请先阅读并同意用户许可协议。"
echo "用户许可协议: https://blog.kejilion.pro/user-license-agreement/"
echo -e "----------------------"
read -r -p "是否同意以上条款?(y/n): " user_input
if [ "$user_input" = "y" ] || [ "$user_input" = "Y" ]; then
send_stats "许可同意"
sed -i 's/^permission_granted="false"/permission_granted="true"/' ~/kejilion.sh
sed -i 's/^permission_granted="false"/permission_granted="true"/' /usr/local/bin/k
else
send_stats "许可拒绝"
clear
exit
fi
}
CheckFirstRun_false
ip_address() {
ipv4_address=$(curl -s ipv4.ip.sb)
ipv6_address=$(curl -s --max-time 1 ipv6.ip.sb)
}
install() {
if [ $# -eq 0 ]; then
echo "未提供软件包参数!"
return
fi
for package in "$@"; do
if ! command -v "$package" &>/dev/null; then
echo -e "${gl_huang}正在安装 $package...${gl_bai}"
if command -v dnf &>/dev/null; then
dnf -y update
dnf install -y epel-release
dnf install -y "$package"
elif command -v yum &>/dev/null; then
yum -y update
yum install -y epel-release
yum -y install "$package"
elif command -v apt &>/dev/null; then
apt update -y
apt install -y "$package"
elif command -v apk &>/dev/null; then
apk update
apk add "$package"
elif command -v pacman &>/dev/null; then
pacman -Syu --noconfirm
pacman -S --noconfirm "$package"
elif command -v zypper &>/dev/null; then
zypper refresh
zypper install -y "$package"
elif command -v opkg &>/dev/null; then
opkg update
opkg install "$package"
else
echo "未知的包管理器!"
return
fi
else
echo -e "${gl_lv}$package 已经安装${gl_bai}"
fi
done
return
}
install_dependency() {
install wget socat unzip tar
}
remove() {
if [ $# -eq 0 ]; then
echo "未提供软件包参数!"
return
fi
for package in "$@"; do
echo -e "${gl_huang}正在卸载 $package...${gl_bai}"
if command -v dnf &>/dev/null; then
dnf remove -y "${package}"*
elif command -v yum &>/dev/null; then
yum remove -y "${package}"*
elif command -v apt &>/dev/null; then
apt purge -y "${package}"*
elif command -v apk &>/dev/null; then
apk del "${package}*"
elif command -v pacman &>/dev/null; then
pacman -Rns --noconfirm "${package}"
elif command -v zypper &>/dev/null; then
zypper remove -y "${package}"
elif command -v opkg &>/dev/null; then
opkg remove "${package}"
else
echo "未知的包管理器!"
return
fi
done
return
}
# 通用 systemctl 函数,适用于各种发行版
systemctl() {
local COMMAND="$1"
local SERVICE_NAME="$2"
if command -v apk &>/dev/null; then
service "$SERVICE_NAME" "$COMMAND"
else
/bin/systemctl "$COMMAND" "$SERVICE_NAME"
fi
}
# 重启服务
restart() {
systemctl restart "$1"
if [ $? -eq 0 ]; then
echo "$1 服务已重启。"
else
echo "错误:重启 $1 服务失败。"
fi
}
# 启动服务
start() {
systemctl start "$1"
if [ $? -eq 0 ]; then
echo "$1 服务已启动。"
else
echo "错误:启动 $1 服务失败。"
fi
}
# 停止服务
stop() {
systemctl stop "$1"
if [ $? -eq 0 ]; then
echo "$1 服务已停止。"
else
echo "错误:停止 $1 服务失败。"
fi
}
# 查看服务状态
status() {
systemctl status "$1"
if [ $? -eq 0 ]; then
echo "$1 服务状态已显示。"
else
echo "错误:无法显示 $1 服务状态。"
fi
}
enable() {
local SERVICE_NAME="$1"
if command -v apk &>/dev/null; then
rc-update add "$SERVICE_NAME" default
else
/bin/systemctl enable "$SERVICE_NAME"
fi
echo "$SERVICE_NAME 已设置为开机自启。"
}
break_end() {
echo -e "${gl_lv}操作完成${gl_bai}"
echo "按任意键继续..."
read -n 1 -s -r -p ""
echo ""
clear
}
kejilion() {
cd ~
kejilion_sh
}
check_port() {
install lsof
local containers=$(docker ps --filter "publish=443" --format "{{.ID}} " 2>/dev/null)
if [ -n "$containers" ] ; then
docker stop $containers
else
for pid in $(lsof -t -i:443); do
kill -9 $pid
done
fi
}
install_add_docker_cn() {
local country=$(curl -s ipinfo.io/country)
if [ "$country" = "CN" ]; then
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://docker.kejilion.pro"]
}
EOF
fi
enable docker
start docker
restart docker
}
install_add_docker_guanfang() {
local country=$(curl -s ipinfo.io/country)
if [ "$country" = "CN" ]; then
cd ~
curl -sS -O ${gh_proxy}https://raw.githubusercontent.com/kejilion/docker/main/install && chmod +x install
sh install --mirror Aliyun
rm -f install
else
curl -fsSL https://get.docker.com | sh
fi
install_add_docker_cn
}
install_add_docker() {
echo -e "${gl_huang}正在安装docker环境...${gl_bai}"
if [ -f /etc/os-release ] && grep -q "Fedora" /etc/os-release; then
install_add_docker_guanfang
elif command -v dnf &>/dev/null; then
dnf update -y
dnf install -y yum-utils device-mapper-persistent-data lvm2
rm -f /etc/yum.repos.d/docker*.repo > /dev/null
country=$(curl -s ipinfo.io/country)
arch=$(uname -m)
if [ "$country" = "CN" ]; then
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo | tee /etc/yum.repos.d/docker-ce.repo > /dev/null
else
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo > /dev/null
fi
dnf install -y docker-ce docker-ce-cli containerd.io
install_add_docker_cn
elif [ -f /etc/os-release ] && grep -q "Kali" /etc/os-release; then
apt update
apt upgrade -y
apt install -y apt-transport-https ca-certificates curl gnupg lsb-release
rm -f /usr/share/keyrings/docker-archive-keyring.gpg
local country=$(curl -s ipinfo.io/country)
local arch=$(uname -m)
if [ "$country" = "CN" ]; then
if [ "$arch" = "x86_64" ]; then
sed -i '/^deb \[arch=amd64 signed-by=\/etc\/apt\/keyrings\/docker-archive-keyring.gpg\] https:\/\/mirrors.aliyun.com\/docker-ce\/linux\/debian bullseye stable/d' /etc/apt/sources.list.d/docker.list > /dev/null
mkdir -p /etc/apt/keyrings
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker-archive-keyring.gpg > /dev/null
echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/debian bullseye stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
elif [ "$arch" = "aarch64" ]; then
sed -i '/^deb \[arch=arm64 signed-by=\/etc\/apt\/keyrings\/docker-archive-keyring.gpg\] https:\/\/mirrors.aliyun.com\/docker-ce\/linux\/debian bullseye stable/d' /etc/apt/sources.list.d/docker.list > /dev/null
mkdir -p /etc/apt/keyrings
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker-archive-keyring.gpg > /dev/null
echo "deb [arch=arm64 signed-by=/etc/apt/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/debian bullseye stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
fi
else
if [ "$arch" = "x86_64" ]; then
sed -i '/^deb \[arch=amd64 signed-by=\/usr\/share\/keyrings\/docker-archive-keyring.gpg\] https:\/\/download.docker.com\/linux\/debian bullseye stable/d' /etc/apt/sources.list.d/docker.list > /dev/null
mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker-archive-keyring.gpg > /dev/null
echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian bullseye stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
elif [ "$arch" = "aarch64" ]; then
sed -i '/^deb \[arch=arm64 signed-by=\/usr\/share\/keyrings\/docker-archive-keyring.gpg\] https:\/\/download.docker.com\/linux\/debian bullseye stable/d' /etc/apt/sources.list.d/docker.list > /dev/null
mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker-archive-keyring.gpg > /dev/null
echo "deb [arch=arm64 signed-by=/etc/apt/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian bullseye stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
fi
fi
apt update
apt install -y docker-ce docker-ce-cli containerd.io
install_add_docker_cn
elif command -v apt &>/dev/null || command -v yum &>/dev/null; then
install_add_docker_guanfang
else
install docker docker-compose
install_add_docker_cn
fi
sleep 2
}
install_docker() {
if ! command -v docker &>/dev/null; then
install_add_docker
else
echo -e "${gl_lv}Docker环境已经安装${gl_bai}"
fi
}
docker_ps() {
while true; do
clear
send_stats "Docker容器管理"
echo "Docker容器列表"
docker ps -a
echo ""
echo "容器操作"
echo "------------------------"
echo "1. 创建新的容器"
echo "------------------------"
echo "2. 启动指定容器 6. 启动所有容器"
echo "3. 停止指定容器 7. 停止所有容器"
echo "4. 删除指定容器 8. 删除所有容器"
echo "5. 重启指定容器 9. 重启所有容器"
echo "------------------------"
echo "11. 进入指定容器 12. 查看容器日志"
echo "13. 查看容器网络 14. 查看容器占用"
echo "------------------------"
echo "0. 返回上一级选单"
echo "------------------------"
read -e -p "请输入你的选择: " sub_choice
case $sub_choice in
1)
send_stats "新建容器"
read -e -p "请输入创建命令: " dockername
$dockername
;;
2)
send_stats "启动指定容器"
read -e -p "请输入容器名(多个容器名请用空格分隔): " dockername
docker start $dockername
;;
3)
send_stats "停止指定容器"
read -e -p "请输入容器名(多个容器名请用空格分隔): " dockername
docker stop $dockername
;;
4)
send_stats "删除指定容器"
read -e -p "请输入容器名(多个容器名请用空格分隔): " dockername
docker rm -f $dockername
;;
5)
send_stats "重启指定容器"
read -e -p "请输入容器名(多个容器名请用空格分隔): " dockername
docker restart $dockername
;;
6)
send_stats "启动所有容器"
docker start $(docker ps -a -q)
;;
7)
send_stats "停止所有容器"
docker stop $(docker ps -q)
;;
8)
send_stats "删除所有容器"
read -e -p "$(echo -e "${gl_hong}注意: ${gl_bai}确定删除所有容器吗?(Y/N): ")" choice
case "$choice" in
[Yy])
docker rm -f $(docker ps -a -q)
;;
[Nn])
;;
*)
echo "无效的选择,请输入 Y 或 N。"
;;
esac
;;
9)
send_stats "重启所有容器"
docker restart $(docker ps -q)
;;
11)
send_stats "进入容器"
read -e -p "请输入容器名: " dockername
docker exec -it $dockername /bin/sh
break_end
;;
12)
send_stats "查看容器日志"
read -e -p "请输入容器名: " dockername
docker logs $dockername
break_end
;;
13)
send_stats "查看容器网络"
echo ""
container_ids=$(docker ps -q)
echo "------------------------------------------------------------"
printf "%-25s %-25s %-25s\n" "容器名称" "网络名称" "IP地址"
for container_id in $container_ids; do
local container_info=$(docker inspect --format '{{ .Name }}{{ range $network, $config := .NetworkSettings.Networks }} {{ $network }} {{ $config.IPAddress }}{{ end }}' "$container_id")
local container_name=$(echo "$container_info" | awk '{print $1}')
local network_info=$(echo "$container_info" | cut -d' ' -f2-)
while IFS= read -r line; do
local network_name=$(echo "$line" | awk '{print $1}')
local ip_address=$(echo "$line" | awk '{print $2}')
printf "%-20s %-20s %-15s\n" "$container_name" "$network_name" "$ip_address"
done <<< "$network_info"
done
break_end
;;
14)
send_stats "查看容器占用"
docker stats --no-stream
break_end
;;
0)
break # 跳出循环,退出菜单
;;
*)
break # 跳出循环,退出菜单
;;
esac
done
}
docker_image() {
while true; do
clear
send_stats "Docker镜像管理"
echo "Docker镜像列表"
docker image ls
echo ""
echo "镜像操作"
echo "------------------------"
echo "1. 获取指定镜像 3. 删除指定镜像"
echo "2. 更新指定镜像 4. 删除所有镜像"
echo "------------------------"
echo "0. 返回上一级选单"
echo "------------------------"
read -e -p "请输入你的选择: " sub_choice
case $sub_choice in
1)
send_stats "拉取镜像"
read -e -p "请输入镜像名(多个镜像名请用空格分隔): " imagenames
for name in $imagenames; do
echo -e "${gl_huang}正在获取镜像: $name${gl_bai}"
docker pull $name
done
;;
2)
send_stats "更新镜像"
read -e -p "请输入镜像名(多个镜像名请用空格分隔): " imagenames
for name in $imagenames; do
echo -e "${gl_huang}正在更新镜像: $name${gl_bai}"
docker pull $name
done
;;
3)
send_stats "删除镜像"
read -e -p "请输入镜像名(多个镜像名请用空格分隔): " imagenames
for name in $imagenames; do
docker rmi -f $name
done
;;
4)
send_stats "删除所有镜像"
read -e -p "$(echo -e "${gl_hong}注意: ${gl_bai}确定删除所有镜像吗?(Y/N): ")" choice
case "$choice" in
[Yy])
docker rmi -f $(docker images -q)
;;
[Nn])
;;
*)
echo "无效的选择,请输入 Y 或 N。"
;;
esac
;;
0)
break # 跳出循环,退出菜单
;;
*)
break # 跳出循环,退出菜单
;;
esac
done
}
check_crontab_installed() {
if command -v crontab >/dev/null 2>&1; then
echo -e "${gl_lv}crontab 已经安装${gl_bai}"
return
else
install_crontab
return
fi
}
install_crontab() {
if [ -f /etc/os-release ]; then
. /etc/os-release
case "$ID" in
ubuntu|debian|kali)
apt update
apt install -y cron
systemctl enable cron
systemctl start cron
;;
centos|rhel|almalinux|rocky|fedora)
yum install -y cronie
systemctl enable crond
systemctl start crond
;;
alpine)
apk add --no-cache cronie
rc-update add crond
rc-service crond start
;;
arch|manjaro)
pacman -S --noconfirm cronie
systemctl enable cronie
systemctl start cronie
;;
opensuse|suse|opensuse-tumbleweed)
zypper install -y cron
systemctl enable cron
systemctl start cron
;;
openwrt|lede)
opkg update
opkg install cron
/etc/init.d/cron enable
/etc/init.d/cron start
;;
*)
echo "不支持的发行版: $ID"
return
;;
esac
else
echo "无法确定操作系统。"
return
fi
echo -e "${gl_lv}crontab 已安装且 cron 服务正在运行。${gl_bai}"
}
docker_ipv6_on() {
root_use
install jq
local CONFIG_FILE="/etc/docker/daemon.json"
local REQUIRED_IPV6_CONFIG='{"ipv6": true, "fixed-cidr-v6": "2001:db8:1::/64"}'
# 检查配置文件是否存在,如果不存在则创建文件并写入默认设置
if [ ! -f "$CONFIG_FILE" ]; then
echo "$REQUIRED_IPV6_CONFIG" | jq . > "$CONFIG_FILE"
restart docker
else
# 使用jq处理配置文件的更新
local ORIGINAL_CONFIG=$(<"$CONFIG_FILE")
# 检查当前配置是否已经有 ipv6 设置
local CURRENT_IPV6=$(echo "$ORIGINAL_CONFIG" | jq '.ipv6 // false')
# 更新配置,开启 IPv6
if [[ "$CURRENT_IPV6" == "false" ]]; then
UPDATED_CONFIG=$(echo "$ORIGINAL_CONFIG" | jq '. + {ipv6: true, "fixed-cidr-v6": "2001:db8:1::/64"}')
else
UPDATED_CONFIG=$(echo "$ORIGINAL_CONFIG" | jq '. + {"fixed-cidr-v6": "2001:db8:1::/64"}')
fi
# 对比原始配置与新配置
if [[ "$ORIGINAL_CONFIG" == "$UPDATED_CONFIG" ]]; then
echo -e "${gl_huang}当前已开启ipv6访问${gl_bai}"
else
echo "$UPDATED_CONFIG" | jq . > "$CONFIG_FILE"
restart docker
fi
fi
}
docker_ipv6_off() {
root_use
install jq
local CONFIG_FILE="/etc/docker/daemon.json"
# 检查配置文件是否存在
if [ ! -f "$CONFIG_FILE" ]; then
echo -e "${gl_hong}配置文件不存在${gl_bai}"
return
fi
# 读取当前配置
local ORIGINAL_CONFIG=$(<"$CONFIG_FILE")
# 使用jq处理配置文件的更新
local UPDATED_CONFIG=$(echo "$ORIGINAL_CONFIG" | jq 'del(.["fixed-cidr-v6"]) | .ipv6 = false')
# 检查当前的 ipv6 状态
local CURRENT_IPV6=$(echo "$ORIGINAL_CONFIG" | jq -r '.ipv6 // false')
# 对比原始配置与新配置
if [[ "$CURRENT_IPV6" == "false" ]]; then
echo -e "${gl_huang}当前已关闭ipv6访问${gl_bai}"
else
echo "$UPDATED_CONFIG" | jq . > "$CONFIG_FILE"
restart docker
echo -e "${gl_huang}已成功关闭ipv6访问${gl_bai}"
fi
}
iptables_open() {
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
ip6tables -P INPUT ACCEPT
ip6tables -P FORWARD ACCEPT
ip6tables -P OUTPUT ACCEPT
ip6tables -F
}
add_swap() {
local new_swap=$1 # 获取传入的参数
# 获取当前系统中所有的 swap 分区
local swap_partitions=$(grep -E '^/dev/' /proc/swaps | awk '{print $1}')
# 遍历并删除所有的 swap 分区
for partition in $swap_partitions; do
swapoff "$partition"
wipefs -a "$partition"
mkswap -f "$partition"
done
# 确保 /swapfile 不再被使用
swapoff /swapfile
# 删除旧的 /swapfile
rm -f /swapfile
# 创建新的 swap 分区
fallocate -l ${new_swap}M /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
if [ -f /etc/alpine-release ]; then
# 删除已有的 swap 条目
sed -i '/\/swapfile/d' /etc/fstab
echo "/swapfile swap swap defaults 0 0" >> /etc/fstab
# 确保在 local.d 中启动 swap
echo "nohup swapon /swapfile" > /etc/local.d/swap.start
chmod +x /etc/local.d/swap.start
rc-update add local
else
# 删除已有的 swap 条目
sed -i '/\/swapfile/d' /etc/fstab
echo "/swapfile swap swap defaults 0 0" >> /etc/fstab
fi
echo -e "虚拟内存大小已调整为${gl_huang}${new_swap}${gl_bai}MB"
}
check_swap() {
local swap_total=$(free -m | awk 'NR==3{print $2}')
# 判断是否需要创建虚拟内存
[ "$swap_total" -gt 0 ] || add_swap 1024
}
ldnmp_v() {
# 获取nginx版本
local nginx_version=$(docker exec nginx nginx -v 2>&1)
local nginx_version=$(echo "$nginx_version" | grep -oP "nginx/\K[0-9]+\.[0-9]+\.[0-9]+")
echo -n -e "nginx : ${gl_huang}v$nginx_version${gl_bai}"
# 获取mysql版本
local dbrootpasswd=$(grep -oP 'MYSQL_ROOT_PASSWORD:\s*\K.*' /home/web/docker-compose.yml | tr -d '[:space:]')
local mysql_version=$(docker exec mysql mysql -u root -p"$dbrootpasswd" -e "SELECT VERSION();" 2>/dev/null | tail -n 1)
echo -n -e " mysql : ${gl_huang}v$mysql_version${gl_bai}"
# 获取php版本
local php_version=$(docker exec php php -v 2>/dev/null | grep -oP "PHP \K[0-9]+\.[0-9]+\.[0-9]+")
echo -n -e " php : ${gl_huang}v$php_version${gl_bai}"
# 获取redis版本
local redis_version=$(docker exec redis redis-server -v 2>&1 | grep -oP "v=+\K[0-9]+\.[0-9]+")
echo -e " redis : ${gl_huang}v$redis_version${gl_bai}"
echo "------------------------"
echo ""
}
install_ldnmp_conf() {
# 创建必要的目录和文件
cd /home && mkdir -p web/html web/mysql web/certs web/conf.d web/redis web/log/nginx && touch web/docker-compose.yml
wget -O /home/web/nginx.conf ${gh_proxy}https://raw.githubusercontent.com/kejilion/nginx/main/nginx10.conf
wget -O /home/web/conf.d/default.conf ${gh_proxy}https://raw.githubusercontent.com/kejilion/nginx/main/default10.conf
default_server_ssl
# 下载 docker-compose.yml 文件并进行替换
wget -O /home/web/docker-compose.yml ${gh_proxy}https://raw.githubusercontent.com/kejilion/docker/main/LNMP-docker-compose-10.yml
dbrootpasswd=$(openssl rand -base64 16) ; dbuse=$(openssl rand -hex 4) ; dbusepasswd=$(openssl rand -base64 8)
# 在 docker-compose.yml 文件中进行替换
sed -i "s#webroot#$dbrootpasswd#g" /home/web/docker-compose.yml
sed -i "s#kejilionYYDS#$dbusepasswd#g" /home/web/docker-compose.yml
sed -i "s#kejilion#$dbuse#g" /home/web/docker-compose.yml
}
install_ldnmp() {
check_swap
if ! grep -q "kjlion/php:fpm-alpine" /home/web/docker-compose.yml; then
sed -i -e 's|php:fpm-alpine|kjlion/php:fpm-alpine|g' \
-e 's|php:7.4-fpm-alpine|kjlion/php:7.4-fpm-alpine|g' /home/web/docker-compose.yml
fi
cd /home/web && docker compose up -d
restart_ldnmp
for i in {20..1}; do
echo -ne " 环境正在启动,倒计时${gl_lv}$i${gl_bai}秒...\r"
sleep 1
done
echo -e "\n"
clear
echo "LDNMP环境安装完毕"
echo "------------------------"
ldnmp_v
}
install_certbot() {
cd ~
curl -sS -O ${gh_proxy}https://raw.githubusercontent.com/kejilion/sh/main/auto_cert_renewal.sh
chmod +x auto_cert_renewal.sh
check_crontab_installed
local cron_job="0 0 * * * ~/auto_cert_renewal.sh"
local existing_cron=$(crontab -l 2>/dev/null | grep -F "$cron_job")
if [ -z "$existing_cron" ]; then
(crontab -l 2>/dev/null; echo "$cron_job") | crontab -
echo "续签任务已添加"
fi
}
install_ssltls() {
docker stop nginx > /dev/null 2>&1
iptables_open > /dev/null 2>&1
check_port > /dev/null 2>&1
cd ~
local file_path="/etc/letsencrypt/live/$yuming/fullchain.pem"
if [ ! -f "$file_path" ]; then
local ipv4_pattern='^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'
local ipv6_pattern='^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|(2[0-4][0-9]|[01]?[0-9][0-9]?))|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|(2[0-4][0-9]|[01]?[0-9][0-9]?))|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|(2[0-4][0-9]|[01]?[0-9][0-9]?))|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|(2[0-4][0-9]|[01]?[0-9][0-9]?))))$'
if [[ ($yuming =~ $ipv4_pattern || $yuming =~ $ipv6_pattern) ]]; then
mkdir -p /etc/letsencrypt/live/$yuming/
if command -v dnf &>/dev/null || command -v yum &>/dev/null; then
openssl req -x509 -nodes -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -keyout /etc/letsencrypt/live/$yuming/privkey.pem -out /etc/letsencrypt/live/$yuming/fullchain.pem -days 5475 -subj "/C=US/ST=State/L=City/O=Organization/OU=Organizational Unit/CN=Common Name"
else
openssl genpkey -algorithm Ed25519 -out /etc/letsencrypt/live/$yuming/privkey.pem
openssl req -x509 -key /etc/letsencrypt/live/$yuming/privkey.pem -out /etc/letsencrypt/live/$yuming/fullchain.pem -days 5475 -subj "/C=US/ST=State/L=City/O=Organization/OU=Organizational Unit/CN=Common Name"
fi
else
docker run -it --rm -p 80:80 -v /etc/letsencrypt/:/etc/letsencrypt certbot/certbot certonly --standalone -d "$yuming" --email [email protected] --agree-tos --no-eff-email --force-renewal --key-type ecdsa
fi
fi
cp /etc/letsencrypt/live/$yuming/fullchain.pem /home/web/certs/${yuming}_cert.pem > /dev/null 2>&1
cp /etc/letsencrypt/live/$yuming/privkey.pem /home/web/certs/${yuming}_key.pem > /dev/null 2>&1
docker start nginx > /dev/null 2>&1
}
install_ssltls_text() {
echo -e "${gl_huang}$yuming 公钥信息${gl_bai}"
cat /etc/letsencrypt/live/$yuming/fullchain.pem
echo ""
echo -e "${gl_huang}$yuming 私钥信息${gl_bai}"
cat /etc/letsencrypt/live/$yuming/privkey.pem
echo ""
echo -e "${gl_huang}证书存放路径${gl_bai}"
echo "公钥: /etc/letsencrypt/live/$yuming/fullchain.pem"
echo "私钥: /etc/letsencrypt/live/$yuming/privkey.pem"
echo ""
}
add_ssl() {
yuming="${1:-}"
if [ -z "$yuming" ]; then
add_yuming
fi
install_docker
install_certbot
docker run -it --rm -v /etc/letsencrypt/:/etc/letsencrypt certbot/certbot delete --cert-name "$yuming" -n
install_ssltls
certs_status
install_ssltls_text
ssl_ps
}
ssl_ps() {
echo -e "${gl_huang}已申请的证书到期情况${gl_bai}"
echo "站点信息 证书到期时间"
echo "------------------------"
for cert_dir in /etc/letsencrypt/live/*; do