install.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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
  10. if [[ -f /etc/redhat-release ]]; then
  11. release="centos"
  12. if grep -q "Fedora" /etc/redhat-release; then
  13. release="fedora"
  14. fi
  15. elif cat /etc/issue | grep -Eqi "debian"; then
  16. release="debian"
  17. elif cat /etc/issue | grep -Eqi "ubuntu"; then
  18. release="ubuntu"
  19. elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
  20. release="centos"
  21. elif cat /proc/version | grep -Eqi "debian"; then
  22. release="debian"
  23. elif cat /proc/version | grep -Eqi "ubuntu"; then
  24. release="ubuntu"
  25. elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
  26. release="centos"
  27. elif grep -q "Fedora" /etc/*-release; then
  28. release="fedora"
  29. else
  30. echo -e "${red} Check system OS failed, please contact the author! ${plain}\n" && exit 1
  31. fi
  32. arch=$(arch)
  33. if [[ $arch == "x86_64" || $arch == "x64" || $arch == "amd64" ]]; then
  34. arch="amd64"
  35. elif [[ $arch == "aarch64" || $arch == "arm64" ]]; then
  36. arch="arm64"
  37. else
  38. arch="amd64"
  39. echo -e "${red} Failed to check system arch, will use default arch: ${arch}${plain}"
  40. fi
  41. echo "arch: ${arch}"
  42. if [ $(getconf WORD_BIT) != '32' ] && [ $(getconf LONG_BIT) != '64' ]; then
  43. 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!"
  44. exit -1
  45. fi
  46. os_version=""
  47. # get OS version
  48. if [[ -f /etc/os-release ]]; then
  49. os_version=$(awk -F'[= ."]' '/VERSION_ID/{print $3}' /etc/os-release)
  50. fi
  51. if [[ -z "$os_version" && -f /etc/lsb-release ]]; then
  52. os_version=$(awk -F'[= ."]+' '/DISTRIB_RELEASE/{print $2}' /etc/lsb-release)
  53. fi
  54. # set minimum version number for each OS
  55. case ${release} in
  56. centos)
  57. min_version=8
  58. ;;
  59. ubuntu)
  60. min_version=20
  61. ;;
  62. debian)
  63. min_version=10
  64. ;;
  65. fedora)
  66. min_version=29
  67. ;;
  68. *)
  69. echo -e "${red} Unsupported OS ${plain}\n" && exit 1
  70. ;;
  71. esac
  72. # check if OS version meets minimum version requirement
  73. if [[ ${os_version} -lt ${min_version} ]]; then
  74. echo -e "${red} Please use ${release^} ${min_version} or higher ${plain}\n" && exit 1
  75. fi
  76. install_base() {
  77. if [[ x"${release}" == x"centos" ]]; then
  78. yum install wget curl tar -y
  79. else
  80. apt install wget curl tar -y
  81. fi
  82. }
  83. #This function will be called when user installed x-ui out of sercurity
  84. config_after_install() {
  85. echo -e "${yellow}Install/update finished! For security it's recommended to modify panel settings ${plain}"
  86. read -p "Do you want to continue with the modification [y/n]? ": config_confirm
  87. if [[ x"${config_confirm}" == x"y" || x"${config_confirm}" == x"Y" ]]; then
  88. read -p "Please set up your username:" config_account
  89. echo -e "${yellow}Your username will be:${config_account}${plain}"
  90. read -p "Please set up your password:" config_password
  91. echo -e "${yellow}Your password will be:${config_password}${plain}"
  92. read -p "Please set up the panel port:" config_port
  93. echo -e "${yellow}Your panel port is:${config_port}${plain}"
  94. echo -e "${yellow}Initializing, please wait...${plain}"
  95. /usr/local/x-ui/x-ui setting -username ${config_account} -password ${config_password}
  96. echo -e "${yellow}Account name and password set successfully!${plain}"
  97. /usr/local/x-ui/x-ui setting -port ${config_port}
  98. echo -e "${yellow}Panel port set successfully!${plain}"
  99. else
  100. echo -e "${red}cancel...${plain}"
  101. if [[ ! -f "/etc/x-ui/x-ui.db" ]]; then
  102. local usernameTemp=$(head -c 6 /dev/urandom | base64)
  103. local passwordTemp=$(head -c 6 /dev/urandom | base64)
  104. /usr/local/x-ui/x-ui setting -username ${usernameTemp} -password ${passwordTemp}
  105. echo -e "this is a fresh installation,will generate random login info for security concerns:"
  106. echo -e "###############################################"
  107. echo -e "${green}user name:${usernameTemp}${plain}"
  108. echo -e "${green}user password:${passwordTemp}${plain}"
  109. echo -e "${red}web port:${portTemp}${plain}"
  110. echo -e "###############################################"
  111. echo -e "${red}if you forgot your login info,you can type x-ui and then type 7 to check after installation${plain}"
  112. else
  113. 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}"
  114. fi
  115. fi
  116. }
  117. install_x-ui() {
  118. systemctl stop x-ui
  119. cd /usr/local/
  120. if [ $# == 0 ]; then
  121. last_version=$(curl -Ls "https://api.github.com/repos/mhsanaei/3x-ui/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
  122. if [[ ! -n "$last_version" ]]; then
  123. echo -e "${red}Failed to fetch x-ui version, it maybe due to Github API restrictions, please try it later${plain}"
  124. exit 1
  125. fi
  126. echo -e "Got x-ui latest version: ${last_version}, beginning the installation..."
  127. 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
  128. if [[ $? -ne 0 ]]; then
  129. echo -e "${red}Downloading x-ui failed, please be sure that your server can access Github ${plain}"
  130. exit 1
  131. fi
  132. else
  133. last_version=$1
  134. url="https://github.com/mhsanaei/3x-ui/releases/download/${last_version}/x-ui-linux-${arch}.tar.gz"
  135. echo -e "Begining to install x-ui $1"
  136. wget -N --no-check-certificate -O /usr/local/x-ui-linux-${arch}.tar.gz ${url}
  137. if [[ $? -ne 0 ]]; then
  138. echo -e "${red}Download x-ui $1 failed,please check the version exists${plain}"
  139. exit 1
  140. fi
  141. fi
  142. if [[ -e /usr/local/x-ui/ ]]; then
  143. rm /usr/local/x-ui/ -rf
  144. fi
  145. tar zxvf x-ui-linux-${arch}.tar.gz
  146. rm x-ui-linux-${arch}.tar.gz -f
  147. cd x-ui
  148. chmod +x x-ui bin/xray-linux-${arch}
  149. cp -f x-ui.service /etc/systemd/system/
  150. wget --no-check-certificate -O /usr/bin/x-ui https://raw.githubusercontent.com/mhsanaei/3x-ui/main/x-ui.sh
  151. chmod +x /usr/local/x-ui/x-ui.sh
  152. chmod +x /usr/bin/x-ui
  153. config_after_install
  154. #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"
  155. #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}"
  156. # 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"
  157. #echo -e ""
  158. #echo -e "If it is updated panel, access the panel in your previous way"
  159. #echo -e ""
  160. systemctl daemon-reload
  161. systemctl enable x-ui
  162. systemctl start x-ui
  163. echo -e "${green}x-ui ${last_version}${plain} installation finished, it is running now..."
  164. echo -e ""
  165. echo -e "x-ui control menu usages: "
  166. echo -e "----------------------------------------------"
  167. echo -e "x-ui - Enter Admin menu"
  168. echo -e "x-ui start - Start x-ui"
  169. echo -e "x-ui stop - Stop x-ui"
  170. echo -e "x-ui restart - Restart x-ui"
  171. echo -e "x-ui status - Show x-ui status"
  172. echo -e "x-ui enable - Enable x-ui on system startup"
  173. echo -e "x-ui disable - Disable x-ui on system startup"
  174. echo -e "x-ui log - Check x-ui logs"
  175. echo -e "x-ui update - Update x-ui"
  176. echo -e "x-ui install - Install x-ui"
  177. echo -e "x-ui uninstall - Uninstall x-ui"
  178. echo -e "----------------------------------------------"
  179. }
  180. echo -e "${green}Running...${plain}"
  181. install_base
  182. install_x-ui $1