install.sh 10 KB

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