install.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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}错误:${plain} 必须使用root用户运行此脚本!\n" && exit 1
  9. # check os
  10. if [[ -f /etc/redhat-release ]]; then
  11. release="centos"
  12. elif cat /etc/issue | grep -Eqi "debian"; then
  13. release="debian"
  14. elif cat /etc/issue | grep -Eqi "ubuntu"; then
  15. release="ubuntu"
  16. elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
  17. release="centos"
  18. elif cat /proc/version | grep -Eqi "debian"; then
  19. release="debian"
  20. elif cat /proc/version | grep -Eqi "ubuntu"; then
  21. release="ubuntu"
  22. elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
  23. release="centos"
  24. else
  25. echo -e "${red}未检测到系统版本,请联系脚本作者!${plain}\n" && exit 1
  26. fi
  27. arch=$(arch)
  28. if [[ $arch == "x86_64" || $arch == "x64" || $arch == "amd64" ]]; then
  29. arch="64"
  30. elif [[ $arch == "aarch64" || $arch == "arm64" ]]; then
  31. arch="arm64-v8a"
  32. elif [[ $arch == "s390x" ]]; then
  33. arch="s390x"
  34. else
  35. arch="64"
  36. echo -e "${red}检测架构失败,使用默认架构: ${arch}${plain}"
  37. fi
  38. echo "架构: ${arch}"
  39. if [ "$(getconf WORD_BIT)" != '32' ] && [ "$(getconf LONG_BIT)" != '64' ] ; then
  40. echo "本软件不支持 32 位系统(x86),请使用 64 位系统(x86_64),如果检测有误,请联系作者"
  41. exit 2
  42. fi
  43. os_version=""
  44. # os version
  45. if [[ -f /etc/os-release ]]; then
  46. os_version=$(awk -F'[= ."]' '/VERSION_ID/{print $3}' /etc/os-release)
  47. fi
  48. if [[ -z "$os_version" && -f /etc/lsb-release ]]; then
  49. os_version=$(awk -F'[= ."]+' '/DISTRIB_RELEASE/{print $2}' /etc/lsb-release)
  50. fi
  51. if [[ x"${release}" == x"centos" ]]; then
  52. if [[ ${os_version} -le 6 ]]; then
  53. echo -e "${red}请使用 CentOS 7 或更高版本的系统!${plain}\n" && exit 1
  54. fi
  55. elif [[ x"${release}" == x"ubuntu" ]]; then
  56. if [[ ${os_version} -lt 16 ]]; then
  57. echo -e "${red}请使用 Ubuntu 16 或更高版本的系统!${plain}\n" && exit 1
  58. fi
  59. elif [[ x"${release}" == x"debian" ]]; then
  60. if [[ ${os_version} -lt 8 ]]; then
  61. echo -e "${red}请使用 Debian 8 或更高版本的系统!${plain}\n" && exit 1
  62. fi
  63. fi
  64. install_base() {
  65. if [[ x"${release}" == x"centos" ]]; then
  66. yum install epel-release -y
  67. yum install wget curl unzip tar crontabs socat -y
  68. else
  69. apt update -y
  70. apt install wget curl unzip tar cron socat -y
  71. fi
  72. }
  73. # 0: running, 1: not running, 2: not installed
  74. check_status() {
  75. if [[ ! -f /etc/systemd/system/XrayR.service ]]; then
  76. return 2
  77. fi
  78. temp=$(systemctl status XrayR | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
  79. if [[ x"${temp}" == x"running" ]]; then
  80. return 0
  81. else
  82. return 1
  83. fi
  84. }
  85. install_acme() {
  86. curl https://get.acme.sh | sh
  87. }
  88. install_XrayR() {
  89. if [[ -e /usr/local/XrayR/ ]]; then
  90. rm /usr/local/XrayR/ -rf
  91. fi
  92. mkdir /usr/local/XrayR/ -p
  93. cd /usr/local/XrayR/
  94. if [ $# == 0 ] ;then
  95. last_version=$(curl -Ls "https://api.github.com/repos/XrayR-project/XrayR/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
  96. if [[ ! -n "$last_version" ]]; then
  97. echo -e "${red}检测 XrayR 版本失败,可能是超出 Github API 限制,请稍后再试,或手动指定 XrayR 版本安装${plain}"
  98. exit 1
  99. fi
  100. echo -e "检测到 XrayR 最新版本:${last_version},开始安装"
  101. wget -q -N --no-check-certificate -O /usr/local/XrayR/XrayR-linux.zip https://github.com/XrayR-project/XrayR/releases/download/${last_version}/XrayR-linux-${arch}.zip
  102. if [[ $? -ne 0 ]]; then
  103. echo -e "${red}下载 XrayR 失败,请确保你的服务器能够下载 Github 的文件${plain}"
  104. exit 1
  105. fi
  106. else
  107. if [[ $1 == v* ]]; then
  108. last_version=$1
  109. else
  110. last_version="v"$1
  111. fi
  112. url="https://github.com/XrayR-project/XrayR/releases/download/${last_version}/XrayR-linux-${arch}.zip"
  113. echo -e "开始安装 XrayR ${last_version}"
  114. wget -q -N --no-check-certificate -O /usr/local/XrayR/XrayR-linux.zip ${url}
  115. if [[ $? -ne 0 ]]; then
  116. echo -e "${red}下载 XrayR ${last_version} 失败,请确保此版本存在${plain}"
  117. exit 1
  118. fi
  119. fi
  120. unzip XrayR-linux.zip
  121. rm XrayR-linux.zip -f
  122. chmod +x XrayR
  123. mkdir /etc/XrayR/ -p
  124. rm /etc/systemd/system/XrayR.service -f
  125. file="https://github.com/XrayR-project/XrayR-release/raw/master/XrayR.service"
  126. wget -q -N --no-check-certificate -O /etc/systemd/system/XrayR.service ${file}
  127. #cp -f XrayR.service /etc/systemd/system/
  128. systemctl daemon-reload
  129. systemctl stop XrayR
  130. systemctl enable XrayR
  131. echo -e "${green}XrayR ${last_version}${plain} 安装完成,已设置开机自启"
  132. cp geoip.dat /etc/XrayR/
  133. cp geosite.dat /etc/XrayR/
  134. if [[ ! -f /etc/XrayR/config.yml ]]; then
  135. cp config.yml /etc/XrayR/
  136. echo -e ""
  137. echo -e "全新安装,请先参看教程:https://github.com/XrayR-project/XrayR,配置必要的内容"
  138. else
  139. systemctl start XrayR
  140. sleep 2
  141. check_status
  142. echo -e ""
  143. if [[ $? == 0 ]]; then
  144. echo -e "${green}XrayR 重启成功${plain}"
  145. else
  146. echo -e "${red}XrayR 可能启动失败,请稍后使用 XrayR log 查看日志信息,若无法启动,则可能更改了配置格式,请前往 wiki 查看:https://github.com/XrayR-project/XrayR/wiki${plain}"
  147. fi
  148. fi
  149. if [[ ! -f /etc/XrayR/dns.json ]]; then
  150. cp dns.json /etc/XrayR/
  151. fi
  152. if [[ ! -f /etc/XrayR/route.json ]]; then
  153. cp route.json /etc/XrayR/
  154. fi
  155. if [[ ! -f /etc/XrayR/custom_outbound.json ]]; then
  156. cp custom_outbound.json /etc/XrayR/
  157. fi
  158. if [[ ! -f /etc/XrayR/custom_inbound.json ]]; then
  159. cp custom_inbound.json /etc/XrayR/
  160. fi
  161. if [[ ! -f /etc/XrayR/rulelist ]]; then
  162. cp rulelist /etc/XrayR/
  163. fi
  164. curl -o /usr/bin/XrayR -Ls https://raw.githubusercontent.com/XrayR-project/XrayR-release/master/XrayR.sh
  165. chmod +x /usr/bin/XrayR
  166. ln -s /usr/bin/XrayR /usr/bin/xrayr # 小写兼容
  167. chmod +x /usr/bin/xrayr
  168. cd $cur_dir
  169. rm -f install.sh
  170. echo -e ""
  171. echo "XrayR 管理脚本使用方法 (兼容使用xrayr执行,大小写不敏感): "
  172. echo "------------------------------------------"
  173. echo "XrayR - 显示管理菜单 (功能更多)"
  174. echo "XrayR start - 启动 XrayR"
  175. echo "XrayR stop - 停止 XrayR"
  176. echo "XrayR restart - 重启 XrayR"
  177. echo "XrayR status - 查看 XrayR 状态"
  178. echo "XrayR enable - 设置 XrayR 开机自启"
  179. echo "XrayR disable - 取消 XrayR 开机自启"
  180. echo "XrayR log - 查看 XrayR 日志"
  181. echo "XrayR update - 更新 XrayR"
  182. echo "XrayR update x.x.x - 更新 XrayR 指定版本"
  183. echo "XrayR config - 显示配置文件内容"
  184. echo "XrayR install - 安装 XrayR"
  185. echo "XrayR uninstall - 卸载 XrayR"
  186. echo "XrayR version - 查看 XrayR 版本"
  187. echo "------------------------------------------"
  188. }
  189. echo -e "${green}开始安装${plain}"
  190. install_base
  191. # install_acme
  192. install_XrayR $1