install_script.sh 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011
  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.2.0"
  84. trojan_panel_current_version=""
  85. trojan_panel_latest_version="v2.3.0"
  86. trojan_panel_core_current_version=""
  87. trojan_panel_core_latest_version="v2.3.0"
  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 &&
  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
  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 &&
  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
  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 &&
  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_LOGS}:${TROJAN_PANEL_CORE_LOGS} \
  1116. -v ${TROJAN_PANEL_CORE_CONFIG}:${TROJAN_PANEL_CORE_CONFIG} \
  1117. -v ${CERT_PATH}:${CERT_PATH} \
  1118. -v ${WEB_PATH}:${WEB_PATH} \
  1119. -v /etc/localtime:/etc/localtime \
  1120. -e GIN_MODE=release \
  1121. -e "mariadb_ip=${mariadb_ip}" \
  1122. -e "mariadb_port=${mariadb_port}" \
  1123. -e "mariadb_user=${mariadb_user}" \
  1124. -e "mariadb_pas=${mariadb_pas}" \
  1125. -e "database=${database}" \
  1126. -e "account-table=${account_table}" \
  1127. -e "redis_host=${redis_host}" \
  1128. -e "redis_port=${redis_port}" \
  1129. -e "redis_pass=${redis_pass}" \
  1130. -e "crt_path=${CERT_PATH}${domain}.crt" \
  1131. -e "key_path=${CERT_PATH}${domain}.key" \
  1132. -e "grpc_port=${grpc_port}" \
  1133. -e "server_port=${trojan_panel_core_port}" \
  1134. jonssonyan/trojan-panel-core
  1135. if [[ -n $(docker ps -q -f "name=^trojan-panel-core$" -f "status=running") ]]; then
  1136. echo_content skyBlue "---> Trojan Panel Core installation completed"
  1137. else
  1138. echo_content red "---> Trojan Panel Core installation fails or runs abnormally, please try to repair or uninstall and reinstall"
  1139. exit 0
  1140. fi
  1141. else
  1142. echo_content skyBlue "---> You have installed the Trojan Panel Core"
  1143. fi
  1144. }
  1145. # Update Trojan Panel database structure
  1146. update_trojan_panel_database() {
  1147. echo_content skyBlue "---> Update Trojan Panel database structure"
  1148. version_214_215=("v2.1.4")
  1149. if [[ "${version_214_215[*]}" =~ "${trojan_panel_current_version}" ]]; then
  1150. 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 &&
  1151. trojan_panel_current_version="v2.1.5"
  1152. fi
  1153. version_215_230=("v2.1.5" "v2.1.6" "v2.1.7" "v2.1.8" "v2.2.0" "v2.2.1")
  1154. if [[ "${version_215_230[*]}" =~ "${trojan_panel_current_version}" ]]; then
  1155. 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 &&
  1156. trojan_panel_current_version="v2.3.0"
  1157. fi
  1158. echo_content skyBlue "---> Trojan Panel database structure update completed"
  1159. }
  1160. # Update Trojan Panel Core database structure
  1161. update_trojan_panel_core_database() {
  1162. echo_content skyBlue "---> Update Trojan Panel Core database structure"
  1163. echo_content skyBlue "---> Trojan Panel Core database structure update completed"
  1164. }
  1165. # Update Trojan Panel Frontend
  1166. update_trojan_panel_ui() {
  1167. if [[ -z $(docker ps -a -q -f "name=^trojan-panel-ui$") ]]; then
  1168. echo_content red "---> Please install the Trojan Panel Frontend first"
  1169. exit 0
  1170. fi
  1171. trojan_panel_ui_current_version=$(docker exec trojan-panel-ui cat ${TROJAN_PANEL_UI_DATA}version)
  1172. if [[ -z "${trojan_panel_ui_current_version}" || ! "${trojan_panel_ui_current_version}" =~ ^v.* ]]; then
  1173. echo_content red "---> The current version does not support online updates"
  1174. exit 0
  1175. fi
  1176. 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}"
  1177. if [[ "${trojan_panel_ui_current_version}" != "${trojan_panel_ui_latest_version}" ]]; then
  1178. echo_content green "---> Update Trojan Panel Frontend"
  1179. docker rm -f trojan-panel-ui &&
  1180. docker rmi -f jonssonyan/trojan-panel-ui
  1181. docker pull jonssonyan/trojan-panel-ui &&
  1182. docker run -d --name trojan-panel-ui --restart always \
  1183. --network=host \
  1184. -v "${UI_NGINX_CONFIG}":"/etc/nginx/conf.d/default.conf" \
  1185. -v ${CERT_PATH}:${CERT_PATH} \
  1186. jonssonyan/trojan-panel-ui
  1187. if [[ -n $(docker ps -q -f "name=^trojan-panel-ui$" -f "status=running") ]]; then
  1188. echo_content skyBlue "---> Trojan Panel Frontend update completed"
  1189. else
  1190. echo_content red "---> Trojan Panel Frontend update fails or runs abnormally, please try to repair or uninstall and reinstall"
  1191. fi
  1192. else
  1193. echo_content skyBlue "---> You have installed the latest version of the Trojan Panel Frontend"
  1194. fi
  1195. }
  1196. # Update Trojan Panel Backend
  1197. update_trojan_panel() {
  1198. if [[ -z $(docker ps -a -q -f "name=^trojan-panel$") ]]; then
  1199. echo_content red "---> Please install the Trojan Panel Backend first"
  1200. exit 0
  1201. fi
  1202. trojan_panel_current_version=$(docker exec trojan-panel ./trojan-panel -version)
  1203. if [[ -z "${trojan_panel_current_version}" || ! "${trojan_panel_current_version}" =~ ^v.* || ! $(version_ge "${trojan_panel_current_version}" "v2.1.4") ]]; then
  1204. echo_content red "---> The current version does not support online updates"
  1205. exit 0
  1206. fi
  1207. 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}"
  1208. if [[ "${trojan_panel_current_version}" != "${trojan_panel_latest_version}" ]]; then
  1209. echo_content green "---> Update Trojan Panel Backend"
  1210. mariadb_ip=$(get_ini_value ${trojan_panel_config_path} mysql.host)
  1211. mariadb_port=$(get_ini_value ${trojan_panel_config_path} mysql.port)
  1212. mariadb_user=$(get_ini_value ${trojan_panel_config_path} mysql.user)
  1213. mariadb_pas=$(get_ini_value ${trojan_panel_config_path} mysql.password)
  1214. redis_host=$(get_ini_value ${trojan_panel_config_path} redis.host)
  1215. redis_port=$(get_ini_value ${trojan_panel_config_path} redis.port)
  1216. redis_pass=$(get_ini_value ${trojan_panel_config_path} redis.password)
  1217. trojan_panel_port=$(get_ini_value ${trojan_panel_config_path} server.port)
  1218. update_trojan_panel_database
  1219. docker exec trojan-panel-redis redis-cli -h "${redis_host}" -p "${redis_port}" -a "${redis_pass}" -e "flushall" &>/dev/null
  1220. docker rm -f trojan-panel &&
  1221. docker rmi -f jonssonyan/trojan-panel
  1222. docker pull jonssonyan/trojan-panel &&
  1223. docker run -d --name trojan-panel --restart always \
  1224. --network=host \
  1225. -v ${WEB_PATH}:${TROJAN_PANEL_WEBFILE} \
  1226. -v ${TROJAN_PANEL_LOGS}:${TROJAN_PANEL_LOGS} \
  1227. -v ${TROJAN_PANEL_CONFIG}:${TROJAN_PANEL_CONFIG} \
  1228. -v /etc/localtime:/etc/localtime \
  1229. -e GIN_MODE=release \
  1230. -e "mariadb_ip=${mariadb_ip}" \
  1231. -e "mariadb_port=${mariadb_port}" \
  1232. -e "mariadb_user=${mariadb_user}" \
  1233. -e "mariadb_pas=${mariadb_pas}" \
  1234. -e "redis_host=${redis_host}" \
  1235. -e "redis_port=${redis_port}" \
  1236. -e "redis_pass=${redis_pass}" \
  1237. -e "server_port=${trojan_panel_port}" \
  1238. jonssonyan/trojan-panel
  1239. if [[ -n $(docker ps -q -f "name=^trojan-panel$" -f "status=running") ]]; then
  1240. echo_content skyBlue "---> Trojan Panel backend update completed"
  1241. else
  1242. echo_content red "---> Trojan Panel Backend update fails or runs abnormally, please try to repair or uninstall and reinstall"
  1243. fi
  1244. else
  1245. echo_content skyBlue "---> You have installed the latest version of the Trojan Panel Backend"
  1246. fi
  1247. }
  1248. # Update Trojan Panel Core
  1249. update_trojan_panel_core() {
  1250. if [[ -z $(docker ps -a -q -f "name=^trojan-panel-core$") ]]; then
  1251. echo_content red "---> Please install the Trojan Panel Core first"
  1252. exit 0
  1253. fi
  1254. trojan_panel_core_current_version=$(docker exec trojan-panel-core ./trojan-panel-core -version)
  1255. if [[ -z "${trojan_panel_core_current_version}" || ! "${trojan_panel_core_current_version}" =~ ^v.* || ! $(version_ge "${trojan_panel_core_current_version}" "v2.1.1") ]]; then
  1256. echo_content red "---> The current version does not support online updates"
  1257. exit 0
  1258. fi
  1259. 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}"
  1260. if [[ "${trojan_panel_core_current_version}" != "${trojan_panel_core_latest_version}" ]]; then
  1261. echo_content green "---> Update Trojan Panel Core"
  1262. mariadb_ip=$(get_ini_value ${trojan_panel_core_config_path} mysql.host)
  1263. mariadb_port=$(get_ini_value ${trojan_panel_core_config_path} mysql.port)
  1264. mariadb_user=$(get_ini_value ${trojan_panel_core_config_path} mysql.user)
  1265. mariadb_pas=$(get_ini_value ${trojan_panel_core_config_path} mysql.password)
  1266. redis_host=$(get_ini_value ${trojan_panel_core_config_path} redis.host)
  1267. redis_port=$(get_ini_value ${trojan_panel_core_config_path} redis.port)
  1268. redis_pass=$(get_ini_value ${trojan_panel_core_config_path} redis.password)
  1269. grpc_port=$(get_ini_value ${trojan_panel_core_config_path} grpc.port)
  1270. trojan_panel_core_port=$(get_ini_value ${trojan_panel_core_config_path} server.port)
  1271. update_trojan_panel_core_database
  1272. docker exec trojan-panel-redis redis-cli -h "${redis_host}" -p "${redis_port}" -a "${redis_pass}" -e "flushall" &>/dev/null
  1273. docker rm -f trojan-panel-core &&
  1274. docker rmi -f jonssonyan/trojan-panel-core
  1275. domain=$(cat "${DOMAIN_FILE}")
  1276. docker pull jonssonyan/trojan-panel-core &&
  1277. docker run -d --name trojan-panel-core --restart always \
  1278. --network=host \
  1279. -v ${TROJAN_PANEL_CORE_DATA}bin/xray/config/:${TROJAN_PANEL_CORE_DATA}bin/xray/config/ \
  1280. -v ${TROJAN_PANEL_CORE_DATA}bin/trojango/config/:${TROJAN_PANEL_CORE_DATA}bin/trojango/config/ \
  1281. -v ${TROJAN_PANEL_CORE_DATA}bin/hysteria/config/:${TROJAN_PANEL_CORE_DATA}bin/hysteria/config/ \
  1282. -v ${TROJAN_PANEL_CORE_DATA}bin/naiveproxy/config/:${TROJAN_PANEL_CORE_DATA}bin/naiveproxy/config/ \
  1283. -v ${TROJAN_PANEL_CORE_LOGS}:${TROJAN_PANEL_CORE_LOGS} \
  1284. -v ${TROJAN_PANEL_CORE_CONFIG}:${TROJAN_PANEL_CORE_CONFIG} \
  1285. -v ${CERT_PATH}:${CERT_PATH} \
  1286. -v ${WEB_PATH}:${WEB_PATH} \
  1287. -v /etc/localtime:/etc/localtime \
  1288. -e GIN_MODE=release \
  1289. -e "mariadb_ip=${mariadb_ip}" \
  1290. -e "mariadb_port=${mariadb_port}" \
  1291. -e "mariadb_user=${mariadb_user}" \
  1292. -e "mariadb_pas=${mariadb_pas}" \
  1293. -e "database=${database}" \
  1294. -e "account-table=${account_table}" \
  1295. -e "redis_host=${redis_host}" \
  1296. -e "redis_port=${redis_port}" \
  1297. -e "redis_pass=${redis_pass}" \
  1298. -e "crt_path=${CERT_PATH}${domain}.crt" \
  1299. -e "key_path=${CERT_PATH}${domain}.key" \
  1300. -e "grpc_port=${grpc_port}" \
  1301. -e "server_port=${trojan_panel_core_port}" \
  1302. jonssonyan/trojan-panel-core
  1303. if [[ -n $(docker ps -q -f "name=^trojan-panel-core$" -f "status=running") ]]; then
  1304. echo_content skyBlue "---> Trojan Panel Core update completed"
  1305. else
  1306. echo_content red "---> Trojan Panel Core update fails or runs abnormally, please try to repair or uninstall and reinstall"
  1307. fi
  1308. else
  1309. echo_content skyBlue "---> You have installed the latest version of the Trojan Panel Core"
  1310. fi
  1311. }
  1312. # Uninstall Caddy2+https
  1313. uninstall_caddy2() {
  1314. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-caddy$") ]]; then
  1315. echo_content green "---> Uninstall Caddy2+https"
  1316. docker rm -f trojan-panel-caddy &&
  1317. rm -rf ${CADDY_DATA}
  1318. echo_content skyBlue "---> Caddy2+https uninstallation completed"
  1319. else
  1320. echo_content red "---> Please install Caddy2+https first"
  1321. fi
  1322. }
  1323. # Uninstall Nginx
  1324. uninstall_nginx() {
  1325. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-nginx") ]]; then
  1326. echo_content green "---> Uninstall Nginx"
  1327. docker rm -f trojan-panel-nginx &&
  1328. rm -rf ${NGINX_DATA}
  1329. echo_content skyBlue "---> Nginx uninstallation completed"
  1330. else
  1331. echo_content red "---> Please install Nginx first"
  1332. fi
  1333. }
  1334. # Uninstall MariaDB
  1335. uninstall_mariadb() {
  1336. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-mariadb$") ]]; then
  1337. echo_content green "---> Uninstall MariaDB"
  1338. docker rm -f trojan-panel-mariadb &&
  1339. rm -rf ${MARIA_DATA}
  1340. echo_content skyBlue "---> MariaDB uninstall completed"
  1341. else
  1342. echo_content red "---> Please install MariaDB first"
  1343. fi
  1344. }
  1345. # Uninstall Redis
  1346. uninstall_redis() {
  1347. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-redis$") ]]; then
  1348. echo_content green "---> Uninstall Redis"
  1349. docker rm -f trojan-panel-redis &&
  1350. rm -rf ${REDIS_DATA}
  1351. echo_content skyBlue "---> Redis uninstall completed"
  1352. else
  1353. echo_content red "---> Please install Redis first"
  1354. fi
  1355. }
  1356. # Uninstall Trojan Panel Frontend
  1357. uninstall_trojan_panel_ui() {
  1358. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-ui$") ]]; then
  1359. echo_content green "---> Uninstall Trojan Panel Frontend"
  1360. docker rm -f trojan-panel-ui &&
  1361. docker rmi -f jonssonyan/trojan-panel-ui &&
  1362. rm -rf ${TROJAN_PANEL_UI_DATA}
  1363. echo_content skyBlue "---> Trojan Panel Frontend uninstallation completed"
  1364. else
  1365. echo_content red "---> Please install the Trojan Panel Frontend first"
  1366. fi
  1367. }
  1368. # Uninstall Trojan Panel Backend
  1369. uninstall_trojan_panel() {
  1370. if [[ -n $(docker ps -a -q -f "name=^trojan-panel$") ]]; then
  1371. echo_content green "---> Uninstall Trojan Panel Backend"
  1372. docker rm -f trojan-panel &&
  1373. docker rmi -f jonssonyan/trojan-panel &&
  1374. rm -rf ${TROJAN_PANEL_DATA}
  1375. echo_content skyBlue "---> Trojan Panel Backend uninstallation completed"
  1376. else
  1377. echo_content red "---> Please install the Trojan Panel Backend first"
  1378. fi
  1379. }
  1380. # Uninstall Trojan Panel Core
  1381. uninstall_trojan_panel_core() {
  1382. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-core$") ]]; then
  1383. echo_content green "---> Uninstall Trojan Panel Core"
  1384. docker rm -f trojan-panel-core &&
  1385. docker rmi -f jonssonyan/trojan-panel-core &&
  1386. rm -rf ${TROJAN_PANEL_CORE_DATA}
  1387. echo_content skyBlue "---> Trojan Panel Core uninstallation completed"
  1388. else
  1389. echo_content red "---> Please install the Trojan Panel Core first"
  1390. fi
  1391. }
  1392. # Uninstall all Trojan Panel related containers
  1393. uninstall_all() {
  1394. echo_content green "---> Uninstall all Trojan Panel related containers"
  1395. docker rm -f $(docker ps -a -q -f "name=^trojan-panel")
  1396. docker rmi -f $(docker images | grep "^jonssonyan/trojan-panel" | awk '{print $3}')
  1397. rm -rf ${TP_DATA}
  1398. echo_content skyBlue "---> Uninstall all Trojan Panel related containers completed"
  1399. }
  1400. # Modify Trojan Panel Frontend port
  1401. update_trojan_panel_ui_port() {
  1402. if [[ -n $(docker ps -q -f "name=^trojan-panel-ui$" -f "status=running") ]]; then
  1403. echo_content green "---> Modify Trojan Panel Frontend port"
  1404. trojan_panel_ui_port=$(grep 'listen.*ssl' ${UI_NGINX_CONFIG} | awk '{print $2}')
  1405. if [[ -z "${trojan_panel_ui_port}" ]]; then
  1406. ui_https=0
  1407. trojan_panel_ui_port=$(grep -oP 'listen\s+\K\d+' ${UI_NGINX_CONFIG} | awk 'NR==1')
  1408. fi
  1409. if [[ -z "${trojan_panel_ui_port}" ]]; then
  1410. echo_content red "---> Trojan Panel Frontend port not queried"
  1411. exit 0
  1412. fi
  1413. echo_content yellow "Tip: The current port of the Trojan Panel Frontend (trojan-panel-ui) is ${trojan_panel_ui_port}"
  1414. read -r -p "Please enter the new port of the Trojan Panel Frontend (default: 8888): " trojan_panel_ui_port
  1415. [[ -z "${trojan_panel_ui_port}" ]] && trojan_panel_ui_port="8888"
  1416. if [[ ${ui_https} == 0 ]]; then
  1417. # http
  1418. sed -i "s/listen.*;/listen ${trojan_panel_ui_port};/g" ${UI_NGINX_CONFIG} &&
  1419. sed -i "s/http:\/\/\$host:.*\$request_uri;/http:\/\/\$host:${trojan_panel_ui_port}\$request_uri;/g" ${UI_NGINX_CONFIG} &&
  1420. docker restart trojan-panel-ui
  1421. else
  1422. # https
  1423. sed -i "s/listen.*ssl;/listen ${trojan_panel_ui_port} ssl;/g" ${UI_NGINX_CONFIG} &&
  1424. sed -i "s/https:\/\/\$host:.*\$request_uri;/https:\/\/\$host:${trojan_panel_ui_port}\$request_uri;/g" ${UI_NGINX_CONFIG} &&
  1425. docker restart trojan-panel-ui
  1426. fi
  1427. if [[ "$?" == "0" ]]; then
  1428. echo_content skyBlue "---> Trojan Panel Frontend port modification completed"
  1429. else
  1430. echo_content red "---> Trojan Panel Frontend port modification failed"
  1431. fi
  1432. else
  1433. echo_content red "---> The Trojan Panel Frontend is not installed or is running abnormally, please repair or uninstall and reinstall and try again"
  1434. fi
  1435. }
  1436. # Refresh Redis cache
  1437. redis_flush_all() {
  1438. if [[ -z $(docker ps -a -q -f "name=^trojan-panel-redis$") ]]; then
  1439. echo_content red "---> Please install Redis first"
  1440. exit 0
  1441. fi
  1442. if [[ -z $(docker ps -q -f "name=^trojan-panel-redis$" -f "status=running") ]]; then
  1443. echo_content red "---> Redis is running abnormally"
  1444. exit 0
  1445. fi
  1446. echo_content green "---> Refresh Redis cache"
  1447. read -r -p "Please enter the IP address of Redis (default: local host): " redis_host
  1448. [[ -z "${redis_host}" ]] && redis_host="127.0.0.1"
  1449. read -r -p "Please enter the port of Redis (default: 6378): " redis_port
  1450. [[ -z "${redis_port}" ]] && redis_port=6378
  1451. while read -r -p "Please enter the Redis password (required): " redis_pass; do
  1452. if [[ -z "${redis_pass}" ]]; then
  1453. echo_content red "Password can not be empty"
  1454. else
  1455. break
  1456. fi
  1457. done
  1458. docker exec trojan-panel-redis redis-cli -h "${redis_host}" -p "${redis_port}" -a "${redis_pass}" -e "flushall" &>/dev/null
  1459. echo_content skyBlue "---> Redis cache refresh completed"
  1460. }
  1461. # Replace certificate
  1462. change_cert() {
  1463. domain_1=$(cat "${DOMAIN_FILE}")
  1464. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-caddy$") ]]; then
  1465. docker rm -f trojan-panel-caddy &&
  1466. rm -rf ${CADDY_LOG}* &&
  1467. echo "" >${CADDY_CONFIG} &&
  1468. rm -rf ${WEB_PATH}*
  1469. fi
  1470. rm -rf ${CERT_PATH}* &&
  1471. echo "" >${DOMAIN_FILE}
  1472. install_reverse_proxy
  1473. install_cert
  1474. domain_2=$(cat "${DOMAIN_FILE}")
  1475. if [[ -n "${domain_1}" && -n "${domain_2}" ]]; then
  1476. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-nginx$") ]]; then
  1477. sed -i "s/${domain_1}/${domain_2}/g" ${NGINX_CONFIG} &&
  1478. docker restart trojan-panel-nginx
  1479. fi
  1480. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-ui$") ]]; then
  1481. sed -i "s/${domain_1}/${domain_2}/g" ${UI_NGINX_DATA} &&
  1482. docker restart trojan-panel-ui
  1483. fi
  1484. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-core$") ]]; then
  1485. find /tpdata/trojan-panel-core/bin/ -type f -exec sed -i "s/${domain_1}/${domain_2}/g" {} + &&
  1486. sed -i "s/${domain_1}/${domain_2}/g" ${trojan_panel_core_config_path} &&
  1487. docker restart trojan-panel-core
  1488. fi
  1489. fi
  1490. }
  1491. # Forgot sysadmin password
  1492. forget_pass() {
  1493. while :; do
  1494. echo_content yellow "1. Query MariaDB password"
  1495. echo_content yellow "2. Query Redis password"
  1496. echo_content yellow "3. Reset the username and password of the admin panel system administrator"
  1497. echo_content yellow "4. Quit"
  1498. read -r -p "Please choose (default: 4): " forget_pass_option
  1499. [[ -z "${forget_pass_option}" ]] && forget_pass_option=4
  1500. case ${forget_pass_option} in
  1501. 1)
  1502. if [[ -n $(docker ps -a -q -f "name=^trojan-panel$") ]]; then
  1503. mariadb_user=$(get_ini_value ${trojan_panel_config_path} mysql.user)
  1504. mariadb_pas=$(get_ini_value ${trojan_panel_config_path} mysql.password)
  1505. echo_content red "\n=============================================================="
  1506. echo_content yellow "MariaDB ${mariadb_user} password (please keep it safe): ${mariadb_pas}"
  1507. echo_content red "\n=============================================================="
  1508. else
  1509. echo_content red "---> Please execute on the Trojan Panel backend server"
  1510. fi
  1511. ;;
  1512. 2)
  1513. if [[ -n $(docker ps -a -q -f "name=^trojan-panel$") ]]; then
  1514. redis_pass=$(get_ini_value ${trojan_panel_config_path} redis.password)
  1515. echo_content red "\n=============================================================="
  1516. echo_content yellow "Redis password (please keep it safe): ${redis_pass}"
  1517. echo_content red "\n=============================================================="
  1518. else
  1519. echo_content red "---> Please execute on the Trojan Panel backend server"
  1520. fi
  1521. ;;
  1522. 3)
  1523. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-mariadb$") ]]; then
  1524. read -r -p "Please enter the IP address of MariaDB (default: local host): " mariadb_ip
  1525. [[ -z "${mariadb_ip}" ]] && mariadb_ip="127.0.0.1"
  1526. read -r -p "Please enter the port of MariaDB (default: 9507): " mariadb_port
  1527. [[ -z "${mariadb_port}" ]] && mariadb_port=9507
  1528. read -r -p "Please enter the username of MariaDB (default: root): " mariadb_user
  1529. [[ -z "${mariadb_user}" ]] && mariadb_user="root"
  1530. while read -r -p "Please enter the password of MariaDB (required): " mariadb_pas; do
  1531. if [[ -z "${mariadb_pas}" ]]; then
  1532. echo_content red "Password can not be empty"
  1533. else
  1534. break
  1535. fi
  1536. done
  1537. 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"
  1538. if [[ "$?" == "0" ]]; then
  1539. echo_content red "\n=============================================================="
  1540. echo_content yellow "System administrator Default username: sysadmin Default password: 123456"
  1541. echo_content yellow "Please log in to the management panel to change the password in time"
  1542. echo_content red "\n=============================================================="
  1543. else
  1544. echo_content red "Admin panel sysadmin username and password reset failed"
  1545. fi
  1546. else
  1547. echo_content red "---> Please execute on the MariaDB server"
  1548. fi
  1549. ;;
  1550. 4)
  1551. break
  1552. ;;
  1553. *)
  1554. echo_content red "No such option"
  1555. continue
  1556. ;;
  1557. esac
  1558. done
  1559. }
  1560. # Fault detection
  1561. failure_testing() {
  1562. echo_content green "---> Start troubleshooting"
  1563. if [[ ! $(docker -v 2>/dev/null) ]]; then
  1564. echo_content red "---> Docker is running abnormally"
  1565. else
  1566. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-caddy$") ]]; then
  1567. if [[ -z $(docker ps -q -f "name=^trojan-panel-caddy$" -f "status=running") ]]; then
  1568. echo_content red "---> Caddy2 is running abnormally and the running log is as follows:"
  1569. docker logs trojan-panel-caddy
  1570. fi
  1571. domain=$(cat "${DOMAIN_FILE}")
  1572. if [[ -n ${domain} && ! -f "${CERT_PATH}${domain}.crt" ]]; then
  1573. 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"
  1574. if [[ -f ${CADDY_LOG}error.log ]]; then
  1575. echo_content red "Caddy2 error log is as follows:"
  1576. tail -n 20 ${CADDY_LOG}error.log | grep error
  1577. fi
  1578. fi
  1579. fi
  1580. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-mariadb$") && -z $(docker ps -q -f "name=^trojan-panel-mariadb$" -f "status=running") ]]; then
  1581. echo_content red "---> The MariaDB is running abnormally and the running log is as follows:"
  1582. docker logs trojan-panel-mariadb
  1583. fi
  1584. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-redis$") && -z $(docker ps -q -f "name=^trojan-panel-redis$" -f "status=running") ]]; then
  1585. echo_content red "---> The Redis is running abnormally and the running log is as follows:"
  1586. docker logs trojan-panel-redis
  1587. fi
  1588. if [[ -n $(docker ps -a -q -f "name=^trojan-panel$") && -z $(docker ps -q -f "name=^trojan-panel$" -f "status=running") ]]; then
  1589. echo_content red "---> The Trojan Panel Backend is running abnormally and the running log is as follows:"
  1590. if [[ -f ${TROJAN_PANEL_LOGS}trojan-panel.log ]]; then
  1591. tail -n 20 ${TROJAN_PANEL_LOGS}trojan-panel.log | grep error
  1592. else
  1593. docker logs trojan-panel
  1594. fi
  1595. fi
  1596. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-ui$") && -z $(docker ps -q -f "name=^trojan-panel-ui$" -f "status=running") ]]; then
  1597. echo_content red "---> The Trojan Panel Frontend is running abnormally and the running log is as follows:"
  1598. docker logs trojan-panel-ui
  1599. fi
  1600. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-core$") && -z $(docker ps -q -f "name=^trojan-panel-core$" -f "status=running") ]]; then
  1601. echo_content red "---> The Trojan Panel Core is running abnormally and the running log is as follows:"
  1602. if [[ -f ${TROJAN_PANEL_CORE_LOGS}trojan-panel.log ]]; then
  1603. tail -n 20 ${TROJAN_PANEL_CORE_LOGS}trojan-panel.log | grep error
  1604. else
  1605. docker logs trojan-panel-core
  1606. fi
  1607. fi
  1608. fi
  1609. echo_content green "---> Troubleshooting ended"
  1610. }
  1611. log_query() {
  1612. while :; do
  1613. echo_content skyBlue "Applications that can query logs are as follows:"
  1614. echo_content yellow "1. Trojan Panel Backend"
  1615. echo_content yellow "2. Trojan Panel Frontend"
  1616. echo_content yellow "3. Quit"
  1617. read -r -p "Please select an application (default: 3): " select_log_query_type
  1618. [[ -z "${select_log_query_type}" ]] && select_log_query_type=3
  1619. case ${select_log_query_type} in
  1620. 1)
  1621. log_file_path=${TROJAN_PANEL_LOGS}trojan-panel.log
  1622. ;;
  1623. 2)
  1624. log_file_path=${TROJAN_PANEL_CORE_LOGS}trojan-panel-core.log
  1625. ;;
  1626. 3)
  1627. break
  1628. ;;
  1629. *)
  1630. echo_content red "No such option"
  1631. continue
  1632. ;;
  1633. esac
  1634. read -r -p "Please enter the number of rows to query (default: 20): " select_log_query_line_type
  1635. [[ -z "${select_log_query_line_type}" ]] && select_log_query_line_type=20
  1636. if [[ -f ${log_file_path} ]]; then
  1637. echo_content skyBlue "The log is as follows:"
  1638. tail -n ${select_log_query_line_type} ${log_file_path}
  1639. else
  1640. echo_content red "No log file exists"
  1641. fi
  1642. done
  1643. }
  1644. version_query() {
  1645. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-ui$") && -n $(docker ps -q -f "name=^trojan-panel-ui$" -f "status=running") ]]; then
  1646. trojan_panel_ui_current_version=$(docker exec trojan-panel-ui cat ${TROJAN_PANEL_UI_DATA}version)
  1647. 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}"
  1648. fi
  1649. if [[ -n $(docker ps -a -q -f "name=^trojan-panel$") && -n $(docker ps -q -f "name=^trojan-panel$" -f "status=running") ]]; then
  1650. trojan_panel_current_version=$(docker exec trojan-panel ./trojan-panel -version)
  1651. 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}"
  1652. fi
  1653. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-core$") && -n $(docker ps -q -f "name=^trojan-panel-core$" -f "status=running") ]]; then
  1654. trojan_panel_core_current_version=$(docker exec trojan-panel-core ./trojan-panel-core -version)
  1655. 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}"
  1656. fi
  1657. }
  1658. main() {
  1659. cd "$HOME" || exit 0
  1660. init_var
  1661. mkdir_tools
  1662. check_sys
  1663. depend_install
  1664. clear
  1665. echo_content red "\n=============================================================="
  1666. echo_content skyBlue "System Required: CentOS 7+/Ubuntu 18+/Debian 10+"
  1667. echo_content skyBlue "Version: v2.2.0"
  1668. echo_content skyBlue "Description: One click Install Trojan Panel server"
  1669. echo_content skyBlue "Author: jonssonyan <https://jonssonyan.com>"
  1670. echo_content skyBlue "Github: https://github.com/trojanpanel"
  1671. echo_content skyBlue "Docs: https://trojanpanel.github.io"
  1672. echo_content red "\n=============================================================="
  1673. echo_content yellow "1. Install Trojan Panel Frontend"
  1674. echo_content yellow "2. Install Trojan Panel Backend"
  1675. echo_content yellow "3. Install Trojan Panel Core"
  1676. echo_content yellow "4. Install Caddy2+https"
  1677. echo_content yellow "5. Install Nginx"
  1678. echo_content yellow "6. Install MariaDB"
  1679. echo_content yellow "7. Install Redis"
  1680. echo_content green "\n=============================================================="
  1681. echo_content yellow "8. Update Trojan Panel Frontend"
  1682. echo_content yellow "9. Update Trojan Panel Backend"
  1683. echo_content yellow "10. Update Trojan Panel Core"
  1684. echo_content green "\n=============================================================="
  1685. echo_content yellow "11. Uninstall Trojan Panel Frontend"
  1686. echo_content yellow "12. Uninstall Trojan Panel Backend"
  1687. echo_content yellow "13. Uninstall Trojan Panel Core"
  1688. echo_content yellow "14. Uninstall Caddy2+https"
  1689. echo_content yellow "15. Uninstall Nginx"
  1690. echo_content yellow "16. Uninstall MariaDB"
  1691. echo_content yellow "17. Uninstall Redis"
  1692. echo_content yellow "18. Uninstall all Trojan Panel related containers"
  1693. echo_content green "\n=============================================================="
  1694. echo_content yellow "19. Modify Trojan Panel Frontend port"
  1695. echo_content yellow "20. Refresh Redis cache"
  1696. echo_content yellow "21. Replace certificate"
  1697. echo_content yellow "22. Forgot sysadmin password"
  1698. echo_content green "\n=============================================================="
  1699. echo_content yellow "23. Fault detection"
  1700. echo_content yellow "24. Log query"
  1701. echo_content yellow "25. Version query"
  1702. read -r -p "Please choose: " selectInstall_type
  1703. case ${selectInstall_type} in
  1704. 1)
  1705. install_docker
  1706. install_reverse_proxy
  1707. install_cert
  1708. install_trojan_panel_ui
  1709. ;;
  1710. 2)
  1711. install_docker
  1712. install_mariadb
  1713. install_redis
  1714. install_trojan_panel
  1715. ;;
  1716. 3)
  1717. install_docker
  1718. install_reverse_proxy
  1719. install_cert
  1720. install_trojan_panel_core
  1721. ;;
  1722. 4)
  1723. install_docker
  1724. install_caddy2
  1725. ;;
  1726. 5)
  1727. install_docker
  1728. install_nginx
  1729. ;;
  1730. 6)
  1731. install_docker
  1732. install_mariadb
  1733. ;;
  1734. 7)
  1735. install_docker
  1736. install_redis
  1737. ;;
  1738. 8)
  1739. update_trojan_panel_ui
  1740. ;;
  1741. 9)
  1742. update_trojan_panel
  1743. ;;
  1744. 10)
  1745. update_trojan_panel_core
  1746. ;;
  1747. 11)
  1748. uninstall_trojan_panel_ui
  1749. ;;
  1750. 12)
  1751. uninstall_trojan_panel
  1752. ;;
  1753. 13)
  1754. uninstall_trojan_panel_core
  1755. ;;
  1756. 14)
  1757. uninstall_caddy2
  1758. ;;
  1759. 15)
  1760. uninstall_nginx
  1761. ;;
  1762. 16)
  1763. uninstall_mariadb
  1764. ;;
  1765. 17)
  1766. uninstall_redis
  1767. ;;
  1768. 18)
  1769. uninstall_all
  1770. ;;
  1771. 19)
  1772. update_trojan_panel_ui_port
  1773. ;;
  1774. 20)
  1775. redis_flush_all
  1776. ;;
  1777. 21)
  1778. change_cert
  1779. ;;
  1780. 22)
  1781. forget_pass
  1782. ;;
  1783. 23)
  1784. failure_testing
  1785. ;;
  1786. 24)
  1787. log_query
  1788. ;;
  1789. 25)
  1790. version_query
  1791. ;;
  1792. *)
  1793. echo_content red "No such option"
  1794. ;;
  1795. esac
  1796. }
  1797. main