install.sh 11 KB

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