-
Notifications
You must be signed in to change notification settings - Fork 467
/
install_script.sh
2013 lines (1799 loc) · 71.4 KB
/
install_script.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
#!/usr/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# System Required: CentOS 7+/Ubuntu 18+/Debian 10+
# Version: v2.3.2
# Description: One click Install Trojan Panel server
# Author: jonssonyan <https://jonssonyan.com>
# Github: https://github.com/trojanpanel/install-script
init_var() {
ECHO_TYPE="echo -e"
package_manager=""
release=""
get_arch=""
can_google=0
# Docker
DOCKER_MIRROR='"https://hub-mirror.c.163.com","https://ccr.ccs.tencentyun.com","https://mirror.baidubce.com","https://dockerproxy.com"'
# Project directory
TP_DATA="/tpdata/"
STATIC_HTML="https://github.com/trojanpanel/install-script/releases/download/v1.0/html.tar.gz"
# Web
WEB_PATH="/tpdata/web/"
# Cert
CERT_PATH="/tpdata/cert/"
DOMAIN_FILE="/tpdata/domain.lock"
domain=""
crt_path=""
key_path=""
# Caddy2
CADDY_DATA="/tpdata/caddy/"
CADDY_CONFIG="${CADDY_DATA}config.json"
CADDY_LOG="${CADDY_DATA}logs/"
CADDY_CERT_DIR="${CERT_PATH}certificates/acme-v02.api.letsencrypt.org-directory/"
caddy_port=80
caddy_remote_port=8863
your_email=""
ssl_option=1
ssl_module_type=1
ssl_module="acme"
# Nginx
NGINX_DATA="/tpdata/nginx/"
NGINX_CONFIG="${NGINX_DATA}default.conf"
nginx_port=80
nginx_remote_port=8863
nginx_https=1
# MariaDB
MARIA_DATA="/tpdata/mariadb/"
mariadb_ip="127.0.0.1"
mariadb_port=9507
mariadb_user="root"
mariadb_pas=""
# Redis
REDIS_DATA="/tpdata/redis/"
redis_host="127.0.0.1"
redis_port=6378
redis_pass=""
# Trojan Panel Frontend
TROJAN_PANEL_UI_DATA="/tpdata/trojan-panel-ui/"
# Nginx
UI_NGINX_DATA="${TROJAN_PANEL_UI_DATA}nginx/"
UI_NGINX_CONFIG="${UI_NGINX_DATA}default.conf"
trojan_panel_ui_port=8888
ui_https=1
trojan_panel_ip="127.0.0.1"
trojan_panel_server_port=8081
# Trojan Panel Backend
TROJAN_PANEL_DATA="/tpdata/trojan-panel/"
TROJAN_PANEL_WEBFILE="${TROJAN_PANEL_DATA}webfile/"
TROJAN_PANEL_LOGS="${TROJAN_PANEL_DATA}logs/"
TROJAN_PANEL_CONFIG="${TROJAN_PANEL_DATA}config/"
trojan_panel_config_path="${TROJAN_PANEL_DATA}config/config.ini"
trojan_panel_port=8081
# Trojan Panel Core
TROJAN_PANEL_CORE_DATA="/tpdata/trojan-panel-core/"
TROJAN_PANEL_CORE_LOGS="${TROJAN_PANEL_CORE_DATA}logs/"
TROJAN_PANEL_CORE_CONFIG="${TROJAN_PANEL_CORE_DATA}config/"
trojan_panel_core_config_path="${TROJAN_PANEL_CORE_DATA}config/config.ini"
database="trojan_panel_db"
account_table="account"
grpc_port=8100
trojan_panel_core_port=8082
# Version
trojan_panel_ui_current_version=""
trojan_panel_ui_latest_version="v2.3.0"
trojan_panel_current_version=""
trojan_panel_latest_version="v2.3.1"
trojan_panel_core_current_version=""
trojan_panel_core_latest_version="v2.3.1"
# SQL
sql_215="alter table account change validity_period preset_expire int unsigned default 0 not null comment '预设过期时长';alter table account add preset_quota bigint default 0 not null comment '预设配额' after preset_expire;update account set preset_quota = quota where last_login_time = 0;update account set quota = 0 where last_login_time = 0;alter table node add priority int default 100 not null comment '优先级' after port;INSERT INTO casbin_rule (p_type, v0, v1, v2, v3, v4, v5) VALUES ('p', 'sysadmin', '/api/account/clashSubscribeForSb', 'GET', 'default', 'default', 'default');alter table node_hysteria add server_name varchar(64) default '' not null comment '用于验证服务端证书的 hostname' after down_mbps;alter table node_hysteria add insecure tinyint(1) default 0 not null comment '忽略一切证书错误' after server_name;alter table node_hysteria add fast_open tinyint(1) default 0 not null comment '启用 Fast Open (降低连接建立延迟)' after insecure;"
sql_230="CREATE TABLE node_hysteria2 ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键', obfs_password varchar(64) NOT NULL DEFAULT '' COMMENT '混淆密码', up_mbps int(10) NOT NULL DEFAULT '100' COMMENT '单客户端最大上传速度 单位:Mbps', down_mbps int(10) NOT NULL DEFAULT '100' COMMENT '单客户端最大下载速度 单位:Mbps', server_name varchar(64) NOT NULL DEFAULT '' COMMENT '用于验证服务端证书的 hostname', insecure tinyint(1) NOT NULL DEFAULT '0' COMMENT '忽略一切证书错误', create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Hysteria2节点';INSERT INTO node_type (id, name, create_time, update_time) VALUES (5, 'hysteria2', '2022-04-01 00:00:00', '2022-04-01 00:00:00');"
}
echo_content() {
case $1 in
"red")
${ECHO_TYPE} "\033[31m$2\033[0m"
;;
"green")
${ECHO_TYPE} "\033[32m$2\033[0m"
;;
"yellow")
${ECHO_TYPE} "\033[33m$2\033[0m"
;;
"blue")
${ECHO_TYPE} "\033[34m$2\033[0m"
;;
"purple")
${ECHO_TYPE} "\033[35m$2\033[0m"
;;
"skyBlue")
${ECHO_TYPE} "\033[36m$2\033[0m"
;;
"white")
${ECHO_TYPE} "\033[37m$2\033[0m"
;;
esac
}
mkdir_tools() {
# Project directory
mkdir -p ${TP_DATA}
# Web
mkdir -p ${WEB_PATH}
# Cert
mkdir -p ${CERT_PATH}
touch ${DOMAIN_FILE}
# Caddy2
mkdir -p ${CADDY_DATA}
touch ${CADDY_CONFIG}
mkdir -p ${CADDY_LOG}
# Nginx
mkdir -p ${NGINX_DATA}
touch ${NGINX_CONFIG}
# MariaDB
mkdir -p ${MARIA_DATA}
# Redis
mkdir -p ${REDIS_DATA}
# Trojan Panel Frontend
mkdir -p ${TROJAN_PANEL_UI_DATA}
# Nginx
mkdir -p ${UI_NGINX_DATA}
touch ${UI_NGINX_CONFIG}
# Trojan Panel Backend
mkdir -p ${TROJAN_PANEL_DATA}
mkdir -p ${TROJAN_PANEL_LOGS}
# Trojan Panel Core
mkdir -p ${TROJAN_PANEL_CORE_DATA}
mkdir -p ${TROJAN_PANEL_CORE_LOGS}
}
can_connect() {
ping -c2 -i0.3 -W1 "$1" &>/dev/null
if [[ "$?" == "0" ]]; then
return 0
else
return 1
fi
}
# query .ini configuration file information
get_ini_value() {
local config_file="$1"
local key="$2"
local section=""
local section_flag=0
# split group and key names
IFS='.' read -r group_name key_name <<<"$key"
while IFS='=' read -r name val; do
# processing section name
if [[ $name =~ ^\[(.*)\]$ ]]; then
section="${BASH_REMATCH[1]}"
if [[ $section == $group_name ]]; then
section_flag=1
else
section_flag=0
fi
continue
fi
# extract the value of the configuration item
if [[ $section_flag -eq 1 && $name == $key_name ]]; then
echo "$val"
return
fi
done <"$config_file"
}
# Version number comparison greater than or equal to
version_ge() {
local v1=${1#v}
local v2=${2#v}
local v1_parts=(${v1//./ })
local v2_parts=(${v2//./ })
for ((i = 0; i < 3; i++)); do
if ((${v1_parts[i]} < ${v2_parts[i]})); then
echo false
return 0
elif ((${v1_parts[i]} > ${v2_parts[i]})); then
echo true
return 0
fi
done
echo true
}
check_sys() {
if [[ $(command -v yum) ]]; then
package_manager='yum'
elif [[ $(command -v dnf) ]]; then
package_manager='dnf'
elif [[ $(command -v apt) ]]; then
package_manager='apt'
elif [[ $(command -v apt-get) ]]; then
package_manager='apt-get'
fi
if [[ -z "${package_manager}" ]]; then
echo_content red "The system is not currently supported"
exit 0
fi
if [[ -n $(find /etc -name "redhat-release") ]] || grep </proc/version -q -i "centos"; then
release="centos"
elif grep </etc/issue -q -i "debian" && [[ -f "/etc/issue" ]] || grep </etc/issue -q -i "debian" && [[ -f "/proc/version" ]]; then
release="debian"
elif grep </etc/issue -q -i "ubuntu" && [[ -f "/etc/issue" ]] || grep </etc/issue -q -i "ubuntu" && [[ -f "/proc/version" ]]; then
release="ubuntu"
fi
if [[ -z "${release}" ]]; then
echo_content red "The operating system only supports CentOS 7+/Ubuntu 18+/Debian 10+"
exit 0
fi
if [[ $(arch) =~ ("x86_64"|"amd64"|"arm64"|"aarch64"|"arm"|"s390x") ]]; then
get_arch=$(arch)
fi
if [[ -z "${get_arch}" ]]; then
echo_content red "The processor architecture only supports amd64/arm64/arm/s390x"
exit 0
fi
can_connect www.google.com
[[ "$?" == "0" ]] && can_google=1
}
depend_install() {
if [[ "${package_manager}" != 'yum' && "${package_manager}" != 'dnf' ]]; then
${package_manager} update -y
fi
${package_manager} install -y \
curl \
wget \
tar \
lsof \
systemd
}
# Install Docker
install_docker() {
if [[ ! $(docker -v 2>/dev/null) ]]; then
echo_content green "---> Install Docker"
# turn off firewall
if [[ "${release}" == "centos" ]]; then
systemctl disable firewalld
elif [[ "${release}" == "debian" || "${release}" == "ubuntu" ]]; then
sudo ufw disable
fi
# set time zone
timedatectl set-timezone Asia/Shanghai
if [[ ${can_google} == 0 ]]; then
sh <(curl -sL https://get.docker.com) --mirror Aliyun
mkdir -p /etc/docker &&
cat >/etc/docker/daemon.json <<EOF
{
"registry-mirrors":[${DOCKER_MIRROR}],
"log-driver":"json-file",
"log-opts":{
"max-size":"50m",
"max-file":"3"
}
}
EOF
else
sh <(curl -sL https://get.docker.com)
mkdir -p /etc/docker &&
cat >/etc/docker/daemon.json <<EOF
{
"log-driver":"json-file",
"log-opts":{
"max-size":"50m",
"max-file":"3"
}
}
EOF
fi
systemctl enable docker &&
systemctl restart docker
if [[ $(docker -v 2>/dev/null) ]]; then
echo_content skyBlue "---> Docker installation completed"
else
echo_content red "---> Docker installation failed"
exit 0
fi
else
echo_content skyBlue "---> You have installed Docker"
fi
}
# Custom Settings Certificate
install_custom_cert() {
if [[ -z "$(cat "${DOMAIN_FILE}")" ]]; then
while read -r -p "Please enter the file path of the .crt certificate (required): " crt_path; do
if [[ -z "${crt_path}" ]]; then
echo_content red "Path cannot be empty"
else
if [[ ! -f "${crt_path}" ]]; then
echo_content red "The file path for the .crt certificate does not exist"
else
cp "${crt_path}" "${CERT_PATH}$1.crt"
break
fi
fi
done
while read -r -p "Please enter the file path of the .key certificate (required): " key_path; do
if [[ -z "${key_path}" ]]; then
echo_content red "Path cannot be empty"
else
if [[ ! -f "${key_path}" ]]; then
echo_content red "The file path for the .key certificate does not exist"
else
cp "${key_path}" "${CERT_PATH}$1.key"
break
fi
fi
done
cat >${DOMAIN_FILE} <<EOF
$1
EOF
echo_content red "\n=============================================================="
echo_content skyBlue "---> Custom settings certificate installation completed"
echo_content yellow "Certificate Directory: ${CERT_PATH}"
echo_content red "\n=============================================================="
fi
}
# Caddy2 https custom settings certificate configuration file
caddy2_https_config() {
domain=$1
cat >${CADDY_CONFIG} <<EOF
{
"admin":{
"disabled":true
},
"logging":{
"logs":{
"default":{
"writer":{
"output":"file",
"filename":"${CADDY_LOG}error.log"
},
"level":"ERROR"
}
}
},
"storage":{
"module":"file_system",
"root":"${CERT_PATH}"
},
"apps":{
"http":{
"http_port": ${caddy_port},
"servers":{
"srv0":{
"listen":[
":${caddy_port}"
],
"routes":[
{
"match":[
{
"host":[
"${domain}"
]
}
],
"handle":[
{
"handler":"static_response",
"headers":{
"Location":[
"https://{http.request.host}:${caddy_remote_port}{http.request.uri}"
]
},
"status_code":301
}
]
}
]
},
"srv1":{
"listen":[
":${caddy_remote_port}"
],
"routes":[
{
"handle":[
{
"handler":"subroute",
"routes":[
{
"match":[
{
"host":[
"${domain}"
]
}
],
"handle":[
{
"handler":"file_server",
"root":"${WEB_PATH}",
"index_names":[
"index.html",
"index.htm"
]
}
],
"terminal":true
}
]
}
]
}
],
"tls_connection_policies":[
{
"match":{
"sni":[
"${domain}"
]
}
}
],
"automatic_https":{
"disable":true
}
}
}
},
"tls":{
"certificates":{
"automate":[
"${domain}"
],
"load_files":[
{
"certificate":"${CADDY_CERT_DIR}${domain}/${domain}.crt",
"key":"${CADDY_CERT_DIR}${domain}/${domain}.key"
}
]
},
"automation":{
"policies":[
{
"issuers":[
{
"module":"${ssl_module}",
"email":"${your_email}"
}
]
}
]
}
}
}
}
EOF
}
# Caddy2 https automatic application and renewal certificate configuration file
caddy2_https_auto_config() {
domain=$1
cat >${CADDY_CONFIG} <<EOF
{
"admin":{
"disabled":true
},
"logging":{
"logs":{
"default":{
"writer":{
"output":"file",
"filename":"${CADDY_LOG}error.log"
},
"level":"ERROR"
}
}
},
"storage":{
"module":"file_system",
"root":"${CERT_PATH}"
},
"apps":{
"http":{
"http_port": ${caddy_port},
"servers":{
"srv0":{
"listen":[
":${caddy_port}"
],
"routes":[
{
"match":[
{
"host":[
"${domain}"
]
}
],
"handle":[
{
"handler":"static_response",
"headers":{
"Location":[
"https://{http.request.host}:${caddy_remote_port}{http.request.uri}"
]
},
"status_code":301
}
]
}
]
},
"srv1":{
"listen":[
":${caddy_remote_port}"
],
"routes":[
{
"handle":[
{
"handler":"subroute",
"routes":[
{
"match":[
{
"host":[
"${domain}"
]
}
],
"handle":[
{
"handler":"file_server",
"root":"${WEB_PATH}",
"index_names":[
"index.html",
"index.htm"
]
}
],
"terminal":true
}
]
}
]
}
],
"tls_connection_policies":[
{
"match":{
"sni":[
"${domain}"
]
}
}
],
"automatic_https":{
"disable":true
}
}
}
},
"tls":{
"certificates":{
"automate":[
"${domain}"
]
},
"automation":{
"policies":[
{
"issuers":[
{
"module":"${ssl_module}",
"email":"${your_email}"
}
]
}
]
}
}
}
}
EOF
}
# Install Caddy2
install_caddy2() {
if [[ -z $(docker ps -a -q -f "name=^trojan-panel-caddy$") ]]; then
echo_content green "---> Install Caddy2+https"
wget --no-check-certificate -O ${WEB_PATH}html.tar.gz -N ${STATIC_HTML} &&
tar -zxvf ${WEB_PATH}html.tar.gz -k -C ${WEB_PATH}
read -r -p "Please enter the port of Caddy2 (default: 80): " caddy_port
[[ -z "${caddy_port}" ]] && caddy_port=80
read -r -p "Please enter the forwarding port of Caddy2 (default: 8863): " caddy_remote_port
[[ -z "${caddy_remote_port}" ]] && caddy_remote_port=8863
echo_content yellow "Tip: Please confirm that the domain name has been resolved to this machine, otherwise the installation may fail"
while read -r -p "Please enter your domain name (required): " domain; do
if [[ -z "${domain}" ]]; then
echo_content red "Domain name cannot be empty"
else
break
fi
done
read -r -p "Please enter your email (optional): " your_email
while read -r -p "Please choose the way to set up the certificate? (1/automatically apply for and renew the certificate 2/manually set the certificate path default: 1: " ssl_option; do
if [[ -z ${ssl_option} || ${ssl_option} == 1 ]]; then
while read -r -p "Please choose the way to apply for the certificate (1/acme 2/zerossl default: 1: " ssl_module_type; do
if [[ -z "${ssl_module_type}" || ${ssl_module_type} == 1 ]]; then
ssl_module="acme"
CADDY_CERT_DIR="${CERT_PATH}certificates/acme-v02.api.letsencrypt.org-directory/"
break
elif [[ ${ssl_module_type} == 2 ]]; then
ssl_module="zerossl"
CADDY_CERT_DIR="${CERT_PATH}certificates/acme.zerossl.com-v2-dv90/"
break
else
echo_content red "Cannot enter other characters except 1 and 2"
fi
done
caddy2_https_auto_config "${domain}"
break
elif [[ ${ssl_option} == 2 ]]; then
install_custom_cert "${domain}"
caddy2_https_config "${domain}"
break
else
echo_content red "Cannot enter other characters except 1 and 2"
fi
done
# Caddy2 temporary listening port for automatic certificate application
if [[ -n $(lsof -i:${caddy_port},${caddy_remote_port} -t) ]]; then
kill -9 "$(lsof -i:${caddy_port},${caddy_remote_port} -t)"
fi
docker pull caddy:2.6.2 &&
docker run -d --name trojan-panel-caddy --restart always \
--network=host \
-v "${CADDY_CONFIG}":"${CADDY_CONFIG}" \
-v ${CERT_PATH}:"${CADDY_CERT_DIR}${domain}/" \
-v ${WEB_PATH}:${WEB_PATH} \
-v ${CADDY_LOG}:${CADDY_LOG} \
caddy:2.6.2 caddy run --config ${CADDY_CONFIG}
cat >${DOMAIN_FILE} <<EOF
${domain}
EOF
if [[ -n $(docker ps -q -f "name=^trojan-panel-caddy$" -f "status=running") ]]; then
echo_content red "\n=============================================================="
echo_content skyBlue "---> Caddy2+https installation completed"
echo_content yellow "Certificate Directory: ${CERT_PATH}"
echo_content red "\n=============================================================="
else
echo_content red "---> Caddy2+https installation fails or runs abnormally, please try to repair or uninstall and reinstall"
exit 0
fi
else
echo_content skyBlue "---> You have installed Caddy2+https"
fi
}
# Nginx http configuration file
nginx_http_config() {
cat >${NGINX_CONFIG} <<-EOF
server {
listen ${nginx_port};
server_name localhost;
location / {
root ${WEB_PATH};
index index.html index.htm;
}
error_page 497 http://\$host:${nginx_port}\$request_uri;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
EOF
}
# Nginx https configuration file
nginx_https_config() {
domain=$1
cat >${NGINX_CONFIG} <<-EOF
server {
listen ${nginx_port};
server_name localhost;
return 301 http://\$host:${nginx_remote_port}\$request_uri;
}
server {
listen ${nginx_remote_port} ssl;
server_name localhost;
# force ssl
ssl on;
ssl_certificate ${CERT_PATH}${domain}.crt;
ssl_certificate_key ${CERT_PATH}${domain}.key;
# cache validity period
ssl_session_timeout 5m;
# secure link optional encryption protocol
ssl_protocols TLSv1.3;
# encryption algorithm
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
# use server-side preferred algorithm
ssl_prefer_server_ciphers on;
#access_log /var/log/nginx/host.access.log main;
location / {
root ${WEB_PATH};
index index.html index.htm;
}
#error_page 404 /404.html;
#497 http->https
error_page 497 https://\$host:${nginx_remote_port}\$request_uri;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
EOF
}
# Install Nginx
install_nginx() {
if [[ -z $(docker ps -a -q -f "name=^trojan-panel-nginx$") ]]; then
echo_content green "---> Install Nginx"
wget --no-check-certificate -O ${WEB_PATH}html.tar.gz -N ${STATIC_HTML} &&
tar -zxvf ${WEB_PATH}html.tar.gz -k -C ${WEB_PATH}
read -r -p "Please enter the port of Nginx (default: 80): " nginx_port
[[ -z "${nginx_port}" ]] && nginx_port=80
read -r -p "Please enter the forwarding port of Nginx (default: 8863): " nginx_remote_port
[[ -z "${nginx_remote_port}" ]] && nginx_remote_port=8863
while read -r -p "Please choose whether to enable https in Nginx? (0/off 1/on default: 1): " nginx_https; do
if [[ -z ${nginx_https} || ${nginx_https} == 1 ]]; then
install_custom_cert "custom_cert"
nginx_https_config "custom_cert"
break
elif [[ ${nginx_https} == 0 ]]; then
nginx_http_config
break
else
echo_content red "Cannot enter other characters except 1 and 2"
fi
done
docker pull nginx:1.20-alpine &&
docker run -d --name trojan-panel-nginx --restart always \
--network=host \
-v "${NGINX_CONFIG}":"/etc/nginx/conf.d/default.conf" \
-v ${CERT_PATH}:${CERT_PATH} \
-v ${WEB_PATH}:${WEB_PATH} \
nginx:1.20-alpine
if [[ -n $(docker ps -q -f "name=^trojan-panel-nginx$" -f "status=running") ]]; then
echo_content skyBlue "---> Nginx installation completed"
else
echo_content red "---> Nginx installation fails or runs abnormally, please try to repair or uninstall and reinstall"
exit 0
fi
else
echo_content skyBlue "---> You have installed Nginx"
fi
}
# Install a web server
install_reverse_proxy() {
if [[ -z $(docker ps -a -q -f "name=^trojan-panel-caddy$|^trojan-panel-nginx$") ]]; then
echo_content green "---> Install a web server"
while :; do
echo_content yellow "1. Install Caddy2+https (recommend)"
echo_content yellow "2. Install Nginx"
echo_content yellow "3. Not install"
read -r -p "Please select (default: 1): " whether_install_reverse_proxy
[[ -z "${whether_install_reverse_proxy}" ]] && whether_install_reverse_proxy=1
case ${whether_install_reverse_proxy} in
1)
install_caddy2
break
;;
2)
install_nginx
break
;;
3)
break
;;
*)
echo_content red "No such option"
continue
;;
esac
done
echo_content skyBlue "---> Web server installation completed"
fi
}
# Set certificate
install_cert() {
if [[ -z "$(cat "${DOMAIN_FILE}")" ]]; then
echo_content green "---> Set certificate"
while :; do
echo_content yellow "1. Custom certificate"
echo_content yellow "2. Not set"
read -r -p "Please select (default: 1): " whether_install_cert
[[ -z "${whether_install_cert}" ]] && whether_install_cert=1
case ${whether_install_cert} in
1)
install_custom_cert "custom_cert"
break
;;
2)
break
;;
*)
echo_content red "No such option"
continue
;;
esac
done
echo_content green "---> Certificate setup completed"
fi
}
# Install MariaDB
install_mariadb() {
if [[ -z $(docker ps -a -q -f "name=^trojan-panel-mariadb$") ]]; then
echo_content green "---> Install MariaDB"
read -r -p "Please enter the port of MariaDB (default: 9507): " mariadb_port
[[ -z "${mariadb_port}" ]] && mariadb_port=9507
read -r -p "Please enter the username of MariaDB (default: root): " mariadb_user
[[ -z "${mariadb_user}" ]] && mariadb_user="root"
while read -r -p "Please enter the password of MariaDB (required): " mariadb_pas; do
if [[ -z "${mariadb_pas}" ]]; then
echo_content red "Password can not be empty"
else
break
fi
done
if [[ "${mariadb_user}" == "root" ]]; then
docker pull mariadb:10.7.3 &&
docker run -d --name trojan-panel-mariadb --restart always \
--network=host \
-e MYSQL_DATABASE="trojan_panel_db" \
-e MYSQL_ROOT_PASSWORD="${mariadb_pas}" \
-e TZ=Asia/Shanghai \
mariadb:10.7.3 \
--port ${mariadb_port} \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_unicode_ci
else
docker pull mariadb:10.7.3 &&
docker run -d --name trojan-panel-mariadb --restart always \
--network=host \
-e MYSQL_DATABASE="trojan_panel_db" \
-e MYSQL_ROOT_PASSWORD="${mariadb_pas}" \
-e MYSQL_USER="${mariadb_user}" \
-e MYSQL_PASSWORD="${mariadb_pas}" \
-e TZ=Asia/Shanghai \
mariadb:10.7.3 \
--port ${mariadb_port} \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_unicode_ci
fi
if [[ -n $(docker ps -q -f "name=^trojan-panel-mariadb$" -f "status=running") ]]; then
echo_content skyBlue "---> MariaDB installation completed"
echo_content yellow "---> The MariaDB password of root (please keep it safe): ${mariadb_pas}"
if [[ "${mariadb_user}" != "root" ]]; then
echo_content yellow "---> The MariaDB password of ${mariadb_user} (please keep it safe): ${mariadb_pas}"
fi
else
echo_content red "---> MariaDB installation fails or runs abnormally, please try to repair or uninstall and reinstall"
exit 0
fi
else
echo_content skyBlue "---> You have installed MariaDB"
fi
}
# Install Redis
install_redis() {
if [[ -z $(docker ps -a -q -f "name=^trojan-panel-redis$") ]]; then
echo_content green "---> Install Redis"
read -r -p "Please enter the port of Redis (default: 6378): " redis_port
[[ -z "${redis_port}" ]] && redis_port=6378
while read -r -p "Please enter the Redis password (required): " redis_pass; do
if [[ -z "${redis_pass}" ]]; then
echo_content red "Password can not be empty"
else
break
fi
done
docker pull redis:6.2.7 &&
docker run -d --name trojan-panel-redis --restart always \
--network=host \
redis:6.2.7 \
redis-server --requirepass "${redis_pass}" --port "${redis_port}"
if [[ -n $(docker ps -q -f "name=^trojan-panel-redis$" -f "status=running") ]]; then
echo_content skyBlue "---> Redis installation completed"
echo_content yellow "---> Redis password (please keep it safe): ${redis_pass}"
else
echo_content red "---> Redis installation fails or runs abnormally, please try to repair or uninstall and reinstall"
exit 0
fi
else
echo_content skyBlue "---> You have installed Redis"
fi
}
# Trojan Panel Frontend Nginx http configuration file
ui_http_config() {