-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_script.sh
1147 lines (1010 loc) · 38.8 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: v1.3.1
# 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://registry.docker-cn.com","https://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn"'
# Project Directory
TP_DATA="/tpdata/"
STATIC_HTML="https://github.com/trojanpanel/install-script/releases/download/v1.0.0/html.tar.gz"
# Caddy
CADDY_DATA="/tpdata/caddy/"
CADDY_Caddyfile="/tpdata/caddy/Caddyfile"
CADDY_SRV="/tpdata/caddy/srv/"
CADDY_ACME="/tpdata/caddy/acme/"
DOMAIN_FILE="/tpdata/caddy/domain.lock"
domain=""
caddy_remote_port=8863
your_email="[email protected]"
crt_path=""
key_path=""
ssl_option=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
TROJAN_PANEL_DATA="/tpdata/trojan-panel/"
TROJAN_PANEL_WEBFILE="/tpdata/trojan-panel/webfile/"
TROJAN_PANEL_LOGS="/tpdata/trojan-panel/logs/"
# Trojan Panel UI
TROJAN_PANEL_UI_DATA="/tpdata/trojan-panel-ui/"
# Nginx
NGINX_DATA="/tpdata/nginx/"
NGINX_CONFIG="/tpdata/nginx/default.conf"
trojan_panel_ui_port=8888
https_enable=1
# Trojan Panel Core
TROJAN_PANEL_CORE_DATA="/tpdata/trojan-panel-core/"
TROJAN_PANEL_CORE_LOGS="/tpdata/trojan-panel-core/logs/"
database="trojan_panel_db"
account_table="account"
}
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() {
# 项目目录
mkdir -p ${TP_DATA}
# Caddy
mkdir -p ${CADDY_DATA}
touch ${CADDY_Caddyfile}
mkdir -p ${CADDY_SRV}
mkdir -p ${CADDY_ACME}
# MariaDB
mkdir -p ${MARIA_DATA}
# Redis
mkdir -p ${REDIS_DATA}
# Trojan Panel
mkdir -p ${TROJAN_PANEL_DATA}
mkdir -p ${TROJAN_PANEL_LOGS}
# Trojan Panel UI
mkdir -p ${TROJAN_PANEL_UI_DATA}
# # Nginx
mkdir -p ${NGINX_DATA}
touch ${NGINX_CONFIG}
# 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
}
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 supported at this time"
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 "Only CentOS 7+/Ubuntu 18+/Debian 10+ systems are supported"
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 "Only support amd64/arm64/arm/s390x processor architecture"
exit 0
fi
}
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 [[ "$(firewall-cmd --state 2>/dev/null)" == "running" ]]; then
systemctl stop firewalld.service && systemctl disable firewalld.service
fi
# Time zone
timedatectl set-timezone Asia/Shanghai
can_connect www.google.com
[[ "$?" == "0" ]] && can_google=1
if [[ ${can_google} == 0 ]]; then
sh <(curl -sL https://get.docker.com) --mirror Aliyun
# Setting up Docker domestic sources
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)
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 already have Docker installed"
fi
}
# 安装Caddy TLS
install_caddy_tls() {
if [[ -z $(docker ps -a -q -f "name=^trojan-panel-caddy$") ]]; then
echo_content green "---> Installing Caddy TLS"
wget --no-check-certificate -O ${CADDY_DATA}html.tar.gz ${STATIC_HTML} &&
tar -zxvf ${CADDY_DATA}html.tar.gz -C ${CADDY_SRV}
read -r -p "Please enter the forwarding port of Caddy (for certificate application, default: 8863): " caddy_remote_port
[[ -z "${caddy_remote_port}" ]] && caddy_remote_port=8863
while read -r -p "Enter your domain name (required): " domain; do
if [[ -z "${domain}" ]]; then
echo_content red "Domain name cannot be empty"
else
break
fi
done
mkdir "${CADDY_ACME}${domain}"
while read -r -p "Please select the way to set the certificate? (1/Automatic application and renewal of certificate 2/Manual setting of certificate path Default:1/Automatic application and renewal of certificate): " ssl_option; do
if [[ -z ${ssl_option} || ${ssl_option} == 1 ]]; then
echo_content yellow "Domain name is being detected, please wait..."
ping_ip=$(ping "${domain}" -s1 -c1 | grep "ttl=" | head -n1 | cut -d"(" -f2 | cut -d")" -f1)
curl_ip=$(curl ifconfig.io)
if [[ "${ping_ip}" != "${curl_ip}" ]]; then
echo_content yellow "Your domain name is not resolved to the local IP, please try again later"
echo_content red "---> Caddy installation failed"
exit 0
fi
read -r -p "Please enter your email address (for certificate application, default: [email protected]): " your_email
[[ -z "${your_email}" ]] && your_email="[email protected]"
cat >${CADDY_Caddyfile} <<EOF
http://${domain}:80 {
redir https://${domain}:${caddy_remote_port}{url}
}
https://${domain}:${caddy_remote_port} {
gzip
tls ${your_email}
root ${CADDY_SRV}
}
EOF
break
else
if [[ ${ssl_option} != 2 ]]; then
echo_content red "No characters other than 1 and 2 can be entered"
else
while read -r -p "Please enter the .crt file path of the 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 .crt file path for the certificate does not exist"
else
cp "${crt_path}" "${CADDY_ACME}${domain}/${domain}.crt"
break
fi
fi
done
while read -r -p "Please enter the .key file path of the 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 .key file path of the certificate does not exist"
else
cp "${key_path}" "${CADDY_ACME}${domain}/${domain}.key"
break
fi
fi
done
cat >${CADDY_Caddyfile} <<EOF
http://${domain}:80 {
redir https://${domain}:${caddy_remote_port}{url}
}
https://${domain}:${caddy_remote_port} {
gzip
tls /root/.caddy/acme/acme-v02.api.letsencrypt.org/sites/${domain}/${domain}.crt /root/.caddy/acme/acme-v02.api.letsencrypt.org/sites/${domain}/${domain}.key
root ${CADDY_SRV}
}
EOF
break
fi
fi
done
if [[ -n $(lsof -i:80,443 -t) ]]; then
kill -9 "$(lsof -i:80,443 -t)"
fi
docker pull teddysun/caddy:1.0.5 &&
docker run -d --name trojan-panel-caddy --restart always \
--network=host \
-v ${CADDY_Caddyfile}:"/etc/caddy/Caddyfile" \
-v ${CADDY_ACME}:"/root/.caddy/acme/acme-v02.api.letsencrypt.org/sites/" \
-v ${CADDY_SRV}:${CADDY_SRV} \
teddysun/caddy:1.0.5
if [[ -n $(docker ps -q -f "name=^trojan-panel-caddy$" -f "status=running") ]]; then
cat >${DOMAIN_FILE} <<EOF
${domain}
EOF
echo_content skyBlue "---> Caddy installation complete"
else
echo_content red "---> Caddy installation fails or runs abnormally, please try to repair or uninstall and reinstall"
exit 0
fi
else
domain=$(cat "${DOMAIN_FILE}")
echo_content skyBlue "---> You have installed Caddy"
fi
}
# 安装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 the database (default: 9507): " mariadb_port
[[ -z "${mariadb_port}" ]] && mariadb_port=9507
read -r -p "Please enter the username of the database (default: root): " mariadb_user
[[ -z "${mariadb_user}" ]] && mariadb_user="root"
while read -r -p "Please enter the password for the database (required): " mariadb_pas; do
if [[ -z "${mariadb_pas}" ]]; then
echo_content red "Password cannot 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}
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}
fi
if [[ -n $(docker ps -q -f "name=^trojan-panel-mariadb$" -f "status=running") ]]; then
echo_content skyBlue "---> MariaDB installation completed"
echo_content yellow "---> MariaDB root's database password (please save it properly): ${mariadb_pas}"
if [[ "${mariadb_user}" != "root" ]]; then
echo_content yellow "---> MariaDB ${mariadb_user}The password for the database (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 already have MariaDB installed"
fi
}
# 安装Redis
install_redis() {
if [[ -z $(docker ps -a -q -f "name=^trojan-panel-redis$") ]]; then
echo_content green "---> Installing 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 your Redis password (required): " redis_pass; do
if [[ -z "${redis_pass}" ]]; then
echo_content red "Password cannot 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 complete"
echo_content yellow "---> Database password for Redis (please keep it safe): ${redis_pass}"
else
echo_content red "---> Redis installation failed or is running abnormally, please try to repair or uninstall and reinstall"
exit 0
fi
else
echo_content skyBlue "---> You already have Redis installed"
fi
}
# 安装TrojanPanel
install_trojan_panel() {
if [[ -z $(docker ps -a -q -f "name=^trojan-panel$") ]]; then
echo_content green "---> Installing Trojan Panel"
read -r -p "Please enter the IP address of the database (default: local database): " mariadb_ip
[[ -z "${mariadb_ip}" ]] && mariadb_ip="127.0.0.1"
read -r -p "Please enter the port of the database (default: 9507): " mariadb_port
[[ -z "${mariadb_port}" ]] && mariadb_port=9507
read -r -p "Please enter the username of the database (default: root): " mariadb_user
[[ -z "${mariadb_user}" ]] && mariadb_user="root"
while read -r -p "Please enter the password for the database (required): " mariadb_pas; do
if [[ -z "${mariadb_pas}" ]]; then
echo_content red "Password cannot be empty"
else
break
fi
done
if [[ "${mariadb_ip}" == "127.0.0.1" ]]; then
docker exec trojan-panel-mariadb mysql -p"${mariadb_pas}" -e "drop database trojan_panel_db;" &&
docker exec trojan-panel-mariadb mysql -p"${mariadb_pas}" -e "create database trojan_panel_db;"
else
docker exec trojan-panel-mariadb mysql -h"${mariadb_ip}" -P"${mariadb_port}" -u"${mariadb_user}" -p"${mariadb_pas}" -e "drop database trojan_panel_db;" &>/dev/null &&
docker exec trojan-panel-mariadb mysql -h"${mariadb_ip}" -P"${mariadb_port}" -u"${mariadb_user}" -p"${mariadb_pas}" -e "create database trojan_panel_db;" &>/dev/null
fi
read -r -p "Please enter the IP address of Redis (default: local Redis): " redis_host
[[ -z "${redis_host}" ]] && redis_host="127.0.0.1"
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 your Redis password (required): " redis_pass; do
if [[ -z "${redis_pass}" ]]; then
echo_content red "Password cannot be empty"
else
break
fi
done
if [[ "${redis_host}" == "127.0.0.1" ]]; then
docker exec trojan-panel-redis redis-cli -a "${redis_pass}" -e "flushall" &>/dev/null
else
docker exec trojan-panel-redis redis-cli -h "${redis_host}" -p ${redis_port} -a "${redis_pass}" -e "flushall" &>/dev/null
fi
docker pull jonssonyan/trojan-panel &&
docker run -d --name trojan-panel --restart always \
--network=host \
-v ${CADDY_SRV}:${TROJAN_PANEL_WEBFILE} \
-v ${TROJAN_PANEL_LOGS}:${TROJAN_PANEL_LOGS} \
-v /etc/localtime:/etc/localtime \
-e "mariadb_ip=${mariadb_ip}" \
-e "mariadb_port=${mariadb_port}" \
-e "mariadb_user=${mariadb_user}" \
-e "mariadb_pas=${mariadb_pas}" \
-e "redis_host=${redis_host}" \
-e "redis_port=${redis_port}" \
-e "redis_pass=${redis_pass}" \
jonssonyan/trojan-panel
if [[ -n $(docker ps -q -f "name=^trojan-panel$" -f "status=running") ]]; then
echo_content skyBlue "---> Trojan Panel backend installation completed"
else
echo_content red "---> Trojan Panel backend failed to install or run abnormally, please try to repair or uninstall and reinstall"
exit 0
fi
else
echo_content skyBlue "---> You have installed the Trojan Panel backend"
fi
if [[ -z $(docker ps -a -q -f "name=^trojan-panel-ui$") ]]; then
read -r -p "Please enter the Trojan Panel front-end port (default: 8888): " trojan_panel_ui_port
[[ -z "${trojan_panel_ui_port}" ]] && trojan_panel_ui_port="8888"
while read -r -p "Please select whether Trojan Panel front-end is https enabled or not?(0/off 1/on default:1/on): " https_enable; do
if [[ -z ${https_enable} || ${https_enable} == 1 ]]; then
# 配置Nginx
cat >${NGINX_CONFIG} <<-EOF
server {
listen ${trojan_panel_ui_port} ssl;
server_name ${domain};
#Forced ssl
ssl on;
ssl_certificate ${CADDY_ACME}${domain}/${domain}.crt;
ssl_certificate_key ${CADDY_ACME}${domain}/${domain}.key;
#Cache expiration date
ssl_session_timeout 5m;
#Optional encryption protocol for secure links
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#Encryption algorithms
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
#Use the server-side preferred algorithm
ssl_prefer_server_ciphers on;
#access_log /var/log/nginx/host.access.log main;
location / {
root ${TROJAN_PANEL_UI_DATA};
index index.html index.htm;
}
location /api {
proxy_pass http://127.0.0.1:8081;
}
#error_page 404 /404.html;
#497 http->https
error_page 497 https://\$host:${trojan_panel_ui_port}\$uri?\$args;
# 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
break
else
if [[ ${https_enable} != 0 ]]; then
echo_content red "No characters other than 0 and 1 can be entered"
else
cat >${NGINX_CONFIG} <<-EOF
server {
listen ${trojan_panel_ui_port};
server_name localhost;
location / {
root ${TROJAN_PANEL_UI_DATA};
index index.html index.htm;
}
location /api {
proxy_pass http://127.0.0.1:8081;
}
error_page 497 http://\$host:${trojan_panel_ui_port}\$uri?\$args;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
EOF
break
fi
fi
done
docker pull jonssonyan/trojan-panel-ui &&
docker run -d --name trojan-panel-ui --restart always \
--network=host \
-v ${NGINX_CONFIG}:/etc/nginx/conf.d/default.conf \
-v ${CADDY_ACME}"${domain}":${CADDY_ACME}"${domain}" \
jonssonyan/trojan-panel-ui
if [[ -n $(docker ps -q -f "name=^trojan-panel-ui$" -f "status=running") ]]; then
echo_content skyBlue "---> Trojan Panel front-end installation completed"
else
echo_content red "---> Trojan Panel front-end installation fails or runs abnormally, please try to repair or uninstall and reinstall"
exit 0
fi
else
echo_content skyBlue "---> You have installed the Trojan Panel frontend"
fi
https_flag=$([[ -z ${https_enable} || ${https_enable} == 1 ]] && echo "https" || echo "http")
echo_content red "\n=============================================================="
echo_content skyBlue "Trojan Panel installed successfully"
echo_content yellow "MariaDB ${mariadb_user}password (please keep it safe): ${mariadb_pas}"
echo_content yellow "Redis password (please keep it safe): ${redis_pass}"
echo_content yellow "Admin panel address: ${https_flag}://${domain}:${trojan_panel_ui_port}"
echo_content yellow "System administrator Default username: sysadmin Default password: 123456 Please log in the management panel to change your password in time"
echo_content yellow "Trojan Panel Private Key and Certificate Catalog: ${CADDY_ACME}${domain}/"
echo_content red "\n=============================================================="
}
# 安装Trojan Panel Core
install_trojan_panel_core() {
if [[ -z $(docker ps -a -q -f "name=^trojan-panel-core$") ]]; then
echo_content green "---> Installing Trojan Panel Core"
read -r -p "Please enter the IP address of the database (default: local database): " mariadb_ip
[[ -z "${mariadb_ip}" ]] && mariadb_ip="127.0.0.1"
read -r -p "Please enter the port of the database (default: 9507): " mariadb_port
[[ -z "${mariadb_port}" ]] && mariadb_port=9507
read -r -p "Please enter the username of the database (default: root): " mariadb_user
[[ -z "${mariadb_user}" ]] && mariadb_user="root"
while read -r -p "Please enter the password for the database (required): " mariadb_pas; do
if [[ -z "${mariadb_pas}" ]]; then
echo_content red "Password cannot be empty"
else
break
fi
done
read -r -p "Please enter the database name (default: trojan_panel_db): " database
[[ -z "${database}" ]] && database="trojan_panel_db"
read -r -p "Please enter the name of the user table in the database (default: account): " account_table
[[ -z "${account_table}" ]] && account_table="account"
read -r -p "Please enter the IP address of Redis (default: local Redis): " redis_host
[[ -z "${redis_host}" ]] && redis_host="127.0.0.1"
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 your Redis password (required): " redis_pass; do
if [[ -z "${redis_pass}" ]]; then
echo_content red "Password cannot be empty"
else
break
fi
done
domain=$(cat "${DOMAIN_FILE}")
docker pull jonssonyan/trojan-panel-core &&
docker run -d --name trojan-panel-core --restart always \
--network=host \
-v ${TROJAN_PANEL_CORE_DATA}bin/xray/config:${TROJAN_PANEL_CORE_DATA}bin/xray/config \
-v ${TROJAN_PANEL_CORE_DATA}bin/trojango/config:${TROJAN_PANEL_CORE_DATA}bin/trojango/config \
-v ${TROJAN_PANEL_CORE_DATA}bin/hysteria/config:${TROJAN_PANEL_CORE_DATA}bin/hysteria/config \
-v ${TROJAN_PANEL_CORE_DATA}bin/naiveproxy/config:${TROJAN_PANEL_CORE_DATA}bin/naiveproxy/config \
-v ${TROJAN_PANEL_CORE_LOGS}:${TROJAN_PANEL_CORE_LOGS} \
-v ${CADDY_ACME}:${CADDY_ACME} \
-v ${CADDY_SRV}:${CADDY_SRV} \
-v /etc/localtime:/etc/localtime \
-e "mariadb_ip=${mariadb_ip}" \
-e "mariadb_port=${mariadb_port}" \
-e "mariadb_user=${mariadb_user}" \
-e "mariadb_pas=${mariadb_pas}" \
-e "database=${database}" \
-e "account-table=${account_table}" \
-e "redis_host=${redis_host}" \
-e "redis_port=${redis_port}" \
-e "redis_pass=${redis_pass}" \
-e "crt_path=${CADDY_ACME}${domain}/${domain}.crt" \
-e "key_path=${CADDY_ACME}${domain}/${domain}.key" \
jonssonyan/trojan-panel-core
if [[ -n $(docker ps -q -f "name=^trojan-panel-core$" -f "status=running") ]]; then
echo_content skyBlue "---> Trojan Panel Core安装完成"
else
echo_content red "---> Trojan Panel Core backend installation fails or runs abnormally, please try to repair or uninstall and reinstall"
exit 0
fi
else
echo_content skyBlue "---> You have installed Trojan Panel Core"
fi
}
# 更新Trojan Panel
update_trojan_panel() {
# 判断Trojan Panel是否安装
if [[ -z $(docker ps -a -q -f "name=^trojan-panel$") ]]; then
echo_content red "---> Please install Trojan Panel first"
exit 0
fi
echo_content green "---> Update Trojan Panel"
read -r -p "Please enter the IP address of the database (default: local database): " mariadb_ip
[[ -z "${mariadb_ip}" ]] && mariadb_ip="127.0.0.1"
read -r -p "Please enter the port of the database (default: 9507): " mariadb_port
[[ -z "${mariadb_port}" ]] && mariadb_port=9507
read -r -p "Please enter the username of the database (default: root): " mariadb_user
[[ -z "${mariadb_user}" ]] && mariadb_user="root"
while read -r -p "Please enter the password for the database (required): " mariadb_pas; do
if [[ -z "${mariadb_pas}" ]]; then
echo_content red "Password cannot be empty"
else
break
fi
done
if [[ "${mariadb_ip}" == "127.0.0.1" ]]; then
docker exec trojan-panel-mariadb mysql -p"${mariadb_pas}" -e "drop database trojan_panel_db;"
docker exec trojan-panel-mariadb mysql -p"${mariadb_pas}" -e "create database trojan_panel_db;"
else
docker exec trojan-panel-mariadb mysql -h"${mariadb_ip}" -P"${mariadb_port}" -u"${mariadb_user}" -p"${mariadb_pas}" -e "drop database trojan_panel_db;" &>/dev/null
docker exec trojan-panel-mariadb mysql -h"${mariadb_ip}" -P"${mariadb_port}" -u"${mariadb_user}" -p"${mariadb_pas}" -e "create database trojan_panel_db;" &>/dev/null
fi
read -r -p "Please enter the IP address of Redis (default: local Redis): " redis_host
[[ -z "${redis_host}" ]] && redis_host="127.0.0.1"
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 your Redis password (required): " redis_pass; do
if [[ -z "${redis_pass}" ]]; then
echo_content red "Password cannot be empty"
else
break
fi
done
if [[ "${redis_host}" == "127.0.0.1" ]]; then
docker exec trojan-panel-redis redis-cli -a "${redis_pass}" -e "flushall" &>/dev/null
else
docker exec trojan-panel-redis redis-cli -h "${redis_host}" -p ${redis_port} -a "${redis_pass}" -e "flushall" &>/dev/null
fi
docker rm -f trojan-panel &&
docker rmi -f jonssonyan/trojan-panel &&
rm -rf ${TROJAN_PANEL_DATA}
docker rm -f trojan-panel-ui &&
docker rmi -f jonssonyan/trojan-panel-ui &&
rm -rf ${TROJAN_PANEL_UI_DATA}
docker pull jonssonyan/trojan-panel &&
docker run -d --name trojan-panel --restart always \
--network=host \
-v ${CADDY_SRV}:${TROJAN_PANEL_WEBFILE} \
-v ${TROJAN_PANEL_LOGS}:${TROJAN_PANEL_LOGS} \
-v /etc/localtime:/etc/localtime \
-e "mariadb_ip=${mariadb_ip}" \
-e "mariadb_port=${mariadb_port}" \
-e "mariadb_user=${mariadb_user}" \
-e "mariadb_pas=${mariadb_pas}" \
-e "redis_host=${redis_host}" \
-e "redis_port=${redis_port}" \
-e "redis_pass=${redis_pass}" \
jonssonyan/trojan-panel
if [[ -n $(docker ps -q -f "name=^trojan-panel$" -f "status=running") ]]; then
echo_content skyBlue "---> Trojan Panel back-end update completed"
else
echo_content red "---> Trojan Panel backend failed to update or run abnormally, please try to repair or uninstall and reinstall"
fi
docker pull jonssonyan/trojan-panel-ui &&
docker run -d --name trojan-panel-ui --restart always \
--network=host \
-v ${NGINX_CONFIG}:/etc/nginx/conf.d/default.conf \
-v ${CADDY_ACME}"${domain}":${CADDY_ACME}"${domain}" \
jonssonyan/trojan-panel-ui
if [[ -n $(docker ps -q -f "name=^trojan-panel-ui$" -f "status=running") ]]; then
echo_content skyBlue "---> Trojan Panel front-end update completed"
else
echo_content red "---> Trojan Panel front-end update failed or run abnormally, please try to repair or uninstall and reinstall"
fi
}
# 更新Trojan Panel Core
update_trojan_panel_core() {
# 判断Trojan Panel Core是否安装
if [[ -z $(docker ps -a -q -f "name=^trojan-panel-core$") ]]; then
echo_content red "---> Please install Trojan Panel Core first"
exit 0
fi
echo_content green "---> Update Trojan Panel Core"
read -r -p "Please enter the IP address of the database (default: local database): " mariadb_ip
[[ -z "${mariadb_ip}" ]] && mariadb_ip="127.0.0.1"
read -r -p "Please enter the port of the database (default: 9507): " mariadb_port
[[ -z "${mariadb_port}" ]] && mariadb_port=9507
read -r -p "Please enter the username of the database (default: root): " mariadb_user
[[ -z "${mariadb_user}" ]] && mariadb_user="root"
while read -r -p "Please enter the password for the database (required): " mariadb_pas; do
if [[ -z "${mariadb_pas}" ]]; then
echo_content red "Password cannot be empty"
else
break
fi
done
read -r -p "Please enter the database name (default: trojan_panel_db): " database
[[ -z "${database}" ]] && database="trojan_panel_db"
read -r -p "Please enter the name of the user table in the database (default: account): " account_table
[[ -z "${account_table}" ]] && account_table="account"
read -r -p "Please enter the IP address of Redis (default: local Redis): " redis_host
[[ -z "${redis_host}" ]] && redis_host="127.0.0.1"
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 your Redis password (required): " redis_pass; do
if [[ -z "${redis_pass}" ]]; then
echo_content red "Password cannot be empty"
else
break
fi
done
docker rm -f trojan-panel-core &&
docker rmi -f jonssonyan/trojan-panel-core &&
rm -rf ${TROJAN_PANEL_CORE_DATA}
docker pull jonssonyan/trojan-panel-core &&
docker run -d --name trojan-panel-core --restart always \
--network=host \
-v ${TROJAN_PANEL_CORE_DATA}bin:${TROJAN_PANEL_CORE_DATA}bin \
-v ${TROJAN_PANEL_CORE_LOGS}:${TROJAN_PANEL_CORE_LOGS} \
-v ${CADDY_ACME}:${CADDY_ACME} \
-v /etc/localtime:/etc/localtime \
-e "mariadb_ip=${mariadb_ip}" \
-e "mariadb_port=${mariadb_port}" \
-e "mariadb_user=${mariadb_user}" \
-e "mariadb_pas=${mariadb_pas}" \
-e "database=${database}" \
-e "account-table=${account_table}" \
-e "redis_host=${redis_host}" \
-e "redis_port=${redis_port}" \
-e "redis_pass=${redis_pass}" \
jonssonyan/trojan-panel-core
if [[ "$?" == "0" ]]; then
echo_content skyBlue "---> Trojan Panel Core update completed"
else
echo_content red "---> Trojan Panel Core update failed"
fi
}
# 卸载Caddy TLS
uninstall_caddy_tls() {
# 判断Caddy TLS是否安装
if [[ -n $(docker ps -a -q -f "name=^trojan-panel-caddy$") ]]; then
echo_content green "---> Uninstall Caddy TLS"
docker rm -f trojan-panel-caddy &&
rm -rf ${CADDY_DATA}
echo_content skyBlue "---> Caddy TLS uninstallation complete"
else
echo_content red "---> Please install Caddy first TLS"
fi
}
# 卸载MariaDB
uninstall_mariadb() {
# 判断MariaDB是否安装
if [[ -n $(docker ps -a -q -f "name=^trojan-panel-mariadb$") ]]; then
echo_content green "---> Uninstalling MariaDB"
docker rm -f trojan-panel-mariadb &&
rm -rf ${MARIA_DATA}
echo_content skyBlue "---> MariaDB uninstallation completed"
else
echo_content red "---> Please install MariaDB first"
fi
}
# 卸载Redis
uninstall_redis() {
# 判断Redis是否安装
if [[ -n $(docker ps -a -q -f "name=^trojan-panel-redis$") ]]; then
echo_content green "---> Uninstalling Redis"
docker rm -f trojan-panel-redis &&
rm -rf ${REDIS_DATA}
echo_content skyBlue "---> Redis uninstallation complete"
else
echo_content red "---> Please install Redis first"
fi
}
# 卸载Trojan Panel
uninstall_trojan_panel() {
# 判断Trojan Panel是否安装
if [[ -n $(docker ps -a -q -f "name=^trojan-panel$") ]]; then
echo_content green "---> Uninstall Trojan Panel"
docker rm -f trojan-panel &&
docker rmi -f jonssonyan/trojan-panel &&
rm -rf ${TROJAN_PANEL_DATA}
docker rm -f trojan-panel-ui &&
docker rmi -f jonssonyan/trojan-panel-ui &&
rm -rf ${TROJAN_PANEL_UI_DATA} &&
rm -rf ${NGINX_DATA}
echo_content skyBlue "---> Trojan Panel uninstallation is complete"
else
echo_content red "---> Please install Trojan first Panel"
fi
}
# 卸载Trojan Panel Core
uninstall_trojan_panel_core() {
# 判断Trojan Panel Core是否安装
if [[ -n $(docker ps -a -q -f "name=^trojan-panel-core$") ]]; then
echo_content green "---> Uninstall Trojan Panel Core"
docker rm -f trojan-panel-core &&
docker rmi -f jonssonyan/trojan-panel-core &&
rm -rf ${TROJAN_PANEL_CORE_DATA}
echo_content skyBlue "---> Trojan Panel Core uninstallation complete"
else
echo_content red "---> Please install Trojan first Panel Core"
fi
}
# 卸载全部Trojan Panel相关的容器
uninstall_all() {
echo_content green "---> Uninstall all Trojan Panel related containers"
docker rm -f $(docker ps -a -q -f "name=^trojan-panel") &&
docker rmi -f $(docker images | grep "^jonssonyan/trojan-panel" | awk '{print $3}') &&
rm -rf ${TP_DATA}
echo_content skyBlue "---> Uninstall all Trojan Panel related containers to finish"
}
# 刷新Redis缓存
redis_flush_all() {
# 判断Redis是否安装
if [[ -z $(docker ps -a -q -f "name=^trojan-panel-redis$") ]]; then
echo_content red "---> Please install Redis first"
exit 0
fi
if [[ -z $(docker ps -q -f "name=^trojan-panel-redis$" -f "status=running") ]]; then
echo_content red "---> Redis is running abnormally"
exit 0
fi
echo_content green "---> Refresh Redis cache"
read -r -p "Please enter the IP address of Redis (default: local Redis): " redis_host
[[ -z "${redis_host}" ]] && redis_host="127.0.0.1"
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 cannot be empty"
else
break
fi
done
if [[ "${redis_host}" == "127.0.0.1" ]]; then
docker exec trojan-panel-redis redis-cli -a "${redis_pass}" -e "flushall" &>/dev/null
else
docker exec trojan-panel-redis redis-cli -h "${redis_host}" -p ${redis_port} -a "${redis_pass}" -e "flushall" &>/dev/null
fi
echo_content skyBlue "---> Redis cache flush complete"
}
# 故障检测
failure_testing() {
echo_content green "---> Fault detection starting..."
if [[ ! $(docker -v 2>/dev/null) ]]; then
echo_content red "---> Troubleshooting Docker Run Exceptions"
else
if [[ -n $(docker ps -a -q -f "name=^trojan-panel-caddy$") ]]; then
if [[ -z $(docker ps -q -f "name=^trojan-panel-caddy$" -f "status=running") ]]; then
echo_content red "---> Caddy TLS operation exception"
fi
domain=$(cat "${DOMAIN_FILE}")
if [[ -z $(cat "${DOMAIN_FILE}") || ! -d "${CADDY_ACME}${domain}" || ! -f "${CADDY_ACME}${domain}/${domain}.crt" ]]; then
echo_content red "---> The certificate application is abnormal, please try to restart the server to reapply for the certificate or rebuild and select the custom certificate option"
fi
fi