-
Notifications
You must be signed in to change notification settings - Fork 15
/
sing-box.sh
1480 lines (1365 loc) · 37.4 KB
/
sing-box.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
# sing-box 一键安装脚本
# Author: yuehen7
plain='\033[0m'
red='\033[0;31m'
blue='\033[1;34m'
pink='\033[1;35m'
green='\033[0;32m'
yellow='\033[0;33m'
#os
OS_RELEASE=''
#arch
OS_ARCH=''
#sing-box version
SING_BOX_VERSION=''
#script version
SING_BOX_YES_VERSION='1.0.12'
#package download path
DOWNLAOD_PATH='/usr/local/sing-box'
#scritp install path
SCRIPT_FILE_PATH='/usr/local/sbin/sing-box'
#config install path
CONFIG_FILE_PATH='/usr/local/etc/sing-box'
#binary install path
BINARY_FILE_PATH='/usr/local/bin/sing-box'
#service install path
SERVICE_FILE_PATH='/etc/systemd/system/sing-box.service'
#log file save path
DEFAULT_LOG_FILE_SAVE_PATH='/usr/local/sing-box/sing-box.log'
NGINX_CONF_PATH="/etc/nginx/conf.d/"
#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
#log file size which will trigger log clear
#here we set it as 25M
declare -r DEFAULT_LOG_FILE_DELETE_TRIGGER=25
#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
}
[[ $EUID -ne 0 ]] && LOGE "请使用root用户运行该脚本" && exit 1
os_check() {
LOGI "检测当前系统中..."
if [[ -f /etc/redhat-release ]]; then
OS_RELEASE="centos"
elif cat /etc/issue | grep -Eqi "debian"; then
OS_RELEASE="debian"
elif cat /etc/issue | grep -Eqi "ubuntu"; then
OS_RELEASE="ubuntu"
elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
OS_RELEASE="centos"
elif cat /proc/version | grep -Eqi "debian"; then
OS_RELEASE="debian"
elif cat /proc/version | grep -Eqi "ubuntu"; then
OS_RELEASE="ubuntu"
elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
OS_RELEASE="centos"
else
LOGE "系统检测错误,请联系脚本作者!" && exit 1
fi
LOGI "系统检测完毕,当前系统为:${OS_RELEASE}"
}
arch_check() {
LOGI "检测当前系统架构中..."
OS_ARCH=$(arch)
LOGI "当前系统架构为 ${OS_ARCH}"
if [[ ${OS_ARCH} == "x86_64" || ${OS_ARCH} == "x64" || ${OS_ARCH} == "amd64" ]]; then
OS_ARCH="amd64"
elif [[ ${OS_ARCH} == "aarch64" || ${OS_ARCH} == "arm64" ]]; then
OS_ARCH="arm64"
else
OS_ARCH="amd64"
LOGE "检测系统架构失败,使用默认架构: ${OS_ARCH}"
fi
LOGI "系统架构检测完毕,当前系统架构为:${OS_ARCH}"
}
status_check() {
if [[ ! -f "${SERVICE_FILE_PATH}" ]]; then
return ${SING_BOX_STATUS_NOT_INSTALL}
fi
temp=$(systemctl status sing-box | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
if [[ x"${temp}" == x"running" ]]; then
return ${SING_BOX_STATUS_RUNNING}
else
return ${SING_BOX_STATUS_NOT_RUNNING}
fi
}
config_check() {
if [[ ! -f "${CONFIG_FILE_PATH}/config.json" ]]; then
LOGE "${CONFIG_FILE_PATH}/config.json 不存在,配置检查失败"
return
else
info=$(${BINARY_FILE_PATH} check -c ${CONFIG_FILE_PATH}/config.json)
if [[ $? -ne 0 ]]; then
LOGE "配置检查失败,请查看日志"
else
LOGI "恭喜:配置检查通过"
fi
fi
}
set_as_entrance() {
if [[ ! -f "${SCRIPT_FILE_PATH}" ]]; then
wget --no-check-certificate -O ${SCRIPT_FILE_PATH} https://raw.githubusercontent.com/yuehen7/scripts/main/sing-box.sh
chmod +x ${SCRIPT_FILE_PATH}
fi
}
update_shell(){
wget --no-check-certificate -O ${SCRIPT_FILE_PATH} https://raw.githubusercontent.com/yuehen7/scripts/main/sing-box.sh
chmod +x ${SCRIPT_FILE_PATH}
echo -e "脚本已更新..." && exit 0
}
show_status() {
status_check
case $? in
0)
show_sing_box_version
echo -e "[INF] sing-box状态: ${yellow}未运行${plain}"
show_enable_status
LOGI "配置文件路径:${CONFIG_FILE_PATH}/config.json"
LOGI "可执行文件路径:${BINARY_FILE_PATH}"
;;
1)
show_sing_box_version
echo -e "[INF] sing-box状态: ${green}已运行${plain}"
show_enable_status
show_running_status
LOGI "配置文件路径:${CONFIG_FILE_PATH}/config.json"
LOGI "可执行文件路径:${BINARY_FILE_PATH}"
;;
255)
echo -e "[INF] sing-box状态: ${red}未安装${plain}"
;;
esac
}
show_running_status() {
status_check
if [[ $? == ${SING_BOX_STATUS_RUNNING} ]]; then
local pid=$(pidof sing-box)
local runTime=$(systemctl status sing-box | grep Active | awk '{for (i=5;i<=NF;i++)printf("%s ", $i);print ""}')
local memCheck=$(cat /proc/${pid}/status | grep -i vmrss | awk '{print $2,$3}')
LOGI "#####################"
LOGI "进程ID:${pid}"
LOGI "运行时长:${runTime}"
LOGI "内存占用:${memCheck}"
LOGI "#####################"
else
LOGE "sing-box未运行"
fi
}
show_sing_box_version() {
LOGI "版本信息:$(${BINARY_FILE_PATH} version)"
}
show_enable_status() {
local temp=$(systemctl is-enabled sing-box)
if [[ x"${temp}" == x"enabled" ]]; then
echo -e "[INF] sing-box是否开机自启: ${green}是${plain}"
else
echo -e "[INF] sing-box是否开机自启: ${red}否${plain}"
fi
}
create_or_delete_path() {
if [[ $# -ne 1 ]]; then
LOGE "invalid input,should be one paremete,and can be 0 or 1"
exit 1
fi
if [[ "$1" == "1" ]]; then
LOGI "Will create ${DOWNLAOD_PATH} and ${CONFIG_FILE_PATH} for sing-box..."
rm -rf ${DOWNLAOD_PATH} ${CONFIG_FILE_PATH}
mkdir -p ${DOWNLAOD_PATH} ${CONFIG_FILE_PATH}
if [[ $? -ne 0 ]]; then
LOGE "create ${DOWNLAOD_PATH} and ${CONFIG_FILE_PATH} for sing-box failed"
exit 1
else
LOGI "create ${DOWNLAOD_PATH} adn ${CONFIG_FILE_PATH} for sing-box success"
fi
elif [[ "$1" == "0" ]]; then
LOGI "Will delete ${DOWNLAOD_PATH} and ${CONFIG_FILE_PATH}..."
rm -rf ${DOWNLAOD_PATH} ${CONFIG_FILE_PATH}
if [[ $? -ne 0 ]]; then
LOGE "delete ${DOWNLAOD_PATH} and ${CONFIG_FILE_PATH} failed"
exit 1
else
LOGI "delete ${DOWNLAOD_PATH} and ${CONFIG_FILE_PATH} success"
fi
fi
}
install_base() {
if [[ ${OS_RELEASE} == "ubuntu" || ${OS_RELEASE} == "debian" ]]; then
apt clean all
apt update -y
apt install wget tar unzip vim gcc openssl -y
apt install net-tools -y
apt install libssl-dev g++ -y
elif [[ ${OS_RELEASE} == "centos" ]]; then
yum install wget tar unzip vim gcc openssl -y
yum install net-tools -y
fi
res=`which unzip 2>/dev/null`
if [[ $? -ne 0 ]]; then
LOGE " unzip安装失败,请检查网络${plain}"
exit 1
fi
}
download_sing-box() {
LOGD "开始下载sing-box..."
os_check && arch_check && install_base
local SING_BOX_VERSION_TEMP=$(curl -Ls "https://api.github.com/repos/SagerNet/sing-box/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
SING_BOX_VERSION=${SING_BOX_VERSION_TEMP:1}
LOGI "将选择使用版本:${SING_BOX_VERSION}"
local DOWANLOAD_URL="https://github.com/SagerNet/sing-box/releases/download/${SING_BOX_VERSION_TEMP}/sing-box-${SING_BOX_VERSION}-linux-${OS_ARCH}.tar.gz"
#here we need create directory for sing-box
create_or_delete_path 1
wget -N --no-check-certificate -O ${DOWNLAOD_PATH}/sing-box-${SING_BOX_VERSION}-linux-${OS_ARCH}.tar.gz ${DOWANLOAD_URL}
if [[ $? -ne 0 ]]; then
LOGE "Download sing-box failed,plz be sure that your network work properly and can access github"
create_or_delete_path 0
exit 1
else
LOGI "下载sing-box成功"
fi
}
install_sing-box() {
set_as_entrance
LOGD "开始安装sing-box..."
if [[ $# -ne 0 ]]; then
download_sing-box $1
else
download_sing-box
fi
config_sing-box
setFirewall
if [[ ! -f "${DOWNLAOD_PATH}/sing-box-${SING_BOX_VERSION}-linux-${OS_ARCH}.tar.gz" ]]; then
clear_sing_box
LOGE "could not find sing-box packages,plz check dowanload sing-box whether suceess"
exit 1
fi
cd ${DOWNLAOD_PATH}
tar -xvf sing-box-${SING_BOX_VERSION}-linux-${OS_ARCH}.tar.gz && cd sing-box-${SING_BOX_VERSION}-linux-${OS_ARCH}
if [[ $? -ne 0 ]]; then
clear_sing_box
OGE "解压sing-box安装包失败,脚本退出"
exit 1
else
LOGI "解压sing-box安装包成功"
fi
install -m 755 sing-box ${BINARY_FILE_PATH}
if [[ $? -ne 0 ]]; then
LOGE "install sing-box failed,exit"
exit 1
else
LOGI "install sing-box suceess"
fi
install_systemd_service && enable_sing-box && start_sing-box
LOGI "安装sing-box成功,已启动成功"
}
update_sing-box() {
LOGD "开始更新sing-box..."
if [[ ! -f "${SERVICE_FILE_PATH}" ]]; then
LOGE "system did not install sing-box,please install it firstly"
show_menu
fi
download_sing-box && install_sing-box
if ! systemctl restart sing-box; then
LOGE "update sing-box failed,please check logs"
show_menu
else
LOGI "update sing-box success"
fi
}
clear_sing_box() {
LOGD "开始清除sing-box..."
create_or_delete_path 0 && rm -rf ${SERVICE_FILE_PATH} && rm -rf ${BINARY_FILE_PATH} && rm -rf ${SCRIPT_FILE_PATH}
LOGD "清除sing-box完毕"
}
uninstall_sing-box() {
echo ""
line1=`grep -n 'inbounds' ${CONFIG_FILE_PATH}/config.json | head -n1 | cut -d: -f1`
line11=`expr $line1 + 2`
local type=`sed -n "${line11}p" ${CONFIG_FILE_PATH}/config.json | cut -d: -f2 | tr -d \",' '`
if [[ ${type} == "trojan" ]]; then
LOGI "配置类型为trojan,开始卸载nginx..."
systemctl stop nginx
systemctl disable nginx
if [[ ${OS_RELEASE} == "ubuntu" || ${OS_RELEASE} == "debian" ]]; then
apt autoremove nginx -y
elif [[ ${OS_RELEASE} == "centos" ]]; then
yum autoremove nginx -y
fi
rm -rf /etc/nginx/nginx.conf
if [[ -f /etc/nginx/nginx.conf.bak ]]; then
mv /etc/nginx/nginx.conf.bak /etc/nginx/nginx.conf
fi
rm -rf $NGINX_CONF_PATH${domain}.conf
rm -rf /usr/share/nginx
~/.acme.sh/acme.sh --uninstall
rm -rf ~/.acme.sh
LOGI "nginx及acme.sh卸载完成."
fi
LOGD "开始卸载sing-box..."
pidOfsing_box=$(pidof sing-box)
if [ -n ${pidOfsing_box} ]; then
stop_sing-box
fi
clear_sing_box
if [ $? -ne 0 ]; then
LOGE "卸载sing-box失败,请检查日志"
exit 1
else
LOGI "卸载sing-box成功"
fi
}
install_systemd_service() {
LOGD "开始安装sing-box systemd服务..."
if [ -f "${SERVICE_FILE_PATH}" ]; then
rm -rf ${SERVICE_FILE_PATH}
fi
#create service file
touch ${SERVICE_FILE_PATH}
if [ $? -ne 0 ]; then
LOGE "create service file failed,exit"
exit 1
else
LOGI "create service file success..."
fi
cat >${SERVICE_FILE_PATH} <<EOF
[Unit]
Description=sing-box Service
Documentation=https://sing-box.sagernet.org/
After=network.target nss-lookup.target
Wants=network.target
[Service]
Type=simple
ExecStart=${BINARY_FILE_PATH} run -c ${CONFIG_FILE_PATH}/config.json
Restart=on-failure
RestartSec=30s
RestartPreventExitStatus=23
LimitNPROC=10000
LimitNOFILE=1000000
[Install]
WantedBy=multi-user.target
EOF
chmod 644 ${SERVICE_FILE_PATH}
systemctl daemon-reload
LOGD "安装sing-box systemd服务成功"
}
start_sing-box() {
if [ -f "${SERVICE_FILE_PATH}" ]; then
systemctl start sing-box
sleep 1s
status_check
if [ $? == ${SING_BOX_STATUS_NOT_RUNNING} ]; then
LOGE "start sing-box service failed,exit"
exit 1
elif [ $? == ${SING_BOX_STATUS_RUNNING} ]; then
LOGI "start sing-box service success"
fi
else
LOGE "${SERVICE_FILE_PATH} does not exist,can not start service"
exit 1
fi
}
#restart sing-box
restart_sing-box() {
if [ -f "${SERVICE_FILE_PATH}" ]; then
systemctl restart sing-box
sleep 1s
status_check
if [ $? == 0 ]; then
LOGE "restart sing-box service failed,exit"
exit 1
elif [ $? == 1 ]; then
LOGI "restart sing-box service success"
fi
else
LOGE "${SERVICE_FILE_PATH} does not exist,can not restart service"
exit 1
fi
}
#stop sing-box
stop_sing-box() {
LOGD "开始停止sing-box服务..."
status_check
if [ $? == ${SING_BOX_STATUS_NOT_INSTALL} ]; then
LOGE "sing-box did not install,can not stop it"
exit 1
elif [ $? == ${SING_BOX_STATUS_NOT_RUNNING} ]; then
LOGI "sing-box already stoped,no need to stop it again"
exit 1
elif [ $? == ${SING_BOX_STATUS_RUNNING} ]; then
if ! systemctl stop sing-box; then
LOGE "stop sing-box service failed,plz check logs"
exit 1
fi
fi
LOGD "停止sing-box服务成功"
}
#enable sing-box will set sing-box auto start on system boot
enable_sing-box() {
systemctl enable sing-box
if [[ $? == 0 ]]; then
LOGI "设置sing-box开机自启成功"
else
LOGE "设置sing-box开机自启失败"
fi
}
#disable sing-box
disable_sing-box() {
systemctl disable sing-box
if [[ $? == 0 ]]; then
LOGI "取消sing-box开机自启成功"
else
LOGE "取消sing-box开机自启失败"
fi
}
#show logs
show_log() {
confirm "确认是否已在配置中开启日志记录,默认开启" "y"
if [[ $? -ne 0 ]]; then
LOGI "将从console中读取日志:"
journalctl -u sing-box.service -e --no-pager -f
else
local tempLog=''
read -p "将从日志文件中读取日志,请输入日志文件路径,直接回车将使用默认路径": tempLog
if [[ -n ${tempLog} ]]; then
LOGI "日志文件路径:${tempLog}"
if [[ -f ${tempLog} ]]; then
tail -f ${tempLog} -s 3
else
LOGE "${tempLog}不存在,请确认配置"
fi
else
LOGI "日志文件路径:${DEFAULT_LOG_FILE_SAVE_PATH}"
tail -f ${DEFAULT_LOG_FILE_SAVE_PATH} -s 3
fi
fi
}
#clear log,the paremter is log file path
clear_log() {
local filePath=''
if [[ $# -gt 0 ]]; then
filePath=$1
else
read -p "请输入日志文件路径": filePath
if [[ ! -n ${filePath} ]]; then
LOGI "输入的日志文件路径无效,将使用默认的文件路径"
filePath=${DEFAULT_LOG_FILE_SAVE_PATH}
fi
fi
LOGI "日志路径为:${filePath}"
if [[ ! -f ${filePath} ]]; then
LOGE "清除sing-box 日志文件失败,${filePath}不存在,请确认"
exit 1
fi
fileSize=$(ls -la ${filePath} --block-size=M | awk '{print $5}' | awk -F 'M' '{print$1}')
if [[ ${fileSize} -gt ${DEFAULT_LOG_FILE_DELETE_TRIGGER} ]]; then
rm $1 && systemctl restart sing-box
if [[ $? -ne 0 ]]; then
LOGE "清除sing-box 日志文件失败"
else
LOGI "清除sing-box 日志文件成功"
fi
else
LOGI "当前日志大小为${fileSize}M,小于${DEFAULT_LOG_FILE_DELETE_TRIGGER}M,将不会清除"
fi
}
config_Shadowsocks(){
echo ""
read -p " 请输入Shadowsocks端口[100-65535的一个数字,默认54321]:" port
[[ -z "${port}" ]] && port=54321
if [[ "${port:0:1}" = "0" ]]; then
LOGE "端口不能以0开头"
exit 1
fi
LOGI " Shadowsocks端口:$port"
echo ""
echo "数据加密方式:"
echo -e " ${red}1. 2022-blake3-aes-128-gcm${plain}"
echo -e " ${red}2. chacha20-ietf-poly1305${plain}"
echo ""
read -p "请加密类型,默认为1:" method_type
[[ -z "${method_type}" ]] && method_type=1
case $method_type in
1)
method="2022-blake3-aes-128-gcm"
password=`openssl rand -base64 16`
;;
2)
method="chacha20-ietf-poly1305"
password=`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1`
;;
*)
LOGE " 请输入正确的选项!"
exit 1
esac
LOGI "加密类型:$method"
LOGI "密码:$password"
echo ""
LOGD "开始配置config.json..."
cat > ${CONFIG_FILE_PATH}/config.json <<-EOF
{
"log": {
"disabled": false,
"level": "info",
"output": "/usr/local/sing-box/sing-box.log",
"timestamp": true
},
"dns": {
"servers": [
{
"tag": "google-tls",
"address": "local",
"address_strategy": "prefer_ipv4",
"strategy": "ipv4_only",
"detour": "direct"
},
{
"tag": "google-udp",
"address": "8.8.4.4",
"address_strategy": "prefer_ipv4",
"strategy": "prefer_ipv4",
"detour": "direct"
}
],
"strategy": "prefer_ipv4",
"disable_cache": false,
"disable_expire": false
},
"inbounds": [
{
"type": "shadowsocks",
"tag": "ss-in",
"listen": "0.0.0.0",
"listen_port": ${port},
"method": "${method}",
"password": "${password}",
"network": "tcp",
"domain_strategy": "prefer_ipv4",
"tcp_fast_open": true,
"sniff": true,
"proxy_protocol": false
}
],
"outbounds": [
{
"type": "direct",
"tag": "direct"
},
{
"type": "block",
"tag": "block"
},
{
"type": "dns",
"tag": "dns-out"
}
],
"route": {
"rules": [
{
"protocol": "dns",
"outbound": "dns-out"
},
{
"inbound": ["ss-in"],
"network": "tcp",
"outbound": "direct"
},
{
"geosite": "category-ads-all",
"outbound": "block"
},
{
"geosite": "cn",
"geoip": "cn",
"outbound": "block"
}
],
"geoip": {
"path": "geoip.db",
"download_url": "https://github.com/SagerNet/sing-geoip/releases/latest/download/geoip.db",
"download_detour": "direct"
},
"geosite": {
"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
}
config_ShadowTLS(){
echo ""
read -p " 请输入ShadowTLS端口[100-65535的一个数字,默认443]:" port
[[ -z "${port}" ]] && port=443
if [[ "${port:0:1}" = "0" ]]; then
LOGE "端口不能以0开头"
exit 1
fi
LOGI " ShadowTLS端口:$port"
echo ""
echo "数据加密方式:"
echo -e " ${red}1. 2022-blake3-aes-128-gcm${plain}"
echo -e " ${red}2. chacha20-ietf-poly1305${plain}"
echo ""
read -p "请加密类型,默认为1:" method_type
[[ -z "${method_type}" ]] && method_type=1
case $method_type in
1)
method="2022-blake3-aes-128-gcm"
password=`openssl rand -base64 16`
;;
2)
method="chacha20-ietf-poly1305"
password=`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1`
;;
*)
LOGE " 请输入正确的选项!"
exit 1
esac
LOGI "加密类型:$method"
LOGI "密码:$password"
echo ""
read -p " 请输入handshake域名[默认为:www.bing.com]:" handshake_server
[[ -z "${handshake_server}" ]] && handshake_server="www.bing.com"
echo ""
echo ""
read -p " 请输入handshake端口[默认为:443]:" handshake_port
[[ -z "${handshake_port}" ]] && handshake_port=443
echo ""
LOGI "handshake域名:$handshake_server"
LOGI "handshake端口:$handshake_port"
echo ""
LOGD "开始配置config.json..."
cat > ${CONFIG_FILE_PATH}/config.json <<-EOF
{
"log": {
"disabled": false,
"level": "info",
"output": "/usr/local/sing-box/sing-box.log",
"timestamp": true
},
"dns": {
"servers": [
{
"tag": "google-tls",
"address": "local",
"address_strategy": "prefer_ipv4",
"strategy": "ipv4_only",
"detour": "direct"
},
{
"tag": "google-udp",
"address": "8.8.4.4",
"address_strategy": "prefer_ipv4",
"strategy": "prefer_ipv4",
"detour": "direct"
}
],
"strategy": "prefer_ipv4",
"disable_cache": false,
"disable_expire": false
},
"inbounds": [
{
"type": "shadowtls",
"tag": "shadowtls-in",
"listen": "::",
"listen_port": ${port},
"handshake": {
"server": "${handshake_server}",
"server_port": ${handshake_port}
},
"detour": "ss-in"
},
{
"type": "shadowsocks",
"tag": "ss-in",
"listen": "127.0.0.1",
"method": "${method}",
"password": "${password}"
}
],
"outbounds": [
{
"type": "direct",
"tag": "direct"
},
{
"type": "block",
"tag": "block"
},
{
"type": "dns",
"tag": "dns-out"
}
],
"route": {
"rules": [
{
"protocol": "dns",
"outbound": "dns-out"
},
{
"inbound": ["shadowtls-in"],
"network": "tcp",
"outbound": "direct"
},
{
"geosite": "category-ads-all",
"outbound": "block"
},
{
"geosite": "cn",
"geoip": "cn",
"outbound": "block"
}
],
"geoip": {
"path": "geoip.db",
"download_url": "https://github.com/SagerNet/sing-geoip/releases/latest/download/geoip.db",
"download_detour": "direct"
},
"geosite": {
"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
}
SITES=(
http://www.biqu6.com/
http://www.55shuba.com/
http://www.bequgexs.com/
http://www.tjwl.com/
)
config_Trojan(){
echo ""
ip=`curl -sL -4 ip.sb`
if [[ "$?" != "0" ]]; then
LOGE "暂不支持IPv6服务器!"
exit 1
fi
echo ""
echo " 使用trojan请检查是否满足以下条件:"
echo -e " ${red}1.一个伪装域名${plain}"
echo -e " ${red}2.伪装域名DNS解析指向当前服务器ip(${ip})${plain}"
echo ""
read -p " 确认满足按y,按其他退出脚本:" answer
if [[ "${answer,,}" != "y" ]]; then
exit 0
fi
echo ""
while true
do
read -p " 请输入伪装域名:" domain
if [[ -z "${domain}" ]]; then
LOGE "伪装域名输入错误,请重新输入!${plain}"
else
break
fi
done
LOGI "伪装域名(host):$domain"
echo ""
domain=${domain,,}
resolve=`curl -sL https://lefu.men/hostip.php?d=${domain}`
res=`echo -n ${resolve} | grep ${ip}`
if [[ -z "${res}" ]]; then
echo " ${domain} 解析结果:${resolve}"
LOGE "伪装域名未解析到当前服务器IP(${ip})!${plain}"
exit 1
fi
echo ""
read -p " 请设置trojan密码(不输则随机生成):" password
[[ -z "$password" ]] && password=`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1`
LOGI " trojan密码:$password"
echo ""
read -p " 请输入trojan端口[100-65535的一个数字,默认443]:" port
[[ -z "${port}" ]] && port=443
if [[ "${port:0:1}" = "0" ]]; then
LOGE "端口不能以0开头${plain}"
exit 1
fi
LOGI " trojan端口:$port"
echo ""
while true
do
read -p " 请输入伪装路径,以/开头(不懂请直接回车):" wspath
if [[ -z "${wspath}" ]]; then
len=`shuf -i5-12 -n1`
ws=`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w $len | head -n 1`
wspath="/$ws"
break
elif [[ "${wspath:0:1}" != "/" ]]; then
echo "伪装路径必须以/开头!"
elif [[ "${wspath}" = "/" ]]; then
echo "不能使用根路径!"
else
break
fi
done
echo ""
LOGI " ws路径:$wspath"
echo ""
echo " 请选择伪装站类型:"
echo " 1) 静态网站(位于/usr/share/nginx/html)"
echo " 2) 小说站(随机选择)"
echo " 3) Bing(http://www.bing.com)"
read -p " 请选择伪装网站类型[默认:Bing]" answer
if [[ -z "$answer" ]]; then
proxy_url="https://www.bing.com"
else
case $answer in
1)
proxy_url=""
;;
2)
len=${#SITES[@]}
((len--))
while true
do
index=`shuf -i0-${len} -n1`
proxy_url=${SITES[$index]}
host=`echo ${proxy_url} | cut -d/ -f3`
temp_ip=`curl -sL https://lefu.men/hostip.php?d=${host}`
res=`echo -n ${temp_ip} | grep ${host}`
if [[ "${res}" = "" ]]; then
echo "$temp_ip $host" >> /etc/hosts
break
fi
done
;;
3)
proxy_url="https://www.bing.com"
;;
*)
LOGE " 请输入正确的选项!"
exit 1
esac
fi
remote_host=`echo ${proxy_url} | cut -d/ -f3`
echo ""
LOGI " 伪装网站:$proxy_url"
echo ""
LOGI " 开始安装nginx..."
if [[ ${OS_RELEASE} == "ubuntu" || ${OS_RELEASE} == "debian" ]]; then
apt install nginx -y
if [[ "$?" != "0" ]]; then
LOGE " Nginx安装失败!"
exit 1
fi
elif [[ ${OS_RELEASE} == "centos" ]]; then
yum install epel-release -y
if [[ "$?" != "0" ]]; then
echo '[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true' > /etc/yum.repos.d/nginx.repo
fi
yum install nginx -y
if [[ "$?" != "0" ]]; then
LOGE " Nginx安装失败!"
exit 1
fi
fi
systemctl enable nginx
getCert
config_Nginx
LOGD "开始配置config.json..."
cat > /usr/local/etc/sing-box/config.json <<-EOF
{
"log": {
"disabled": false,
"level": "info",
"output": "/usr/local/sing-box/sing-box.log",
"timestamp": true
},
"dns": {
"servers": [
{
"tag": "google-tls",