install_script_v2.2.0.sh 70 KB

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