install_script_v2.2.1.sh 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005
  1. #!/usr/bin/env bash
  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  3. export PATH
  4. # System Required: CentOS 7+/Ubuntu 18+/Debian 10+
  5. # Version: v2.2.0
  6. # Description: One click Install Trojan Panel server
  7. # Author: jonssonyan <https://jonssonyan.com>
  8. # Github: https://github.com/trojanpanel/install-script
  9. init_var() {
  10. ECHO_TYPE="echo -e"
  11. package_manager=""
  12. release=""
  13. get_arch=""
  14. can_google=0
  15. # Docker
  16. DOCKER_MIRROR='"https://hub-mirror.c.163.com","https://ccr.ccs.tencentyun.com","https://mirror.baidubce.com","https://dockerproxy.com"'
  17. # Project directory
  18. TP_DATA="/tpdata/"
  19. STATIC_HTML="https://github.com/trojanpanel/install-script/releases/download/v1.0/html.tar.gz"
  20. # Web
  21. WEB_PATH="/tpdata/web/"
  22. # Cert
  23. CERT_PATH="/tpdata/cert/"
  24. DOMAIN_FILE="/tpdata/domain.lock"
  25. domain=""
  26. crt_path=""
  27. key_path=""
  28. # Caddy2
  29. CADDY_DATA="/tpdata/caddy/"
  30. CADDY_CONFIG="${CADDY_DATA}config.json"
  31. CADDY_LOG="${CADDY_DATA}logs/"
  32. CADDY_CERT_DIR="${CERT_PATH}certificates/acme-v02.api.letsencrypt.org-directory/"
  33. caddy_port=80
  34. caddy_remote_port=8863
  35. your_email=""
  36. ssl_option=1
  37. ssl_module_type=1
  38. ssl_module="acme"
  39. # Nginx
  40. NGINX_DATA="/tpdata/nginx/"
  41. NGINX_CONFIG="${NGINX_DATA}default.conf"
  42. nginx_port=80
  43. nginx_remote_port=8863
  44. nginx_https=1
  45. # MariaDB
  46. MARIA_DATA="/tpdata/mariadb/"
  47. mariadb_ip="127.0.0.1"
  48. mariadb_port=9507
  49. mariadb_user="root"
  50. mariadb_pas=""
  51. # Redis
  52. REDIS_DATA="/tpdata/redis/"
  53. redis_host="127.0.0.1"
  54. redis_port=6378
  55. redis_pass=""
  56. # Trojan Panel Frontend
  57. TROJAN_PANEL_UI_DATA="/tpdata/trojan-panel-ui/"
  58. # Nginx
  59. UI_NGINX_DATA="${TROJAN_PANEL_UI_DATA}nginx/"
  60. UI_NGINX_CONFIG="${UI_NGINX_DATA}default.conf"
  61. trojan_panel_ui_port=8888
  62. ui_https=1
  63. trojan_panel_ip="127.0.0.1"
  64. trojan_panel_server_port=8081
  65. # Trojan Panel Backend
  66. TROJAN_PANEL_DATA="/tpdata/trojan-panel/"
  67. TROJAN_PANEL_WEBFILE="${TROJAN_PANEL_DATA}webfile/"
  68. TROJAN_PANEL_LOGS="${TROJAN_PANEL_DATA}logs/"
  69. TROJAN_PANEL_CONFIG="${TROJAN_PANEL_DATA}config/"
  70. trojan_panel_config_path="${TROJAN_PANEL_DATA}config/config.ini"
  71. trojan_panel_port=8081
  72. # Trojan Panel Core
  73. TROJAN_PANEL_CORE_DATA="/tpdata/trojan-panel-core/"
  74. TROJAN_PANEL_CORE_LOGS="${TROJAN_PANEL_CORE_DATA}logs/"
  75. TROJAN_PANEL_CORE_CONFIG="${TROJAN_PANEL_CORE_DATA}config/"
  76. trojan_panel_core_config_path="${TROJAN_PANEL_CORE_DATA}config/config.ini"
  77. database="trojan_panel_db"
  78. account_table="account"
  79. grpc_port=8100
  80. trojan_panel_core_port=8082
  81. # Version
  82. trojan_panel_ui_current_version=""
  83. trojan_panel_ui_latest_version="v2.1.6"
  84. trojan_panel_current_version=""
  85. trojan_panel_latest_version="v2.2.0"
  86. trojan_panel_core_current_version=""
  87. trojan_panel_core_latest_version="v2.2.1"
  88. # SQL
  89. 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;"
  90. }
  91. echo_content() {
  92. case $1 in
  93. "red")
  94. ${ECHO_TYPE} "\033[31m$2\033[0m"
  95. ;;
  96. "green")
  97. ${ECHO_TYPE} "\033[32m$2\033[0m"
  98. ;;
  99. "yellow")
  100. ${ECHO_TYPE} "\033[33m$2\033[0m"
  101. ;;
  102. "blue")
  103. ${ECHO_TYPE} "\033[34m$2\033[0m"
  104. ;;
  105. "purple")
  106. ${ECHO_TYPE} "\033[35m$2\033[0m"
  107. ;;
  108. "skyBlue")
  109. ${ECHO_TYPE} "\033[36m$2\033[0m"
  110. ;;
  111. "white")
  112. ${ECHO_TYPE} "\033[37m$2\033[0m"
  113. ;;
  114. esac
  115. }
  116. mkdir_tools() {
  117. # Project directory
  118. mkdir -p ${TP_DATA}
  119. # Web
  120. mkdir -p ${WEB_PATH}
  121. # Cert
  122. mkdir -p ${CERT_PATH}
  123. touch ${DOMAIN_FILE}
  124. # Caddy2
  125. mkdir -p ${CADDY_DATA}
  126. touch ${CADDY_CONFIG}
  127. mkdir -p ${CADDY_LOG}
  128. # Nginx
  129. mkdir -p ${NGINX_DATA}
  130. touch ${NGINX_CONFIG}
  131. # MariaDB
  132. mkdir -p ${MARIA_DATA}
  133. # Redis
  134. mkdir -p ${REDIS_DATA}
  135. # Trojan Panel Frontend
  136. mkdir -p ${TROJAN_PANEL_UI_DATA}
  137. # Nginx
  138. mkdir -p ${UI_NGINX_DATA}
  139. touch ${UI_NGINX_CONFIG}
  140. # Trojan Panel Backend
  141. mkdir -p ${TROJAN_PANEL_DATA}
  142. mkdir -p ${TROJAN_PANEL_LOGS}
  143. # Trojan Panel Core
  144. mkdir -p ${TROJAN_PANEL_CORE_DATA}
  145. mkdir -p ${TROJAN_PANEL_CORE_LOGS}
  146. }
  147. can_connect() {
  148. ping -c2 -i0.3 -W1 "$1" &>/dev/null
  149. if [[ "$?" == "0" ]]; then
  150. return 0
  151. else
  152. return 1
  153. fi
  154. }
  155. # query .ini configuration file information
  156. get_ini_value() {
  157. local config_file="$1"
  158. local key="$2"
  159. local section=""
  160. local section_flag=0
  161. # split group and key names
  162. IFS='.' read -r group_name key_name <<<"$key"
  163. while IFS='=' read -r name val; do
  164. # processing section name
  165. if [[ $name =~ ^\[(.*)\]$ ]]; then
  166. section="${BASH_REMATCH[1]}"
  167. if [[ $section == $group_name ]]; then
  168. section_flag=1
  169. else
  170. section_flag=0
  171. fi
  172. continue
  173. fi
  174. # extract the value of the configuration item
  175. if [[ $section_flag -eq 1 && $name == $key_name ]]; then
  176. echo "$val"
  177. return
  178. fi
  179. done <"$config_file"
  180. }
  181. # Version number comparison greater than or equal to
  182. version_ge() {
  183. local v1=${1#v}
  184. local v2=${2#v}
  185. local v1_parts=(${v1//./ })
  186. local v2_parts=(${v2//./ })
  187. for ((i = 0; i < 3; i++)); do
  188. if ((${v1_parts[i]} < ${v2_parts[i]})); then
  189. echo false
  190. return 0
  191. elif ((${v1_parts[i]} > ${v2_parts[i]})); then
  192. echo true
  193. return 0
  194. fi
  195. done
  196. echo true
  197. }
  198. check_sys() {
  199. if [[ $(command -v yum) ]]; then
  200. package_manager='yum'
  201. elif [[ $(command -v dnf) ]]; then
  202. package_manager='dnf'
  203. elif [[ $(command -v apt) ]]; then
  204. package_manager='apt'
  205. elif [[ $(command -v apt-get) ]]; then
  206. package_manager='apt-get'
  207. fi
  208. if [[ -z "${package_manager}" ]]; then
  209. echo_content red "The system is not currently supported"
  210. exit 0
  211. fi
  212. if [[ -n $(find /etc -name "redhat-release") ]] || grep </proc/version -q -i "centos"; then
  213. release="centos"
  214. elif grep </etc/issue -q -i "debian" && [[ -f "/etc/issue" ]] || grep </etc/issue -q -i "debian" && [[ -f "/proc/version" ]]; then
  215. release="debian"
  216. elif grep </etc/issue -q -i "ubuntu" && [[ -f "/etc/issue" ]] || grep </etc/issue -q -i "ubuntu" && [[ -f "/proc/version" ]]; then
  217. release="ubuntu"
  218. fi
  219. if [[ -z "${release}" ]]; then
  220. echo_content red "The operating system only supports CentOS 7+/Ubuntu 18+/Debian 10+"
  221. exit 0
  222. fi
  223. if [[ $(arch) =~ ("x86_64"|"amd64"|"arm64"|"aarch64"|"arm"|"s390x") ]]; then
  224. get_arch=$(arch)
  225. fi
  226. if [[ -z "${get_arch}" ]]; then
  227. echo_content red "The processor architecture only supports amd64/arm64/arm/s390x"
  228. exit 0
  229. fi
  230. can_connect www.google.com
  231. [[ "$?" == "0" ]] && can_google=1
  232. }
  233. depend_install() {
  234. if [[ "${package_manager}" != 'yum' && "${package_manager}" != 'dnf' ]]; then
  235. ${package_manager} update -y
  236. fi
  237. ${package_manager} install -y \
  238. curl \
  239. wget \
  240. tar \
  241. lsof \
  242. systemd
  243. }
  244. # Install Docker
  245. install_docker() {
  246. if [[ ! $(docker -v 2>/dev/null) ]]; then
  247. echo_content green "---> Install Docker"
  248. # turn off firewall
  249. if [[ "${release}" == "centos" ]]; then
  250. systemctl disable firewalld
  251. elif [[ "${release}" == "debian" || "${release}" == "ubuntu" ]]; then
  252. sudo ufw disable
  253. fi
  254. # set time zone
  255. timedatectl set-timezone Asia/Shanghai
  256. if [[ ${can_google} == 0 ]]; then
  257. sh <(curl -sL https://get.docker.com) --mirror Aliyun
  258. mkdir -p /etc/docker &&
  259. cat >/etc/docker/daemon.json <<EOF
  260. {
  261. "registry-mirrors":[${DOCKER_MIRROR}],
  262. "log-driver":"json-file",
  263. "log-opts":{
  264. "max-size":"50m",
  265. "max-file":"3"
  266. }
  267. }
  268. EOF
  269. else
  270. sh <(curl -sL https://get.docker.com)
  271. mkdir -p /etc/docker &&
  272. cat >/etc/docker/daemon.json <<EOF
  273. {
  274. "log-driver":"json-file",
  275. "log-opts":{
  276. "max-size":"50m",
  277. "max-file":"3"
  278. }
  279. }
  280. EOF
  281. fi
  282. systemctl enable docker &&
  283. systemctl restart docker
  284. if [[ $(docker -v 2>/dev/null) ]]; then
  285. echo_content skyBlue "---> Docker installation completed"
  286. else
  287. echo_content red "---> Docker installation failed"
  288. exit 0
  289. fi
  290. else
  291. echo_content skyBlue "---> You have installed Docker"
  292. fi
  293. }
  294. # Custom Settings Certificate
  295. install_custom_cert() {
  296. if [[ -z "$(cat "${DOMAIN_FILE}")" ]]; then
  297. while read -r -p "Please enter the file path of the .crt certificate (required): " crt_path; do
  298. if [[ -z "${crt_path}" ]]; then
  299. echo_content red "Path cannot be empty"
  300. else
  301. if [[ ! -f "${crt_path}" ]]; then
  302. echo_content red "The file path for the .crt certificate does not exist"
  303. else
  304. cp "${crt_path}" "${CERT_PATH}$1.crt"
  305. break
  306. fi
  307. fi
  308. done
  309. while read -r -p "Please enter the file path of the .key certificate (required): " key_path; do
  310. if [[ -z "${key_path}" ]]; then
  311. echo_content red "Path cannot be empty"
  312. else
  313. if [[ ! -f "${key_path}" ]]; then
  314. echo_content red "The file path for the .key certificate does not exist"
  315. else
  316. cp "${key_path}" "${CERT_PATH}$1.key"
  317. break
  318. fi
  319. fi
  320. done
  321. cat >${DOMAIN_FILE} <<EOF
  322. $1
  323. EOF
  324. echo_content red "\n=============================================================="
  325. echo_content skyBlue "---> Custom settings certificate installation completed"
  326. echo_content yellow "Certificate Directory: ${CERT_PATH}"
  327. echo_content red "\n=============================================================="
  328. fi
  329. }
  330. # Caddy2 https custom settings certificate configuration file
  331. caddy2_https_config() {
  332. domain=$1
  333. cat >${CADDY_CONFIG} <<EOF
  334. {
  335. "admin":{
  336. "disabled":true
  337. },
  338. "logging":{
  339. "logs":{
  340. "default":{
  341. "writer":{
  342. "output":"file",
  343. "filename":"${CADDY_LOG}error.log"
  344. },
  345. "level":"ERROR"
  346. }
  347. }
  348. },
  349. "storage":{
  350. "module":"file_system",
  351. "root":"${CERT_PATH}"
  352. },
  353. "apps":{
  354. "http":{
  355. "http_port": ${caddy_port},
  356. "servers":{
  357. "srv0":{
  358. "listen":[
  359. ":${caddy_port}"
  360. ],
  361. "routes":[
  362. {
  363. "match":[
  364. {
  365. "host":[
  366. "${domain}"
  367. ]
  368. }
  369. ],
  370. "handle":[
  371. {
  372. "handler":"static_response",
  373. "headers":{
  374. "Location":[
  375. "https://{http.request.host}:${caddy_remote_port}{http.request.uri}"
  376. ]
  377. },
  378. "status_code":301
  379. }
  380. ]
  381. }
  382. ]
  383. },
  384. "srv1":{
  385. "listen":[
  386. ":${caddy_remote_port}"
  387. ],
  388. "routes":[
  389. {
  390. "handle":[
  391. {
  392. "handler":"subroute",
  393. "routes":[
  394. {
  395. "match":[
  396. {
  397. "host":[
  398. "${domain}"
  399. ]
  400. }
  401. ],
  402. "handle":[
  403. {
  404. "handler":"file_server",
  405. "root":"${WEB_PATH}",
  406. "index_names":[
  407. "index.html",
  408. "index.htm"
  409. ]
  410. }
  411. ],
  412. "terminal":true
  413. }
  414. ]
  415. }
  416. ]
  417. }
  418. ],
  419. "tls_connection_policies":[
  420. {
  421. "match":{
  422. "sni":[
  423. "${domain}"
  424. ]
  425. }
  426. }
  427. ],
  428. "automatic_https":{
  429. "disable":true
  430. }
  431. }
  432. }
  433. },
  434. "tls":{
  435. "certificates":{
  436. "automate":[
  437. "${domain}"
  438. ],
  439. "load_files":[
  440. {
  441. "certificate":"${CADDY_CERT_DIR}${domain}/${domain}.crt",
  442. "key":"${CADDY_CERT_DIR}${domain}/${domain}.key"
  443. }
  444. ]
  445. },
  446. "automation":{
  447. "policies":[
  448. {
  449. "issuers":[
  450. {
  451. "module":"${ssl_module}",
  452. "email":"${your_email}"
  453. }
  454. ]
  455. }
  456. ]
  457. }
  458. }
  459. }
  460. }
  461. EOF
  462. }
  463. # Caddy2 https automatic application and renewal certificate configuration file
  464. caddy2_https_auto_config() {
  465. domain=$1
  466. cat >${CADDY_CONFIG} <<EOF
  467. {
  468. "admin":{
  469. "disabled":true
  470. },
  471. "logging":{
  472. "logs":{
  473. "default":{
  474. "writer":{
  475. "output":"file",
  476. "filename":"${CADDY_LOG}error.log"
  477. },
  478. "level":"ERROR"
  479. }
  480. }
  481. },
  482. "storage":{
  483. "module":"file_system",
  484. "root":"${CERT_PATH}"
  485. },
  486. "apps":{
  487. "http":{
  488. "http_port": ${caddy_port},
  489. "servers":{
  490. "srv0":{
  491. "listen":[
  492. ":${caddy_port}"
  493. ],
  494. "routes":[
  495. {
  496. "match":[
  497. {
  498. "host":[
  499. "${domain}"
  500. ]
  501. }
  502. ],
  503. "handle":[
  504. {
  505. "handler":"static_response",
  506. "headers":{
  507. "Location":[
  508. "https://{http.request.host}:${caddy_remote_port}{http.request.uri}"
  509. ]
  510. },
  511. "status_code":301
  512. }
  513. ]
  514. }
  515. ]
  516. },
  517. "srv1":{
  518. "listen":[
  519. ":${caddy_remote_port}"
  520. ],
  521. "routes":[
  522. {
  523. "handle":[
  524. {
  525. "handler":"subroute",
  526. "routes":[
  527. {
  528. "match":[
  529. {
  530. "host":[
  531. "${domain}"
  532. ]
  533. }
  534. ],
  535. "handle":[
  536. {
  537. "handler":"file_server",
  538. "root":"${WEB_PATH}",
  539. "index_names":[
  540. "index.html",
  541. "index.htm"
  542. ]
  543. }
  544. ],
  545. "terminal":true
  546. }
  547. ]
  548. }
  549. ]
  550. }
  551. ],
  552. "tls_connection_policies":[
  553. {
  554. "match":{
  555. "sni":[
  556. "${domain}"
  557. ]
  558. }
  559. }
  560. ],
  561. "automatic_https":{
  562. "disable":true
  563. }
  564. }
  565. }
  566. },
  567. "tls":{
  568. "certificates":{
  569. "automate":[
  570. "${domain}"
  571. ]
  572. },
  573. "automation":{
  574. "policies":[
  575. {
  576. "issuers":[
  577. {
  578. "module":"${ssl_module}",
  579. "email":"${your_email}"
  580. }
  581. ]
  582. }
  583. ]
  584. }
  585. }
  586. }
  587. }
  588. EOF
  589. }
  590. # Install Caddy2
  591. install_caddy2() {
  592. if [[ -z $(docker ps -a -q -f "name=^trojan-panel-caddy$") ]]; then
  593. echo_content green "---> Install Caddy2+https"
  594. wget --no-check-certificate -O ${WEB_PATH}html.tar.gz -N ${STATIC_HTML} &&
  595. tar -zxvf ${WEB_PATH}html.tar.gz -k -C ${WEB_PATH}
  596. read -r -p "Please enter the port of Caddy2 (default: 80): " caddy_port
  597. [[ -z "${caddy_port}" ]] && caddy_port=80
  598. read -r -p "Please enter the forwarding port of Caddy2 (default: 8863): " caddy_remote_port
  599. [[ -z "${caddy_remote_port}" ]] && caddy_remote_port=8863
  600. echo_content yellow "Tip: Please confirm that the domain name has been resolved to this machine, otherwise the installation may fail"
  601. while read -r -p "Please enter your domain name (required): " domain; do
  602. if [[ -z "${domain}" ]]; then
  603. echo_content red "Domain name cannot be empty"
  604. else
  605. break
  606. fi
  607. done
  608. read -r -p "Please enter your email (optional): " your_email
  609. 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
  610. if [[ -z ${ssl_option} || ${ssl_option} == 1 ]]; then
  611. while read -r -p "Please choose the way to apply for the certificate (1/acme 2/zerossl default: 1: " ssl_module_type; do
  612. if [[ -z "${ssl_module_type}" || ${ssl_module_type} == 1 ]]; then
  613. ssl_module="acme"
  614. CADDY_CERT_DIR="${CERT_PATH}certificates/acme-v02.api.letsencrypt.org-directory/"
  615. break
  616. elif [[ ${ssl_module_type} == 2 ]]; then
  617. ssl_module="zerossl"
  618. CADDY_CERT_DIR="${CERT_PATH}certificates/acme.zerossl.com-v2-dv90/"
  619. break
  620. else
  621. echo_content red "Cannot enter other characters except 1 and 2"
  622. fi
  623. done
  624. caddy2_https_auto_config "${domain}"
  625. break
  626. elif [[ ${ssl_option} == 2 ]]; then
  627. install_custom_cert "${domain}"
  628. caddy2_https_config "${domain}"
  629. break
  630. else
  631. echo_content red "Cannot enter other characters except 1 and 2"
  632. fi
  633. done
  634. # Caddy2 temporary listening port for automatic certificate application
  635. if [[ -n $(lsof -i:${caddy_port},${caddy_remote_port} -t) ]]; then
  636. kill -9 "$(lsof -i:${caddy_port},${caddy_remote_port} -t)"
  637. fi
  638. docker pull caddy:2.6.2 &&
  639. docker run -d --name trojan-panel-caddy --restart always \
  640. --network=host \
  641. -v "${CADDY_CONFIG}":"${CADDY_CONFIG}" \
  642. -v ${CERT_PATH}:"${CADDY_CERT_DIR}${domain}/" \
  643. -v ${WEB_PATH}:${WEB_PATH} \
  644. -v ${CADDY_LOG}:${CADDY_LOG} \
  645. caddy:2.6.2 caddy run --config ${CADDY_CONFIG}
  646. cat >${DOMAIN_FILE} <<EOF
  647. ${domain}
  648. EOF
  649. if [[ -n $(docker ps -q -f "name=^trojan-panel-caddy$" -f "status=running") ]]; then
  650. echo_content red "\n=============================================================="
  651. echo_content skyBlue "---> Caddy2+https installation completed"
  652. echo_content yellow "Certificate Directory: ${CERT_PATH}"
  653. echo_content red "\n=============================================================="
  654. else
  655. echo_content red "---> Caddy2+https installation fails or runs abnormally, please try to repair or uninstall and reinstall"
  656. exit 0
  657. fi
  658. else
  659. echo_content skyBlue "---> You have installed Caddy2+https"
  660. fi
  661. }
  662. # Nginx http configuration file
  663. nginx_http_config() {
  664. cat >${NGINX_CONFIG} <<-EOF
  665. server {
  666. listen ${nginx_port};
  667. server_name localhost;
  668. location / {
  669. root ${WEB_PATH};
  670. index index.html index.htm;
  671. }
  672. error_page 497 http://\$host:${nginx_port}\$request_uri;
  673. error_page 500 502 503 504 /50x.html;
  674. location = /50x.html {
  675. root /usr/share/nginx/html;
  676. }
  677. }
  678. EOF
  679. }
  680. # Nginx https configuration file
  681. nginx_https_config() {
  682. domain=$1
  683. cat >${NGINX_CONFIG} <<-EOF
  684. server {
  685. listen ${nginx_port};
  686. server_name localhost;
  687. return 301 http://\$host:${nginx_remote_port}\$request_uri;
  688. }
  689. server {
  690. listen ${nginx_remote_port} ssl;
  691. server_name localhost;
  692. # force ssl
  693. ssl on;
  694. ssl_certificate ${CERT_PATH}${domain}.crt;
  695. ssl_certificate_key ${CERT_PATH}${domain}.key;
  696. # cache validity period
  697. ssl_session_timeout 5m;
  698. # secure link optional encryption protocol
  699. ssl_protocols TLSv1.3;
  700. # encryption algorithm
  701. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  702. # use server-side preferred algorithm
  703. ssl_prefer_server_ciphers on;
  704. #access_log /var/log/nginx/host.access.log main;
  705. location / {
  706. root ${WEB_PATH};
  707. index index.html index.htm;
  708. }
  709. #error_page 404 /404.html;
  710. #497 http->https
  711. error_page 497 https://\$host:${nginx_remote_port}\$request_uri;
  712. # redirect server error pages to the static page /50x.html
  713. #
  714. error_page 500 502 503 504 /50x.html;
  715. location = /50x.html {
  716. root /usr/share/nginx/html;
  717. }
  718. }
  719. EOF
  720. }
  721. # Install Nginx
  722. install_nginx() {
  723. if [[ -z $(docker ps -a -q -f "name=^trojan-panel-nginx$") ]]; then
  724. echo_content green "---> Install Nginx"
  725. wget --no-check-certificate -O ${WEB_PATH}html.tar.gz -N ${STATIC_HTML} &&
  726. tar -zxvf ${WEB_PATH}html.tar.gz -k -C ${WEB_PATH}
  727. read -r -p "Please enter the port of Nginx (default: 80): " nginx_port
  728. [[ -z "${nginx_port}" ]] && nginx_port=80
  729. read -r -p "Please enter the forwarding port of Nginx (default: 8863): " nginx_remote_port
  730. [[ -z "${nginx_remote_port}" ]] && nginx_remote_port=8863
  731. while read -r -p "Please choose whether to enable https in Nginx? (0/off 1/on default: 1): " nginx_https; do
  732. if [[ -z ${nginx_https} || ${nginx_https} == 1 ]]; then
  733. install_custom_cert "custom_cert"
  734. nginx_https_config "custom_cert"
  735. break
  736. elif [[ ${nginx_https} == 0 ]]; then
  737. nginx_http_config
  738. break
  739. else
  740. echo_content red "Cannot enter other characters except 1 and 2"
  741. fi
  742. done
  743. docker pull nginx:1.20-alpine &&
  744. docker run -d --name trojan-panel-nginx --restart always \
  745. --network=host \
  746. -v "${NGINX_CONFIG}":"/etc/nginx/conf.d/default.conf" \
  747. -v ${CERT_PATH}:${CERT_PATH} \
  748. -v ${WEB_PATH}:${WEB_PATH} \
  749. nginx:1.20-alpine
  750. if [[ -n $(docker ps -q -f "name=^trojan-panel-nginx$" -f "status=running") ]]; then
  751. echo_content skyBlue "---> Nginx installation completed"
  752. else
  753. echo_content red "---> Nginx installation fails or runs abnormally, please try to repair or uninstall and reinstall"
  754. exit 0
  755. fi
  756. else
  757. echo_content skyBlue "---> You have installed Nginx"
  758. fi
  759. }
  760. # Install a web server
  761. install_reverse_proxy() {
  762. if [[ -z $(docker ps -a -q -f "name=^trojan-panel-caddy$|^trojan-panel-nginx$") ]]; then
  763. echo_content green "---> Install a web server"
  764. while :; do
  765. echo_content yellow "1. Install Caddy2+https (recommend)"
  766. echo_content yellow "2. Install Nginx"
  767. echo_content yellow "3. Not install"
  768. read -r -p "Please select (default: 1): " whether_install_reverse_proxy
  769. [[ -z "${whether_install_reverse_proxy}" ]] && whether_install_reverse_proxy=1
  770. case ${whether_install_reverse_proxy} in
  771. 1)
  772. install_caddy2
  773. break
  774. ;;
  775. 2)
  776. install_nginx
  777. break
  778. ;;
  779. 3)
  780. break
  781. ;;
  782. *)
  783. echo_content red "No such option"
  784. continue
  785. ;;
  786. esac
  787. done
  788. echo_content skyBlue "---> Web server installation completed"
  789. fi
  790. }
  791. # Set certificate
  792. install_cert() {
  793. if [[ -z "$(cat "${DOMAIN_FILE}")" ]]; then
  794. echo_content green "---> Set certificate"
  795. while :; do
  796. echo_content yellow "1. Custom certificate"
  797. echo_content yellow "2. Not set"
  798. read -r -p "Please select (default: 1): " whether_install_cert
  799. [[ -z "${whether_install_cert}" ]] && whether_install_cert=1
  800. case ${whether_install_cert} in
  801. 1)
  802. install_custom_cert "custom_cert"
  803. break
  804. ;;
  805. 2)
  806. break
  807. ;;
  808. *)
  809. echo_content red "No such option"
  810. continue
  811. ;;
  812. esac
  813. done
  814. echo_content green "---> Certificate setup completed"
  815. fi
  816. }
  817. # Install MariaDB
  818. install_mariadb() {
  819. if [[ -z $(docker ps -a -q -f "name=^trojan-panel-mariadb$") ]]; then
  820. echo_content green "---> Install MariaDB"
  821. read -r -p "Please enter the port of MariaDB (default: 9507): " mariadb_port
  822. [[ -z "${mariadb_port}" ]] && mariadb_port=9507
  823. read -r -p "Please enter the username of MariaDB (default: root): " mariadb_user
  824. [[ -z "${mariadb_user}" ]] && mariadb_user="root"
  825. while read -r -p "Please enter the password of MariaDB (required): " mariadb_pas; do
  826. if [[ -z "${mariadb_pas}" ]]; then
  827. echo_content red "Password can not be empty"
  828. else
  829. break
  830. fi
  831. done
  832. if [[ "${mariadb_user}" == "root" ]]; then
  833. docker pull mariadb:10.7.3 &&
  834. docker run -d --name trojan-panel-mariadb --restart always \
  835. --network=host \
  836. -e MYSQL_DATABASE="trojan_panel_db" \
  837. -e MYSQL_ROOT_PASSWORD="${mariadb_pas}" \
  838. -e TZ=Asia/Shanghai \
  839. mariadb:10.7.3 \
  840. --port ${mariadb_port} \
  841. --character-set-server=utf8mb4 \
  842. --collation-server=utf8mb4_unicode_ci
  843. else
  844. docker pull mariadb:10.7.3 &&
  845. docker run -d --name trojan-panel-mariadb --restart always \
  846. --network=host \
  847. -e MYSQL_DATABASE="trojan_panel_db" \
  848. -e MYSQL_ROOT_PASSWORD="${mariadb_pas}" \
  849. -e MYSQL_USER="${mariadb_user}" \
  850. -e MYSQL_PASSWORD="${mariadb_pas}" \
  851. -e TZ=Asia/Shanghai \
  852. mariadb:10.7.3 \
  853. --port ${mariadb_port} \
  854. --character-set-server=utf8mb4 \
  855. --collation-server=utf8mb4_unicode_ci
  856. fi
  857. if [[ -n $(docker ps -q -f "name=^trojan-panel-mariadb$" -f "status=running") ]]; then
  858. echo_content skyBlue "---> MariaDB installation completed"
  859. echo_content yellow "---> The MariaDB password of root (please keep it safe): ${mariadb_pas}"
  860. if [[ "${mariadb_user}" != "root" ]]; then
  861. echo_content yellow "---> The MariaDB password of ${mariadb_user} (please keep it safe): ${mariadb_pas}"
  862. fi
  863. else
  864. echo_content red "---> MariaDB installation fails or runs abnormally, please try to repair or uninstall and reinstall"
  865. exit 0
  866. fi
  867. else
  868. echo_content skyBlue "---> You have installed MariaDB"
  869. fi
  870. }
  871. # Install Redis
  872. install_redis() {
  873. if [[ -z $(docker ps -a -q -f "name=^trojan-panel-redis$") ]]; then
  874. echo_content green "---> Install Redis"
  875. read -r -p "Please enter the port of Redis (default: 6378): " redis_port
  876. [[ -z "${redis_port}" ]] && redis_port=6378
  877. while read -r -p "Please enter the Redis password (required): " redis_pass; do
  878. if [[ -z "${redis_pass}" ]]; then
  879. echo_content red "Password can not be empty"
  880. else
  881. break
  882. fi
  883. done
  884. docker pull redis:6.2.7 &&
  885. docker run -d --name trojan-panel-redis --restart always \
  886. --network=host \
  887. redis:6.2.7 \
  888. redis-server --requirepass "${redis_pass}" --port "${redis_port}"
  889. if [[ -n $(docker ps -q -f "name=^trojan-panel-redis$" -f "status=running") ]]; then
  890. echo_content skyBlue "---> Redis installation completed"
  891. echo_content yellow "---> Redis password (please keep it safe): ${redis_pass}"
  892. else
  893. echo_content red "---> Redis installation fails or runs abnormally, please try to repair or uninstall and reinstall"
  894. exit 0
  895. fi
  896. else
  897. echo_content skyBlue "---> You have installed Redis"
  898. fi
  899. }
  900. # Trojan Panel Frontend Nginx http configuration file
  901. ui_http_config() {
  902. cat >${UI_NGINX_CONFIG} <<-EOF
  903. server {
  904. listen ${trojan_panel_ui_port};
  905. server_name localhost;
  906. location / {
  907. root ${TROJAN_PANEL_UI_DATA};
  908. index index.html index.htm;
  909. }
  910. location /api {
  911. proxy_pass http://${trojan_panel_ip}:${trojan_panel_server_port};
  912. }
  913. error_page 497 http://\$host:${trojan_panel_ui_port}\$request_uri;
  914. error_page 500 502 503 504 /50x.html;
  915. location = /50x.html {
  916. root /usr/share/nginx/html;
  917. }
  918. }
  919. EOF
  920. }
  921. # Trojan Panel Frontend Nginx https configuration file
  922. ui_https_config() {
  923. cat >${UI_NGINX_CONFIG} <<-EOF
  924. server {
  925. listen ${trojan_panel_ui_port} ssl;
  926. server_name localhost;
  927. # force ssl
  928. ssl on;
  929. ssl_certificate ${CERT_PATH}${domain}.crt;
  930. ssl_certificate_key ${CERT_PATH}${domain}.key;
  931. # cache validity period
  932. ssl_session_timeout 5m;
  933. # secure link optional encryption protocol
  934. ssl_protocols TLSv1.3;
  935. # encryption algorithm
  936. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  937. # use server-side preferred algorithm
  938. ssl_prefer_server_ciphers on;
  939. #access_log /var/log/nginx/host.access.log main;
  940. location / {
  941. root ${TROJAN_PANEL_UI_DATA};
  942. index index.html index.htm;
  943. }
  944. location /api {
  945. proxy_pass http://${trojan_panel_ip}:${trojan_panel_server_port};
  946. }
  947. #error_page 404 /404.html;
  948. #497 http->https
  949. error_page 497 https://\$host:${trojan_panel_ui_port}\$request_uri;
  950. # redirect server error pages to the static page /50x.html
  951. #
  952. error_page 500 502 503 504 /50x.html;
  953. location = /50x.html {
  954. root /usr/share/nginx/html;
  955. }
  956. }
  957. EOF
  958. }
  959. # Install Trojan Panel Frontend
  960. install_trojan_panel_ui() {
  961. if [[ -z $(docker ps -a -q -f "name=^trojan-panel-ui$") ]]; then
  962. echo_content green "---> Install Trojan Panel Frontend"
  963. read -r -p "Please enter the IP address of the Trojan Panel Backend (default: local host): " trojan_panel_ip
  964. [[ -z "${trojan_panel_ip}" ]] && trojan_panel_ip="127.0.0.1"
  965. read -r -p "Please enter the service port of the Trojan Panel Backend (default: 8081): " trojan_panel_server_port
  966. [[ -z "${trojan_panel_server_port}" ]] && trojan_panel_server_port=8081
  967. read -r -p "Please enter the port of the Trojan Panel Frontend (default: 8888): " trojan_panel_ui_port
  968. [[ -z "${trojan_panel_ui_port}" ]] && trojan_panel_ui_port="8888"
  969. while read -r -p "Please choose whether to enable https on the Trojan Panel Frontend? (0/off 1/on default: 1): " ui_https; do
  970. if [[ -z ${ui_https} || ${ui_https} == 1 ]]; then
  971. install_custom_cert "custom_cert"
  972. domain=$(cat "${DOMAIN_FILE}")
  973. ui_https_config
  974. break
  975. elif [[ ${ui_https} == 0 ]]; then
  976. ui_http_config
  977. break
  978. else
  979. echo_content red "Cannot enter other characters except 1 and 2"
  980. fi
  981. done
  982. docker pull jonssonyan/trojan-panel-ui:2.1.6 &&
  983. docker run -d --name trojan-panel-ui --restart always \
  984. --network=host \
  985. -v "${UI_NGINX_CONFIG}":"/etc/nginx/conf.d/default.conf" \
  986. -v ${CERT_PATH}:${CERT_PATH} \
  987. jonssonyan/trojan-panel-ui:2.1.6
  988. if [[ -n $(docker ps -q -f "name=^trojan-panel-ui$" -f "status=running") ]]; then
  989. echo_content skyBlue "---> Trojan Panel Frontend installation completed"
  990. https_flag=$([[ -z ${ui_https} || ${ui_https} == 1 ]] && echo "https" || echo "http")
  991. domain_or_ip=$([[ -z ${domain} || "${domain}" == "custom_cert" ]] && echo "ip" || echo "${domain}")
  992. echo_content red "\n=============================================================="
  993. echo_content skyBlue "Trojan Panel Frontend installed successfully"
  994. echo_content yellow "Web management panel address: ${https_flag}://${domain_or_ip}:${trojan_panel_ui_port}"
  995. echo_content red "\n=============================================================="
  996. else
  997. echo_content red "---> Trojan Panel Frontend installation fails or runs abnormally, please try to repair or uninstall and reinstall"
  998. exit 0
  999. fi
  1000. else
  1001. echo_content skyBlue "---> You have installed the Trojan Panel Frontend"
  1002. fi
  1003. }
  1004. # Install Trojan Panel Backend
  1005. install_trojan_panel() {
  1006. if [[ -z $(docker ps -a -q -f "name=^trojan-panel$") ]]; then
  1007. echo_content green "---> Install Trojan Panel Backend"
  1008. read -r -p "Please enter the service port of the Trojan Panel Backend (default: 8081): " trojan_panel_port
  1009. [[ -z "${trojan_panel_port}" ]] && trojan_panel_port=8081
  1010. read -r -p "Please enter the IP address of MariaDB (default: local host): " mariadb_ip
  1011. [[ -z "${mariadb_ip}" ]] && mariadb_ip="127.0.0.1"
  1012. read -r -p "Please enter the port of MariaDB (default: 9507): " mariadb_port
  1013. [[ -z "${mariadb_port}" ]] && mariadb_port=9507
  1014. read -r -p "Please enter the username of MariaDB (default: root): " mariadb_user
  1015. [[ -z "${mariadb_user}" ]] && mariadb_user="root"
  1016. while read -r -p "Please enter the password of MariaDB (required): " mariadb_pas; do
  1017. if [[ -z "${mariadb_pas}" ]]; then
  1018. echo_content red "Password can not be empty"
  1019. else
  1020. break
  1021. fi
  1022. done
  1023. docker exec trojan-panel-mariadb mysql --default-character-set=utf8 -h"${mariadb_ip}" -P"${mariadb_port}" -u"${mariadb_user}" -p"${mariadb_pas}" -e "create database if not exists trojan_panel_db;" &>/dev/null
  1024. read -r -p "Please enter the IP address of Redis (default: local host): " redis_host
  1025. [[ -z "${redis_host}" ]] && redis_host="127.0.0.1"
  1026. read -r -p "Please enter the port of Redis (default: 6378): " redis_port
  1027. [[ -z "${redis_port}" ]] && redis_port=6378
  1028. while read -r -p "Please enter the Redis password (required): " redis_pass; do
  1029. if [[ -z "${redis_pass}" ]]; then
  1030. echo_content red "Password can not be empty"
  1031. else
  1032. break
  1033. fi
  1034. done
  1035. docker exec trojan-panel-redis redis-cli -h "${redis_host}" -p "${redis_port}" -a "${redis_pass}" -e "flushall" &>/dev/null
  1036. docker pull jonssonyan/trojan-panel:2.2.0 &&
  1037. docker run -d --name trojan-panel --restart always \
  1038. --network=host \
  1039. -v ${WEB_PATH}:${TROJAN_PANEL_WEBFILE} \
  1040. -v ${TROJAN_PANEL_LOGS}:${TROJAN_PANEL_LOGS} \
  1041. -v ${TROJAN_PANEL_CONFIG}:${TROJAN_PANEL_CONFIG} \
  1042. -v /etc/localtime:/etc/localtime \
  1043. -e GIN_MODE=release \
  1044. -e "mariadb_ip=${mariadb_ip}" \
  1045. -e "mariadb_port=${mariadb_port}" \
  1046. -e "mariadb_user=${mariadb_user}" \
  1047. -e "mariadb_pas=${mariadb_pas}" \
  1048. -e "redis_host=${redis_host}" \
  1049. -e "redis_port=${redis_port}" \
  1050. -e "redis_pass=${redis_pass}" \
  1051. -e "server_port=${trojan_panel_port}" \
  1052. jonssonyan/trojan-panel:2.2.0
  1053. if [[ -n $(docker ps -q -f "name=^trojan-panel$" -f "status=running") ]]; then
  1054. echo_content skyBlue "---> Trojan Panel Backend installation completed"
  1055. echo_content red "\n=============================================================="
  1056. echo_content skyBlue "Trojan Panel Backend installed successfully"
  1057. echo_content yellow "MariaDB ${mariadb_user} password (please keep it safe): ${mariadb_pas}"
  1058. echo_content yellow "Redis password (please keep it safe): ${redis_pass}"
  1059. echo_content yellow "System administrator Default username: sysadmin Default password: 123456"
  1060. echo_content yellow "Please log in to the management panel to change the password in time"
  1061. echo_content red "\n=============================================================="
  1062. else
  1063. echo_content red "---> Trojan Panel Backend installation fails or runs abnormally, please try to repair or uninstall and reinstall"
  1064. exit 0
  1065. fi
  1066. else
  1067. echo_content skyBlue "---> You have installed the Trojan Panel Backend"
  1068. fi
  1069. }
  1070. # Install Trojan Panel Core
  1071. install_trojan_panel_core() {
  1072. if [[ -z $(docker ps -a -q -f "name=^trojan-panel-core$") ]]; then
  1073. echo_content green "---> Install Trojan Panel Core"
  1074. read -r -p "Please enter the service port of the Trojan Panel Core (default: 8082): " trojan_panel_core_port
  1075. [[ -z "${trojan_panel_core_port}" ]] && trojan_panel_core_port=8082
  1076. read -r -p "Please enter the IP address of MariaDB (default: local host): " mariadb_ip
  1077. [[ -z "${mariadb_ip}" ]] && mariadb_ip="127.0.0.1"
  1078. read -r -p "Please enter the port of MariaDB (default: 9507): " mariadb_port
  1079. [[ -z "${mariadb_port}" ]] && mariadb_port=9507
  1080. read -r -p "Please enter the username of MariaDB (default: root): " mariadb_user
  1081. [[ -z "${mariadb_user}" ]] && mariadb_user="root"
  1082. while read -r -p "Please enter the password of MariaDB (required): " mariadb_pas; do
  1083. if [[ -z "${mariadb_pas}" ]]; then
  1084. echo_content red "Password can not be empty"
  1085. else
  1086. break
  1087. fi
  1088. done
  1089. read -r -p "Please enter the database name (default: trojan_panel_db): " database
  1090. [[ -z "${database}" ]] && database="trojan_panel_db"
  1091. read -r -p "Please enter the user table name of the database (default: account): " account_table
  1092. [[ -z "${account_table}" ]] && account_table="account"
  1093. read -r -p "Please enter the IP address of Redis (default: local host): " redis_host
  1094. [[ -z "${redis_host}" ]] && redis_host="127.0.0.1"
  1095. read -r -p "Please enter the port of Redis (default: 6378): " redis_port
  1096. [[ -z "${redis_port}" ]] && redis_port=6378
  1097. while read -r -p "Please enter the Redis password (required): " redis_pass; do
  1098. if [[ -z "${redis_pass}" ]]; then
  1099. echo_content red "Password can not be empty"
  1100. else
  1101. break
  1102. fi
  1103. done
  1104. read -r -p "Please enter the API port (default: 8100): " grpc_port
  1105. [[ -z "${grpc_port}" ]] && grpc_port=8100
  1106. domain=$(cat "${DOMAIN_FILE}")
  1107. docker pull jonssonyan/trojan-panel-core:2.2.1 &&
  1108. docker run -d --name trojan-panel-core --restart always \
  1109. --network=host \
  1110. -v ${TROJAN_PANEL_CORE_DATA}bin/xray/config/:${TROJAN_PANEL_CORE_DATA}bin/xray/config/ \
  1111. -v ${TROJAN_PANEL_CORE_DATA}bin/trojango/config/:${TROJAN_PANEL_CORE_DATA}bin/trojango/config/ \
  1112. -v ${TROJAN_PANEL_CORE_DATA}bin/hysteria/config/:${TROJAN_PANEL_CORE_DATA}bin/hysteria/config/ \
  1113. -v ${TROJAN_PANEL_CORE_DATA}bin/naiveproxy/config/:${TROJAN_PANEL_CORE_DATA}bin/naiveproxy/config/ \
  1114. -v ${TROJAN_PANEL_CORE_LOGS}:${TROJAN_PANEL_CORE_LOGS} \
  1115. -v ${TROJAN_PANEL_CORE_CONFIG}:${TROJAN_PANEL_CORE_CONFIG} \
  1116. -v ${CERT_PATH}:${CERT_PATH} \
  1117. -v ${WEB_PATH}:${WEB_PATH} \
  1118. -v /etc/localtime:/etc/localtime \
  1119. -e GIN_MODE=release \
  1120. -e "mariadb_ip=${mariadb_ip}" \
  1121. -e "mariadb_port=${mariadb_port}" \
  1122. -e "mariadb_user=${mariadb_user}" \
  1123. -e "mariadb_pas=${mariadb_pas}" \
  1124. -e "database=${database}" \
  1125. -e "account-table=${account_table}" \
  1126. -e "redis_host=${redis_host}" \
  1127. -e "redis_port=${redis_port}" \
  1128. -e "redis_pass=${redis_pass}" \
  1129. -e "crt_path=${CERT_PATH}${domain}.crt" \
  1130. -e "key_path=${CERT_PATH}${domain}.key" \
  1131. -e "grpc_port=${grpc_port}" \
  1132. -e "server_port=${trojan_panel_core_port}" \
  1133. jonssonyan/trojan-panel-core:2.2.1
  1134. if [[ -n $(docker ps -q -f "name=^trojan-panel-core$" -f "status=running") ]]; then
  1135. echo_content skyBlue "---> Trojan Panel Core installation completed"
  1136. else
  1137. echo_content red "---> Trojan Panel Core installation fails or runs abnormally, please try to repair or uninstall and reinstall"
  1138. exit 0
  1139. fi
  1140. else
  1141. echo_content skyBlue "---> You have installed the Trojan Panel Core"
  1142. fi
  1143. }
  1144. # Update Trojan Panel database structure
  1145. update_trojan_panel_database() {
  1146. echo_content skyBlue "---> Update Trojan Panel database structure"
  1147. version_214_215=("v2.1.4")
  1148. if [[ "${version_214_215[*]}" =~ "${trojan_panel_current_version}" ]]; then
  1149. docker exec trojan-panel-mariadb mysql --default-character-set=utf8 -h"${mariadb_ip}" -P"${mariadb_port}" -u"${mariadb_user}" -p"${mariadb_pas}" -Dtrojan_panel_db -e "${sql_215}" &>/dev/null &&
  1150. trojan_panel_current_version="v2.1.5"
  1151. fi
  1152. echo_content skyBlue "---> Trojan Panel database structure update completed"
  1153. }
  1154. # Update Trojan Panel Core database structure
  1155. update_trojan_panel_core_database() {
  1156. echo_content skyBlue "---> Update Trojan Panel Core database structure"
  1157. echo_content skyBlue "---> Trojan Panel Core database structure update completed"
  1158. }
  1159. # Update Trojan Panel Frontend
  1160. update_trojan_panel_ui() {
  1161. if [[ -z $(docker ps -a -q -f "name=^trojan-panel-ui$") ]]; then
  1162. echo_content red "---> Please install the Trojan Panel Frontend first"
  1163. exit 0
  1164. fi
  1165. trojan_panel_ui_current_version=$(docker exec trojan-panel-ui cat ${TROJAN_PANEL_UI_DATA}version)
  1166. if [[ -z "${trojan_panel_ui_current_version}" || ! "${trojan_panel_ui_current_version}" =~ ^v.* ]]; then
  1167. echo_content red "---> The current version does not support online updates"
  1168. exit 0
  1169. fi
  1170. echo_content yellow "Tip: The current version of the Trojan Panel Frontend (trojan-panel-ui) is ${trojan_panel_ui_current_version} the latest version is ${trojan_panel_ui_latest_version}"
  1171. if [[ "${trojan_panel_ui_current_version}" != "${trojan_panel_ui_latest_version}" ]]; then
  1172. echo_content green "---> Update Trojan Panel Frontend"
  1173. docker rm -f trojan-panel-ui &&
  1174. docker rmi -f jonssonyan/trojan-panel-ui:2.1.6
  1175. docker pull jonssonyan/trojan-panel-ui:2.1.6 &&
  1176. docker run -d --name trojan-panel-ui --restart always \
  1177. --network=host \
  1178. -v "${UI_NGINX_CONFIG}":"/etc/nginx/conf.d/default.conf" \
  1179. -v ${CERT_PATH}:${CERT_PATH} \
  1180. jonssonyan/trojan-panel-ui:2.1.6
  1181. if [[ -n $(docker ps -q -f "name=^trojan-panel-ui$" -f "status=running") ]]; then
  1182. echo_content skyBlue "---> Trojan Panel Frontend update completed"
  1183. else
  1184. echo_content red "---> Trojan Panel Frontend update fails or runs abnormally, please try to repair or uninstall and reinstall"
  1185. fi
  1186. else
  1187. echo_content skyBlue "---> You have installed the latest version of the Trojan Panel Frontend"
  1188. fi
  1189. }
  1190. # Update Trojan Panel Backend
  1191. update_trojan_panel() {
  1192. if [[ -z $(docker ps -a -q -f "name=^trojan-panel$") ]]; then
  1193. echo_content red "---> Please install the Trojan Panel Backend first"
  1194. exit 0
  1195. fi
  1196. trojan_panel_current_version=$(docker exec trojan-panel ./trojan-panel -version)
  1197. if [[ -z "${trojan_panel_current_version}" || ! "${trojan_panel_current_version}" =~ ^v.* || ! $(version_ge "${trojan_panel_current_version}" "v2.1.4") ]]; then
  1198. echo_content red "---> The current version does not support online updates"
  1199. exit 0
  1200. fi
  1201. echo_content yellow "Tip: The current version of the Trojan Panel Backend (trojan-panel) is ${trojan_panel_current_version} The latest version is ${trojan_panel_latest_version}"
  1202. if [[ "${trojan_panel_current_version}" != "${trojan_panel_latest_version}" ]]; then
  1203. echo_content green "---> Update Trojan Panel Backend"
  1204. mariadb_ip=$(get_ini_value ${trojan_panel_config_path} mysql.host)
  1205. mariadb_port=$(get_ini_value ${trojan_panel_config_path} mysql.port)
  1206. mariadb_user=$(get_ini_value ${trojan_panel_config_path} mysql.user)
  1207. mariadb_pas=$(get_ini_value ${trojan_panel_config_path} mysql.password)
  1208. redis_host=$(get_ini_value ${trojan_panel_config_path} redis.host)
  1209. redis_port=$(get_ini_value ${trojan_panel_config_path} redis.port)
  1210. redis_pass=$(get_ini_value ${trojan_panel_config_path} redis.password)
  1211. trojan_panel_port=$(get_ini_value ${trojan_panel_config_path} server.port)
  1212. update_trojan_panel_database
  1213. docker exec trojan-panel-redis redis-cli -h "${redis_host}" -p "${redis_port}" -a "${redis_pass}" -e "flushall" &>/dev/null
  1214. docker rm -f trojan-panel &&
  1215. docker rmi -f jonssonyan/trojan-panel:2.2.0
  1216. docker pull jonssonyan/trojan-panel:2.2.0 &&
  1217. docker run -d --name trojan-panel --restart always \
  1218. --network=host \
  1219. -v ${WEB_PATH}:${TROJAN_PANEL_WEBFILE} \
  1220. -v ${TROJAN_PANEL_LOGS}:${TROJAN_PANEL_LOGS} \
  1221. -v ${TROJAN_PANEL_CONFIG}:${TROJAN_PANEL_CONFIG} \
  1222. -v /etc/localtime:/etc/localtime \
  1223. -e GIN_MODE=release \
  1224. -e "mariadb_ip=${mariadb_ip}" \
  1225. -e "mariadb_port=${mariadb_port}" \
  1226. -e "mariadb_user=${mariadb_user}" \
  1227. -e "mariadb_pas=${mariadb_pas}" \
  1228. -e "redis_host=${redis_host}" \
  1229. -e "redis_port=${redis_port}" \
  1230. -e "redis_pass=${redis_pass}" \
  1231. -e "server_port=${trojan_panel_port}" \
  1232. jonssonyan/trojan-panel:2.2.0
  1233. if [[ -n $(docker ps -q -f "name=^trojan-panel$" -f "status=running") ]]; then
  1234. echo_content skyBlue "---> Trojan Panel backend update completed"
  1235. else
  1236. echo_content red "---> Trojan Panel Backend update fails or runs abnormally, please try to repair or uninstall and reinstall"
  1237. fi
  1238. else
  1239. echo_content skyBlue "---> You have installed the latest version of the Trojan Panel Backend"
  1240. fi
  1241. }
  1242. # Update Trojan Panel Core
  1243. update_trojan_panel_core() {
  1244. if [[ -z $(docker ps -a -q -f "name=^trojan-panel-core$") ]]; then
  1245. echo_content red "---> Please install the Trojan Panel Core first"
  1246. exit 0
  1247. fi
  1248. trojan_panel_core_current_version=$(docker exec trojan-panel-core ./trojan-panel-core -version)
  1249. if [[ -z "${trojan_panel_core_current_version}" || ! "${trojan_panel_core_current_version}" =~ ^v.* || ! $(version_ge "${trojan_panel_core_current_version}" "v2.1.1") ]]; then
  1250. echo_content red "---> The current version does not support online updates"
  1251. exit 0
  1252. fi
  1253. echo_content yellow "Tip: The current version of the Trojan Panel Core (trojan-panel-core) is ${trojan_panel_core_current_version} The latest version is ${trojan_panel_core_latest_version}"
  1254. if [[ "${trojan_panel_core_current_version}" != "${trojan_panel_core_latest_version}" ]]; then
  1255. echo_content green "---> Update Trojan Panel Core"
  1256. mariadb_ip=$(get_ini_value ${trojan_panel_core_config_path} mysql.host)
  1257. mariadb_port=$(get_ini_value ${trojan_panel_core_config_path} mysql.port)
  1258. mariadb_user=$(get_ini_value ${trojan_panel_core_config_path} mysql.user)
  1259. mariadb_pas=$(get_ini_value ${trojan_panel_core_config_path} mysql.password)
  1260. redis_host=$(get_ini_value ${trojan_panel_core_config_path} redis.host)
  1261. redis_port=$(get_ini_value ${trojan_panel_core_config_path} redis.port)
  1262. redis_pass=$(get_ini_value ${trojan_panel_core_config_path} redis.password)
  1263. grpc_port=$(get_ini_value ${trojan_panel_core_config_path} grpc.port)
  1264. trojan_panel_core_port=$(get_ini_value ${trojan_panel_core_config_path} server.port)
  1265. update_trojan_panel_core_database
  1266. docker exec trojan-panel-redis redis-cli -h "${redis_host}" -p "${redis_port}" -a "${redis_pass}" -e "flushall" &>/dev/null
  1267. docker rm -f trojan-panel-core &&
  1268. docker rmi -f jonssonyan/trojan-panel-core:2.2.1
  1269. domain=$(cat "${DOMAIN_FILE}")
  1270. docker pull jonssonyan/trojan-panel-core:2.2.1 &&
  1271. docker run -d --name trojan-panel-core --restart always \
  1272. --network=host \
  1273. -v ${TROJAN_PANEL_CORE_DATA}bin/xray/config/:${TROJAN_PANEL_CORE_DATA}bin/xray/config/ \
  1274. -v ${TROJAN_PANEL_CORE_DATA}bin/trojango/config/:${TROJAN_PANEL_CORE_DATA}bin/trojango/config/ \
  1275. -v ${TROJAN_PANEL_CORE_DATA}bin/hysteria/config/:${TROJAN_PANEL_CORE_DATA}bin/hysteria/config/ \
  1276. -v ${TROJAN_PANEL_CORE_DATA}bin/naiveproxy/config/:${TROJAN_PANEL_CORE_DATA}bin/naiveproxy/config/ \
  1277. -v ${TROJAN_PANEL_CORE_LOGS}:${TROJAN_PANEL_CORE_LOGS} \
  1278. -v ${TROJAN_PANEL_CORE_CONFIG}:${TROJAN_PANEL_CORE_CONFIG} \
  1279. -v ${CERT_PATH}:${CERT_PATH} \
  1280. -v ${WEB_PATH}:${WEB_PATH} \
  1281. -v /etc/localtime:/etc/localtime \
  1282. -e GIN_MODE=release \
  1283. -e "mariadb_ip=${mariadb_ip}" \
  1284. -e "mariadb_port=${mariadb_port}" \
  1285. -e "mariadb_user=${mariadb_user}" \
  1286. -e "mariadb_pas=${mariadb_pas}" \
  1287. -e "database=${database}" \
  1288. -e "account-table=${account_table}" \
  1289. -e "redis_host=${redis_host}" \
  1290. -e "redis_port=${redis_port}" \
  1291. -e "redis_pass=${redis_pass}" \
  1292. -e "crt_path=${CERT_PATH}${domain}.crt" \
  1293. -e "key_path=${CERT_PATH}${domain}.key" \
  1294. -e "grpc_port=${grpc_port}" \
  1295. -e "server_port=${trojan_panel_core_port}" \
  1296. jonssonyan/trojan-panel-core:2.2.1
  1297. if [[ -n $(docker ps -q -f "name=^trojan-panel-core$" -f "status=running") ]]; then
  1298. echo_content skyBlue "---> Trojan Panel Core update completed"
  1299. else
  1300. echo_content red "---> Trojan Panel Core update fails or runs abnormally, please try to repair or uninstall and reinstall"
  1301. fi
  1302. else
  1303. echo_content skyBlue "---> You have installed the latest version of the Trojan Panel Core"
  1304. fi
  1305. }
  1306. # Uninstall Caddy2+https
  1307. uninstall_caddy2() {
  1308. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-caddy$") ]]; then
  1309. echo_content green "---> Uninstall Caddy2+https"
  1310. docker rm -f trojan-panel-caddy &&
  1311. rm -rf ${CADDY_DATA}
  1312. echo_content skyBlue "---> Caddy2+https uninstallation completed"
  1313. else
  1314. echo_content red "---> Please install Caddy2+https first"
  1315. fi
  1316. }
  1317. # Uninstall Nginx
  1318. uninstall_nginx() {
  1319. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-nginx") ]]; then
  1320. echo_content green "---> Uninstall Nginx"
  1321. docker rm -f trojan-panel-nginx &&
  1322. rm -rf ${NGINX_DATA}
  1323. echo_content skyBlue "---> Nginx uninstallation completed"
  1324. else
  1325. echo_content red "---> Please install Nginx first"
  1326. fi
  1327. }
  1328. # Uninstall MariaDB
  1329. uninstall_mariadb() {
  1330. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-mariadb$") ]]; then
  1331. echo_content green "---> Uninstall MariaDB"
  1332. docker rm -f trojan-panel-mariadb &&
  1333. rm -rf ${MARIA_DATA}
  1334. echo_content skyBlue "---> MariaDB uninstall completed"
  1335. else
  1336. echo_content red "---> Please install MariaDB first"
  1337. fi
  1338. }
  1339. # Uninstall Redis
  1340. uninstall_redis() {
  1341. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-redis$") ]]; then
  1342. echo_content green "---> Uninstall Redis"
  1343. docker rm -f trojan-panel-redis &&
  1344. rm -rf ${REDIS_DATA}
  1345. echo_content skyBlue "---> Redis uninstall completed"
  1346. else
  1347. echo_content red "---> Please install Redis first"
  1348. fi
  1349. }
  1350. # Uninstall Trojan Panel Frontend
  1351. uninstall_trojan_panel_ui() {
  1352. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-ui$") ]]; then
  1353. echo_content green "---> Uninstall Trojan Panel Frontend"
  1354. docker rm -f trojan-panel-ui &&
  1355. docker rmi -f jonssonyan/trojan-panel-ui:2.1.6 &&
  1356. rm -rf ${TROJAN_PANEL_UI_DATA}
  1357. echo_content skyBlue "---> Trojan Panel Frontend uninstallation completed"
  1358. else
  1359. echo_content red "---> Please install the Trojan Panel Frontend first"
  1360. fi
  1361. }
  1362. # Uninstall Trojan Panel Backend
  1363. uninstall_trojan_panel() {
  1364. if [[ -n $(docker ps -a -q -f "name=^trojan-panel$") ]]; then
  1365. echo_content green "---> Uninstall Trojan Panel Backend"
  1366. docker rm -f trojan-panel &&
  1367. docker rmi -f jonssonyan/trojan-panel:2.2.0 &&
  1368. rm -rf ${TROJAN_PANEL_DATA}
  1369. echo_content skyBlue "---> Trojan Panel Backend uninstallation completed"
  1370. else
  1371. echo_content red "---> Please install the Trojan Panel Backend first"
  1372. fi
  1373. }
  1374. # Uninstall Trojan Panel Core
  1375. uninstall_trojan_panel_core() {
  1376. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-core$") ]]; then
  1377. echo_content green "---> Uninstall Trojan Panel Core"
  1378. docker rm -f trojan-panel-core &&
  1379. docker rmi -f jonssonyan/trojan-panel-core:2.2.1 &&
  1380. rm -rf ${TROJAN_PANEL_CORE_DATA}
  1381. echo_content skyBlue "---> Trojan Panel Core uninstallation completed"
  1382. else
  1383. echo_content red "---> Please install the Trojan Panel Core first"
  1384. fi
  1385. }
  1386. # Uninstall all Trojan Panel related containers
  1387. uninstall_all() {
  1388. echo_content green "---> Uninstall all Trojan Panel related containers"
  1389. docker rm -f $(docker ps -a -q -f "name=^trojan-panel")
  1390. docker rmi -f $(docker images | grep "^jonssonyan/trojan-panel" | awk '{print $3}')
  1391. rm -rf ${TP_DATA}
  1392. echo_content skyBlue "---> Uninstall all Trojan Panel related containers completed"
  1393. }
  1394. # Modify Trojan Panel Frontend port
  1395. update_trojan_panel_ui_port() {
  1396. if [[ -n $(docker ps -q -f "name=^trojan-panel-ui$" -f "status=running") ]]; then
  1397. echo_content green "---> Modify Trojan Panel Frontend port"
  1398. trojan_panel_ui_port=$(grep 'listen.*ssl' ${UI_NGINX_CONFIG} | awk '{print $2}')
  1399. if [[ -z "${trojan_panel_ui_port}" ]]; then
  1400. ui_https=0
  1401. trojan_panel_ui_port=$(grep -oP 'listen\s+\K\d+' ${UI_NGINX_CONFIG} | awk 'NR==1')
  1402. fi
  1403. if [[ -z "${trojan_panel_ui_port}" ]]; then
  1404. echo_content red "---> Trojan Panel Frontend port not queried"
  1405. exit 0
  1406. fi
  1407. echo_content yellow "Tip: The current port of the Trojan Panel Frontend (trojan-panel-ui) is ${trojan_panel_ui_port}"
  1408. read -r -p "Please enter the new port of the Trojan Panel Frontend (default: 8888): " trojan_panel_ui_port
  1409. [[ -z "${trojan_panel_ui_port}" ]] && trojan_panel_ui_port="8888"
  1410. if [[ ${ui_https} == 0 ]]; then
  1411. # http
  1412. sed -i "s/listen.*;/listen ${trojan_panel_ui_port};/g" ${UI_NGINX_CONFIG} &&
  1413. sed -i "s/http:\/\/\$host:.*\$request_uri;/http:\/\/\$host:${trojan_panel_ui_port}\$request_uri;/g" ${UI_NGINX_CONFIG} &&
  1414. docker restart trojan-panel-ui
  1415. else
  1416. # https
  1417. sed -i "s/listen.*ssl;/listen ${trojan_panel_ui_port} ssl;/g" ${UI_NGINX_CONFIG} &&
  1418. sed -i "s/https:\/\/\$host:.*\$request_uri;/https:\/\/\$host:${trojan_panel_ui_port}\$request_uri;/g" ${UI_NGINX_CONFIG} &&
  1419. docker restart trojan-panel-ui
  1420. fi
  1421. if [[ "$?" == "0" ]]; then
  1422. echo_content skyBlue "---> Trojan Panel Frontend port modification completed"
  1423. else
  1424. echo_content red "---> Trojan Panel Frontend port modification failed"
  1425. fi
  1426. else
  1427. echo_content red "---> The Trojan Panel Frontend is not installed or is running abnormally, please repair or uninstall and reinstall and try again"
  1428. fi
  1429. }
  1430. # Refresh Redis cache
  1431. redis_flush_all() {
  1432. if [[ -z $(docker ps -a -q -f "name=^trojan-panel-redis$") ]]; then
  1433. echo_content red "---> Please install Redis first"
  1434. exit 0
  1435. fi
  1436. if [[ -z $(docker ps -q -f "name=^trojan-panel-redis$" -f "status=running") ]]; then
  1437. echo_content red "---> Redis is running abnormally"
  1438. exit 0
  1439. fi
  1440. echo_content green "---> Refresh Redis cache"
  1441. read -r -p "Please enter the IP address of Redis (default: local host): " redis_host
  1442. [[ -z "${redis_host}" ]] && redis_host="127.0.0.1"
  1443. read -r -p "Please enter the port of Redis (default: 6378): " redis_port
  1444. [[ -z "${redis_port}" ]] && redis_port=6378
  1445. while read -r -p "Please enter the Redis password (required): " redis_pass; do
  1446. if [[ -z "${redis_pass}" ]]; then
  1447. echo_content red "Password can not be empty"
  1448. else
  1449. break
  1450. fi
  1451. done
  1452. docker exec trojan-panel-redis redis-cli -h "${redis_host}" -p "${redis_port}" -a "${redis_pass}" -e "flushall" &>/dev/null
  1453. echo_content skyBlue "---> Redis cache refresh completed"
  1454. }
  1455. # Replace certificate
  1456. change_cert() {
  1457. domain_1=$(cat "${DOMAIN_FILE}")
  1458. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-caddy$") ]]; then
  1459. docker rm -f trojan-panel-caddy &&
  1460. rm -rf ${CADDY_LOG}* &&
  1461. echo "" >${CADDY_CONFIG} &&
  1462. rm -rf ${WEB_PATH}*
  1463. fi
  1464. rm -rf ${CERT_PATH}* &&
  1465. echo "" >${DOMAIN_FILE}
  1466. install_reverse_proxy
  1467. install_cert
  1468. domain_2=$(cat "${DOMAIN_FILE}")
  1469. if [[ -n "${domain_1}" && -n "${domain_2}" ]]; then
  1470. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-nginx$") ]]; then
  1471. sed -i "s/${domain_1}/${domain_2}/g" ${NGINX_CONFIG} &&
  1472. docker restart trojan-panel-nginx
  1473. fi
  1474. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-ui$") ]]; then
  1475. sed -i "s/${domain_1}/${domain_2}/g" ${UI_NGINX_DATA} &&
  1476. docker restart trojan-panel-ui
  1477. fi
  1478. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-core$") ]]; then
  1479. find /tpdata/trojan-panel-core/bin/ -type f -exec sed -i "s/${domain_1}/${domain_2}/g" {} + &&
  1480. sed -i "s/${domain_1}/${domain_2}/g" ${trojan_panel_core_config_path} &&
  1481. docker restart trojan-panel-core
  1482. fi
  1483. fi
  1484. }
  1485. # Forgot sysadmin password
  1486. forget_pass() {
  1487. while :; do
  1488. echo_content yellow "1. Query MariaDB password"
  1489. echo_content yellow "2. Query Redis password"
  1490. echo_content yellow "3. Reset the username and password of the admin panel system administrator"
  1491. echo_content yellow "4. Quit"
  1492. read -r -p "Please choose (default: 4): " forget_pass_option
  1493. [[ -z "${forget_pass_option}" ]] && forget_pass_option=4
  1494. case ${forget_pass_option} in
  1495. 1)
  1496. if [[ -n $(docker ps -a -q -f "name=^trojan-panel$") ]]; then
  1497. mariadb_user=$(get_ini_value ${trojan_panel_config_path} mysql.user)
  1498. mariadb_pas=$(get_ini_value ${trojan_panel_config_path} mysql.password)
  1499. echo_content red "\n=============================================================="
  1500. echo_content yellow "MariaDB ${mariadb_user} password (please keep it safe): ${mariadb_pas}"
  1501. echo_content red "\n=============================================================="
  1502. else
  1503. echo_content red "---> Please execute on the Trojan Panel backend server"
  1504. fi
  1505. ;;
  1506. 2)
  1507. if [[ -n $(docker ps -a -q -f "name=^trojan-panel$") ]]; then
  1508. redis_pass=$(get_ini_value ${trojan_panel_config_path} redis.password)
  1509. echo_content red "\n=============================================================="
  1510. echo_content yellow "Redis password (please keep it safe): ${redis_pass}"
  1511. echo_content red "\n=============================================================="
  1512. else
  1513. echo_content red "---> Please execute on the Trojan Panel backend server"
  1514. fi
  1515. ;;
  1516. 3)
  1517. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-mariadb$") ]]; then
  1518. read -r -p "Please enter the IP address of MariaDB (default: local host): " mariadb_ip
  1519. [[ -z "${mariadb_ip}" ]] && mariadb_ip="127.0.0.1"
  1520. read -r -p "Please enter the port of MariaDB (default: 9507): " mariadb_port
  1521. [[ -z "${mariadb_port}" ]] && mariadb_port=9507
  1522. read -r -p "Please enter the username of MariaDB (default: root): " mariadb_user
  1523. [[ -z "${mariadb_user}" ]] && mariadb_user="root"
  1524. while read -r -p "Please enter the password of MariaDB (required): " mariadb_pas; do
  1525. if [[ -z "${mariadb_pas}" ]]; then
  1526. echo_content red "Password can not be empty"
  1527. else
  1528. break
  1529. fi
  1530. done
  1531. docker exec trojan-panel-mariadb mysql --default-character-set=utf8 -h"${mariadb_ip}" -P"${mariadb_port}" -u"${mariadb_user}" -p"${mariadb_pas}" -Dtrojan_panel_db -e "update account set username = 'sysadmin',pass = 'tFjD2X1F6i9FfWp2GDU5Vbi1conuaChDKIYbw9zMFrqvMoSz',hash='4366294571b8b267d9cf15b56660f0a70659568a86fc270a52fdc9e5' where id = 1 limit 1"
  1532. if [[ "$?" == "0" ]]; then
  1533. echo_content red "\n=============================================================="
  1534. echo_content yellow "System administrator Default username: sysadmin Default password: 123456"
  1535. echo_content yellow "Please log in to the management panel to change the password in time"
  1536. echo_content red "\n=============================================================="
  1537. else
  1538. echo_content red "Admin panel sysadmin username and password reset failed"
  1539. fi
  1540. else
  1541. echo_content red "---> Please execute on the MariaDB server"
  1542. fi
  1543. ;;
  1544. 4)
  1545. break
  1546. ;;
  1547. *)
  1548. echo_content red "No such option"
  1549. continue
  1550. ;;
  1551. esac
  1552. done
  1553. }
  1554. # Fault detection
  1555. failure_testing() {
  1556. echo_content green "---> Start troubleshooting"
  1557. if [[ ! $(docker -v 2>/dev/null) ]]; then
  1558. echo_content red "---> Docker is running abnormally"
  1559. else
  1560. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-caddy$") ]]; then
  1561. if [[ -z $(docker ps -q -f "name=^trojan-panel-caddy$" -f "status=running") ]]; then
  1562. echo_content red "---> Caddy2 is running abnormally and the running log is as follows:"
  1563. docker logs trojan-panel-caddy
  1564. fi
  1565. domain=$(cat "${DOMAIN_FILE}")
  1566. if [[ -n ${domain} && ! -f "${CERT_PATH}${domain}.crt" ]]; then
  1567. echo_content red "---> The certificate application is abnormal, please try 1. Change the sub-domain name to re-build 2. Restart the server to re-apply for the certificate 3. Re-build and select the custom certificate option"
  1568. if [[ -f ${CADDY_LOG}error.log ]]; then
  1569. echo_content red "Caddy2 error log is as follows:"
  1570. tail -n 20 ${CADDY_LOG}error.log | grep error
  1571. fi
  1572. fi
  1573. fi
  1574. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-mariadb$") && -z $(docker ps -q -f "name=^trojan-panel-mariadb$" -f "status=running") ]]; then
  1575. echo_content red "---> The MariaDB is running abnormally and the running log is as follows:"
  1576. docker logs trojan-panel-mariadb
  1577. fi
  1578. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-redis$") && -z $(docker ps -q -f "name=^trojan-panel-redis$" -f "status=running") ]]; then
  1579. echo_content red "---> The Redis is running abnormally and the running log is as follows:"
  1580. docker logs trojan-panel-redis
  1581. fi
  1582. if [[ -n $(docker ps -a -q -f "name=^trojan-panel$") && -z $(docker ps -q -f "name=^trojan-panel$" -f "status=running") ]]; then
  1583. echo_content red "---> The Trojan Panel Backend is running abnormally and the running log is as follows:"
  1584. if [[ -f ${TROJAN_PANEL_LOGS}trojan-panel.log ]]; then
  1585. tail -n 20 ${TROJAN_PANEL_LOGS}trojan-panel.log | grep error
  1586. else
  1587. docker logs trojan-panel
  1588. fi
  1589. fi
  1590. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-ui$") && -z $(docker ps -q -f "name=^trojan-panel-ui$" -f "status=running") ]]; then
  1591. echo_content red "---> The Trojan Panel Frontend is running abnormally and the running log is as follows:"
  1592. docker logs trojan-panel-ui
  1593. fi
  1594. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-core$") && -z $(docker ps -q -f "name=^trojan-panel-core$" -f "status=running") ]]; then
  1595. echo_content red "---> The Trojan Panel Core is running abnormally and the running log is as follows:"
  1596. if [[ -f ${TROJAN_PANEL_CORE_LOGS}trojan-panel.log ]]; then
  1597. tail -n 20 ${TROJAN_PANEL_CORE_LOGS}trojan-panel.log | grep error
  1598. else
  1599. docker logs trojan-panel-core
  1600. fi
  1601. fi
  1602. fi
  1603. echo_content green "---> Troubleshooting ended"
  1604. }
  1605. log_query() {
  1606. while :; do
  1607. echo_content skyBlue "Applications that can query logs are as follows:"
  1608. echo_content yellow "1. Trojan Panel Backend"
  1609. echo_content yellow "2. Trojan Panel Frontend"
  1610. echo_content yellow "3. Quit"
  1611. read -r -p "Please select an application (default: 3): " select_log_query_type
  1612. [[ -z "${select_log_query_type}" ]] && select_log_query_type=3
  1613. case ${select_log_query_type} in
  1614. 1)
  1615. log_file_path=${TROJAN_PANEL_LOGS}trojan-panel.log
  1616. ;;
  1617. 2)
  1618. log_file_path=${TROJAN_PANEL_CORE_LOGS}trojan-panel-core.log
  1619. ;;
  1620. 3)
  1621. break
  1622. ;;
  1623. *)
  1624. echo_content red "No such option"
  1625. continue
  1626. ;;
  1627. esac
  1628. read -r -p "Please enter the number of rows to query (default: 20): " select_log_query_line_type
  1629. [[ -z "${select_log_query_line_type}" ]] && select_log_query_line_type=20
  1630. if [[ -f ${log_file_path} ]]; then
  1631. echo_content skyBlue "The log is as follows:"
  1632. tail -n ${select_log_query_line_type} ${log_file_path}
  1633. else
  1634. echo_content red "No log file exists"
  1635. fi
  1636. done
  1637. }
  1638. version_query() {
  1639. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-ui$") && -n $(docker ps -q -f "name=^trojan-panel-ui$" -f "status=running") ]]; then
  1640. trojan_panel_ui_current_version=$(docker exec trojan-panel-ui cat ${TROJAN_PANEL_UI_DATA}version)
  1641. echo_content yellow "The current version of Trojan Panel Frontend(trojan-panel-ui) is ${trojan_panel_ui_current_version} the latest version is ${trojan_panel_ui_latest_version}"
  1642. fi
  1643. if [[ -n $(docker ps -a -q -f "name=^trojan-panel$") && -n $(docker ps -q -f "name=^trojan-panel$" -f "status=running") ]]; then
  1644. trojan_panel_current_version=$(docker exec trojan-panel ./trojan-panel -version)
  1645. echo_content yellow "The current version of Trojan Panel Backend(trojan-panel) is ${trojan_panel_current_version} the latest version is ${trojan_panel_latest_version}"
  1646. fi
  1647. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-core$") && -n $(docker ps -q -f "name=^trojan-panel-core$" -f "status=running") ]]; then
  1648. trojan_panel_core_current_version=$(docker exec trojan-panel-core ./trojan-panel-core -version)
  1649. echo_content yellow "The current version of Trojan Panel Core(trojan-panel-core) is ${trojan_panel_core_current_version} the latest version is ${trojan_panel_core_latest_version}"
  1650. fi
  1651. }
  1652. main() {
  1653. cd "$HOME" || exit 0
  1654. init_var
  1655. mkdir_tools
  1656. check_sys
  1657. depend_install
  1658. clear
  1659. echo_content red "\n=============================================================="
  1660. echo_content skyBlue "System Required: CentOS 7+/Ubuntu 18+/Debian 10+"
  1661. echo_content skyBlue "Version: v2.2.0"
  1662. echo_content skyBlue "Description: One click Install Trojan Panel server"
  1663. echo_content skyBlue "Author: jonssonyan <https://jonssonyan.com>"
  1664. echo_content skyBlue "Github: https://github.com/trojanpanel"
  1665. echo_content skyBlue "Docs: https://trojanpanel.github.io"
  1666. echo_content red "\n=============================================================="
  1667. echo_content yellow "1. Install Trojan Panel Frontend"
  1668. echo_content yellow "2. Install Trojan Panel Backend"
  1669. echo_content yellow "3. Install Trojan Panel Core"
  1670. echo_content yellow "4. Install Caddy2+https"
  1671. echo_content yellow "5. Install Nginx"
  1672. echo_content yellow "6. Install MariaDB"
  1673. echo_content yellow "7. Install Redis"
  1674. echo_content green "\n=============================================================="
  1675. echo_content yellow "8. Update Trojan Panel Frontend"
  1676. echo_content yellow "9. Update Trojan Panel Backend"
  1677. echo_content yellow "10. Update Trojan Panel Core"
  1678. echo_content green "\n=============================================================="
  1679. echo_content yellow "11. Uninstall Trojan Panel Frontend"
  1680. echo_content yellow "12. Uninstall Trojan Panel Backend"
  1681. echo_content yellow "13. Uninstall Trojan Panel Core"
  1682. echo_content yellow "14. Uninstall Caddy2+https"
  1683. echo_content yellow "15. Uninstall Nginx"
  1684. echo_content yellow "16. Uninstall MariaDB"
  1685. echo_content yellow "17. Uninstall Redis"
  1686. echo_content yellow "18. Uninstall all Trojan Panel related containers"
  1687. echo_content green "\n=============================================================="
  1688. echo_content yellow "19. Modify Trojan Panel Frontend port"
  1689. echo_content yellow "20. Refresh Redis cache"
  1690. echo_content yellow "21. Replace certificate"
  1691. echo_content yellow "22. Forgot sysadmin password"
  1692. echo_content green "\n=============================================================="
  1693. echo_content yellow "23. Fault detection"
  1694. echo_content yellow "24. Log query"
  1695. echo_content yellow "25. Version query"
  1696. read -r -p "Please choose: " selectInstall_type
  1697. case ${selectInstall_type} in
  1698. 1)
  1699. install_docker
  1700. install_reverse_proxy
  1701. install_cert
  1702. install_trojan_panel_ui
  1703. ;;
  1704. 2)
  1705. install_docker
  1706. install_mariadb
  1707. install_redis
  1708. install_trojan_panel
  1709. ;;
  1710. 3)
  1711. install_docker
  1712. install_reverse_proxy
  1713. install_cert
  1714. install_trojan_panel_core
  1715. ;;
  1716. 4)
  1717. install_docker
  1718. install_caddy2
  1719. ;;
  1720. 5)
  1721. install_docker
  1722. install_nginx
  1723. ;;
  1724. 6)
  1725. install_docker
  1726. install_mariadb
  1727. ;;
  1728. 7)
  1729. install_docker
  1730. install_redis
  1731. ;;
  1732. 8)
  1733. update_trojan_panel_ui
  1734. ;;
  1735. 9)
  1736. update_trojan_panel
  1737. ;;
  1738. 10)
  1739. update_trojan_panel_core
  1740. ;;
  1741. 11)
  1742. uninstall_trojan_panel_ui
  1743. ;;
  1744. 12)
  1745. uninstall_trojan_panel
  1746. ;;
  1747. 13)
  1748. uninstall_trojan_panel_core
  1749. ;;
  1750. 14)
  1751. uninstall_caddy2
  1752. ;;
  1753. 15)
  1754. uninstall_nginx
  1755. ;;
  1756. 16)
  1757. uninstall_mariadb
  1758. ;;
  1759. 17)
  1760. uninstall_redis
  1761. ;;
  1762. 18)
  1763. uninstall_all
  1764. ;;
  1765. 19)
  1766. update_trojan_panel_ui_port
  1767. ;;
  1768. 20)
  1769. redis_flush_all
  1770. ;;
  1771. 21)
  1772. change_cert
  1773. ;;
  1774. 22)
  1775. forget_pass
  1776. ;;
  1777. 23)
  1778. failure_testing
  1779. ;;
  1780. 24)
  1781. log_query
  1782. ;;
  1783. 25)
  1784. version_query
  1785. ;;
  1786. *)
  1787. echo_content red "No such option"
  1788. ;;
  1789. esac
  1790. }
  1791. main