install_script_v2.2.1.sh 70 KB

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