update.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. fedora | amzn | virtuozzo | rhel | almalinux | rocky | ol)
  69. dnf -y update >/dev/null 2>&1 && dnf install -y -q wget curl tar tzdata >/dev/null 2>&1
  70. ;;
  71. centos)
  72. if [[ "${VERSION_ID}" =~ ^7 ]]; then
  73. yum -y update >/dev/null 2>&1 && yum install -y -q wget curl tar tzdata >/dev/null 2>&1
  74. else
  75. dnf -y update >/dev/null 2>&1 && dnf install -y -q wget curl tar tzdata >/dev/null 2>&1
  76. fi
  77. ;;
  78. arch | manjaro | parch)
  79. pacman -Syu >/dev/null 2>&1 && pacman -Syu --noconfirm wget curl tar tzdata >/dev/null 2>&1
  80. ;;
  81. opensuse-tumbleweed | opensuse-leap)
  82. zypper refresh >/dev/null 2>&1 && zypper -q install -y wget curl tar timezone >/dev/null 2>&1
  83. ;;
  84. alpine)
  85. apk update >/dev/null 2>&1 && apk add wget curl tar tzdata >/dev/null 2>&1
  86. ;;
  87. *)
  88. apt-get update >/dev/null 2>&1 && apt install -y -q wget curl tar tzdata >/dev/null 2>&1
  89. ;;
  90. esac
  91. }
  92. config_after_update() {
  93. echo -e "${yellow}x-ui settings:${plain}"
  94. /usr/local/x-ui/x-ui setting -show true
  95. /usr/local/x-ui/x-ui migrate
  96. }
  97. update_x-ui() {
  98. cd /usr/local/
  99. if [ -f "/usr/local/x-ui/x-ui" ]; then
  100. current_xui_version=$(/usr/local/x-ui/x-ui -v)
  101. echo -e "${green}Current x-ui version: ${current_xui_version}${plain}"
  102. else
  103. _fail "ERROR: Current x-ui version: unknown"
  104. fi
  105. echo -e "${green}Downloading new x-ui version...${plain}"
  106. 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/')
  107. if [[ ! -n "$tag_version" ]]; then
  108. echo -e "${yellow}Trying to fetch version with IPv4...${plain}"
  109. tag_version=$(${curl_bin} -4 -Ls "https://api.github.com/repos/MHSanaei/3x-ui/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
  110. if [[ ! -n "$tag_version" ]]; then
  111. _fail "ERROR: Failed to fetch x-ui version, it may be due to GitHub API restrictions, please try it later"
  112. fi
  113. fi
  114. echo -e "Got x-ui latest version: ${tag_version}, beginning the installation..."
  115. ${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
  116. if [[ $? -ne 0 ]]; then
  117. echo -e "${yellow}Trying to fetch version with IPv4...${plain}"
  118. ${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
  119. if [[ $? -ne 0 ]]; then
  120. _fail "ERROR: Failed to download x-ui, please be sure that your server can access GitHub"
  121. fi
  122. fi
  123. if [[ -e /usr/local/x-ui/ ]]; then
  124. echo -e "${green}Stopping x-ui...${plain}"
  125. if [[ $release == "alpine" ]]; then
  126. if [ -f "/etc/init.d/x-ui" ]; then
  127. rc-service x-ui stop >/dev/null 2>&1
  128. rc-update del x-ui >/dev/null 2>&1
  129. echo -e "${green}Removing old service unit version...${plain}"
  130. rm -f /etc/init.d/x-ui >/dev/null 2>&1
  131. else
  132. rm x-ui-linux-$(arch).tar.gz -f >/dev/null 2>&1
  133. _fail "ERROR: x-ui service unit not installed."
  134. fi
  135. else
  136. if [ -f "/etc/systemd/system/x-ui.service" ]; then
  137. systemctl stop x-ui >/dev/null 2>&1
  138. systemctl disable x-ui >/dev/null 2>&1
  139. echo -e "${green}Removing old systemd unit version...${plain}"
  140. rm /etc/systemd/system/x-ui.service -f >/dev/null 2>&1
  141. systemctl daemon-reload >/dev/null 2>&1
  142. else
  143. rm x-ui-linux-$(arch).tar.gz -f >/dev/null 2>&1
  144. _fail "ERROR: x-ui systemd unit not installed."
  145. fi
  146. fi
  147. echo -e "${green}Removing old x-ui version...${plain}"
  148. rm /usr/bin/x-ui -f >/dev/null 2>&1
  149. rm /usr/local/x-ui/x-ui.service -f >/dev/null 2>&1
  150. rm /usr/local/x-ui/x-ui -f >/dev/null 2>&1
  151. rm /usr/local/x-ui/x-ui.sh -f >/dev/null 2>&1
  152. echo -e "${green}Removing old xray version...${plain}"
  153. rm /usr/local/x-ui/bin/xray-linux-amd64 -f >/dev/null 2>&1
  154. echo -e "${green}Removing old README and LICENSE file...${plain}"
  155. rm /usr/local/x-ui/bin/README.md -f >/dev/null 2>&1
  156. rm /usr/local/x-ui/bin/LICENSE -f >/dev/null 2>&1
  157. else
  158. rm x-ui-linux-$(arch).tar.gz -f >/dev/null 2>&1
  159. _fail "ERROR: x-ui not installed."
  160. fi
  161. echo -e "${green}Installing new x-ui version...${plain}"
  162. tar zxvf x-ui-linux-$(arch).tar.gz >/dev/null 2>&1
  163. rm x-ui-linux-$(arch).tar.gz -f >/dev/null 2>&1
  164. cd x-ui >/dev/null 2>&1
  165. chmod +x x-ui >/dev/null 2>&1
  166. # Check the system's architecture and rename the file accordingly
  167. if [[ $(arch) == "armv5" || $(arch) == "armv6" || $(arch) == "armv7" ]]; then
  168. mv bin/xray-linux-$(arch) bin/xray-linux-arm >/dev/null 2>&1
  169. chmod +x bin/xray-linux-arm >/dev/null 2>&1
  170. fi
  171. chmod +x x-ui bin/xray-linux-$(arch) >/dev/null 2>&1
  172. echo -e "${green}Downloading and installing x-ui.sh script...${plain}"
  173. ${wget_bin} -O /usr/bin/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.sh >/dev/null 2>&1
  174. if [[ $? -ne 0 ]]; then
  175. echo -e "${yellow}Trying to fetch x-ui with IPv4...${plain}"
  176. ${wget_bin} --inet4-only -O /usr/bin/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.sh >/dev/null 2>&1
  177. if [[ $? -ne 0 ]]; then
  178. _fail "ERROR: Failed to download x-ui.sh script, please be sure that your server can access GitHub"
  179. fi
  180. fi
  181. chmod +x /usr/local/x-ui/x-ui.sh >/dev/null 2>&1
  182. chmod +x /usr/bin/x-ui >/dev/null 2>&1
  183. echo -e "${green}Changing owner...${plain}"
  184. chown -R root:root /usr/local/x-ui >/dev/null 2>&1
  185. if [ -f "/usr/local/x-ui/bin/config.json" ]; then
  186. echo -e "${green}Changing on config file permissions...${plain}"
  187. chmod 640 /usr/local/x-ui/bin/config.json >/dev/null 2>&1
  188. fi
  189. if [[ $release == "alpine" ]]; then
  190. echo -e "${green}Downloading and installing startup unit x-ui.rc...${plain}"
  191. ${wget_bin} -O /etc/init.d/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.rc >/dev/null 2>&1
  192. if [[ $? -ne 0 ]]; then
  193. ${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
  194. if [[ $? -ne 0 ]]; then
  195. _fail "ERROR: Failed to download startup unit x-ui.rc, please be sure that your server can access GitHub"
  196. fi
  197. fi
  198. chmod +x /etc/init.d/x-ui >/dev/null 2>&1
  199. chown root:root /etc/init.d/x-ui >/dev/null 2>&1
  200. rc-update add x-ui >/dev/null 2>&1
  201. rc-service x-ui start >/dev/null 2>&1
  202. else
  203. echo -e "${green}Installing systemd unit...${plain}"
  204. cp -f x-ui.service /etc/systemd/system/ >/dev/null 2>&1
  205. chown root:root /etc/systemd/system/x-ui.service >/dev/null 2>&1
  206. systemctl daemon-reload >/dev/null 2>&1
  207. systemctl enable x-ui >/dev/null 2>&1
  208. systemctl start x-ui >/dev/null 2>&1
  209. fi
  210. config_after_update
  211. echo -e "${green}x-ui ${tag_version}${plain} updating finished, it is running now..."
  212. echo -e ""
  213. echo -e "┌───────────────────────────────────────────────────────┐
  214. │ ${blue}x-ui control menu usages (subcommands):${plain} │
  215. │ │
  216. │ ${blue}x-ui${plain} - Admin Management Script │
  217. │ ${blue}x-ui start${plain} - Start │
  218. │ ${blue}x-ui stop${plain} - Stop │
  219. │ ${blue}x-ui restart${plain} - Restart │
  220. │ ${blue}x-ui status${plain} - Current Status │
  221. │ ${blue}x-ui settings${plain} - Current Settings │
  222. │ ${blue}x-ui enable${plain} - Enable Autostart on OS Startup │
  223. │ ${blue}x-ui disable${plain} - Disable Autostart on OS Startup │
  224. │ ${blue}x-ui log${plain} - Check logs │
  225. │ ${blue}x-ui banlog${plain} - Check Fail2ban ban logs │
  226. │ ${blue}x-ui update${plain} - Update │
  227. │ ${blue}x-ui legacy${plain} - Legacy version │
  228. │ ${blue}x-ui install${plain} - Install │
  229. │ ${blue}x-ui uninstall${plain} - Uninstall │
  230. └───────────────────────────────────────────────────────┘"
  231. }
  232. echo -e "${green}Running...${plain}"
  233. install_base
  234. update_x-ui $1