xray-ui.sh 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #!/bin/bash
  2. curl -sS -H "Accept: application/vnd.github.v3+json" -o "/tmp/tmp_file" 'https://api.github.com/repos/qist/xray-ui/releases/latest'
  3. releases_version=($(sed 'y/,/\n/' "/tmp/tmp_file" | grep 'tag_name' | awk -F '"' '{print $4}'))
  4. rm /tmp/tmp_file -f
  5. red='\033[0;31m'
  6. green='\033[0;32m'
  7. yellow='\033[0;33m'
  8. bblue='\033[0;34m'
  9. plain='\033[0m'
  10. red() { echo -e "\033[31m\033[01m$1\033[0m"; }
  11. green() { echo -e "\033[32m\033[01m$1\033[0m"; }
  12. yellow() { echo -e "\033[33m\033[01m$1\033[0m"; }
  13. blue() { echo -e "\033[36m\033[01m$1\033[0m"; }
  14. white() { echo -e "\033[37m\033[01m$1\033[0m"; }
  15. readp() { read -p "$(yellow "$1")" $2; }
  16. remoteV=${releases_version}
  17. clear
  18. white "Github项目 :github.com/qist/xray-ui"
  19. yellow "感谢xray-ui代码贡献者们(vaxilu)"
  20. green "当前安装版本: $remoteV"
  21. yellow "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  22. sleep 2
  23. cur_dir=$(pwd)
  24. # check root
  25. [[ $EUID -ne 0 ]] && echo -e "${red}错误:${plain} 必须使用root用户运行此脚本!\n" && exit 1
  26. # check os
  27. if [[ -f /etc/redhat-release ]]; then
  28. release="centos"
  29. elif cat /etc/issue | grep -Eqi "debian"; then
  30. release="debian"
  31. elif cat /etc/issue | grep -Eqi "ubuntu"; then
  32. release="ubuntu"
  33. elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
  34. release="centos"
  35. elif cat /proc/version | grep -Eqi "debian"; then
  36. release="debian"
  37. elif cat /proc/version | grep -Eqi "ubuntu"; then
  38. release="ubuntu"
  39. elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
  40. release="centos"
  41. elif cat /etc/system-release-cpe | grep -Eqi "amazon_linux"; then
  42. release="amazon_linux"
  43. else
  44. echo -e "${red}未检测到系统版本,请联系脚本作者!${plain}\n" && exit 1
  45. fi
  46. arch() {
  47. case "$(uname -m)" in
  48. x86_64 | x64 | amd64) echo 'amd64' ;;
  49. i*86 | x86) echo '386' ;;
  50. armv8* | armv8 | arm64 | aarch64) echo 'arm64' ;;
  51. armv7* | armv7 | arm) echo 'armv7' ;;
  52. armv6* | armv6) echo 'armv6' ;;
  53. armv5* | armv5) echo 'armv5' ;;
  54. s390x) echo 's390x' ;;
  55. *) echo -e "${green}Unsupported CPU architecture! ${plain}" && rm -f install.sh && exit 1 ;;
  56. esac
  57. }
  58. echo "arch: $(arch)"
  59. sys() {
  60. [ -f /etc/os-release ] && grep -i pretty_name /etc/os-release | cut -d \" -f2 && return
  61. [ -f /etc/lsb-release ] && grep -i description /etc/lsb-release | cut -d \" -f2 && return
  62. [ -f /etc/redhat-release ] && awk '{print $0}' /etc/redhat-release && return
  63. }
  64. op=$(sys)
  65. version=$(uname -r | awk -F "-" '{print $1}')
  66. vi=$(systemd-detect-virt)
  67. white "VPS操作系统: $(blue "$op") \c" && white " 内核版本: $(blue "$version") \c" && white " CPU架构 : $(blue "$arch") \c" && white " 虚拟化类型: $(blue "$vi")"
  68. sleep 2
  69. if [ $(getconf WORD_BIT) != '32' ] && [ $(getconf LONG_BIT) != '64' ]; then
  70. echo "本软件不支持 32 位系统(x86),请使用 64 位系统(x86_64),如果检测有误,请联系作者"
  71. exit -1
  72. fi
  73. os_version=""
  74. # os version
  75. if [[ -f /etc/os-release ]]; then
  76. os_version=$(awk -F'[= ."]' '/VERSION_ID/{print $3}' /etc/os-release)
  77. fi
  78. if [[ -z "$os_version" && -f /etc/lsb-release ]]; then
  79. os_version=$(awk -F'[= ."]+' '/DISTRIB_RELEASE/{print $2}' /etc/lsb-release)
  80. fi
  81. if [[ x"${release}" == x"centos" ]]; then
  82. if [[ ${os_version} -le 6 ]]; then
  83. echo -e "${red}请使用 CentOS 7 或更高版本的系统!${plain}\n" && exit 1
  84. fi
  85. elif [[ x"${release}" == x"ubuntu" ]]; then
  86. if [[ ${os_version} -lt 16 ]]; then
  87. echo -e "${red}请使用 Ubuntu 16 或更高版本的系统!${plain}\n" && exit 1
  88. fi
  89. elif [[ x"${release}" == x"debian" ]]; then
  90. if [[ ${os_version} -lt 8 ]]; then
  91. echo -e "${red}请使用 Debian 8 或更高版本的系统!${plain}\n" && exit 1
  92. fi
  93. elif [[ x"${release}" == x"amazon_linux" ]]; then
  94. if [[ ${os_version} -lt 2 ]]; then
  95. echo -e "${red}请使用 Amazon Linux 2 或更高版本的系统!${plain}\n" && exit 1
  96. fi
  97. fi
  98. ports=$(/usr/local/xray-ui/xray-ui 2>&1 | grep tcp | awk '{print $5}' | sed "s/://g")
  99. if [[ -n $ports ]]; then
  100. green "经检测,xray-ui已安装"
  101. echo
  102. acp=$(/usr/local/xray-ui/xray-ui setting -show 2>/dev/null)
  103. green "$acp"
  104. echo
  105. readp "是否直接重装xray-ui,请输入Y/y键并回车。如不重装,输入N/n键回车退出脚本):" ins
  106. if [[ $ins = [Yy] ]]; then
  107. systemctl stop xray-ui
  108. systemctl disable xray-ui
  109. rm /etc/systemd/system/xray-ui.service -f
  110. systemctl daemon-reload
  111. systemctl reset-failed
  112. rm /etc/xray-ui/ -rf
  113. rm /usr/local/xray-ui/ -rf
  114. rm -rf /root/rayuil.sh /root/acme.sh
  115. sed -i '/xrayuil.sh/d' /etc/crontab
  116. sed -i '/xray-ui restart/d' /etc/crontab
  117. else
  118. exit 1
  119. fi
  120. fi
  121. install_base() {
  122. if [[ x"${release}" == x"centos" ]]; then
  123. if [[ ${os_version} =~ 8 ]]; then
  124. yum clean all && yum makecache
  125. fi
  126. yum install epel-release -y && yum install wget curl tar gzip lsof -y
  127. setenforce 0 >/dev/null 2>&1
  128. else
  129. apt update && apt install wget curl tar lsof gzip -y
  130. fi
  131. }
  132. generate_random_string() {
  133. local n=$1
  134. # 定义数字、大写字母和小写字母的集合
  135. local characters='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  136. # 生成随机字符并限制在指定字符集中
  137. # 从 /dev/urandom 生成随机字节,使用 tr 进行过滤
  138. local random_string=$(cat /dev/urandom | tr -dc "$characters" | fold -w "$n" | head -n 1)
  139. echo "$random_string"
  140. }
  141. install_xray-ui() {
  142. systemctl stop xray-ui
  143. cd /usr/local/
  144. if [ $# == 0 ]; then
  145. wget --no-check-certificate -O /usr/local/xray-ui-linux-$(arch).tar.gz https://github.com/qist/xray-ui/releases/download/${releases_version}/xray-ui-linux-$(arch).tar.gz
  146. if [[ $? -ne 0 ]]; then
  147. echo -e "${red}下载 xray-ui 失败,请确保你的服务器能够下载 Github 的文件${plain}"
  148. rm -f install.sh
  149. exit 1
  150. fi
  151. else
  152. last_version=$1
  153. url="https://github.com/qist/xray-ui/releases/download/${releases_version}/xray-ui-linux-$(arch).tar.gz"
  154. echo -e "开始安装 xray-ui v$1"
  155. wget --no-check-certificate -O /usr/local/xray-ui-linux-$(arch).tar.gz ${url}
  156. if [[ $? -ne 0 ]]; then
  157. echo -e "${red}下载 xray-ui v$1 失败,请确保此版本存在${plain}"
  158. rm -f install.sh
  159. exit 1
  160. fi
  161. fi
  162. if [[ -e /usr/local/xray-ui/ ]]; then
  163. rm /usr/local/xray-ui/ -rf
  164. fi
  165. tar -zxvf xray-ui-linux-$(arch).tar.gz
  166. rm xray-ui-linux-$(arch).tar.gz -f
  167. cd xray-ui
  168. # Check the system's architecture and rename the file accordingly
  169. if [[ $(arch) == "armv5" || $(arch) == "armv6" || $(arch) == "armv7" ]]; then
  170. mv bin/xray-linux-$(arch) bin/xray-linux-arm
  171. chmod +x bin/xray-linux-arm
  172. fi
  173. chmod +x xray-ui bin/xray-linux-$(arch)
  174. cp -f xray-ui.service /etc/systemd/system/
  175. wget --no-check-certificate -O /usr/bin/xray-ui https://raw.githubusercontent.com/qist/xray-ui/main/xray-ui.sh
  176. chmod +x /usr/bin/xray-ui
  177. systemctl daemon-reload
  178. systemctl enable xray-ui
  179. systemctl start xray-ui
  180. sleep 2
  181. cat >/root/xrayuil.sh <<-\EOF
  182. #!/bin/bash
  183. xui=`ps -aux |grep "xray-ui" |grep -v "grep" |wc -l`
  184. xray=`ps -aux |grep "xray-linux" |grep -v "grep" |wc -l`
  185. sleep 1
  186. if [ $xui = 0 ];then
  187. xray-ui restart
  188. fi
  189. if [ $xray = 0 ];then
  190. xray-ui restart
  191. fi
  192. EOF
  193. chmod +x /root/xrayuil.sh
  194. sed -i '/xrayuil.sh/d' /etc/crontab
  195. echo "*/1 * * * * root bash /root/xrayuil.sh >/dev/null 2>&1" >>/etc/crontab
  196. sed -i '/xray-ui restart/d' /etc/crontab
  197. echo "0 1 1 * * root xray-ui restart >/dev/null 2>&1" >>/etc/crontab
  198. sleep 1
  199. echo -e ""
  200. blue "以下设置内容建议自定义,以防止账号密码路径及端口泄露"
  201. echo -e ""
  202. readp "设置xray-ui登录用户名(回车跳过为随机6位字符):" username
  203. if [[ -z ${username} ]]; then
  204. uauto=$(date +%s%N | md5sum | cut -c 1-6)
  205. username=$uauto
  206. fi
  207. sleep 1
  208. green "xray-ui登录用户名:${username}"
  209. echo -e ""
  210. readp "设置xray-ui登录密码(回车跳过为随机6位字符):" password
  211. if [[ -z ${password} ]]; then
  212. pauto=$(date +%s%N | md5sum | cut -c 1-6)
  213. password=$pauto
  214. fi
  215. green "xray-ui登录密码:${password}"
  216. /usr/local/xray-ui/xray-ui setting -username ${username} -password ${password} >/dev/null 2>&1
  217. sleep 1
  218. echo -e ""
  219. readp "设置xray-ui登录端口[1-65535](回车跳过为2000-65535之间的随机端口):" port
  220. if [[ -z $port ]]; then
  221. port=$(shuf -i 2000-65535 -n 1)
  222. until [[ -z $(ss -ntlp | awk '{print $4}' | grep -w "$port") ]]; do
  223. [[ -n $(ss -ntlp | awk '{print $4}' | grep -w "$port") ]] && yellow "\n端口被占用,请重新输入端口" && readp "自定义xray-ui端口:" port
  224. done
  225. else
  226. until [[ -z $(ss -ntlp | awk '{print $4}' | grep -w "$port") ]]; do
  227. [[ -n $(ss -ntlp | awk '{print $4}' | grep -w "$port") ]] && yellow "\n端口被占用,请重新输入端口" && readp "自定义xray-ui端口:" port
  228. done
  229. fi
  230. /usr/local/xray-ui/xray-ui setting -port $port >/dev/null 2>&1
  231. green "xray-ui登录端口:${port}"
  232. sleep 1
  233. echo -e ""
  234. readp "设置xray-ui web 路径 (回车跳过为随机10位字符):" path
  235. if [[ -z ${path} ]]; then
  236. path=$(generate_random_string 10)
  237. fi
  238. /usr/local/xray-ui/xray-ui setting -webBasePath $path >/dev/null 2>&1
  239. green "xray-ui web 路径:${path}"
  240. sleep 1
  241. xray-ui restart
  242. xuilogin() {
  243. v4=$(curl -s4m8 http://ip.sb -k)
  244. v6=$(curl -s6m8 http://ip.sb -k)
  245. if [[ -z $v4 ]]; then
  246. int="${green}请在浏览器地址栏复制${plain} ${bblue}[$v6]:$ports/$path${plain} ${green}进入xray-ui登录界面\n当前xray-ui登录用户名:${plain}${bblue}${username}${plain}${green} \n当前xray-ui登录密码:${plain}${bblue}${password}${plain}"
  247. elif [[ -n $v4 && -n $v6 ]]; then
  248. int="${green}请在浏览器地址栏复制${plain} ${bblue}$v4:$ports/$path${plain} ${yellow}或者${plain} ${bblue}[$v6]:$ports/$path${plain} ${green}进入xray-ui登录界面\n当前xray-ui登录用户名:${plain}${bblue}${username}${plain}${green} \n当前xray-ui登录密码:${plain}${bblue}${password}${plain}"
  249. else
  250. int="${green}请在浏览器地址栏复制${plain} ${bblue}$v4:$ports/$path${plain} ${green}进入xray-ui登录界面\n当前xray-ui登录用户名:${plain}${bblue}${username}${plain}${green} \n当前xray-ui登录密码:${plain}${bblue}${password}${plain}"
  251. fi
  252. }
  253. ssh_forwarding() {
  254. # 获取 IPv4 和 IPv6 地址
  255. v4=$(curl -s4m8 http://ip.sb -k)
  256. v6=$(curl -s6m8 http://ip.sb -k)
  257. echo -e ""
  258. read -p "设置 xray-ui ssh 转发端口 [1-65535](回车跳过为 2000-65535 之间的随机端口):" ssh_port
  259. # 如果未输入端口,则随机生成一个2000-65535之间的端口
  260. if [[ -z $ssh_port ]]; then
  261. ssh_port=$(shuf -i 2000-65535 -n 1)
  262. fi
  263. # 检查端口是否被占用,直到找到未占用的端口
  264. while [[ -n $(ss -ntlp | awk '{print $4}' | grep -w ":$ssh_port") ]]; do
  265. echo -e "\n端口 $ssh_port 被占用,请重新输入端口"
  266. read -p "自定义 xray-ui ssh 转发端口:" ssh_port
  267. if [[ -z $ssh_port ]]; then
  268. ssh_port=$(shuf -i 2000-65535 -n 1)
  269. fi
  270. done
  271. # 检查 IP 并输出相应的 SSH 和浏览器访问信息
  272. if [[ -z $v4 ]]; then
  273. # echo -e "${green}请在 xray-ui 服务器系统输入${plain} ${bblue}ssh -f -N -L [::]:$ssh_port:127.0.0.1:$ports root@127.0.0.1${plain} 输入 root 密码进行转发 不建议使用"
  274. # echo -e "${green}请在浏览器地址栏复制${plain} ${bblue}[$v6]:$ssh_port/$path${plain} ${green}进入 xray-ui 登录界面"
  275. echo -e "${green}客户端转发 安全性高${plain} ${bblue}ssh -f -N -L [::]:$ssh_port:127.0.0.1:$ports root@[$v6]${plain} 输入 root 密码进行转发"
  276. echo -e "${green}请在浏览器地址栏复制${plain} ${bblue}[::1]:$ssh_port/$path${plain} ${green}进入 xray-ui 登录界面"
  277. echo -e "${green}当前 xray-ui 登录用户名:${plain}${bblue}${username}${plain}"
  278. echo -e "${green}当前 xray-ui 登录密码:${plain}${bblue}${password}${plain}"
  279. yellow "不使用ssh 转发请配置nginx https代理或者xray-ui 配置证书"
  280. elif [[ -n $v4 && -n $v6 ]]; then
  281. # echo -e "${green}请在 xray-ui 服务器系统输入${plain} ${bblue}ssh -f -N -L 0.0.0.0:$ssh_port:127.0.0.1:$ports root@127.0.0.1${plain} ${yellow}或者 ${bblue}ssh -f -N -L [::]:$ssh_port:127.0.0.1:$ports root@127.0.0.1${plain} 输入 root 密码进行转发 不建议使用"
  282. # echo -e "${green}请在浏览器地址栏复制${plain} ${bblue}$v4:$ssh_port/$path${plain} ${yellow}或者${plain} ${bblue}[$v6]:$ssh_port/$path${plain} ${green}进入 xray-ui 登录界面"
  283. echo -e "${green}客户端转发 安全性高 ${plain} ${bblue}ssh -f -N -L 0.0.0.0:$ssh_port:127.0.0.1:$ports root@$v4${plain} ${yellow}或者 ${bblue}ssh -f -N -L [::]:$ssh_port:127.0.0.1:$ports root@[$v6]${plain} 输入 root 密码进行转发"
  284. echo -e "${green}请在浏览器地址栏复制${plain} ${bblue}127.0.0.1:$ssh_port/$path${plain} ${yellow}或者${plain} ${bblue}[::1]:$ssh_port/$path${plain} ${green}进入 xray-ui 登录界面"
  285. echo -e "${green}当前 xray-ui 登录用户名:${plain}${bblue}${username}${plain}"
  286. echo -e "${green}当前 xray-ui 登录密码:${plain}${bblue}${password}${plain}"
  287. yellow "不使用ssh 转发请配置nginx https代理或者xray-ui 配置证书"
  288. else
  289. # echo -e "${green}请在 xray-ui 服务器系统输入${plain} ${bblue}ssh -f -N -L 0.0.0.0:$ssh_port:127.0.0.1:$ports root@127.0.0.1${plain} 输入 root 密码进行转发 "
  290. # echo -e "${green}请在浏览器地址栏复制${plain} ${bblue}$v4:$ssh_port/$path${plain} ${green}进入 xray-ui 登录界面"
  291. echo -e "${green}客户端转发 安全性高${plain} ${bblue}ssh -f -N -L 0.0.0.0:$ssh_port:127.0.0.1:$ports root@$v4${plain} 输入 root 密码进行转发"
  292. echo -e "${green}请在浏览器地址栏复制${plain} ${bblue}127.0.0.1:$ssh_port/$path${plain} ${green}进入 xray-ui 登录界面"
  293. echo -e "${green}当前 xray-ui 登录用户名:${plain}${bblue}${username}${plain}"
  294. echo -e "${green}当前 xray-ui 登录密码:${plain}${bblue}${password}${plain}"
  295. yellow "不使用ssh 转发请配置nginx https代理或者xray-ui 配置证书"
  296. fi
  297. }
  298. ports=$(/usr/local/xray-ui/xray-ui 2>&1 | grep "tcp" | awk '{print $5}' | cut -d':' -f2)
  299. if [[ -n $ports ]]; then
  300. echo -e ""
  301. yellow "xray-ui $remoteV 安装成功,请稍等3秒,检测IP环境,输出xray-ui登录信息……"
  302. ssh_forwarding
  303. yellow "下面是xray-ui tls mTLS 配置信息"
  304. yellow "证书管理 xray-ui ssl_main cf 证书申请 xray-ui ssl_CF"
  305. yellow "TLS 配置 /usr/local/xray-ui/xray-ui cert -webCert /root/cert/你的域名/fullchain.pem -webCertKey /root/cert/你的域名/privkey.pem 重启 xray-ui restart 生效"
  306. yellow "mTLS 配置 /usr/local/xray-ui/xray-ui cert -webCert /root/cert/你的域名/fullchain.pem -webCertKey /root/cert/你的域名/privkey.pem -webCa /root/cert/ca.cer 重启 xray-ui restart 生效"
  307. yellow "访问:https://你的域名:$ports/$path"
  308. yellow "mTLS windows 使用....."
  309. yellow "生成windows客户端证书 client.p12..."
  310. yellow "openssl pkcs12 -export -out client.p12 -inkey /root/cert/你的域名/privkey.pem -in /root/cert/${domain}.cer -certfile /root/cert/ca.cer"
  311. yellow "client.p12: windows客户端证书 记得设置密码 导入证书需要密码"
  312. yellow "client.p12 文件导入windows系统 桌面双击打开->导入->下一步->将所有证书都放入下列存储->个人->完成 如果导入失败开始菜单搜证书 打开管理用户证书管理->个人->所有任务->导入->输入密码"
  313. else
  314. red "xray-ui安装失败,请查看日志,运行 xray-ui log"
  315. fi
  316. sleep 1
  317. echo -e ""
  318. echo -e "$int"
  319. echo -e ""
  320. echo -e "xray-ui 管理脚本使用方法: "
  321. echo -e "----------------------------------------------"
  322. echo -e "xray-ui - 显示管理菜单"
  323. echo -e "xray-ui start - 启动 xray-ui 面板"
  324. echo -e "xray-ui stop - 停止 xray-ui 面板"
  325. echo -e "xray-ui restart - 重启 xray-ui 面板"
  326. echo -e "xray-ui status - 查看 xray-ui 状态"
  327. echo -e "xray-ui enable - 设置 xray-ui 开机自启"
  328. echo -e "xray-ui disable - 取消 xray-ui 开机自启"
  329. echo -e "xray-ui log - 查看 xray-ui 日志"
  330. echo -e "xray-ui v2-ui - 迁移本机器的 v2-ui 账号数据至 xray-ui"
  331. echo -e "xray-ui update - 更新 xray-ui 面板"
  332. echo -e "xray-ui geoip - 更新 geoip ip库"
  333. echo -e "xray-ui update_shell - 更新 xray-ui 脚本"
  334. echo -e "xray-ui install - 安装 xray-ui 面板"
  335. echo -e "xray-ui x25519 - REALITY key 生成"
  336. echo -e "xray-ui ssl_main - SSL 证书管理"
  337. echo -e "xray-ui ssl_CF - Cloudflare SSL 证书"
  338. echo -e "xray-ui crontab - 添加geoip到任务计划每天凌晨1.30执行"
  339. echo -e "xray-ui uninstall - 卸载 xray-ui 面板"
  340. echo -e "----------------------------------------------"
  341. rm -f install.sh
  342. }
  343. echo -e "${green}开始安装xray-ui必要依赖${plain}"
  344. install_base
  345. echo -e "${green}开始安装xray-ui核心组件${plain}"
  346. install_xray-ui $1