install.sh 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. cur_dir=$(pwd)
  8. show_ip_service_lists=("https://api.ipify.org" "https://4.ident.me")
  9. # check root
  10. [[ $EUID -ne 0 ]] && echo -e "${red}Fatal error: ${plain} Please run this script with root privilege \n " && exit 1
  11. # Check OS and set release variable
  12. if [[ -f /etc/os-release ]]; then
  13. source /etc/os-release
  14. release=$ID
  15. elif [[ -f /usr/lib/os-release ]]; then
  16. source /usr/lib/os-release
  17. release=$ID
  18. else
  19. echo "Failed to check the system OS, please contact the author!" >&2
  20. exit 1
  21. fi
  22. echo "The OS release is: $release"
  23. arch() {
  24. case "$(uname -m)" in
  25. x86_64 | x64 | amd64) echo 'amd64' ;;
  26. i*86 | x86) echo '386' ;;
  27. armv8* | armv8 | arm64 | aarch64) echo 'arm64' ;;
  28. armv7* | armv7 | arm) echo 'armv7' ;;
  29. armv6* | armv6) echo 'armv6' ;;
  30. armv5* | armv5) echo 'armv5' ;;
  31. s390x) echo 's390x' ;;
  32. *) echo -e "${green}Unsupported CPU architecture! ${plain}" && rm -f install.sh && exit 1 ;;
  33. esac
  34. }
  35. echo "Arch: $(arch)"
  36. install_base() {
  37. case "${release}" in
  38. ubuntu | debian | armbian)
  39. apt-get update && apt-get install -y -q wget curl tar tzdata
  40. ;;
  41. centos | rhel | almalinux | rocky | ol)
  42. yum -y update && yum install -y -q wget curl tar tzdata
  43. ;;
  44. fedora | amzn | virtuozzo)
  45. dnf -y update && dnf install -y -q wget curl tar tzdata
  46. ;;
  47. arch | manjaro | parch)
  48. pacman -Syu && pacman -Syu --noconfirm wget curl tar tzdata
  49. ;;
  50. opensuse-tumbleweed)
  51. zypper refresh && zypper -q install -y wget curl tar timezone
  52. ;;
  53. *)
  54. apt-get update && apt install -y -q wget curl tar tzdata
  55. ;;
  56. esac
  57. }
  58. gen_random_string() {
  59. local length="$1"
  60. local random_string=$(LC_ALL=C tr -dc 'a-zA-Z0-9' </dev/urandom | fold -w "$length" | head -n 1)
  61. echo "$random_string"
  62. }
  63. config_after_install() {
  64. local existing_hasDefaultCredential=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'hasDefaultCredential: .+' | awk '{print $2}')
  65. local existing_webBasePath=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  66. local existing_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  67. for ip_service_addr in "${show_ip_service_lists[@]}"; do
  68. local server_ip=$(curl -s --max-time 3 ${ip_service_addr} 2>/dev/null)
  69. if [ -n "${server_ip}" ]; then
  70. break
  71. fi
  72. done
  73. if [[ ${#existing_webBasePath} -lt 4 ]]; then
  74. if [[ "$existing_hasDefaultCredential" == "true" ]]; then
  75. local config_webBasePath=$(gen_random_string 18)
  76. local config_username=$(gen_random_string 10)
  77. local config_password=$(gen_random_string 10)
  78. read -rp "Would you like to customize the Panel Port settings? (If not, a random port will be applied) [y/n]: " config_confirm
  79. if [[ "${config_confirm}" == "y" || "${config_confirm}" == "Y" ]]; then
  80. read -rp "Please set up the panel port: " config_port
  81. echo -e "${yellow}Your Panel Port is: ${config_port}${plain}"
  82. else
  83. local config_port=$(shuf -i 1024-62000 -n 1)
  84. echo -e "${yellow}Generated random port: ${config_port}${plain}"
  85. fi
  86. /usr/local/x-ui/x-ui setting -username "${config_username}" -password "${config_password}" -port "${config_port}" -webBasePath "${config_webBasePath}"
  87. echo -e "This is a fresh installation, generating random login info for security concerns:"
  88. echo -e "###############################################"
  89. echo -e "${green}Username: ${config_username}${plain}"
  90. echo -e "${green}Password: ${config_password}${plain}"
  91. echo -e "${green}Port: ${config_port}${plain}"
  92. echo -e "${green}WebBasePath: ${config_webBasePath}${plain}"
  93. echo -e "${green}Access URL: http://${server_ip}:${config_port}/${config_webBasePath}${plain}"
  94. echo -e "###############################################"
  95. else
  96. local config_webBasePath=$(gen_random_string 18)
  97. echo -e "${yellow}WebBasePath is missing or too short. Generating a new one...${plain}"
  98. /usr/local/x-ui/x-ui setting -webBasePath "${config_webBasePath}"
  99. echo -e "${green}New WebBasePath: ${config_webBasePath}${plain}"
  100. echo -e "${green}Access URL: http://${server_ip}:${existing_port}/${config_webBasePath}${plain}"
  101. fi
  102. else
  103. if [[ "$existing_hasDefaultCredential" == "true" ]]; then
  104. local config_username=$(gen_random_string 10)
  105. local config_password=$(gen_random_string 10)
  106. echo -e "${yellow}Default credentials detected. Security update required...${plain}"
  107. /usr/local/x-ui/x-ui setting -username "${config_username}" -password "${config_password}"
  108. echo -e "Generated new random login credentials:"
  109. echo -e "###############################################"
  110. echo -e "${green}Username: ${config_username}${plain}"
  111. echo -e "${green}Password: ${config_password}${plain}"
  112. echo -e "###############################################"
  113. else
  114. echo -e "${green}Username, Password, and WebBasePath are properly set. Exiting...${plain}"
  115. fi
  116. fi
  117. /usr/local/x-ui/x-ui migrate
  118. }
  119. install_x-ui() {
  120. cd /usr/local/
  121. # Download resources
  122. if [ $# == 0 ]; then
  123. tag_version=$(curl -Ls "https://api.github.com/repos/MHSanaei/3x-ui/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
  124. if [[ ! -n "$tag_version" ]]; then
  125. echo -e "${red}Failed to fetch x-ui version, it may be due to GitHub API restrictions, please try it later${plain}"
  126. exit 1
  127. fi
  128. echo -e "Got x-ui latest version: ${tag_version}, beginning the installation..."
  129. wget -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
  130. if [[ $? -ne 0 ]]; then
  131. echo -e "${red}Downloading x-ui failed, please be sure that your server can access GitHub ${plain}"
  132. exit 1
  133. fi
  134. else
  135. tag_version=$1
  136. tag_version_numeric=${tag_version#v}
  137. min_version="2.3.5"
  138. if [[ "$(printf '%s\n' "$min_version" "$tag_version_numeric" | sort -V | head -n1)" != "$min_version" ]]; then
  139. echo -e "${red}Please use a newer version (at least v2.3.5). Exiting installation.${plain}"
  140. exit 1
  141. fi
  142. url="https://github.com/MHSanaei/3x-ui/releases/download/${tag_version}/x-ui-linux-$(arch).tar.gz"
  143. echo -e "Beginning to install x-ui $1"
  144. wget -N -O /usr/local/x-ui-linux-$(arch).tar.gz ${url}
  145. if [[ $? -ne 0 ]]; then
  146. echo -e "${red}Download x-ui $1 failed, please check if the version exists ${plain}"
  147. exit 1
  148. fi
  149. fi
  150. wget -O /usr/bin/x-ui-temp https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.sh
  151. # Stop x-ui service and remove old resources
  152. if [[ -e /usr/local/x-ui/ ]]; then
  153. systemctl stop x-ui
  154. rm /usr/local/x-ui/ -rf
  155. fi
  156. # Extract resources and set permissions
  157. tar zxvf x-ui-linux-$(arch).tar.gz
  158. rm x-ui-linux-$(arch).tar.gz -f
  159. cd x-ui
  160. chmod +x x-ui
  161. chmod +x x-ui.sh
  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
  165. chmod +x bin/xray-linux-arm
  166. fi
  167. chmod +x x-ui bin/xray-linux-$(arch)
  168. # Update x-ui cli and se set permission
  169. mv -f /usr/bin/x-ui-temp /usr/bin/x-ui
  170. chmod +x /usr/bin/x-ui
  171. config_after_install
  172. cp -f x-ui.service /etc/systemd/system/
  173. systemctl daemon-reload
  174. systemctl enable x-ui
  175. systemctl start x-ui
  176. echo -e "${green}x-ui ${tag_version}${plain} installation finished, it is running now..."
  177. echo -e ""
  178. echo -e "┌───────────────────────────────────────────────────────┐
  179. │ ${blue}x-ui control menu usages (subcommands):${plain} │
  180. │ │
  181. │ ${blue}x-ui${plain} - Admin Management Script │
  182. │ ${blue}x-ui start${plain} - Start │
  183. │ ${blue}x-ui stop${plain} - Stop │
  184. │ ${blue}x-ui restart${plain} - Restart │
  185. │ ${blue}x-ui status${plain} - Current Status │
  186. │ ${blue}x-ui settings${plain} - Current Settings │
  187. │ ${blue}x-ui enable${plain} - Enable Autostart on OS Startup │
  188. │ ${blue}x-ui disable${plain} - Disable Autostart on OS Startup │
  189. │ ${blue}x-ui log${plain} - Check logs │
  190. │ ${blue}x-ui banlog${plain} - Check Fail2ban ban logs │
  191. │ ${blue}x-ui update${plain} - Update │
  192. │ ${blue}x-ui legacy${plain} - legacy version │
  193. │ ${blue}x-ui install${plain} - Install │
  194. │ ${blue}x-ui uninstall${plain} - Uninstall │
  195. └───────────────────────────────────────────────────────┘"
  196. }
  197. echo -e "${green}Running...${plain}"
  198. install_base
  199. install_x-ui $1