v2-ui.sh 13 KB

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