init.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/bin/bash
  2. author=233boy
  3. # github=https://github.com/233boy/sing-box
  4. # bash fonts colors
  5. red='\e[31m'
  6. yellow='\e[33m'
  7. gray='\e[90m'
  8. green='\e[92m'
  9. blue='\e[94m'
  10. magenta='\e[95m'
  11. cyan='\e[96m'
  12. none='\e[0m'
  13. _red() { echo -e ${red}$@${none}; }
  14. _blue() { echo -e ${blue}$@${none}; }
  15. _cyan() { echo -e ${cyan}$@${none}; }
  16. _green() { echo -e ${green}$@${none}; }
  17. _yellow() { echo -e ${yellow}$@${none}; }
  18. _magenta() { echo -e ${magenta}$@${none}; }
  19. _red_bg() { echo -e "\e[41m$@${none}"; }
  20. _rm() {
  21. rm -rf "$@"
  22. }
  23. _cp() {
  24. cp -rf "$@"
  25. }
  26. _sed() {
  27. sed -i "$@"
  28. }
  29. _mkdir() {
  30. mkdir -p "$@"
  31. }
  32. is_err=$(_red_bg 错误!)
  33. is_warn=$(_red_bg 警告!)
  34. err() {
  35. echo -e "\n$is_err $@\n"
  36. [[ $is_dont_auto_exit ]] && return
  37. exit 1
  38. }
  39. warn() {
  40. echo -e "\n$is_warn $@\n"
  41. }
  42. # load bash script.
  43. load() {
  44. . $is_sh_dir/src/$1
  45. }
  46. # wget add --no-check-certificate
  47. _wget() {
  48. # [[ $proxy ]] && export https_proxy=$proxy
  49. wget --no-check-certificate "$@"
  50. }
  51. # yum or apt-get
  52. cmd=$(type -P apt-get || type -P yum)
  53. # x64
  54. case $(arch) in
  55. amd64 | x86_64)
  56. is_arch="amd64"
  57. ;;
  58. *aarch64* | *armv8*)
  59. is_arch="arm64"
  60. ;;
  61. *)
  62. err "此脚本仅支持 64 位系统..."
  63. ;;
  64. esac
  65. is_core=sing-box
  66. is_core_name=sing-box
  67. is_core_dir=/develop/$is_core
  68. is_core_bin=$is_core_dir/bin/$is_core
  69. is_core_repo=SagerNet/$is_core
  70. is_conf_dir=$is_core_dir/conf
  71. is_log_dir=/var/log/$is_core
  72. is_sh_bin=/usr/local/bin/$is_core
  73. is_sh_dir=$is_core_dir/sh
  74. is_sh_repo=$author/$is_core
  75. is_pkg="wget unzip tar qrencode"
  76. is_config_json=$is_core_dir/config.json
  77. is_caddy_bin=/usr/local/bin/caddy
  78. is_caddy_dir=/etc/caddy
  79. is_caddy_repo=caddyserver/caddy
  80. is_caddyfile=$is_caddy_dir/Caddyfile
  81. is_caddy_conf=$is_caddy_dir/$author
  82. is_caddy_service=$(systemctl list-units --full -all | grep caddy.service)
  83. is_http_port=80
  84. is_https_port=443
  85. # core ver
  86. is_core_ver=$($is_core_bin version | head -n1 | cut -d " " -f3)
  87. # tmp tls key
  88. is_tls_cer=$is_core_dir/bin/tls.cer
  89. is_tls_key=$is_core_dir/bin/tls.key
  90. [[ ! -f $is_tls_cer || ! -f $is_tls_key ]] && {
  91. is_tls_tmp=${is_tls_key/key/tmp}
  92. $is_core_bin generate tls-keypair tls -m 456 >$is_tls_tmp
  93. awk '/BEGIN PRIVATE KEY/,/END PRIVATE KEY/' $is_tls_tmp >$is_tls_key
  94. awk '/BEGIN CERTIFICATE/,/END CERTIFICATE/' $is_tls_tmp >$is_tls_cer
  95. rm $is_tls_tmp
  96. }
  97. if [[ $(pgrep -f $is_core_bin) ]]; then
  98. is_core_status=$(_green running)
  99. else
  100. is_core_status=$(_red_bg stopped)
  101. is_core_stop=1
  102. fi
  103. if [[ -f $is_caddy_bin && -d $is_caddy_dir && $is_caddy_service ]]; then
  104. is_caddy=1
  105. # fix caddy run; ver >= 2.8.2
  106. [[ ! $(grep '\-\-adapter caddyfile' /lib/systemd/system/caddy.service) ]] && {
  107. load systemd.sh
  108. install_service caddy
  109. systemctl restart caddy &
  110. }
  111. is_caddy_ver=$($is_caddy_bin version | head -n1 | cut -d " " -f1)
  112. is_tmp_http_port=$(grep -E '^ {2,}http_port|^http_port' $is_caddyfile | grep -E -o [0-9]+)
  113. is_tmp_https_port=$(grep -E '^ {2,}https_port|^https_port' $is_caddyfile | grep -E -o [0-9]+)
  114. [[ $is_tmp_http_port ]] && is_http_port=$is_tmp_http_port
  115. [[ $is_tmp_https_port ]] && is_https_port=$is_tmp_https_port
  116. if [[ $(pgrep -f $is_caddy_bin) ]]; then
  117. is_caddy_status=$(_green running)
  118. else
  119. is_caddy_status=$(_red_bg stopped)
  120. is_caddy_stop=1
  121. fi
  122. fi
  123. load core.sh
  124. [[ ! $args ]] && args=main
  125. main $args