install_script_standalone.sh 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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 "Version: v2.2.0"
  874. echo_content skyBlue "Description: One click Install Trojan Panel standalone server"
  875. echo_content skyBlue "Author: jonssonyan <https://jonssonyan.com>"
  876. echo_content skyBlue "Github: https://github.com/trojanpanel"
  877. echo_content skyBlue "Docs: https://trojanpanel.github.io"
  878. echo_content red "\n=============================================================="
  879. echo_content yellow "2. Install TrojanGO+Caddy2+Web+TLS+Websocket"
  880. echo_content yellow "3. Install Hysteria"
  881. echo_content yellow "4. Install NaiveProxy(Caddy2+ForwardProxy)"
  882. echo_content yellow "5. Install Caddy2+https"
  883. echo_content green "\n=============================================================="
  884. echo_content yellow "7. Uninstall TrojanGO+Caddy2+Web+TLS+Websocket"
  885. echo_content yellow "8. Uninstall Hysteria"
  886. echo_content yellow "9. Uninstall NaiveProxy(Caddy2+ForwardProxy)"
  887. echo_content yellow "10. Uninstall Caddy2+https"
  888. echo_content yellow "11. Uninstall all Trojan Panel related containers"
  889. echo_content green "\n=============================================================="
  890. echo_content yellow "12. Fault detection"
  891. read -r -p "Please choose: " selectInstall_type
  892. case ${selectInstall_type} in
  893. 1)
  894. install_docker
  895. install_caddy2
  896. install_trojanGO_standalone
  897. ;;
  898. 2)
  899. install_docker
  900. install_caddy2
  901. install_hysteria_standalone
  902. ;;
  903. 3)
  904. install_docker
  905. install_caddy2
  906. install_navieproxy_standalone
  907. ;;
  908. 4)
  909. install_docker
  910. install_caddy2
  911. ;;
  912. 5)
  913. uninstall_trojanGO_standalone
  914. ;;
  915. 6)
  916. uninstall_hysteria_standalone
  917. ;;
  918. 7)
  919. uninstall_navieproxy_standalone
  920. ;;
  921. 8)
  922. uninstall_caddy2
  923. ;;
  924. 9)
  925. uninstall_all
  926. ;;
  927. 10)
  928. failure_testing
  929. ;;
  930. *)
  931. echo_content red "No such option"
  932. ;;
  933. esac
  934. }
  935. main