1
0

install.sh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/bin/bash
  2. red='\033[0;31m'
  3. green='\033[0;32m'
  4. yellow='\033[0;33m'
  5. plain='\033[0m'
  6. cur_dir=$(pwd)
  7. # check root
  8. [[ $EUID -ne 0 ]] && echo -e "${red}Fatal error:${plain} Please run this script with root privilege \n " && exit 1
  9. # Check OS and set release variable
  10. if [[ -f /etc/os-release ]]; then
  11. source /etc/os-release
  12. release=$ID
  13. elif [[ -f /usr/lib/os-release ]]; then
  14. source /usr/lib/os-release
  15. release=$ID
  16. else
  17. echo "Failed to check the system OS, please contact the author!" >&2
  18. exit 1
  19. fi
  20. echo "The OS release is: $release"
  21. arch=$(arch)
  22. if [[ $arch == "x86_64" || $arch == "x64" || $arch == "amd64" ]]; then
  23. arch="amd64"
  24. elif [[ $arch == "aarch64" || $arch == "arm64" ]]; then
  25. arch="arm64"
  26. else
  27. arch="amd64"
  28. echo -e "${red} Failed to check system arch, will use default arch: ${arch}${plain}"
  29. fi
  30. echo "arch: ${arch}"
  31. if [ $(getconf WORD_BIT) != '32' ] && [ $(getconf LONG_BIT) != '64' ]; then
  32. echo "x-ui dosen't support 32-bit(x86) system, please use 64 bit operating system(x86_64) instead, if there is something wrong, please get in touch with me!"
  33. exit -1
  34. fi
  35. os_version=""
  36. os_version=$(grep -i version_id /etc/os-release | cut -d \" -f2 | cut -d . -f1)
  37. if [[ "${release}" == "centos" ]]; then
  38. if [[ ${os_version} -lt 8 ]]; then
  39. echo -e "${red} Please use CentOS 8 or higher ${plain}\n" && exit 1
  40. fi
  41. elif [[ "${release}" == "ubuntu" ]]; then
  42. if [[ ${os_version} -lt 20 ]]; then
  43. echo -e "${red}please use Ubuntu 20 or higher version!${plain}\n" && exit 1
  44. fi
  45. elif [[ "${release}" == "fedora" ]]; then
  46. if [[ ${os_version} -lt 36 ]]; then
  47. echo -e "${red}please use Fedora 36 or higher version!${plain}\n" && exit 1
  48. fi
  49. elif [[ "${release}" == "debian" ]]; then
  50. if [[ ${os_version} -lt 10 ]]; then
  51. echo -e "${red} Please use Debian 10 or higher ${plain}\n" && exit 1
  52. fi
  53. else
  54. echo -e "${red}Failed to check the OS version, please contact the author!${plain}" && exit 1
  55. fi
  56. install_base() {
  57. if [[ "${release}" == "centos" ]]; then
  58. yum install wget curl tar -y
  59. else
  60. apt install wget curl tar -y
  61. fi
  62. }
  63. #This function will be called when user installed x-ui out of sercurity
  64. config_after_install() {
  65. echo -e "${yellow}Install/update finished! For security it's recommended to modify panel settings ${plain}"
  66. read -p "Do you want to continue with the modification [y/n]? ": config_confirm
  67. if [[ x"${config_confirm}" == x"y" || x"${config_confirm}" == x"Y" ]]; then
  68. read -p "Please set up your username:" config_account
  69. echo -e "${yellow}Your username will be:${config_account}${plain}"
  70. read -p "Please set up your password:" config_password
  71. echo -e "${yellow}Your password will be:${config_password}${plain}"
  72. read -p "Please set up the panel port:" config_port
  73. echo -e "${yellow}Your panel port is:${config_port}${plain}"
  74. echo -e "${yellow}Initializing, please wait...${plain}"
  75. /usr/local/x-ui/x-ui setting -username ${config_account} -password ${config_password}
  76. echo -e "${yellow}Account name and password set successfully!${plain}"
  77. /usr/local/x-ui/x-ui setting -port ${config_port}
  78. echo -e "${yellow}Panel port set successfully!${plain}"
  79. else
  80. echo -e "${red}cancel...${plain}"
  81. if [[ ! -f "/etc/x-ui/x-ui.db" ]]; then
  82. local usernameTemp=$(head -c 6 /dev/urandom | base64)
  83. local passwordTemp=$(head -c 6 /dev/urandom | base64)
  84. /usr/local/x-ui/x-ui setting -username ${usernameTemp} -password ${passwordTemp}
  85. echo -e "this is a fresh installation,will generate random login info for security concerns:"
  86. echo -e "###############################################"
  87. echo -e "${green}username:${usernameTemp}${plain}"
  88. echo -e "${green}password:${passwordTemp}${plain}"
  89. echo -e "###############################################"
  90. echo -e "${red}if you forgot your login info,you can type x-ui and then type 7 to check after installation${plain}"
  91. else
  92. echo -e "${red} this is your upgrade,will keep old settings,if you forgot your login info,you can type x-ui and then type 7 to check${plain}"
  93. fi
  94. fi
  95. }
  96. install_x-ui() {
  97. systemctl stop x-ui
  98. cd /usr/local/
  99. if [ $# == 0 ]; then
  100. last_version=$(curl -Ls "https://api.github.com/repos/MHSanaei/3x-ui/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
  101. if [[ ! -n "$last_version" ]]; then
  102. echo -e "${red}Failed to fetch x-ui version, it maybe due to Github API restrictions, please try it later${plain}"
  103. exit 1
  104. fi
  105. echo -e "Got x-ui latest version: ${last_version}, beginning the installation..."
  106. wget -N --no-check-certificate -O /usr/local/x-ui-linux-${arch}.tar.gz https://github.com/MHSanaei/3x-ui/releases/download/${last_version}/x-ui-linux-${arch}.tar.gz
  107. if [[ $? -ne 0 ]]; then
  108. echo -e "${red}Downloading x-ui failed, please be sure that your server can access Github ${plain}"
  109. exit 1
  110. fi
  111. else
  112. last_version=$1
  113. url="https://github.com/MHSanaei/3x-ui/releases/download/${last_version}/x-ui-linux-${arch}.tar.gz"
  114. echo -e "Begining to install x-ui $1"
  115. wget -N --no-check-certificate -O /usr/local/x-ui-linux-${arch}.tar.gz ${url}
  116. if [[ $? -ne 0 ]]; then
  117. echo -e "${red}Download x-ui $1 failed,please check the version exists${plain}"
  118. exit 1
  119. fi
  120. fi
  121. if [[ -e /usr/local/x-ui/ ]]; then
  122. rm /usr/local/x-ui/ -rf
  123. fi
  124. tar zxvf x-ui-linux-${arch}.tar.gz
  125. rm x-ui-linux-${arch}.tar.gz -f
  126. cd x-ui
  127. chmod +x x-ui bin/xray-linux-${arch}
  128. cp -f x-ui.service /etc/systemd/system/
  129. wget --no-check-certificate -O /usr/bin/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.sh
  130. chmod +x /usr/local/x-ui/x-ui.sh
  131. chmod +x /usr/bin/x-ui
  132. config_after_install
  133. #echo -e "If it is a new installation, the default web port is ${green}2053${plain}, The username and password are ${green}admin${plain} by default"
  134. #echo -e "Please make sure that this port is not occupied by other procedures,${yellow} And make sure that port 2053 has been released${plain}"
  135. # echo -e "If you want to modify the 2053 to other ports and enter the x-ui command to modify it, you must also ensure that the port you modify is also released"
  136. #echo -e ""
  137. #echo -e "If it is updated panel, access the panel in your previous way"
  138. #echo -e ""
  139. systemctl daemon-reload
  140. systemctl enable x-ui
  141. systemctl start x-ui
  142. echo -e "${green}x-ui ${last_version}${plain} installation finished, it is running now..."
  143. echo -e ""
  144. echo -e "x-ui control menu usages: "
  145. echo -e "----------------------------------------------"
  146. echo -e "x-ui - Enter Admin menu"
  147. echo -e "x-ui start - Start x-ui"
  148. echo -e "x-ui stop - Stop x-ui"
  149. echo -e "x-ui restart - Restart x-ui"
  150. echo -e "x-ui status - Show x-ui status"
  151. echo -e "x-ui enable - Enable x-ui on system startup"
  152. echo -e "x-ui disable - Disable x-ui on system startup"
  153. echo -e "x-ui log - Check x-ui logs"
  154. echo -e "x-ui update - Update x-ui"
  155. echo -e "x-ui install - Install x-ui"
  156. echo -e "x-ui uninstall - Uninstall x-ui"
  157. echo -e "----------------------------------------------"
  158. }
  159. echo -e "${green}Running...${plain}"
  160. install_base
  161. install_x-ui $1