install_script_standalone.sh 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. #!/usr/bin/env bash
  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  3. export PATH
  4. init_var() {
  5. ECHO_TYPE="echo -e"
  6. package_manager=""
  7. release=""
  8. get_arch=""
  9. can_google=0
  10. # Docker
  11. DOCKER_MIRROR='"https://registry.docker-cn.com","https://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn"'
  12. # Project directory
  13. TP_DATA="/tpdata/"
  14. STATIC_HTML="https://github.com/trojanpanel/install-script/releases/download/v1.0/html.tar.gz"
  15. # Web
  16. WEB_PATH="/tpdata/web/"
  17. # Cert
  18. CERT_PATH="/tpdata/cert/"
  19. DOMAIN_FILE="/tpdata/domain.lock"
  20. domain=""
  21. # Caddy2
  22. CADDY_DATA="/tpdata/caddy/"
  23. CADDY_CONFIG="${CADDY_DATA}config.json"
  24. CADDY_LOG="${CADDY_DATA}logs/"
  25. CADDY_CERT_DIR="${CERT_PATH}certificates/acme-v02.api.letsencrypt.org-directory/"
  26. caddy_port=80
  27. caddy_remote_port=8863
  28. your_email=""
  29. ssl_module_type=1
  30. ssl_module="acme"
  31. # TrojanGO
  32. TROJANGO_DATA="/tpdata/trojanGO/"
  33. TROJANGO_STANDALONE_CONFIG="/tpdata/trojanGO/standalone_config.json"
  34. trojanGO_port=443
  35. trojanGO_websocket_enable=0
  36. trojanGO_websocket_path="trojan-panel-websocket-path"
  37. trojanGO_shadowsocks_enable=0
  38. trojanGO_shadowsocks_method="AES-128-GCM"
  39. trojanGO_shadowsocks_password=""
  40. trojanGO_mux_enable=1
  41. # trojan
  42. trojan_pas=""
  43. remote_addr="127.0.0.1"
  44. # Hysteria
  45. HYSTERIA_DATA="/tpdata/hysteria/"
  46. HYSTERIA_STANDALONE_CONFIG="/tpdata/hysteria/standalone_config.json"
  47. hysteria_port=443
  48. hysteria_password=""
  49. hysteria_protocol="udp"
  50. hysteria_up_mbps=100
  51. hysteria_down_mbps=100
  52. # NaiveProxy
  53. NAIVEPROXY_DATA="/tpdata/naiveproxy/"
  54. NAIVEPROXY_STANDALONE_CONFIG="/tpdata/naiveproxy/standalone_config.json"
  55. naiveproxy_port=443
  56. naiveproxy_username=""
  57. naiveproxy_pass=""
  58. }
  59. echo_content() {
  60. case $1 in
  61. "red")
  62. ${ECHO_TYPE} "\033[31m$2\033[0m"
  63. ;;
  64. "green")
  65. ${ECHO_TYPE} "\033[32m$2\033[0m"
  66. ;;
  67. "yellow")
  68. ${ECHO_TYPE} "\033[33m$2\033[0m"
  69. ;;
  70. "blue")
  71. ${ECHO_TYPE} "\033[34m$2\033[0m"
  72. ;;
  73. "purple")
  74. ${ECHO_TYPE} "\033[35m$2\033[0m"
  75. ;;
  76. "skyBlue")
  77. ${ECHO_TYPE} "\033[36m$2\033[0m"
  78. ;;
  79. "white")
  80. ${ECHO_TYPE} "\033[37m$2\033[0m"
  81. ;;
  82. esac
  83. }
  84. mkdir_tools() {
  85. # Project directory
  86. mkdir -p ${TP_DATA}
  87. # Web
  88. mkdir -p ${WEB_PATH}
  89. # Cert
  90. mkdir -p ${CERT_PATH}
  91. touch ${DOMAIN_FILE}
  92. # Caddy2
  93. mkdir -p ${CADDY_DATA}
  94. touch ${CADDY_CONFIG}
  95. mkdir -p ${CADDY_LOG}
  96. # TrojanGO
  97. mkdir -p ${TROJANGO_DATA}
  98. touch ${TROJANGO_STANDALONE_CONFIG}
  99. # Hysteria
  100. mkdir -p ${HYSTERIA_DATA}
  101. touch ${HYSTERIA_STANDALONE_CONFIG}
  102. # NaiveProxy
  103. mkdir -p ${NAIVEPROXY_DATA}
  104. touch ${NAIVEPROXY_STANDALONE_CONFIG}
  105. }
  106. can_connect() {
  107. ping -c2 -i0.3 -W1 "$1" &>/dev/null
  108. if [[ "$?" == "0" ]]; then
  109. return 0
  110. else
  111. return 1
  112. fi
  113. }
  114. check_sys() {
  115. if [[ $(command -v yum) ]]; then
  116. package_manager='yum'
  117. elif [[ $(command -v dnf) ]]; then
  118. package_manager='dnf'
  119. elif [[ $(command -v apt) ]]; then
  120. package_manager='apt'
  121. elif [[ $(command -v apt-get) ]]; then
  122. package_manager='apt-get'
  123. fi
  124. if [[ -z "${package_manager}" ]]; then
  125. echo_content red "The system is not currently supported"
  126. exit 0
  127. fi
  128. if [[ -n $(find /etc -name "redhat-release") ]] || grep </proc/version -q -i "centos"; then
  129. release="centos"
  130. elif grep </etc/issue -q -i "debian" && [[ -f "/etc/issue" ]] || grep </etc/issue -q -i "debian" && [[ -f "/proc/version" ]]; then
  131. release="debian"
  132. elif grep </etc/issue -q -i "ubuntu" && [[ -f "/etc/issue" ]] || grep </etc/issue -q -i "ubuntu" && [[ -f "/proc/version" ]]; then
  133. release="ubuntu"
  134. fi
  135. if [[ -z "${release}" ]]; then
  136. echo_content red "The operating system only supports CentOS 7+/Ubuntu 18+/Debian 10+"
  137. exit 0
  138. fi
  139. if [[ $(arch) =~ ("x86_64"|"amd64"|"arm64"|"aarch64"|"arm"|"s390x") ]]; then
  140. get_arch=$(arch)
  141. fi
  142. if [[ -z "${get_arch}" ]]; then
  143. echo_content red "The processor architecture only supports amd64/arm64/arm/s390x"
  144. exit 0
  145. fi
  146. can_connect www.google.com
  147. [[ "$?" == "0" ]] && can_google=1
  148. }
  149. depend_install() {
  150. if [[ "${package_manager}" != 'yum' && "${package_manager}" != 'dnf' ]]; then
  151. ${package_manager} update -y
  152. fi
  153. ${package_manager} install -y \
  154. curl \
  155. wget \
  156. tar \
  157. lsof \
  158. systemd
  159. }
  160. # Install Docker
  161. install_docker() {
  162. if [[ ! $(docker -v 2>/dev/null) ]]; then
  163. echo_content green "---> Install Docker"
  164. # turn off firewall
  165. if [[ "$(firewall-cmd --state 2>/dev/null)" == "running" ]]; then
  166. if [[ "${release}" == "centos" ]]; then
  167. systemctl disable firewalld
  168. elif [[ "${release}" == "debian" || "${release}" == "ubuntu" ]]; then
  169. sudo ufw disable
  170. fi
  171. fi
  172. # set time zone
  173. timedatectl set-timezone Asia/Shanghai
  174. if [[ ${can_google} == 0 ]]; then
  175. sh <(curl -sL https://get.docker.com) --mirror Aliyun
  176. mkdir -p /etc/docker &&
  177. cat >/etc/docker/daemon.json <<EOF
  178. {
  179. "registry-mirrors":[${DOCKER_MIRROR}],
  180. "log-driver":"json-file",
  181. "log-opts":{
  182. "max-size":"50m",
  183. "max-file":"3"
  184. }
  185. }
  186. EOF
  187. else
  188. sh <(curl -sL https://get.docker.com)
  189. mkdir -p /etc/docker &&
  190. cat >/etc/docker/daemon.json <<EOF
  191. {
  192. "log-driver":"json-file",
  193. "log-opts":{
  194. "max-size":"50m",
  195. "max-file":"3"
  196. }
  197. }
  198. EOF
  199. fi
  200. systemctl enable docker &&
  201. systemctl restart docker
  202. if [[ $(docker -v 2>/dev/null) ]]; then
  203. echo_content skyBlue "---> Docker installation completed"
  204. else
  205. echo_content red "---> Docker installation failed"
  206. exit 0
  207. fi
  208. else
  209. echo_content skyBlue "---> You have installed Docker"
  210. fi
  211. }
  212. # Caddy2 https automatic application and renewal certificate configuration file
  213. caddy2_https_auto_config() {
  214. cat >${CADDY_CONFIG} <<EOF
  215. {
  216. "admin":{
  217. "disabled":true
  218. },
  219. "logging":{
  220. "logs":{
  221. "default":{
  222. "writer":{
  223. "output":"file",
  224. "filename":"${CADDY_LOG}error.log"
  225. },
  226. "level":"ERROR"
  227. }
  228. }
  229. },
  230. "storage":{
  231. "module":"file_system",
  232. "root":"${CERT_PATH}"
  233. },
  234. "apps":{
  235. "http":{
  236. "http_port": ${caddy_port},
  237. "servers":{
  238. "srv0":{
  239. "listen":[
  240. ":${caddy_port}"
  241. ],
  242. "routes":[
  243. {
  244. "match":[
  245. {
  246. "host":[
  247. "${domain}"
  248. ]
  249. }
  250. ],
  251. "handle":[
  252. {
  253. "handler":"static_response",
  254. "headers":{
  255. "Location":[
  256. "https://{http.request.host}:${caddy_remote_port}{http.request.uri}"
  257. ]
  258. },
  259. "status_code":301
  260. }
  261. ]
  262. }
  263. ]
  264. },
  265. "srv1":{
  266. "listen":[
  267. ":${caddy_remote_port}"
  268. ],
  269. "routes":[
  270. {
  271. "handle":[
  272. {
  273. "handler":"subroute",
  274. "routes":[
  275. {
  276. "match":[
  277. {
  278. "host":[
  279. "${domain}"
  280. ]
  281. }
  282. ],
  283. "handle":[
  284. {
  285. "handler":"file_server",
  286. "root":"${WEB_PATH}",
  287. "index_names":[
  288. "index.html",
  289. "index.htm"
  290. ]
  291. }
  292. ],
  293. "terminal":true
  294. }
  295. ]
  296. }
  297. ]
  298. }
  299. ],
  300. "tls_connection_policies":[
  301. {
  302. "match":{
  303. "sni":[
  304. "${domain}"
  305. ]
  306. }
  307. }
  308. ],
  309. "automatic_https":{
  310. "disable":true
  311. }
  312. }
  313. }
  314. },
  315. "tls":{
  316. "certificates":{
  317. "automate":[
  318. "${domain}"
  319. ]
  320. },
  321. "automation":{
  322. "policies":[
  323. {
  324. "issuers":[
  325. {
  326. "module":"${ssl_module}",
  327. "email":"${your_email}"
  328. }
  329. ]
  330. }
  331. ]
  332. }
  333. }
  334. }
  335. }
  336. EOF
  337. }
  338. # Install Caddy2+https
  339. install_caddy2() {
  340. if [[ -z $(docker ps -a -q -f "name=^trojan-panel-caddy$") ]]; then
  341. echo_content green "---> Install Caddy2+https"
  342. wget --no-check-certificate -O ${WEB_PATH}html.tar.gz -N ${STATIC_HTML} &&
  343. tar -zxvf ${WEB_PATH}html.tar.gz -k -C ${WEB_PATH}
  344. read -r -p "Please enter the port of Caddy2 (default: 80): " caddy_port
  345. [[ -z "${caddy_port}" ]] && caddy_port=80
  346. read -r -p "Please enter the forwarding port of Caddy2 (default: 8863): " caddy_remote_port
  347. [[ -z "${caddy_remote_port}" ]] && caddy_remote_port=8863
  348. echo_content yellow "Tip: Please confirm that the domain name has been resolved to this machine, otherwise the installation may fail"
  349. while read -r -p "Please enter your domain name (required): " domain; do
  350. if [[ -z "${domain}" ]]; then
  351. echo_content red "Domain name cannot be empty"
  352. else
  353. break
  354. fi
  355. done
  356. read -r -p "Please enter your email (optional): " your_email
  357. while read -r -p "Please choose the way to apply for the certificate (1/acme 2/zerossl default: 1: " ssl_module_type; do
  358. if [[ -z "${ssl_module_type}" || ${ssl_module_type} == 1 ]]; then
  359. ssl_module="acme"
  360. CADDY_CERT_DIR="${CERT_PATH}certificates/acme-v02.api.letsencrypt.org-directory/"
  361. break
  362. elif [[ ${ssl_module_type} == 2 ]]; then
  363. ssl_module="zerossl"
  364. CADDY_CERT_DIR="${CERT_PATH}certificates/acme.zerossl.com-v2-dv90/"
  365. break
  366. else
  367. echo_content red "Cannot enter other characters except 1 and 2"
  368. fi
  369. done
  370. caddy2_https_auto_config
  371. # Caddy2 temporary listening port for automatic certificate application
  372. if [[ -n $(lsof -i:${caddy_port},${caddy_remote_port} -t) ]]; then
  373. kill -9 "$(lsof -i:${caddy_port},${caddy_remote_port} -t)"
  374. fi
  375. docker pull caddy:2.6.2 &&
  376. docker run -d --name trojan-panel-caddy --restart always \
  377. --network=host \
  378. -v "${CADDY_CONFIG}":"${CADDY_CONFIG}" \
  379. -v ${CERT_PATH}:"${CADDY_CERT_DIR}${domain}/" \
  380. -v ${WEB_PATH}:${WEB_PATH} \
  381. -v ${CADDY_LOG}:${CADDY_LOG} \
  382. caddy:2.6.2 caddy run --config ${CADDY_CONFIG}
  383. if [[ -n $(docker ps -q -f "name=^trojan-panel-caddy$" -f "status=running") ]]; then
  384. cat >${DOMAIN_FILE} <<EOF
  385. ${domain}
  386. EOF
  387. echo_content red "\n=============================================================="
  388. echo_content skyBlue "---> Caddy2+https installation completed"
  389. echo_content yellow "Certificate Directory: ${CERT_PATH}"
  390. echo_content red "\n=============================================================="
  391. else
  392. echo_content red "---> Caddy2+https installation fails or runs abnormally, please try to repair or uninstall and reinstall"
  393. exit 0
  394. fi
  395. else
  396. echo_content skyBlue "---> You have installed Caddy2+https"
  397. fi
  398. }
  399. # Install TrojanGO+Caddy2+Web+TLS+Websocket
  400. install_trojanGO_standalone() {
  401. if [[ -z $(docker ps -a -q -f "name=^trojan-panel-trojanGO-standalone$") ]]; then
  402. echo_content green "---> Install TrojanGO+Caddy2+Web+TLS+Websocket"
  403. read -r -p "Please enter the port of TrojanGO (default: 443): " trojanGO_port
  404. [[ -z "${trojanGO_port}" ]] && trojanGO_port=443
  405. while read -r -p "Please enter TrojanGO password (required): " trojan_pas; do
  406. if [[ -z "${trojan_pas}" ]]; then
  407. echo_content red "Password can not be empty"
  408. else
  409. break
  410. fi
  411. done
  412. while read -r -p "Is multiplexing enabled? (0/disabled 1/enabled default: 1): " trojanGO_mux_enable; do
  413. if [[ -z "${trojanGO_mux_enable}" || ${trojanGO_mux_enable} == 1 ]]; then
  414. trojanGO_mux_enable=1
  415. break
  416. elif [[ ${trojanGO_mux_enable} == 0 ]]; then
  417. trojanGO_mux_enable=0
  418. break
  419. else
  420. echo_content red "Cannot enter other characters except 0 and 1"
  421. fi
  422. done
  423. while read -r -p "Is Websocket enabled? (0/disabled 1/enabled default: 0): " trojanGO_websocket_enable; do
  424. if [[ -z "${trojanGO_websocket_enable}" || ${trojanGO_websocket_enable} == 0 ]]; then
  425. trojanGO_websocket_enable=0
  426. break
  427. elif [[ ${trojanGO_websocket_enable} == 1 ]]; then
  428. trojanGO_websocket_enable=1
  429. read -r -p "Please enter the Websocket path (default: trojan-panel-websocket-path): " trojanGO_websocket_path
  430. [[ -z "${trojanGO_websocket_path}" ]] && trojanGO_websocket_path="trojan-panel-websocket-path"
  431. break
  432. else
  433. echo_content red "Cannot enter other characters except 0 and 1"
  434. fi
  435. done
  436. while read -r -p "Do you want to enable Shadowsocks AEAD encryption? (0/disabled 1/enabled default: 0): " trojanGO_shadowsocks_enable; do
  437. if [[ -z "${trojanGO_shadowsocks_enable}" || ${trojanGO_shadowsocks_enable} == 0 ]]; then
  438. trojanGO_shadowsocks_enable=0
  439. break
  440. elif [[ ${trojanGO_shadowsocks_enable} == 1 ]]; then
  441. echo_content skyBlue "Shadowsocks AEAD encryption method is as follows:"
  442. echo_content yellow "1. AES-128-GCM(default)"
  443. echo_content yellow "2. CHACHA20-IETF-POLY1305"
  444. echo_content yellow "3. AES-256-GCM"
  445. read -r -p "Please enter the Shadowsocks AEAD encryption method (default: 1): " select_method_type
  446. [[ -z "${select_method_type}" ]] && select_method_type=1
  447. case ${select_method_type} in
  448. 1)
  449. trojanGO_shadowsocks_method="AES-128-GCM"
  450. ;;
  451. 2)
  452. trojanGO_shadowsocks_method="CHACHA20-IETF-POLY1305"
  453. ;;
  454. 3)
  455. trojanGO_shadowsocks_method="AES-256-GCM"
  456. ;;
  457. *)
  458. trojanGO_shadowsocks_method="AES-128-GCM"
  459. ;;
  460. esac
  461. while read -r -p "Please enter the Shadowsocks AEAD encryption password (required): " trojanGO_shadowsocks_password; do
  462. if [[ -z "${trojanGO_shadowsocks_password}" ]]; then
  463. echo_content red "Password can not be empty"
  464. else
  465. break
  466. fi
  467. done
  468. break
  469. else
  470. echo_content yellow "Cannot enter other characters except 0 and 1"
  471. fi
  472. done
  473. cat >${TROJANGO_STANDALONE_CONFIG} <<EOF
  474. {
  475. "run_type": "server",
  476. "local_addr": "0.0.0.0",
  477. "local_port": ${trojanGO_port},
  478. "remote_addr": "${remote_addr}",
  479. "remote_port": 80,
  480. "log_level": 1,
  481. "log_file": "",
  482. "password": [
  483. "${trojan_pas}"
  484. ],
  485. "disable_http_check": false,
  486. "udp_timeout": 60,
  487. "ssl": {
  488. "verify": true,
  489. "verify_hostname": true,
  490. "cert": "${CERT_PATH}${domain}.crt",
  491. "key": "${CERT_PATH}${domain}.key",
  492. "key_password": "",
  493. "cipher": "",
  494. "curves": "",
  495. "prefer_server_cipher": false,
  496. "sni": "",
  497. "alpn": [
  498. "http/1.1"
  499. ],
  500. "session_ticket": true,
  501. "reuse_session": true,
  502. "plain_http_response": "",
  503. "fallback_addr": "",
  504. "fallback_port": 80,
  505. "fingerprint": ""
  506. },
  507. "tcp": {
  508. "no_delay": true,
  509. "keep_alive": true,
  510. "prefer_ipv4": false
  511. },
  512. "mux": {
  513. "enabled": ${trojanGO_mux_enable},
  514. "concurrency": 8,
  515. "idle_timeout": 60
  516. },
  517. "websocket": {
  518. "enabled": ${trojanGO_websocket_enable},
  519. "path": "/${trojanGO_websocket_path}",
  520. "host": "${domain}"
  521. },
  522. "shadowsocks": {
  523. "enabled": ${trojanGO_shadowsocks_enable},
  524. "method": "${trojanGO_shadowsocks_method}",
  525. "password": "${trojanGO_shadowsocks_password}"
  526. },
  527. "mysql": {
  528. "enabled": false,
  529. "server_addr": "localhost",
  530. "server_port": 3306,
  531. "database": "",
  532. "username": "",
  533. "password": "",
  534. "check_rate": 60
  535. }
  536. }
  537. EOF
  538. docker pull p4gefau1t/trojan-go &&
  539. docker run -d --name trojan-panel-trojanGO-standalone --restart=always \
  540. --network=host \
  541. -v ${TROJANGO_STANDALONE_CONFIG}:"/etc/trojan-go/config.json" \
  542. -v ${CERT_PATH}:${CERT_PATH} \
  543. p4gefau1t/trojan-go
  544. if [[ -n $(docker ps -q -f "name=^trojan-panel-trojanGO-standalone$" -f "status=running") ]]; then
  545. echo_content skyBlue "---> TrojanGO+Caddy+Web+TLS+Websocket installation completed"
  546. echo_content red "\n=============================================================="
  547. echo_content skyBlue "TrojanGO+Caddy+Web+TLS+Websocket installed successfully"
  548. echo_content yellow "domain: ${domain}"
  549. echo_content yellow "Port of TrojanGO: ${trojanGO_port}"
  550. echo_content yellow "Password for TrojanGO: ${trojan_pas}"
  551. echo_content yellow "Certificate Directory: ${CERT_PATH}"
  552. if [[ ${trojanGO_websocket_enable} == 1 ]]; then
  553. echo_content yellow "Websocket Path: ${trojanGO_websocket_path}"
  554. fi
  555. if [[ ${trojanGO_shadowsocks_enable} == 1 ]]; then
  556. echo_content yellow "Shadowsocks AEAD encryption method: ${trojanGO_shadowsocks_method}"
  557. echo_content yellow "Shadowsocks AEAD encryption password: ${trojanGO_shadowsocks_password}"
  558. fi
  559. echo_content red "\n=============================================================="
  560. else
  561. echo_content red "---> TrojanGO+Caddy+Web+TLS+Websocket fails to install or runs abnormally, please try to repair or uninstall and reinstall"
  562. exit 0
  563. fi
  564. else
  565. echo_content skyBlue "---> You have installed TrojanGO+Caddy+Web+TLS+Websocket"
  566. fi
  567. }
  568. # Install Hysteria
  569. install_hysteria_standalone() {
  570. if [[ -z $(docker ps -a -q -f "name=^trojan-panel-hysteria-standalone$") ]]; then
  571. echo_content green "---> Install Hysteria"
  572. echo_content skyBlue "Hysteria's schema is as follows:"
  573. echo_content yellow "1. udp(default)"
  574. echo_content yellow "2. faketcp"
  575. read -r -p "Please enter the mode of Hysteria (default: 1): " selectProtocolType
  576. [[ -z "${selectProtocolType}" ]] && selectProtocolType=1
  577. case ${selectProtocolType} in
  578. 1)
  579. hysteria_protocol="udp"
  580. ;;
  581. 2)
  582. hysteria_protocol="faketcp"
  583. ;;
  584. *)
  585. hysteria_protocol="udp"
  586. ;;
  587. esac
  588. read -r -p "Please enter the port of Hysteria (default: 443): " hysteria_port
  589. [[ -z ${hysteria_port} ]] && hysteria_port=443
  590. read -r -p "Please enter the maximum upload speed of a single client/Mbps (default: 100): " hysteria_up_mbps
  591. [[ -z "${hysteria_up_mbps}" ]] && hysteria_up_mbps=100
  592. read -r -p "Please enter the maximum download speed of a single client/Mbps (default: 100): " hysteria_down_mbps
  593. [[ -z "${hysteria_down_mbps}" ]] && hysteria_down_mbps=100
  594. while read -r -p "Please enter the password of Hysteria (required): " hysteria_password; do
  595. if [[ -z ${hysteria_password} ]]; then
  596. echo_content red "Password can not be empty"
  597. else
  598. break
  599. fi
  600. done
  601. cat >${HYSTERIA_STANDALONE_CONFIG} <<EOF
  602. {
  603. "listen": ":${hysteria_port}",
  604. "protocol": "${hysteria_protocol}",
  605. "cert": "${CERT_PATH}${domain}.crt",
  606. "key": "${CERT_PATH}${domain}.key",
  607. "up_mbps": ${hysteria_up_mbps},
  608. "down_mbps": ${hysteria_down_mbps},
  609. "auth_str": "${hysteria_password}"
  610. }
  611. EOF
  612. docker pull tobyxdd/hysteria &&
  613. docker run -d --name trojan-panel-hysteria-standalone --restart=always \
  614. --network=host \
  615. -v ${HYSTERIA_STANDALONE_CONFIG}:/etc/hysteria.json \
  616. -v ${CERT_PATH}:${CERT_PATH} \
  617. tobyxdd/hysteria -c /etc/hysteria.json server
  618. if [[ -n $(docker ps -q -f "name=^trojan-panel-hysteria-standalone$" -f "status=running") ]]; then
  619. echo_content skyBlue "---> Hysteria installation completed"
  620. echo_content red "\n=============================================================="
  621. echo_content skyBlue "Hysteria installed successfully"
  622. echo_content yellow "domain: ${domain}"
  623. echo_content yellow "Port of Hysteria: ${hysteria_port}"
  624. echo_content yellow "Password for Hysteria: ${hysteria_password}"
  625. echo_content yellow "Certificate Directory: ${CERT_PATH}"
  626. echo_content red "\n=============================================================="
  627. else
  628. echo_content red "---> Hysteria installation fails or runs abnormally, please try to repair or uninstall and reinstall"
  629. exit 0
  630. fi
  631. else
  632. echo_content skyBlue "---> You have installed Hysteria"
  633. fi
  634. }
  635. # Install NaiveProxy (Caddy+ForwardProxy)
  636. install_navieproxy_standalone() {
  637. if [[ -z $(docker ps -a -q -f "name=^trojan-panel-navieproxy-standalone$") ]]; then
  638. echo_content green "---> Install NaiveProxy (Caddy+ForwardProxy)"
  639. read -r -p "Please enter the port of NaiveProxy (default: 443): " naiveproxy_port
  640. [[ -z "${naiveproxy_port}" ]] && naiveproxy_port=443
  641. while read -r -p "Please enter the username of NaiveProxy (required): " naiveproxy_username; do
  642. if [[ -z "${naiveproxy_username}" ]]; then
  643. echo_content red "Username can not be empty"
  644. else
  645. break
  646. fi
  647. done
  648. while read -r -p "Please enter the password of NaiveProxy (required): " naiveproxy_pass; do
  649. if [[ -z "${naiveproxy_pass}" ]]; then
  650. echo_content red "Password can not be empty"
  651. else
  652. break
  653. fi
  654. done
  655. domain=$(cat "${DOMAIN_FILE}")
  656. cat >${NAIVEPROXY_STANDALONE_CONFIG} <<EOF
  657. {
  658. "admin": {
  659. "disabled": true
  660. },
  661. "logging": {
  662. "sink": {
  663. "writer": {
  664. "output": "discard"
  665. }
  666. },
  667. "logs": {
  668. "default": {
  669. "writer": {
  670. "output": "discard"
  671. }
  672. }
  673. }
  674. },
  675. "apps": {
  676. "http": {
  677. "servers": {
  678. "srv0": {
  679. "listen": [
  680. ":${naiveproxy_port}"
  681. ],
  682. "routes": [
  683. {
  684. "handle": [
  685. {
  686. "handler": "subroute",
  687. "routes": [
  688. {
  689. "handle": [
  690. {
  691. "auth_pass_deprecated": "${naiveproxy_pass}",
  692. "auth_user_deprecated": "${naiveproxy_username}",
  693. "handler": "forward_proxy",
  694. "hide_ip": true,
  695. "hide_via": true,
  696. "probe_resistance": {}
  697. }
  698. ]
  699. },
  700. {
  701. "match": [
  702. {
  703. "host": [
  704. "${domain}"
  705. ]
  706. }
  707. ],
  708. "handle": [
  709. {
  710. "handler": "file_server",
  711. "root": "/caddy-forwardproxy/dist/",
  712. "index_names": [
  713. "index.html",
  714. "index.htm"
  715. ]
  716. }
  717. ],
  718. "terminal": true
  719. }
  720. ]
  721. }
  722. ]
  723. }
  724. ],
  725. "tls_connection_policies": [
  726. {
  727. "match": {
  728. "sni": [
  729. "${domain}"
  730. ]
  731. }
  732. }
  733. ],
  734. "automatic_https": {
  735. "disable": true
  736. }
  737. }
  738. }
  739. },
  740. "tls": {
  741. "certificates": {
  742. "load_files": [
  743. {
  744. "certificate": "${CERT_PATH}${domain}.crt",
  745. "key": "${CERT_PATH}${domain}.crt"
  746. }
  747. ]
  748. }
  749. }
  750. }
  751. }
  752. EOF
  753. docker pull jonssonyan/caddy-forwardproxy &&
  754. docker run -d --name trojan-panel-navieproxy-standalone --restart=always \
  755. --network=host \
  756. -v ${NAIVEPROXY_STANDALONE_CONFIG}:"/caddy-forwardproxy/config/config.json" \
  757. -v ${CERT_PATH}:${CERT_PATH} \
  758. jonssonyan/caddy-forwardproxy
  759. if [[ -n $(docker ps -q -f "name=^trojan-panel-navieproxy-standalone$" -f "status=running") ]]; then
  760. echo_content skyBlue "---> NaiveProxy(Caddy+ForwardProxy) installation completed"
  761. echo_content red "\n=============================================================="
  762. echo_content skyBlue "NaiveProxy(Caddy+ForwardProxy) installed successfully"
  763. echo_content yellow "domain: ${domain}"
  764. echo_content yellow "Port of NaiveProxy: ${naiveproxy_port}"
  765. echo_content yellow "Username for NaiveProxy: ${naiveproxy_username}"
  766. echo_content yellow "Password for NaiveProxy: ${naiveproxy_pass}"
  767. echo_content yellow "Certificate Directory: ${CERT_PATH}"
  768. echo_content red "\n=============================================================="
  769. else
  770. echo_content red "---> NaiveProxy(Caddy+ForwardProxy) failed to install or run abnormally, please try to repair or uninstall and reinstall"
  771. exit 0
  772. fi
  773. else
  774. echo_content skyBlue "---> You have installed NaiveProxy(Caddy+ForwardProxy)"
  775. fi
  776. }
  777. # Uninstall Caddy2
  778. uninstall_caddy2() {
  779. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-caddy$") ]]; then
  780. echo_content green "---> Uninstall Caddy2"
  781. docker rm -f trojan-panel-caddy &&
  782. rm -rf ${CADDY_DATA}
  783. echo_content skyBlue "---> Caddy2 uninstallation completed"
  784. else
  785. echo_content red "---> Please install Caddy2 first"
  786. fi
  787. }
  788. # Uninstall TrojanGO+Caddy+Web+TLS+Websocket
  789. uninstall_trojanGO_standalone() {
  790. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-trojanGO-standalone$") ]]; then
  791. echo_content green "---> Uninstall TrojanGO+Caddy+Web+TLS+Websocket"
  792. docker rm -f trojan-panel-trojanGO-standalone &&
  793. docker rmi -f p4gefau1t/trojan-go &&
  794. rm -f ${TROJANGO_STANDALONE_CONFIG}
  795. echo_content skyBlue "---> TrojanGO+Caddy+Web+TLS+Websocket uninstallation completed"
  796. else
  797. echo_content red "---> Please install TrojanGO+Caddy+Web+TLS+Websocket first"
  798. fi
  799. }
  800. # Uninstall Hysteria
  801. uninstall_hysteria_standalone() {
  802. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-hysteria-standalone$") ]]; then
  803. echo_content green "---> Uninstall Hysteria"
  804. docker rm -f trojan-panel-hysteria-standalone &&
  805. docker rmi -f tobyxdd/hysteria &&
  806. rm -f ${HYSTERIA_STANDALONE_CONFIG}
  807. echo_content skyBlue "---> Hysteria uninstallation completed"
  808. else
  809. echo_content red "---> Please install Hysteria"
  810. fi
  811. }
  812. # Uninstall NaiveProxy (Caddy+ForwardProxy)
  813. uninstall_navieproxy_standalone() {
  814. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-navieproxy-standalone$") ]]; then
  815. echo_content green "---> Uninstall NaiveProxy (Caddy+ForwardProxy)"
  816. docker rm -f trojan-panel-navieproxy-standalone &&
  817. docker rmi -f jonssonyan/caddy-forwardproxy &&
  818. rm -f ${NAIVEPROXY_STANDALONE_CONFIG}
  819. echo_content skyBlue "---> NaiveProxy(Caddy+ForwardProxy) uninstallation completed"
  820. else
  821. echo_content red "---> Please install NaiveProxy(Caddy+ForwardProxy)"
  822. fi
  823. }
  824. # Uninstall all Trojan Panel related containers
  825. uninstall_all() {
  826. echo_content green "---> Uninstall all Trojan Panel related containers"
  827. docker rm -f $(docker ps -a -q -f "name=^trojan-panel")
  828. docker rmi -f $(docker images | grep "^jonssonyan/trojan-panel" | awk '{print $3}')
  829. rm -rf ${TP_DATA}
  830. echo_content skyBlue "---> Uninstall all Trojan Panel related containers completed"
  831. }
  832. # Fault detection
  833. failure_testing() {
  834. echo_content green "---> Start troubleshooting"
  835. if [[ ! $(docker -v 2>/dev/null) ]]; then
  836. echo_content red "---> Docker is running abnormally"
  837. else
  838. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-caddy$") ]]; then
  839. if [[ -z $(docker ps -q -f "name=^trojan-panel-caddy$" -f "status=running") ]]; then
  840. echo_content red "---> Caddy2 is running abnormally and the running log is as follows:"
  841. docker logs trojan-panel-caddy
  842. fi
  843. domain=$(cat "${DOMAIN_FILE}")
  844. if [[ -n ${domain} && ! -f "${CERT_PATH}${domain}.crt" ]]; then
  845. 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"
  846. if [[ -f ${CADDY_LOG}error.log ]]; then
  847. echo_content red "Caddy2 error log is as follows:"
  848. tail -n 20 ${CADDY_LOG}error.log | grep error
  849. fi
  850. fi
  851. fi
  852. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-trojanGO-standalone$") && -z $(docker ps -q -f "name=^trojan-panel-trojanGO-standalone$" -f "status=running") ]]; then
  853. echo_content red "---> TrojanGO is running abnormally"
  854. fi
  855. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-hysteria-standalone$") && -z $(docker ps -q -f "name=^trojan-panel-hysteria-standalone$" -f "status=running") ]]; then
  856. echo_content red "---> Hysteria is running abnormally"
  857. fi
  858. if [[ -n $(docker ps -a -q -f "name=^trojan-panel-navieproxy-standalone$") && -z $(docker ps -q -f "name=^trojan-panel-navieproxy-standalone$" -f "status=running") ]]; then
  859. echo_content red "---> NaiveProxy(Caddy+ForwardProxy) is running abnormally"
  860. fi
  861. fi
  862. echo_content green "---> Troubleshooting ended"
  863. }
  864. main() {
  865. cd "$HOME" || exit 0
  866. init_var
  867. mkdir_tools
  868. check_sys
  869. depend_install
  870. clear
  871. echo_content red "\n=============================================================="
  872. echo_content skyBlue "System Required: CentOS 7+/Ubuntu 18+/Debian 10+"
  873. echo_content skyBlue "Description: One click Install Trojan Panel standalone server"
  874. echo_content skyBlue "Author: jonssonyan <https://jonssonyan.com>"
  875. echo_content skyBlue "Github: https://github.com/trojanpanel"
  876. echo_content skyBlue "Docs: https://trojanpanel.github.io"
  877. echo_content red "\n=============================================================="
  878. echo_content yellow "2. Install TrojanGO+Caddy2+Web+TLS+Websocket"
  879. echo_content yellow "3. Install Hysteria"
  880. echo_content yellow "4. Install NaiveProxy(Caddy2+ForwardProxy)"
  881. echo_content yellow "5. Install Caddy2+https"
  882. echo_content green "\n=============================================================="
  883. echo_content yellow "7. Uninstall TrojanGO+Caddy2+Web+TLS+Websocket"
  884. echo_content yellow "8. Uninstall Hysteria"
  885. echo_content yellow "9. Uninstall NaiveProxy(Caddy2+ForwardProxy)"
  886. echo_content yellow "10. Uninstall Caddy2+https"
  887. echo_content yellow "11. Uninstall all Trojan Panel related containers"
  888. echo_content green "\n=============================================================="
  889. echo_content yellow "12. Fault detection"
  890. read -r -p "Please choose: " selectInstall_type
  891. case ${selectInstall_type} in
  892. 1)
  893. install_docker
  894. install_caddy2
  895. install_trojanGO_standalone
  896. ;;
  897. 2)
  898. install_docker
  899. install_caddy2
  900. install_hysteria_standalone
  901. ;;
  902. 3)
  903. install_docker
  904. install_caddy2
  905. install_navieproxy_standalone
  906. ;;
  907. 4)
  908. install_docker
  909. install_caddy2
  910. ;;
  911. 5)
  912. uninstall_trojanGO_standalone
  913. ;;
  914. 6)
  915. uninstall_hysteria_standalone
  916. ;;
  917. 7)
  918. uninstall_navieproxy_standalone
  919. ;;
  920. 8)
  921. uninstall_caddy2
  922. ;;
  923. 9)
  924. uninstall_all
  925. ;;
  926. 10)
  927. failure_testing
  928. ;;
  929. *)
  930. echo_content red "No such option"
  931. ;;
  932. esac
  933. }
  934. main