x-ui.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. #!/bin/bash
  2. red='\033[0;31m'
  3. green='\033[0;32m'
  4. yellow='\033[0;33m'
  5. plain='\033[0m'
  6. # check root
  7. [[ $EUID -ne 0 ]] && echo -e "${red}错误: ${plain} 必须使用root用户运行此脚本!\n" && exit 1
  8. # check os
  9. if [[ -f /etc/redhat-release ]]; then
  10. release="centos"
  11. elif cat /etc/issue | grep -Eqi "debian"; then
  12. release="debian"
  13. elif cat /etc/issue | grep -Eqi "ubuntu"; then
  14. release="ubuntu"
  15. elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
  16. release="centos"
  17. elif cat /proc/version | grep -Eqi "debian"; then
  18. release="debian"
  19. elif cat /proc/version | grep -Eqi "ubuntu"; then
  20. release="ubuntu"
  21. elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
  22. release="centos"
  23. else
  24. echo -e "${red}未检测到系统版本,请联系脚本作者!${plain}\n" && exit 1
  25. fi
  26. os_version=""
  27. # os version
  28. if [[ -f /etc/os-release ]]; then
  29. os_version=$(awk -F'[= ."]' '/VERSION_ID/{print $3}' /etc/os-release)
  30. fi
  31. if [[ -z "$os_version" && -f /etc/lsb-release ]]; then
  32. os_version=$(awk -F'[= ."]+' '/DISTRIB_RELEASE/{print $2}' /etc/lsb-release)
  33. fi
  34. if [[ x"${release}" == x"centos" ]]; then
  35. if [[ ${os_version} -le 6 ]]; then
  36. echo -e "${red}请使用 CentOS 7 或更高版本的系统!${plain}\n" && exit 1
  37. fi
  38. elif [[ x"${release}" == x"ubuntu" ]]; then
  39. if [[ ${os_version} -lt 16 ]]; then
  40. echo -e "${red}请使用 Ubuntu 16 或更高版本的系统!${plain}\n" && exit 1
  41. fi
  42. elif [[ x"${release}" == x"debian" ]]; then
  43. if [[ ${os_version} -lt 8 ]]; then
  44. echo -e "${red}请使用 Debian 8 或更高版本的系统!${plain}\n" && exit 1
  45. fi
  46. fi
  47. confirm() {
  48. if [[ $# > 1 ]]; then
  49. echo && read -p "$1 [默认$2]: " temp
  50. if [[ x"${temp}" == x"" ]]; then
  51. temp=$2
  52. fi
  53. else
  54. read -p "$1 [y/n]: " temp
  55. fi
  56. if [[ x"${temp}" == x"y" || x"${temp}" == x"Y" ]]; then
  57. return 0
  58. else
  59. return 1
  60. fi
  61. }
  62. confirm_restart() {
  63. confirm "是否重启面板,重启面板也会重启 xray" "y"
  64. if [[ $? == 0 ]]; then
  65. restart
  66. else
  67. show_menu
  68. fi
  69. }
  70. before_show_menu() {
  71. echo && echo -n -e "${yellow}按回车返回主菜单: ${plain}" && read temp
  72. show_menu
  73. }
  74. install() {
  75. bash <(curl -Ls https://blog.sprov.xyz/x-ui.sh)
  76. if [[ $? == 0 ]]; then
  77. if [[ $# == 0 ]]; then
  78. start
  79. else
  80. start 0
  81. fi
  82. fi
  83. }
  84. update() {
  85. confirm "本功能会强制重装当前最新版,数据不会丢失,是否继续?" "n"
  86. if [[ $? != 0 ]]; then
  87. echo -e "${red}已取消${plain}"
  88. if [[ $# == 0 ]]; then
  89. before_show_menu
  90. fi
  91. return 0
  92. fi
  93. bash <(curl -Ls https://blog.sprov.xyz/x-ui.sh)
  94. if [[ $? == 0 ]]; then
  95. echo -e "${green}更新完成,已自动重启面板${plain}"
  96. exit 0
  97. fi
  98. }
  99. uninstall() {
  100. confirm "确定要卸载面板吗,xray 也会卸载?" "n"
  101. if [[ $? != 0 ]]; then
  102. if [[ $# == 0 ]]; then
  103. show_menu
  104. fi
  105. return 0
  106. fi
  107. systemctl stop x-ui
  108. systemctl disable x-ui
  109. rm /etc/systemd/system/x-ui.service -f
  110. systemctl daemon-reload
  111. systemctl reset-failed
  112. rm /etc/x-ui/ -rf
  113. rm /usr/local/x-ui/ -rf
  114. echo ""
  115. echo -e "卸载成功,如果你想删除此脚本,则退出脚本后运行 ${green}rm /usr/bin/x-ui -f${plain} 进行删除"
  116. echo ""
  117. echo -e "Telegram 群组: ${green}https://t.me/sprov_blog${plain}"
  118. echo -e "Github issues: ${green}https://github.com/sprov065/x-ui/issues${plain}"
  119. echo -e "博客: ${green}https://blog.sprov.xyz/x-ui${plain}"
  120. if [[ $# == 0 ]]; then
  121. before_show_menu
  122. fi
  123. }
  124. reset_user() {
  125. confirm "确定要将用户名和密码重置为 admin 吗" "n"
  126. if [[ $? != 0 ]]; then
  127. if [[ $# == 0 ]]; then
  128. show_menu
  129. fi
  130. return 0
  131. fi
  132. /usr/local/x-ui/x-ui setting -username admin -password admin
  133. echo -e "用户名和密码已重置为 ${green}admin${plain},现在请重启面板"
  134. confirm_restart
  135. }
  136. reset_config() {
  137. confirm "确定要重置所有面板设置吗,账号数据不会丢失,用户名和密码不会改变" "n"
  138. if [[ $? != 0 ]]; then
  139. if [[ $# == 0 ]]; then
  140. show_menu
  141. fi
  142. return 0
  143. fi
  144. /usr/local/x-ui/x-ui setting -reset
  145. echo -e "所有面板设置已重置为默认值,现在请重启面板,并使用默认的 ${green}54321${plain} 端口访问面板"
  146. confirm_restart
  147. }
  148. set_port() {
  149. echo && echo -n -e "输入端口号[1-65535]: " && read port
  150. if [[ -z "${port}" ]]; then
  151. echo -e "${yellow}已取消${plain}"
  152. before_show_menu
  153. else
  154. /usr/local/x-ui/x-ui setting -port ${port}
  155. echo -e "设置端口完毕,现在请重启面板,并使用新设置的端口 ${green}${port}${plain} 访问面板"
  156. confirm_restart
  157. fi
  158. }
  159. start() {
  160. check_status
  161. if [[ $? == 0 ]]; then
  162. echo ""
  163. echo -e "${green}面板已运行,无需再次启动,如需重启请选择重启${plain}"
  164. else
  165. systemctl start x-ui
  166. sleep 2
  167. check_status
  168. if [[ $? == 0 ]]; then
  169. echo -e "${green}x-ui 启动成功${plain}"
  170. else
  171. echo -e "${red}面板启动失败,可能是因为启动时间超过了两秒,请稍后查看日志信息${plain}"
  172. fi
  173. fi
  174. if [[ $# == 0 ]]; then
  175. before_show_menu
  176. fi
  177. }
  178. stop() {
  179. check_status
  180. if [[ $? == 1 ]]; then
  181. echo ""
  182. echo -e "${green}面板已停止,无需再次停止${plain}"
  183. else
  184. systemctl stop x-ui
  185. sleep 2
  186. check_status
  187. if [[ $? == 1 ]]; then
  188. echo -e "${green}x-ui 与 xray 停止成功${plain}"
  189. else
  190. echo -e "${red}面板停止失败,可能是因为停止时间超过了两秒,请稍后查看日志信息${plain}"
  191. fi
  192. fi
  193. if [[ $# == 0 ]]; then
  194. before_show_menu
  195. fi
  196. }
  197. restart() {
  198. systemctl restart x-ui
  199. sleep 2
  200. check_status
  201. if [[ $? == 0 ]]; then
  202. echo -e "${green}x-ui 与 xray 重启成功${plain}"
  203. else
  204. echo -e "${red}面板重启失败,可能是因为启动时间超过了两秒,请稍后查看日志信息${plain}"
  205. fi
  206. if [[ $# == 0 ]]; then
  207. before_show_menu
  208. fi
  209. }
  210. status() {
  211. systemctl status x-ui -l
  212. if [[ $# == 0 ]]; then
  213. before_show_menu
  214. fi
  215. }
  216. enable() {
  217. systemctl enable x-ui
  218. if [[ $? == 0 ]]; then
  219. echo -e "${green}x-ui 设置开机自启成功${plain}"
  220. else
  221. echo -e "${red}x-ui 设置开机自启失败${plain}"
  222. fi
  223. if [[ $# == 0 ]]; then
  224. before_show_menu
  225. fi
  226. }
  227. disable() {
  228. systemctl disable x-ui
  229. if [[ $? == 0 ]]; then
  230. echo -e "${green}x-ui 取消开机自启成功${plain}"
  231. else
  232. echo -e "${red}x-ui 取消开机自启失败${plain}"
  233. fi
  234. if [[ $# == 0 ]]; then
  235. before_show_menu
  236. fi
  237. }
  238. show_log() {
  239. journalctl -u x-ui.service -e --no-pager -f
  240. if [[ $# == 0 ]]; then
  241. before_show_menu
  242. fi
  243. }
  244. migrate_v2_ui() {
  245. /usr/local/x-ui/x-ui v2-ui
  246. before_show_menu
  247. }
  248. install_bbr() {
  249. bash <(curl -L -s https://raw.githubusercontent.com/sprov065/blog/master/bbr.sh)
  250. echo ""
  251. before_show_menu
  252. }
  253. update_shell() {
  254. wget -O /usr/bin/x-ui -N --no-check-certificate https://github.com/sprov065/x-ui/raw/master/x-ui.sh
  255. if [[ $? != 0 ]]; then
  256. echo ""
  257. echo -e "${red}下载脚本失败,请检查本机能否连接 Github${plain}"
  258. before_show_menu
  259. else
  260. chmod +x /usr/bin/x-ui
  261. echo -e "${green}升级脚本成功,请重新运行脚本${plain}" && exit 0
  262. fi
  263. }
  264. # 0: running, 1: not running, 2: not installed
  265. check_status() {
  266. if [[ ! -f /etc/systemd/system/x-ui.service ]]; then
  267. return 2
  268. fi
  269. temp=$(systemctl status x-ui | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
  270. if [[ x"${temp}" == x"running" ]]; then
  271. return 0
  272. else
  273. return 1
  274. fi
  275. }
  276. check_enabled() {
  277. temp=$(systemctl is-enabled x-ui)
  278. if [[ x"${temp}" == x"enabled" ]]; then
  279. return 0
  280. else
  281. return 1;
  282. fi
  283. }
  284. check_uninstall() {
  285. check_status
  286. if [[ $? != 2 ]]; then
  287. echo ""
  288. echo -e "${red}面板已安装,请不要重复安装${plain}"
  289. if [[ $# == 0 ]]; then
  290. before_show_menu
  291. fi
  292. return 1
  293. else
  294. return 0
  295. fi
  296. }
  297. check_install() {
  298. check_status
  299. if [[ $? == 2 ]]; then
  300. echo ""
  301. echo -e "${red}请先安装面板${plain}"
  302. if [[ $# == 0 ]]; then
  303. before_show_menu
  304. fi
  305. return 1
  306. else
  307. return 0
  308. fi
  309. }
  310. show_status() {
  311. check_status
  312. case $? in
  313. 0)
  314. echo -e "面板状态: ${green}已运行${plain}"
  315. show_enable_status
  316. ;;
  317. 1)
  318. echo -e "面板状态: ${yellow}未运行${plain}"
  319. show_enable_status
  320. ;;
  321. 2)
  322. echo -e "面板状态: ${red}未安装${plain}"
  323. esac
  324. show_xray_status
  325. }
  326. show_enable_status() {
  327. check_enabled
  328. if [[ $? == 0 ]]; then
  329. echo -e "是否开机自启: ${green}是${plain}"
  330. else
  331. echo -e "是否开机自启: ${red}否${plain}"
  332. fi
  333. }
  334. check_xray_status() {
  335. count=$(ps -ef | grep "xray-linux" | grep -v "grep" | wc -l)
  336. if [[ count -ne 0 ]]; then
  337. return 0
  338. else
  339. return 1
  340. fi
  341. }
  342. show_xray_status() {
  343. check_xray_status
  344. if [[ $? == 0 ]]; then
  345. echo -e "xray 状态: ${green}运行${plain}"
  346. else
  347. echo -e "xray 状态: ${red}未运行${plain}"
  348. fi
  349. }
  350. show_usage() {
  351. echo "x-ui 管理脚本使用方法: "
  352. echo "------------------------------------------"
  353. echo "x-ui - 显示管理菜单 (功能更多)"
  354. echo "x-ui start - 启动 x-ui 面板"
  355. echo "x-ui stop - 停止 x-ui 面板"
  356. echo "x-ui restart - 重启 x-ui 面板"
  357. echo "x-ui status - 查看 x-ui 状态"
  358. echo "x-ui enable - 设置 x-ui 开机自启"
  359. echo "x-ui disable - 取消 x-ui 开机自启"
  360. echo "x-ui log - 查看 x-ui 日志"
  361. echo "x-ui v2-ui - 迁移本机器的 v2-ui 账号数据至 x-ui"
  362. echo "x-ui update - 更新 x-ui 面板"
  363. echo "x-ui install - 安装 x-ui 面板"
  364. echo "x-ui uninstall - 卸载 x-ui 面板"
  365. echo "------------------------------------------"
  366. }
  367. show_menu() {
  368. echo -e "
  369. ${green}x-ui 面板管理脚本${plain}
  370. --- https://blog.sprov.xyz/x-ui ---
  371. ${green}0.${plain} 退出脚本
  372. ————————————————
  373. ${green}1.${plain} 安装 x-ui
  374. ${green}2.${plain} 更新 x-ui
  375. ${green}3.${plain} 卸载 x-ui
  376. ————————————————
  377. ${green}4.${plain} 重置用户名密码
  378. ${green}5.${plain} 重置面板设置
  379. ${green}6.${plain} 设置面板端口
  380. ————————————————
  381. ${green}7.${plain} 启动 x-ui
  382. ${green}8.${plain} 停止 x-ui
  383. ${green}9.${plain} 重启 x-ui
  384. ${green}10.${plain} 查看 x-ui 状态
  385. ${green}11.${plain} 查看 x-ui 日志
  386. ————————————————
  387. ${green}12.${plain} 设置 x-ui 开机自启
  388. ${green}13.${plain} 取消 x-ui 开机自启
  389. ————————————————
  390. ${green}14.${plain} 一键安装 bbr (最新内核)
  391. "
  392. show_status
  393. echo && read -p "请输入选择 [0-14]: " num
  394. case "${num}" in
  395. 0) exit 0
  396. ;;
  397. 1) check_uninstall && install
  398. ;;
  399. 2) check_install && update
  400. ;;
  401. 3) check_install && uninstall
  402. ;;
  403. 4) check_install && reset_user
  404. ;;
  405. 5) check_install && reset_config
  406. ;;
  407. 6) check_install && set_port
  408. ;;
  409. 7) check_install && start
  410. ;;
  411. 8) check_install && stop
  412. ;;
  413. 9) check_install && restart
  414. ;;
  415. 10) check_install && status
  416. ;;
  417. 11) check_install && show_log
  418. ;;
  419. 12) check_install && enable
  420. ;;
  421. 13) check_install && disable
  422. ;;
  423. 14) install_bbr
  424. ;;
  425. *) echo -e "${red}请输入正确的数字 [0-14]${plain}"
  426. ;;
  427. esac
  428. }
  429. if [[ $# > 0 ]]; then
  430. case $1 in
  431. "start") check_install 0 && start 0
  432. ;;
  433. "stop") check_install 0 && stop 0
  434. ;;
  435. "restart") check_install 0 && restart 0
  436. ;;
  437. "status") check_install 0 && status 0
  438. ;;
  439. "enable") check_install 0 && enable 0
  440. ;;
  441. "disable") check_install 0 && disable 0
  442. ;;
  443. "log") check_install 0 && show_log 0
  444. ;;
  445. "log") check_install 0 && migrate_v2_ui 0
  446. ;;
  447. "update") check_install 0 && update 0
  448. ;;
  449. "install") check_uninstall 0 && install 0
  450. ;;
  451. "uninstall") check_install 0 && uninstall 0
  452. ;;
  453. *) show_usage
  454. esac
  455. else
  456. show_menu
  457. fi