install.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. # check root
  9. [[ $EUID -ne 0 ]] && echo -e "${red}Fatal error: ${plain} Please run this script with root privilege \n " && exit 1
  10. # Check OS and set release variable
  11. if [[ -f /etc/os-release ]]; then
  12. source /etc/os-release
  13. release=$ID
  14. elif [[ -f /usr/lib/os-release ]]; then
  15. source /usr/lib/os-release
  16. release=$ID
  17. else
  18. echo "Failed to check the system OS, please contact the author!" >&2
  19. exit 1
  20. fi
  21. echo "The OS release is: $release"
  22. arch() {
  23. case "$(uname -m)" in
  24. x86_64 | x64 | amd64) echo 'amd64' ;;
  25. i*86 | x86) echo '386' ;;
  26. armv8* | armv8 | arm64 | aarch64) echo 'arm64' ;;
  27. armv7* | armv7 | arm) echo 'armv7' ;;
  28. armv6* | armv6) echo 'armv6' ;;
  29. armv5* | armv5) echo 'armv5' ;;
  30. s390x) echo 's390x' ;;
  31. *) echo -e "${green}Unsupported CPU architecture! ${plain}" && rm -f install.sh && exit 1 ;;
  32. esac
  33. }
  34. echo "Arch: $(arch)"
  35. install_base() {
  36. case "${release}" in
  37. ubuntu | debian | armbian)
  38. apt-get update && apt-get install -y -q wget curl tar tzdata
  39. ;;
  40. centos | rhel | almalinux | rocky | ol)
  41. yum -y update && yum install -y -q wget curl tar tzdata
  42. ;;
  43. fedora | amzn | virtuozzo)
  44. dnf -y update && dnf install -y -q wget curl tar tzdata
  45. ;;
  46. arch | manjaro | parch)
  47. pacman -Syu && pacman -Syu --noconfirm wget curl tar tzdata
  48. ;;
  49. opensuse-tumbleweed)
  50. zypper refresh && zypper -q install -y wget curl tar timezone
  51. ;;
  52. alpine)
  53. apk update && apk add wget curl tar tzdata
  54. ;;
  55. *)
  56. apt-get update && apt-get install -y -q wget curl tar tzdata
  57. ;;
  58. esac
  59. }
  60. gen_random_string() {
  61. local length="$1"
  62. local random_string=$(LC_ALL=C tr -dc 'a-zA-Z0-9' </dev/urandom | fold -w "$length" | head -n 1)
  63. echo "$random_string"
  64. }
  65. config_after_install() {
  66. local existing_hasDefaultCredential=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'hasDefaultCredential: .+' | awk '{print $2}')
  67. local existing_webBasePath=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  68. local existing_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  69. local URL_lists=(
  70. "https://api4.ipify.org"
  71. "https://ipv4.icanhazip.com"
  72. "https://v4.api.ipinfo.io/ip"
  73. "https://ipv4.myexternalip.com/raw"
  74. "https://4.ident.me"
  75. "https://check-host.net/ip"
  76. )
  77. local server_ip=""
  78. for ip_address in "${URL_lists[@]}"; do
  79. server_ip=$(curl -s --max-time 3 "${ip_address}" 2>/dev/null | tr -d '[:space:]')
  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. if [[ $release == "alpine" ]]; then
  165. rc-service x-ui stop
  166. else
  167. systemctl stop x-ui
  168. fi
  169. rm /usr/local/x-ui/ -rf
  170. fi
  171. # Extract resources and set permissions
  172. tar zxvf x-ui-linux-$(arch).tar.gz
  173. rm x-ui-linux-$(arch).tar.gz -f
  174. cd x-ui
  175. chmod +x x-ui
  176. chmod +x x-ui.sh
  177. # Check the system's architecture and rename the file accordingly
  178. if [[ $(arch) == "armv5" || $(arch) == "armv6" || $(arch) == "armv7" ]]; then
  179. mv bin/xray-linux-$(arch) bin/xray-linux-arm
  180. chmod +x bin/xray-linux-arm
  181. fi
  182. chmod +x x-ui bin/xray-linux-$(arch)
  183. # Update x-ui cli and se set permission
  184. mv -f /usr/bin/x-ui-temp /usr/bin/x-ui
  185. chmod +x /usr/bin/x-ui
  186. config_after_install
  187. if [[ $release == "alpine" ]]; then
  188. wget -O /etc/init.d/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.rc
  189. chmod +x /etc/init.d/x-ui
  190. rc-update add x-ui
  191. rc-service x-ui start
  192. else
  193. cp -f x-ui.service /etc/systemd/system/
  194. systemctl daemon-reload
  195. systemctl enable x-ui
  196. systemctl start x-ui
  197. fi
  198. echo -e "${green}x-ui ${tag_version}${plain} installation finished, it is running now..."
  199. echo -e ""
  200. echo -e "┌───────────────────────────────────────────────────────┐
  201. │ ${blue}x-ui control menu usages (subcommands):${plain} │
  202. │ │
  203. │ ${blue}x-ui${plain} - Admin Management Script │
  204. │ ${blue}x-ui start${plain} - Start │
  205. │ ${blue}x-ui stop${plain} - Stop │
  206. │ ${blue}x-ui restart${plain} - Restart │
  207. │ ${blue}x-ui status${plain} - Current Status │
  208. │ ${blue}x-ui settings${plain} - Current Settings │
  209. │ ${blue}x-ui enable${plain} - Enable Autostart on OS Startup │
  210. │ ${blue}x-ui disable${plain} - Disable Autostart on OS Startup │
  211. │ ${blue}x-ui log${plain} - Check logs │
  212. │ ${blue}x-ui banlog${plain} - Check Fail2ban ban logs │
  213. │ ${blue}x-ui update${plain} - Update │
  214. │ ${blue}x-ui legacy${plain} - legacy version │
  215. │ ${blue}x-ui install${plain} - Install │
  216. │ ${blue}x-ui uninstall${plain} - Uninstall │
  217. └───────────────────────────────────────────────────────┘"
  218. }
  219. echo -e "${green}Running...${plain}"
  220. install_base
  221. install_x-ui $1