install_script_v2.3.2.sh 71 KB

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