update.sh 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #!/bin/bash
  2. red='\033[0;31m'
  3. green='\033[0;32m'
  4. blue='\033[0;34m'
  5. yellow='\033[0;33m'
  6. plain='\033[0m'
  7. # Don't edit this config
  8. b_source="${BASH_SOURCE[0]}"
  9. while [ -h "$b_source" ]; do
  10. b_dir="$(cd -P "$(dirname "$b_source")" >/dev/null 2>&1 && pwd || pwd -P)"
  11. b_source="$(readlink "$b_source")"
  12. [[ $b_source != /* ]] && b_source="$b_dir/$b_source"
  13. done
  14. cur_dir="$(cd -P "$(dirname "$b_source")" >/dev/null 2>&1 && pwd || pwd -P)"
  15. script_name=$(basename "$0")
  16. # Check command exist function
  17. _command_exists() {
  18. type "$1" &>/dev/null
  19. }
  20. # Fail, log and exit script function
  21. _fail() {
  22. local msg=${1}
  23. echo -e "${red}${msg}${plain}"
  24. exit 2
  25. }
  26. # check root
  27. [[ $EUID -ne 0 ]] && _fail "FATAL ERROR: Please run this script with root privilege."
  28. if _command_exists wget; then
  29. wget_bin=$(which wget)
  30. else
  31. _fail "ERROR: Command 'wget' not found."
  32. fi
  33. if _command_exists curl; then
  34. curl_bin=$(which curl)
  35. else
  36. _fail "ERROR: Command 'curl' not found."
  37. fi
  38. # Check OS and set release variable
  39. if [[ -f /etc/os-release ]]; then
  40. source /etc/os-release
  41. release=$ID
  42. elif [[ -f /usr/lib/os-release ]]; then
  43. source /usr/lib/os-release
  44. release=$ID
  45. else
  46. _fail "Failed to check the system OS, please contact the author!"
  47. fi
  48. echo "The OS release is: $release"
  49. arch() {
  50. case "$(uname -m)" in
  51. x86_64 | x64 | amd64) echo 'amd64' ;;
  52. i*86 | x86) echo '386' ;;
  53. armv8* | armv8 | arm64 | aarch64) echo 'arm64' ;;
  54. armv7* | armv7 | arm) echo 'armv7' ;;
  55. armv6* | armv6) echo 'armv6' ;;
  56. armv5* | armv5) echo 'armv5' ;;
  57. s390x) echo 's390x' ;;
  58. *) echo -e "${red}Unsupported CPU architecture!${plain}" && rm -f "${cur_dir}/${script_name}" >/dev/null 2>&1 && exit 2;;
  59. esac
  60. }
  61. echo "Arch: $(arch)"
  62. install_base() {
  63. echo -e "${green}Updating and install dependency packages...${plain}"
  64. case "${release}" in
  65. ubuntu | debian | armbian)
  66. apt-get update >/dev/null 2>&1 && apt-get install -y -q wget curl tar tzdata >/dev/null 2>&1
  67. ;;
  68. centos | almalinux | rocky | ol)
  69. yum -y update >/dev/null 2>&1 && yum install -y -q wget curl tar tzdata >/dev/null 2>&1
  70. ;;
  71. fedora | amzn | virtuozzo)
  72. dnf -y update >/dev/null 2>&1 && dnf install -y -q wget curl tar tzdata >/dev/null 2>&1
  73. ;;
  74. arch | manjaro | parch)
  75. pacman -Syu >/dev/null 2>&1 && pacman -Syu --noconfirm wget curl tar tzdata >/dev/null 2>&1
  76. ;;
  77. opensuse-tumbleweed)
  78. zypper refresh >/dev/null 2>&1 && zypper -q install -y wget curl tar timezone >/dev/null 2>&1
  79. ;;
  80. alpine)
  81. apk update >/dev/null 2>&1 && apk add wget curl tar tzdata >/dev/null 2>&1
  82. ;;
  83. *)
  84. apt-get update >/dev/null 2>&1 && apt install -y -q wget curl tar tzdata >/dev/null 2>&1
  85. ;;
  86. esac
  87. }
  88. config_after_update() {
  89. echo -e "${yellow}x-ui settings:${plain}"
  90. /usr/local/x-ui/x-ui setting -show true
  91. /usr/local/x-ui/x-ui migrate
  92. }
  93. update_x-ui() {
  94. cd /usr/local/
  95. if [ -f "/usr/local/x-ui/x-ui" ]; then
  96. current_xui_version=$(/usr/local/x-ui/x-ui -v)
  97. echo -e "${green}Current x-ui version: ${current_xui_version}${plain}"
  98. else
  99. _fail "ERROR: Current x-ui version: unknown"
  100. fi
  101. echo -e "${green}Downloading new x-ui version...${plain}"
  102. tag_version=$(${curl_bin} -Ls "https://api.github.com/repos/MHSanaei/3x-ui/releases/latest" 2>/dev/null | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
  103. if [[ ! -n "$tag_version" ]]; then
  104. echo -e "${yellow}Trying to fetch version with IPv4...${plain}"
  105. tag_version=$(${curl_bin} -4 -Ls "https://api.github.com/repos/MHSanaei/3x-ui/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
  106. if [[ ! -n "$tag_version" ]]; then
  107. _fail "ERROR: Failed to fetch x-ui version, it may be due to GitHub API restrictions, please try it later"
  108. fi
  109. fi
  110. echo -e "Got x-ui latest version: ${tag_version}, beginning the installation..."
  111. ${wget_bin} -N -O /usr/local/x-ui-linux-$(arch).tar.gz https://github.com/MHSanaei/3x-ui/releases/download/${tag_version}/x-ui-linux-$(arch).tar.gz 2>/dev/null
  112. if [[ $? -ne 0 ]]; then
  113. echo -e "${yellow}Trying to fetch version with IPv4...${plain}"
  114. ${wget_bin} --inet4-only -N -O /usr/local/x-ui-linux-$(arch).tar.gz https://github.com/MHSanaei/3x-ui/releases/download/${tag_version}/x-ui-linux-$(arch).tar.gz 2>/dev/null
  115. if [[ $? -ne 0 ]]; then
  116. _fail "ERROR: Failed to download x-ui, please be sure that your server can access GitHub"
  117. fi
  118. fi
  119. if [[ -e /usr/local/x-ui/ ]]; then
  120. echo -e "${green}Stopping x-ui...${plain}"
  121. if [[ $release == "alpine" ]]; then
  122. if [ -f "/etc/init.d/x-ui" ]; then
  123. rc-service x-ui stop >/dev/null 2>&1
  124. rc-update del x-ui >/dev/null 2>&1
  125. echo -e "${green}Removing old service unit version...${plain}"
  126. rm -f /etc/init.d/x-ui >/dev/null 2>&1
  127. else
  128. rm x-ui-linux-$(arch).tar.gz -f >/dev/null 2>&1
  129. _fail "ERROR: x-ui service unit not installed."
  130. fi
  131. else
  132. if [ -f "/etc/systemd/system/x-ui.service" ]; then
  133. systemctl stop x-ui >/dev/null 2>&1
  134. systemctl disable x-ui >/dev/null 2>&1
  135. echo -e "${green}Removing old systemd unit version...${plain}"
  136. rm /etc/systemd/system/x-ui.service -f >/dev/null 2>&1
  137. systemctl daemon-reload >/dev/null 2>&1
  138. else
  139. rm x-ui-linux-$(arch).tar.gz -f >/dev/null 2>&1
  140. _fail "ERROR: x-ui systemd unit not installed."
  141. fi
  142. fi
  143. echo -e "${green}Removing old x-ui version...${plain}"
  144. rm /usr/bin/x-ui -f >/dev/null 2>&1
  145. rm /usr/local/x-ui/x-ui.service -f >/dev/null 2>&1
  146. rm /usr/local/x-ui/x-ui -f >/dev/null 2>&1
  147. rm /usr/local/x-ui/x-ui.sh -f >/dev/null 2>&1
  148. echo -e "${green}Removing old xray version...${plain}"
  149. rm /usr/local/x-ui/bin/xray-linux-amd64 -f >/dev/null 2>&1
  150. echo -e "${green}Removing old README and LICENSE file...${plain}"
  151. rm /usr/local/x-ui/bin/README.md -f >/dev/null 2>&1
  152. rm /usr/local/x-ui/bin/LICENSE -f >/dev/null 2>&1
  153. else
  154. rm x-ui-linux-$(arch).tar.gz -f >/dev/null 2>&1
  155. _fail "ERROR: x-ui not installed."
  156. fi
  157. echo -e "${green}Installing new x-ui version...${plain}"
  158. tar zxvf x-ui-linux-$(arch).tar.gz >/dev/null 2>&1
  159. rm x-ui-linux-$(arch).tar.gz -f >/dev/null 2>&1
  160. cd x-ui >/dev/null 2>&1
  161. chmod +x x-ui >/dev/null 2>&1
  162. # Check the system's architecture and rename the file accordingly
  163. if [[ $(arch) == "armv5" || $(arch) == "armv6" || $(arch) == "armv7" ]]; then
  164. mv bin/xray-linux-$(arch) bin/xray-linux-arm >/dev/null 2>&1
  165. chmod +x bin/xray-linux-arm >/dev/null 2>&1
  166. fi
  167. chmod +x x-ui bin/xray-linux-$(arch) >/dev/null 2>&1
  168. echo -e "${green}Downloading and installing x-ui.sh script...${plain}"
  169. ${wget_bin} -O /usr/bin/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.sh >/dev/null 2>&1
  170. if [[ $? -ne 0 ]]; then
  171. echo -e "${yellow}Trying to fetch x-ui with IPv4...${plain}"
  172. ${wget_bin} --inet4-only -O /usr/bin/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.sh >/dev/null 2>&1
  173. if [[ $? -ne 0 ]]; then
  174. _fail "ERROR: Failed to download x-ui.sh script, please be sure that your server can access GitHub"
  175. fi
  176. fi
  177. chmod +x /usr/local/x-ui/x-ui.sh >/dev/null 2>&1
  178. chmod +x /usr/bin/x-ui >/dev/null 2>&1
  179. echo -e "${green}Changing owner...${plain}"
  180. chown -R root:root /usr/local/x-ui >/dev/null 2>&1
  181. if [ -f "/usr/local/x-ui/bin/config.json" ]; then
  182. echo -e "${green}Changing on config file permissions...${plain}"
  183. chmod 640 /usr/local/x-ui/bin/config.json >/dev/null 2>&1
  184. fi
  185. if [[ $release == "alpine" ]]; then
  186. echo -e "${green}Downloading and installing startup unit x-ui.rc...${plain}"
  187. ${wget_bin} -O /etc/init.d/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.rc >/dev/null 2>&1
  188. if [[ $? -ne 0 ]]; then
  189. ${wget_bin} --inet4-only -O /etc/init.d/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.rc >/dev/null 2>&1
  190. if [[ $? -ne 0 ]]; then
  191. _fail "ERROR: Failed to download startup unit x-ui.rc, please be sure that your server can access GitHub"
  192. fi
  193. fi
  194. chmod +x /etc/init.d/x-ui >/dev/null 2>&1
  195. chown root:root /etc/init.d/x-ui >/dev/null 2>&1
  196. rc-update add x-ui >/dev/null 2>&1
  197. rc-service x-ui start >/dev/null 2>&1
  198. else
  199. echo -e "${green}Installing systemd unit...${plain}"
  200. cp -f x-ui.service /etc/systemd/system/ >/dev/null 2>&1
  201. chown root:root /etc/systemd/system/x-ui.service >/dev/null 2>&1
  202. systemctl daemon-reload >/dev/null 2>&1
  203. systemctl enable x-ui >/dev/null 2>&1
  204. systemctl start x-ui >/dev/null 2>&1
  205. fi
  206. config_after_update
  207. echo -e "${green}x-ui ${tag_version}${plain} updating finished, it is running now..."
  208. echo -e ""
  209. echo -e "┌───────────────────────────────────────────────────────┐
  210. │ ${blue}x-ui control menu usages (subcommands):${plain} │
  211. │ │
  212. │ ${blue}x-ui${plain} - Admin Management Script │
  213. │ ${blue}x-ui start${plain} - Start │
  214. │ ${blue}x-ui stop${plain} - Stop │
  215. │ ${blue}x-ui restart${plain} - Restart │
  216. │ ${blue}x-ui status${plain} - Current Status │
  217. │ ${blue}x-ui settings${plain} - Current Settings │
  218. │ ${blue}x-ui enable${plain} - Enable Autostart on OS Startup │
  219. │ ${blue}x-ui disable${plain} - Disable Autostart on OS Startup │
  220. │ ${blue}x-ui log${plain} - Check logs │
  221. │ ${blue}x-ui banlog${plain} - Check Fail2ban ban logs │
  222. │ ${blue}x-ui update${plain} - Update │
  223. │ ${blue}x-ui legacy${plain} - legacy version │
  224. │ ${blue}x-ui install${plain} - Install │
  225. │ ${blue}x-ui uninstall${plain} - Uninstall │
  226. └───────────────────────────────────────────────────────┘"
  227. }
  228. echo -e "${green}Running...${plain}"
  229. install_base
  230. update_x-ui $1