1
0

install.sh 7.9 KB

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