-
Notifications
You must be signed in to change notification settings - Fork 0
/
onekey.sh
1321 lines (1257 loc) · 36.4 KB
/
onekey.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
#####################################################
# ssfun's Linux Onekey Tool
# Author: ssfun
# Date: 2023-11-1
# Version: 3.0.0
#####################################################
#Basic definitions
plain='\033[0m'
red='\033[0;31m'
blue='\033[1;34m'
pink='\033[1;35m'
green='\033[0;32m'
yellow='\033[0;33m'
#os arch evn
OS=''
ARCH=''
#caddy env
CADDY_VERSION=''
CADDY_CONFIG_PATH='/usr/local/etc/caddy'
CADDY_LOG_PATH='/var/log/caddy'
CADDY_TLS_PATH='/home/tls'
CADDY_WWW_PATH='/var/www'
CADDY_404_PATH='/var/www/404'
CADDY_BINARY='/usr/local/bin/caddy'
CADDY_SERVICE='/etc/systemd/system/caddy.service'
#caddy status define
declare -r CADDY_STATUS_RUNNING=1
declare -r CADDY_STATUS_NOT_RUNNING=0
declare -r CADDY_STATUS_NOT_INSTALL=255
#sing-box env
SING_BOX_VERSION=''
SING_BOX_CONFIG_PATH='/usr/local/etc/sing-box'
SING_BOX_LOG_PATH='/var/log/sing-box'
SING_BOX_LIB_PATH='/var/lib/sing-box'
SING_BOX_BINARY='/usr/local/bin/sing-box'
SING_BOX_SERVICE='/etc/systemd/system/sing-box.service'
#sing-box status define
declare -r SING_BOX_STATUS_RUNNING=1
declare -r SING_BOX_STATUS_NOT_RUNNING=0
declare -r SING_BOX_STATUS_NOT_INSTALL=255
#filebrowser env
FILEBROWSER_VERSION=''
FILEBROWSER_CONFIG_PATH='/usr/local/etc/filebrowser'
FILEBROWSER_LOG_PATH='/var/log/filebrowser'
FILEBROWSER_DATA_PATH='/home/filebrowser'
FILEBROWSER_DATABASE_PATH='/opt/filebrowser'
FILEBROWSER_BINARY='/usr/local/bin/filebrowser'
FILEBROWSER_SERVICE='/etc/systemd/system/filebrowser.service'
#filebrowser status define
declare -r FILEBROWSER_STATUS_RUNNING=1
declare -r FILEBROWSER_STATUS_NOT_RUNNING=0
declare -r FILEBROWSER_STATUS_NOT_INSTALL=255
#plex env
PLEX_LIBRARY_PATH='/var/lib/plexmediaserver'
PLEX_SERVICE='/lib/systemd/system/plexmediaserver.service'
#plex status define
declare -r PLEX_STATUS_RUNNING=1
declare -r PLEX_STATUS_NOT_RUNNING=0
declare -r PLEX_STATUS_NOT_INSTALL=255
#utils
function LOGE() {
echo -e "${red}[ERR] $* ${plain}"
}
function LOGI() {
echo -e "${green}[INF] $* ${plain}"
}
function LOGD() {
echo -e "${yellow}[DEG] $* ${plain}"
}
confirm() {
if [[ $# > 1 ]]; then
echo && read -p "$1 [默认$2]: " temp
if [[ x"${temp}" == x"" ]]; then
temp=$2
fi
else
read -p "$1 [y/n]: " temp
fi
if [[ x"${temp}" == x"y" || x"${temp}" == x"Y" ]]; then
return 0
else
return 1
fi
}
#root user check
[[ $EUID -ne 0 ]] && LOGE "请使用root用户运行该脚本" && exit 1
#System check
os_check() {
LOGI "检测当前系统中..."
if [[ -f /etc/redhat-release ]]; then
OS="centos"
elif cat /etc/issue | grep -Eqi "debian"; then
OS="debian"
elif cat /etc/issue | grep -Eqi "ubuntu"; then
OS="ubuntu"
elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
OS="centos"
elif cat /proc/version | grep -Eqi "debian"; then
OS="debian"
elif cat /proc/version | grep -Eqi "ubuntu"; then
OS="ubuntu"
elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
OS="centos"
else
LOGE "系统检测错误,当前系统不支持!" && exit 1
fi
LOGI "系统检测完毕,当前系统为:${OS}"
}
arch_check() {
LOGI "检测当前系统架构中..."
ARCH=$(arch)
LOGI "当前系统架构为 ${ARCH}"
if [[ ${ARCH} == "x86_64" || ${ARCH} == "x64" || ${ARCH} == "amd64" ]]; then
ARCH="amd64"
elif [[ ${ARCH} == "aarch64" || ${ARCH} == "arm64" ]]; then
ARCH="arm64"
else
LOGE "检测系统架构失败,当前系统架构不支持!" && exit 1
fi
LOGI "系统架构检测完毕,当前系统架构为:${ARCH}"
}
#install some common utils
install_base() {
if [[ ${OS} == "ubuntu" || ${OS} == "debian" ]]; then
if ! dpkg -s wget tar >/dev/null 2>&1; then
apt install wget tar -y
fi
elif [[ ${OS} == "centos" ]]; then
if ! rpm -q wget tar >/dev/null 2>&1; then
yum install wget tar -y
fi
fi
}
#plex status check,-1 means didn't install,0 means failed,1 means running
plex_status_check() {
if [[ ! -f "${PLEX_SERVICE}" ]]; then
return ${PLEX_STATUS_NOT_INSTALL}
fi
local plex_status_temp=$(systemctl is-active plexmediaserver)
if [[ "${plex_status_temp}" == "active" ]]; then
return ${PLEX_STATUS_RUNNING}
else
return ${PLEX_STATUS_NOT_RUNNING}
fi
}
show_plex_status() {
plex_status_check
case $? in
0)
echo -e "[INF] plex状态: ${yellow}未运行${plain}"
show_plex_enable_status
;;
1)
echo -e "[INF] plex状态: ${green}已运行${plain}"
show_plex_enable_status
;;
255)
echo -e "[INF] plex状态: ${red}未安装${plain}"
;;
esac
}
show_plex_enable_status() {
local plex_enable_status_temp=$(systemctl is-enabled plexmediaserver)
if [[ "${plex_enable_status_temp}" == "enabled" ]]; then
echo -e "[INF] plex是否开机自启: ${green}是${plain}"
else
echo -e "[INF] plex是否开机自启: ${red}否${plain}"
fi
}
install_plex() {
LOGD "开始下载 plex..."
os_check && arch_check && install_base
# getting the latest version of plex"
LATEST_PLEX_VERSION="$(wget -qO- -t1 -T2 "https://plex.tv/api/downloads/5.json" | grep -o '"version":"[^"]*' | grep -o '[^"]*$' | head -n 1)"
PLEX_LINK="https://downloads.plex.tv/plex-media-server-new/${LATEST_PLEX_VERSION}/debian/plexmediaserver_${LATEST_PLEX_VERSION}_${ARCH}.deb"
cd `mktemp -d`
wget -nv "${PLEX_LINK}" -O plexmediaserver.deb
dpkg -i plexmediaserver.deb
LOGI "plex 已完成安装"
}
update_plex() {
LOGD "开始更新plex..."
if [[ ! -f "${PLEX_SERVICE}" ]]; then
LOGE "当前系统未安装plex,更新失败"
show_menu
fi
os_check && arch_check
install_plex
LOGI "plex 已完成升级"
}
uninstall_plex() {
LOGD "开始卸载plex..."
dpkg -r plexmediaserver
rm -rf ${PLEX_LIBRARY_PATH}
LOGI "卸载plex成功"
}
#filebrowser status check,-1 means didn't install,0 means failed,1 means running
fb_status_check() {
if [[ ! -f "${FILEBROWSER_SERVICE}" ]]; then
return ${FILEBROWSER_STATUS_NOT_INSTALL}
fi
filebrowser_status_temp=$(systemctl is-active filebrowser)
if [[ "${filebrowser_status_temp}" == "active" ]]; then
return ${FILEBROWSER_STATUS_RUNNING}
else
return ${FILEBROWSER_STATUS_NOT_RUNNING}
fi
}
show_fb_status() {
fb_status_check
case $? in
0)
echo -e "[INF] filebrowser 状态: ${yellow}未运行${plain}"
show_fb_enable_status
;;
1)
echo -e "[INF] filebrowser 状态: ${green}已运行${plain}"
show_fb_enable_status
;;
255)
echo -e "[INF] filebrowser 状态: ${red}未安装${plain}"
;;
esac
}
show_fb_enable_status() {
local fb_enable_status_temp=$(systemctl is-enabled filebrowser)
if [[ "${fb_enable_status_temp}" == "enabled" ]]; then
echo -e "[INF] filebrowser 是否开机自启: ${green}是${plain}"
else
echo -e "[INF] filebrowser 是否开机自启: ${red}否${plain}"
fi
}
download_fb() {
LOGD "开始下载 filebrowser..."
# getting the latest version of caddy & filebrowser"
LATEST_FILEBROWSER_VERSION="$(wget -qO- -t1 -T2 "https://api.github.com/repos/filebrowser/filebrowser/releases" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')"
FILEBROWSER_LINK="https://github.com/filebrowser/filebrowser/releases/download/${LATEST_FILEBROWSER_VERSION}/linux-${ARCH}-filebrowser.tar.gz"
cd `mktemp -d`
wget -nv "${FILEBROWSER_LINK}" -O filebrowser.tar.gz
tar -zxvf filebrowser.tar.gz
mv filebrowser ${FILEBROWSER_BINARY} && chmod +x ${FILEBROWSER_BINARY}
LOGI "filebrowser 下载完毕"
}
install_fb_systemd_service() {
LOGD "开始安装 filebrowser systemd 服务..."
cat <<EOF >${FILEBROWSER_SERVICE}
[Unit]
Description=filebrowser
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service
[Service]
User=root
Restart=on-failure
RestartSec=5s
ExecStart=${FILEBROWSER_BINARY} -c ${FILEBROWSER_CONFIG_PATH}/config.json
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable filebrowser
LOGD "安装 filebrowser systemd 服务成功"
}
configuration_fb_config() {
LOGD "开始配置 filebrowser 配置文件..."
# set config
cat <<EOF >${FILEBROWSER_CONFIG_PATH}/config.json
{
"address":"127.0.0.1",
"database":"${FILEBROWSER_DATABASE_PATH}/filebrowser.db",
"log":"${FILEBROWSER_LOG_PATH}/filebrowser.log",
"port":40333,
"root":"${FILEBROWSER_DATA_PATH}",
"username":"admin"
}
EOF
LOGD "filebrowser 配置文件完成"
}
install_fb() {
LOGD "开始安装 filebrowser..."
os_check && arch_check && install_base
mkdir -p "${FILEBROWSER_CONFIG_PATH}"
mkdir -p "${FILEBROWSER_LOG_PATH}"
mkdir -p "${FILEBROWSER_DATABASE_PATH}"
mkdir -p "${FILEBROWSER_DATA_PATH}"
download_fb
install_fb_systemd_service
configuration_fb_config
systemctl start filebrowser
LOGI "filebrowser 已完成安装"
}
update_fb() {
LOGD "开始更新 filebrowser..."
if [[ ! -f "${FILEBROWSER_SERVICE}" ]]; then
LOGE "当前系统未安装 filebrowser,更新失败"
show_menu
fi
os_check && arch_check && install_base
systemctl stop filebrowser
rm -f ${FILEBROWSER_BINARY}
# getting the latest version of filebrowser"
download_fb
LOGI "filebrowser 启动成功"
systemctl restart filebrowser
LOGI "filebrowser 已完成升级"
}
uninstall_fb() {
LOGD "开始卸载 filebrowser..."
systemctl stop filebrowser
systemctl disable filebrowser
rm -f ${FILEBROWSER_SERVICE}
systemctl daemon-reload
rm -f ${FILEBROWSER_BINARY}
rm -rf ${FILEBROWSER_CONFIG_PATH}
rm -rf ${FILEBROWSER_LOG_PATH}
rm -rf ${FILEBROWSER_DATABASE_PATH}
rm -rf ${FILEBROWSER_DATA_PATH}
LOGI "卸载 filebrowser 成功"
}
#sing-box status check,-1 means didn't install,0 means failed,1 means running
sing_box_status_check() {
if [[ ! -f "${SING_BOX_SERVICE}" ]]; then
return ${SING_BOX_STATUS_NOT_INSTALL}
fi
sing_box_status_temp=$(systemctl is-active sing-box)
if [[ "${sing_box_status_temp}" == "active" ]]; then
return ${SING_BOX_STATUS_RUNNING}
else
return ${SING_BOX_STATUS_NOT_RUNNING}
fi
}
#show sing-box status
show_sing_box_status() {
sing_box_status_check
case $? in
0)
echo -e "[INF] sing-box状态: ${yellow}未运行${plain}"
show_sing_box_enable_status
;;
1)
echo -e "[INF] sing-box状态: ${green}已运行${plain}"
show_sing_box_enable_status
;;
255)
echo -e "[INF] sing-box状态: ${red}未安装${plain}"
;;
esac
}
#show sing-box enable status
show_sing_box_enable_status() {
local sing_box_enable_status_temp=$(systemctl is-enabled sing-box)
if [[ "${sing_box_enable_status_temp}" == "enabled" ]]; then
echo -e "[INF] sing-box是否开机自启: ${green}是${plain}"
else
echo -e "[INF] sing-box是否开机自启: ${red}否${plain}"
fi
}
#download sing-box binary
download_sing-box() {
LOGD "开始下载 sing-box..."
# getting the latest version of sing-box"
LATEST_VERSION="$(wget -qO- -t1 -T2 "https://api.github.com/repos/SagerNet/sing-box/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')"
LATEST_NUM="$(wget -qO- -t1 -T2 "https://api.github.com/repos/SagerNet/sing-box/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/v//g;s/,//g;s/ //g')"
LINK="https://github.com/SagerNet/sing-box/releases/download/${LATEST_VERSION}/sing-box-${LATEST_NUM}-linux-${ARCH}.tar.gz"
cd `mktemp -d`
wget -nv "${LINK}" -O sing-box.tar.gz
tar -zxvf sing-box.tar.gz --strip-components=1
mv sing-box ${SING_BOX_BINARY} && chmod +x ${SING_BOX_BINARY}
LOGI "sing-box 下载完毕"
}
#install sing-box systemd service
install_sing_box_systemd_service() {
LOGD "开始安装 sing-box systemd 服务..."
cat <<EOF >${SING_BOX_SERVICE}
[Unit]
Description=sing-box service
Documentation=https://sing-box.sagernet.org
After=network.target nss-lookup.target
[Service]
WorkingDirectory=${SING_BOX_LIB_PATH}
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_SYS_PTRACE CAP_DAC_READ_SEARCH
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_SYS_PTRACE CAP_DAC_READ_SEARCH
ExecStart=${SING_BOX_BINARY} run -c ${SING_BOX_CONFIG_PATH}/config.json
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=10s
LimitNOFILE=infinity
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable sing-box
LOGD "安装 sing-box systemd 服务成功"
}
#configuration sing-box (trojan) config
configuration_sing_box_config() {
LOGD "开始配置sing-box配置文件..."
cat <<EOF >${SING_BOX_CONFIG_PATH}/config.json
{
"log": {
"level": "info",
"output": "${SING_BOX_LOG_PATH}/sing-box.log",
"timestamp": true
},
"inbounds": [
{
"type": "trojan",
"tag": "trojan-in",
"listen": "::",
"listen_port": $port,
"sniff": true,
"sniff_override_destination": false,
"users": [
{
"name": "trojan",
"password": "$pswd"
}
],
"tls": {
"enabled": true,
"server_name": "$host",
"certificate_path": "${CADDY_TLS_PATH}/certificates/acme-v02.api.letsencrypt.org-directory/$host/$host.crt",
"key_path": "${CADDY_TLS_PATH}/certificates/acme-v02.api.letsencrypt.org-directory/$host/$host.key"
},
"fallback": {
"server": "127.0.0.1",
"server_port": 80
},
"fallback_for_alpn": {
"http/1.1": {
"server": "127.0.0.1",
"server_port": 443
}
},
"transport": {
"type": "ws",
"path": "$path",
"max_early_data": 2048,
"early_data_header_name": "Sec-WebSocket-Protocol"
}
}
],
"outbounds": [
{
"type": "direct",
"tag": "direct"
}
],
"route": {
"rules": [],
"geoip": {
"path": "${SING_BOX_CONFIG_PATH}/geoip.db",
"download_url": "https://github.com/SagerNet/sing-geoip/releases/latest/download/geoip.db",
"download_detour": "direct"
},
"geosite": {
"path": "${SING_BOX_CONFIG_PATH}/geosite.db",
"download_url": "https://github.com/SagerNet/sing-geosite/releases/latest/download/geosite.db",
"download_detour": "direct"
},
"final": "direct",
"auto_detect_interface": true
}
}
EOF
LOGD "sing-box 配置文件完成"
}
#configuration sing-box (trojan + warp) config
configuration_sing_box_warp_config() {
LOGD "开始配置sing-box配置文件..."
cat <<EOF >${SING_BOX_CONFIG_PATH}/config.json
{
"log": {
"level": "info",
"output": "${SING_BOX_LOG_PATH}/sing-box.log",
"timestamp": true
},
"inbounds": [
{
"type": "mixed",
"tag": "mixed-in",
"listen": "127.0.0.1",
"listen_port": 1080,
"sniff": true,
"sniff_override_destination": false
},
{
"type": "trojan",
"tag": "trojan-in",
"listen": "::",
"listen_port": $port,
"sniff": true,
"sniff_override_destination": false,
"users": [
{
"name": "trojan",
"password": "$pswd"
}
],
"tls": {
"enabled": true,
"server_name": "$host",
"certificate_path": "${CADDY_TLS_PATH}/certificates/acme-v02.api.letsencrypt.org-directory/$host/$host.crt",
"key_path": "${CADDY_TLS_PATH}/certificates/acme-v02.api.letsencrypt.org-directory/$host/$host.key"
},
"fallback": {
"server": "127.0.0.1",
"server_port": 80
},
"fallback_for_alpn": {
"http/1.1": {
"server": "127.0.0.1",
"server_port": 443
}
},
"transport": {
"type": "ws",
"path": "$path",
"max_early_data": 2048,
"early_data_header_name": "Sec-WebSocket-Protocol"
}
}
],
"outbounds": [
{
"type": "direct",
"tag": "direct"
},
{
"type":"direct",
"tag":"warp-IPv6-out",
"detour":"wireguard-out",
"domain_strategy":"ipv6_only"
},
{
"type": "wireguard",
"tag": "wireguard-out",
"server": "engage.cloudflareclient.com",
"server_port": 2408,
"local_address": [
"172.16.0.2/32",
"$ipv6"
],
"private_key": "$key",
"peer_public_key": "bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=",
"reserved": [$reserved],
"mtu": 1280,
"domain_strategy": "prefer_ipv6",
"fallback_delay": "300ms"
}
],
"route": {
"rules": [
{
"domain_regex": ["^[^.]+\\.itunes\\.apple\\.com$"],
"outbound": "wireguard-out"
},
{
"domain_suffix": [
"imgur.com",
"testflight.apple.com",
"mzstatic.com"
],
"outbound": "wireguard-out"
},
{
"ip_cidr": ["1.1.1.1/32"],
"outbound": "wireguard-out"
},
{
"geosite": ["openai"],
"outbound": "warp-IPv6-out"
},
{
"domain_keyword": ["ipv6"],
"outbound": "warp-IPv6-out"
},
{
"ip_version": 6,
"outbound": "warp-IPv6-out"
}
],
"geoip": {
"path": "${SING_BOX_CONFIG_PATH}/geoip.db",
"download_url": "https://github.com/SagerNet/sing-geoip/releases/latest/download/geoip.db",
"download_detour": "direct"
},
"geosite": {
"path": "${SING_BOX_CONFIG_PATH}/geosite.db",
"download_url": "https://github.com/SagerNet/sing-geosite/releases/latest/download/geosite.db",
"download_detour": "direct"
},
"final": "direct",
"auto_detect_interface": true
}
}
EOF
LOGD "sing-box 配置文件完成"
}
#install sing-box
install_sing-box() {
LOGD "开始安装 sing-box"
mkdir -p "${SING_BOX_CONFIG_PATH}"
mkdir -p "${SING_BOX_LOG_PATH}"
mkdir -p "${SING_BOX_LIB_PATH}"
download_sing-box
install_sing_box_systemd_service
configuration_sing_box_config
systemctl start sing-box
LOGI "sing-box 已完成安装并启动"
}
#install sing-box with warp
install_sing-box_warp() {
LOGD "开始安装 sing-box"
mkdir -p "${SING_BOX_CONFIG_PATH}"
mkdir -p "${SING_BOX_LOG_PATH}"
mkdir -p "${SING_BOX_LIB_PATH}"
download_sing-box
install_sing_box_systemd_service
configuration_sing_box_warp_config
systemctl start sing-box
LOGI "sing-box 已完成安装并启动"
}
#update sing-box
update_sing-box() {
LOGD "开始更新sing-box..."
if [[ ! -f "${SING_BOX_SERVICE}" ]]; then
LOGE "当前系统未安装sing-box,更新失败"
show_menu
fi
os_check && arch_check && install_base
systemctl stop sing-box
rm -f ${SING_BOX_BINARY}
# getting the latest version of sing-box"
download_sing-box
LOGI "sing-box 启动成功"
systemctl restart sing-box
LOGI "sing-box 已完成升级"
}
#uninstall sing-box
uninstall_sing-box() {
LOGD "开始卸载sing-box..."
systemctl stop sing-box
systemctl disable sing-box
rm -f ${SING_BOX_SERVICE}
systemctl daemon-reload
rm -f ${SING_BOX_BINARY}
rm -rf ${SING_BOX_CONFIG_PATH}
rm -rf ${SING_BOX_LOG_PATH}
rm -rf ${SING_BOX_LIB_PATH}
LOGI "卸载sing-box成功"
}
#caddy status check,-1 means didn't install,0 means failed,1 means running
caddy_status_check() {
if [[ ! -f "${CADDY_SERVICE}" ]]; then
return ${CADDY_STATUS_NOT_INSTALL}
fi
caddy_status_temp=$(systemctl is-active caddy)
if [[ "${caddy_status_temp}" == "active" ]]; then
return ${CADDY_STATUS_RUNNING}
else
return ${CADDY_STATUS_NOT_RUNNING}
fi
}
show_caddy_status() {
caddy_status_check
case $? in
0)
echo -e "[INF] caddy状态: ${yellow}未运行${plain}"
show_caddy_enable_status
;;
1)
echo -e "[INF] caddy状态: ${green}已运行${plain}"
show_caddy_enable_status
;;
255)
echo -e "[INF] caddy状态: ${red}未安装${plain}"
;;
esac
}
show_caddy_enable_status() {
local caddy_enable_status_temp=$(systemctl is-enabled caddy)
if [[ "${caddy_enable_status_temp}" == "enabled" ]]; then
echo -e "[INF] caddy是否开机自启: ${green}是${plain}"
else
echo -e "[INF] caddy是否开机自启: ${red}否${plain}"
fi
}
download_caddy() {
LOGD "开始下载 caddy..."
# getting the latest version of caddy"
LATEST_CADDY_VERSION="$(wget -qO- -t1 -T2 "https://api.github.com/repos/lxhao61/integrated-examples/releases" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')"
CADDY_LINK="https://github.com/lxhao61/integrated-examples/releases/download/${LATEST_CADDY_VERSION}/caddy-linux-${ARCH}.tar.gz"
cd `mktemp -d`
wget -nv "${CADDY_LINK}" -O caddy.tar.gz
tar -zxvf caddy.tar.gz
mv caddy ${CADDY_BINARY} && chmod +x ${CADDY_BINARY}
LOGI "caddy 下载完毕"
}
install_caddy_systemd_service() {
LOGD "开始安装 caddy systemd 服务..."
cat <<EOF >${CADDY_SERVICE}
[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target
[Service]
Type=notify
User=root
Group=root
ExecStart=${CADDY_BINARY} run --environ --config ${CADDY_CONFIG_PATH}/config.json
ExecReload=${CADDY_BINARY} reload --config ${CADDY_CONFIG_PATH}/config.json
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable caddy
LOGD "安装 caddy systemd 服务成功"
}
configuration_caddy_config() {
LOGD "开始配置caddy配置文件..."
# set Caddyfile
cat <<EOF >${CADDY_CONFIG_PATH}/config.json
{
"admin": {
"disabled": true,
"config": {
"persist": false
}
},
"logging": {
"logs": {
"default": {
"writer": {
"output": "file",
"filename": "${CADDY_LOG_PATH}/caddy.log"
},
"encoder": {
"format": "console"
},
"level": "WARN"
}
}
},
"storage": {
"module": "file_system",
"root": "${CADDY_TLS_PATH}"
},
"apps": {
"http": {
"servers": {
"srvh1": {
"listen": [":80"],
"routes": [
{
"handle": [
{
"handler": "static_response",
"headers": {
"Location": ["https://{http.request.host}{http.request.uri}"]
},
"status_code": 301
}
]
}
],
"protocols": ["h1"]
},
"srvh2": {
"listen": [":443"],
"routes": [
{
"handle": [
{
"handler": "headers",
"response": {
"set": {
"Strict-Transport-Security": ["max-age=31536000; includeSubDomains; preload"]
}
}
},
{
"handler": "reverse_proxy",
"upstreams": [
{
"dial": "127.0.0.1:40333"
}
]
}
],
"match": [
{
"host": ["$host"]
}
]
}
],
"tls_connection_policies": [
{
"cipher_suites": [
"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
"TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256"
],
"curves": ["x25519","secp521r1","secp384r1","secp256r1"]
}
],
"protocols": ["h1","h2"]
}
}
},
"tls": {
"certificates": {
"automate": ["$host"]
},
"automation": {
"policies": [
{
"issuers": [
{
"module": "acme",
"email": "$email"
}
]
}
]
}
}
}
}
EOF
LOGD "caddy 配置文件完成"
}
configuration_caddy_plex_config() {
LOGD "开始配置caddy配置文件..."
# set Caddyfile
cat <<EOF >${CADDY_CONFIG_PATH}/config.json
{
"admin": {
"disabled": true,
"config": {
"persist": false
}
},
"logging": {
"logs": {
"default": {
"writer": {
"output": "file",
"filename": "${CADDY_LOG_PATH}/caddy.log"
},
"encoder": {
"format": "console"
},
"level": "WARN"
}
}
},
"storage": {
"module": "file_system",
"root": "${CADDY_TLS_PATH}"
},
"apps": {
"http": {
"servers": {
"srvh1": {
"listen": [":80"],
"routes": [
{
"handle": [
{
"handler": "static_response",
"headers": {
"Location": ["https://{http.request.host}{http.request.uri}"]
},
"status_code": 301
}
]
}
],
"protocols": ["h1"]
},
"srvh2": {
"listen": [":443"],
"routes": [
{
"handle": [
{
"handler": "headers",
"response": {
"set": {
"Strict-Transport-Security": ["max-age=31536000; includeSubDomains; preload"]
}
}
},
{
"handler": "reverse_proxy",
"upstreams": [
{
"dial": "127.0.0.1:40333"
}
]
}
],
"match": [
{
"host": ["$host"]
}
]
},
{
"handle": [
{
"handler": "headers",
"response": {
"set": {
"Referrer-Policy": ["no-referrer-when-downgrade"],
"Strict-Transport-Security": ["max-age=31536000; includeSubDomains; preload"],
"X-Content-Type-Options": ["nosniff"],
"X-Frame-Options": ["DENY"],
"X-Xss-Protection": ["1"]
}
}
},
{
"handler": "reverse_proxy",
"upstreams": [
{
"dial": "127.0.0.1:32400"
}
]
},
{
"encodings": {
"gzip": {
}
},
"handler": "encode",
"prefer": ["gzip"]
}
],