install.sh 7.1 KB

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