x-ui.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. #!/bin/bash
  2. red='\033[0;31m'
  3. green='\033[0;32m'
  4. yellow='\033[0;33m'
  5. plain='\033[0m'
  6. #Add some basic function here
  7. function LOGD() {
  8. echo -e "${yellow}[DEG] $* ${plain}"
  9. }
  10. function LOGE() {
  11. echo -e "${red}[ERR] $* ${plain}"
  12. }
  13. function LOGI() {
  14. echo -e "${green}[INF] $* ${plain}"
  15. }
  16. # check root
  17. [[ $EUID -ne 0 ]] && LOGE "错误: 必须使用root用户运行此脚本!\n" && exit 1
  18. # check os
  19. if [[ -f /etc/redhat-release ]]; then
  20. release="centos"
  21. elif cat /etc/issue | grep -Eqi "debian"; then
  22. release="debian"
  23. elif cat /etc/issue | grep -Eqi "ubuntu"; then
  24. release="ubuntu"
  25. elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
  26. release="centos"
  27. elif cat /proc/version | grep -Eqi "debian"; then
  28. release="debian"
  29. elif cat /proc/version | grep -Eqi "ubuntu"; then
  30. release="ubuntu"
  31. elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
  32. release="centos"
  33. else
  34. LOGE "未检测到系统版本,请联系脚本作者!\n" && exit 1
  35. fi
  36. os_version=""
  37. # os version
  38. if [[ -f /etc/os-release ]]; then
  39. os_version=$(awk -F'[= ."]' '/VERSION_ID/{print $3}' /etc/os-release)
  40. fi
  41. if [[ -z "$os_version" && -f /etc/lsb-release ]]; then
  42. os_version=$(awk -F'[= ."]+' '/DISTRIB_RELEASE/{print $2}' /etc/lsb-release)
  43. fi
  44. if [[ x"${release}" == x"centos" ]]; then
  45. if [[ ${os_version} -le 6 ]]; then
  46. LOGE "请使用 CentOS 7 或更高版本的系统!\n" && exit 1
  47. fi
  48. elif [[ x"${release}" == x"ubuntu" ]]; then
  49. if [[ ${os_version} -lt 16 ]]; then
  50. LOGE "请使用 Ubuntu 16 或更高版本的系统!\n" && exit 1
  51. fi
  52. elif [[ x"${release}" == x"debian" ]]; then
  53. if [[ ${os_version} -lt 8 ]]; then
  54. LOGE "请使用 Debian 8 或更高版本的系统!\n" && exit 1
  55. fi
  56. fi
  57. confirm() {
  58. if [[ $# > 1 ]]; then
  59. echo && read -p "$1 [默认$2]: " temp
  60. if [[ x"${temp}" == x"" ]]; then
  61. temp=$2
  62. fi
  63. else
  64. read -p "$1 [y/n]: " temp
  65. fi
  66. if [[ x"${temp}" == x"y" || x"${temp}" == x"Y" ]]; then
  67. return 0
  68. else
  69. return 1
  70. fi
  71. }
  72. confirm_restart() {
  73. confirm "是否重启面板,重启面板也会重启 xray" "y"
  74. if [[ $? == 0 ]]; then
  75. restart
  76. else
  77. show_menu
  78. fi
  79. }
  80. before_show_menu() {
  81. echo && echo -n -e "${yellow}按回车返回主菜单: ${plain}" && read temp
  82. show_menu
  83. }
  84. install() {
  85. bash <(curl -Ls https://raw.githubusercontent.com/FranzKafkaYu/x-ui/main/x-ui.sh)
  86. if [[ $? == 0 ]]; then
  87. if [[ $# == 0 ]]; then
  88. start
  89. else
  90. start 0
  91. fi
  92. fi
  93. }
  94. update() {
  95. confirm "本功能会强制重装当前最新版,数据不会丢失,是否继续?" "n"
  96. if [[ $? != 0 ]]; then
  97. LOGE "已取消"
  98. if [[ $# == 0 ]]; then
  99. before_show_menu
  100. fi
  101. return 0
  102. fi
  103. bash <(curl -Ls https://raw.githubusercontent.com/FranzKafkaYu/x-ui/main/x-ui.sh)
  104. if [[ $? == 0 ]]; then
  105. LOGI "更新完成,已自动重启面板 "
  106. exit 0
  107. fi
  108. }
  109. uninstall() {
  110. confirm "确定要卸载面板吗,xray 也会卸载?" "n"
  111. if [[ $? != 0 ]]; then
  112. if [[ $# == 0 ]]; then
  113. show_menu
  114. fi
  115. return 0
  116. fi
  117. systemctl stop x-ui
  118. systemctl disable x-ui
  119. rm /etc/systemd/system/x-ui.service -f
  120. systemctl daemon-reload
  121. systemctl reset-failed
  122. rm /etc/x-ui/ -rf
  123. rm /usr/local/x-ui/ -rf
  124. echo ""
  125. echo -e "卸载成功,如果你想删除此脚本,则退出脚本后运行 ${green}rm /usr/bin/x-ui -f${plain} 进行删除"
  126. echo ""
  127. if [[ $# == 0 ]]; then
  128. before_show_menu
  129. fi
  130. }
  131. reset_user() {
  132. confirm "确定要将用户名和密码重置为 admin 吗" "n"
  133. if [[ $? != 0 ]]; then
  134. if [[ $# == 0 ]]; then
  135. show_menu
  136. fi
  137. return 0
  138. fi
  139. /usr/local/x-ui/x-ui setting -username admin -password admin
  140. echo -e "用户名和密码已重置为 ${green}admin${plain},现在请重启面板"
  141. confirm_restart
  142. }
  143. reset_config() {
  144. confirm "确定要重置所有面板设置吗,账号数据不会丢失,用户名和密码不会改变" "n"
  145. if [[ $? != 0 ]]; then
  146. if [[ $# == 0 ]]; then
  147. show_menu
  148. fi
  149. return 0
  150. fi
  151. /usr/local/x-ui/x-ui setting -reset
  152. echo -e "所有面板设置已重置为默认值,现在请重启面板,并使用默认的 ${green}54321${plain} 端口访问面板"
  153. confirm_restart
  154. }
  155. set_port() {
  156. echo && echo -n -e "输入端口号[1-65535]: " && read port
  157. if [[ -z "${port}" ]]; then
  158. LOGD "已取消"
  159. before_show_menu
  160. else
  161. /usr/local/x-ui/x-ui setting -port ${port}
  162. echo -e "设置端口完毕,现在请重启面板,并使用新设置的端口 ${green}${port}${plain} 访问面板"
  163. confirm_restart
  164. fi
  165. }
  166. start() {
  167. check_status
  168. if [[ $? == 0 ]]; then
  169. echo ""
  170. LOGI "面板已运行,无需再次启动,如需重启请选择重启"
  171. else
  172. systemctl start x-ui
  173. sleep 2
  174. check_status
  175. if [[ $? == 0 ]]; then
  176. LOGI "x-ui 启动成功"
  177. else
  178. LOGE "面板启动失败,可能是因为启动时间超过了两秒,请稍后查看日志信息"
  179. fi
  180. fi
  181. if [[ $# == 0 ]]; then
  182. before_show_menu
  183. fi
  184. }
  185. stop() {
  186. check_status
  187. if [[ $? == 1 ]]; then
  188. echo ""
  189. LOGI "面板已停止,无需再次停止"
  190. else
  191. systemctl stop x-ui
  192. sleep 2
  193. check_status
  194. if [[ $? == 1 ]]; then
  195. LOGI "x-ui 与 xray 停止成功"
  196. else
  197. LOGE "面板停止失败,可能是因为停止时间超过了两秒,请稍后查看日志信息"
  198. fi
  199. fi
  200. if [[ $# == 0 ]]; then
  201. before_show_menu
  202. fi
  203. }
  204. restart() {
  205. systemctl restart x-ui
  206. sleep 2
  207. check_status
  208. if [[ $? == 0 ]]; then
  209. LOGI "x-ui 与 xray 重启成功"
  210. else
  211. LOGE "面板重启失败,可能是因为启动时间超过了两秒,请稍后查看日志信息"
  212. fi
  213. if [[ $# == 0 ]]; then
  214. before_show_menu
  215. fi
  216. }
  217. status() {
  218. systemctl status x-ui -l
  219. if [[ $# == 0 ]]; then
  220. before_show_menu
  221. fi
  222. }
  223. enable() {
  224. systemctl enable x-ui
  225. if [[ $? == 0 ]]; then
  226. LOGI "x-ui 设置开机自启成功"
  227. else
  228. LOGE "x-ui 设置开机自启失败"
  229. fi
  230. if [[ $# == 0 ]]; then
  231. before_show_menu
  232. fi
  233. }
  234. disable() {
  235. systemctl disable x-ui
  236. if [[ $? == 0 ]]; then
  237. LOGI "x-ui 取消开机自启成功"
  238. else
  239. LOGE "x-ui 取消开机自启失败"
  240. fi
  241. if [[ $# == 0 ]]; then
  242. before_show_menu
  243. fi
  244. }
  245. show_log() {
  246. journalctl -u x-ui.service -e --no-pager -f
  247. if [[ $# == 0 ]]; then
  248. before_show_menu
  249. fi
  250. }
  251. migrate_v2_ui() {
  252. /usr/local/x-ui/x-ui v2-ui
  253. before_show_menu
  254. }
  255. install_bbr() {
  256. # temporary workaround for installing bbr
  257. bash <(curl -L -s https://raw.githubusercontent.com/teddysun/across/master/bbr.sh)
  258. echo ""
  259. before_show_menu
  260. }
  261. update_shell() {
  262. wget -O /usr/bin/x-ui -N --no-check-certificate https://raw.githubusercontent.com/FranzKafkaYu/x-ui/main/x-ui.sh
  263. if [[ $? != 0 ]]; then
  264. echo ""
  265. LOGE "下载脚本失败,请检查本机能否连接 Github"
  266. before_show_menu
  267. else
  268. chmod +x /usr/bin/x-ui
  269. LOGI "升级脚本成功,请重新运行脚本" && exit 0
  270. fi
  271. }
  272. # 0: running, 1: not running, 2: not installed
  273. check_status() {
  274. if [[ ! -f /etc/systemd/system/x-ui.service ]]; then
  275. return 2
  276. fi
  277. temp=$(systemctl status x-ui | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
  278. if [[ x"${temp}" == x"running" ]]; then
  279. return 0
  280. else
  281. return 1
  282. fi
  283. }
  284. check_enabled() {
  285. temp=$(systemctl is-enabled x-ui)
  286. if [[ x"${temp}" == x"enabled" ]]; then
  287. return 0
  288. else
  289. return 1
  290. fi
  291. }
  292. check_uninstall() {
  293. check_status
  294. if [[ $? != 2 ]]; then
  295. echo ""
  296. LOGE "面板已安装,请不要重复安装"
  297. if [[ $# == 0 ]]; then
  298. before_show_menu
  299. fi
  300. return 1
  301. else
  302. return 0
  303. fi
  304. }
  305. check_install() {
  306. check_status
  307. if [[ $? == 2 ]]; then
  308. echo ""
  309. LOGE "请先安装面板"
  310. if [[ $# == 0 ]]; then
  311. before_show_menu
  312. fi
  313. return 1
  314. else
  315. return 0
  316. fi
  317. }
  318. show_status() {
  319. check_status
  320. case $? in
  321. 0)
  322. echo -e "面板状态: ${green}已运行${plain}"
  323. show_enable_status
  324. ;;
  325. 1)
  326. echo -e "面板状态: ${yellow}未运行${plain}"
  327. show_enable_status
  328. ;;
  329. 2)
  330. echo -e "面板状态: ${red}未安装${plain}"
  331. ;;
  332. esac
  333. show_xray_status
  334. }
  335. show_enable_status() {
  336. check_enabled
  337. if [[ $? == 0 ]]; then
  338. echo -e "是否开机自启: ${green}是${plain}"
  339. else
  340. echo -e "是否开机自启: ${red}否${plain}"
  341. fi
  342. }
  343. check_xray_status() {
  344. count=$(ps -ef | grep "xray-linux" | grep -v "grep" | wc -l)
  345. if [[ count -ne 0 ]]; then
  346. return 0
  347. else
  348. return 1
  349. fi
  350. }
  351. show_xray_status() {
  352. check_xray_status
  353. if [[ $? == 0 ]]; then
  354. echo -e "xray 状态: ${green}运行${plain}"
  355. else
  356. echo -e "xray 状态: ${red}未运行${plain}"
  357. fi
  358. }
  359. ssl_cert_issue() {
  360. echo -E ""
  361. LOGD "******使用说明******"
  362. LOGI "该脚本将使用Acme脚本申请证书,使用时需保证:"
  363. LOGI "1.知晓Cloudflare 注册邮箱"
  364. LOGI "2.知晓Cloudflare Global API Key"
  365. LOGI "3.域名已通过Cloudflare进行解析到当前服务器"
  366. LOGI "4.该脚本申请证书默认安装路径为/root/cert目录"
  367. confirm "我已确认以上内容[y/n]" "y"
  368. if [ $? -eq 0 ]; then
  369. cd ~
  370. LOGI "安装Acme脚本"
  371. curl https://get.acme.sh | sh
  372. if [ $? -ne 0 ]; then
  373. LOGE "安装acme脚本失败"
  374. exit 1
  375. fi
  376. CF_Domain=""
  377. CF_GlobalKey=""
  378. CF_AccountEmail=""
  379. certPath=/root/cert
  380. if [ ! -d "$certPath" ]; then
  381. mkdir $certPath
  382. else
  383. rm -rf $certPath
  384. mkdir $certPath
  385. fi
  386. LOGD "请设置域名:"
  387. read -p "Input your domain here:" CF_Domain
  388. LOGD "你的域名设置为:${CF_Domain}"
  389. LOGD "请设置API密钥:"
  390. read -p "Input your key here:" CF_GlobalKey
  391. LOGD "你的API密钥为:${CF_GlobalKey}"
  392. LOGD "请设置注册邮箱:"
  393. read -p "Input your email here:" CF_AccountEmail
  394. LOGD "你的注册邮箱为:${CF_AccountEmail}"
  395. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  396. if [ $? -ne 0 ]; then
  397. LOGE "修改默认CA为Lets'Encrypt失败,脚本退出"
  398. exit 1
  399. fi
  400. export CF_Key="${CF_GlobalKey}"
  401. export CF_Email=${CF_AccountEmail}
  402. ~/.acme.sh/acme.sh --issue --dns dns_cf -d ${CF_Domain} -d *.${CF_Domain} --log
  403. if [ $? -ne 0 ]; then
  404. LOGDE "证书签发失败,脚本退出"
  405. exit 1
  406. else
  407. LOGDI "证书签发成功,安装中..."
  408. fi
  409. ~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} --ca-file /root/cert/ca.cer \
  410. --cert-file /root/cert/${CF_Domain}.cer --key-file /root/cert/${CF_Domain}.key \
  411. --fullchain-file /root/cert/fullchain.cer
  412. if [ $? -ne 0 ]; then
  413. LOGE "证书安装失败,脚本退出"
  414. exit 1
  415. else
  416. LOGDI "证书安装成功,开启自动更新..."
  417. fi
  418. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  419. if [ $? -ne 0 ]; then
  420. LOGE "自动更新设置失败,脚本退出"
  421. ls -lah cert
  422. chmod 755 $certPath
  423. exit 1
  424. else
  425. LOGI "证书已安装且已开启自动更新,具体信息如下"
  426. ls -lah cert
  427. chmod 755 $certPath
  428. fi
  429. else
  430. show_menu
  431. fi
  432. }
  433. show_usage() {
  434. echo "x-ui 管理脚本使用方法: "
  435. echo "------------------------------------------"
  436. echo "x-ui - 显示管理菜单 (功能更多)"
  437. echo "x-ui start - 启动 x-ui 面板"
  438. echo "x-ui stop - 停止 x-ui 面板"
  439. echo "x-ui restart - 重启 x-ui 面板"
  440. echo "x-ui status - 查看 x-ui 状态"
  441. echo "x-ui enable - 设置 x-ui 开机自启"
  442. echo "x-ui disable - 取消 x-ui 开机自启"
  443. echo "x-ui log - 查看 x-ui 日志"
  444. echo "x-ui v2-ui - 迁移本机器的 v2-ui 账号数据至 x-ui"
  445. echo "x-ui update - 更新 x-ui 面板"
  446. echo "x-ui install - 安装 x-ui 面板"
  447. echo "x-ui uninstall - 卸载 x-ui 面板"
  448. echo "------------------------------------------"
  449. }
  450. show_menu() {
  451. echo -e "
  452. ${green}x-ui 面板管理脚本${plain}
  453. ${green}0.${plain} 退出脚本
  454. ————————————————
  455. ${green}1.${plain} 安装 x-ui
  456. ${green}2.${plain} 更新 x-ui
  457. ${green}3.${plain} 卸载 x-ui
  458. ————————————————
  459. ${green}4.${plain} 重置用户名密码
  460. ${green}5.${plain} 重置面板设置
  461. ${green}6.${plain} 设置面板端口
  462. ————————————————
  463. ${green}7.${plain} 启动 x-ui
  464. ${green}8.${plain} 停止 x-ui
  465. ${green}9.${plain} 重启 x-ui
  466. ${green}10.${plain} 查看 x-ui 状态
  467. ${green}11.${plain} 查看 x-ui 日志
  468. ————————————————
  469. ${green}12.${plain} 设置 x-ui 开机自启
  470. ${green}13.${plain} 取消 x-ui 开机自启
  471. ————————————————
  472. ${green}14.${plain} 一键安装 bbr (最新内核)
  473. ${green}15.${plain} 一键申请SSL证书(acme申请)
  474. "
  475. show_status
  476. echo && read -p "请输入选择 [0-14]: " num
  477. case "${num}" in
  478. 0)
  479. exit 0
  480. ;;
  481. 1)
  482. check_uninstall && install
  483. ;;
  484. 2)
  485. check_install && update
  486. ;;
  487. 3)
  488. check_install && uninstall
  489. ;;
  490. 4)
  491. check_install && reset_user
  492. ;;
  493. 5)
  494. check_install && reset_config
  495. ;;
  496. 6)
  497. check_install && set_port
  498. ;;
  499. 7)
  500. check_install && start
  501. ;;
  502. 8)
  503. check_install && stop
  504. ;;
  505. 9)
  506. check_install && restart
  507. ;;
  508. 10)
  509. check_install && status
  510. ;;
  511. 11)
  512. check_install && show_log
  513. ;;
  514. 12)
  515. check_install && enable
  516. ;;
  517. 13)
  518. check_install && disable
  519. ;;
  520. 14)
  521. install_bbr
  522. ;;
  523. 15)
  524. ssl_cert_issue
  525. ;;
  526. *)
  527. LOGE "请输入正确的数字 [0-14]"
  528. ;;
  529. esac
  530. }
  531. if [[ $# > 0 ]]; then
  532. case $1 in
  533. "start")
  534. check_install 0 && start 0
  535. ;;
  536. "stop")
  537. check_install 0 && stop 0
  538. ;;
  539. "restart")
  540. check_install 0 && restart 0
  541. ;;
  542. "status")
  543. check_install 0 && status 0
  544. ;;
  545. "enable")
  546. check_install 0 && enable 0
  547. ;;
  548. "disable")
  549. check_install 0 && disable 0
  550. ;;
  551. "log")
  552. check_install 0 && show_log 0
  553. ;;
  554. "v2-ui")
  555. check_install 0 && migrate_v2_ui 0
  556. ;;
  557. "update")
  558. check_install 0 && update 0
  559. ;;
  560. "install")
  561. check_uninstall 0 && install 0
  562. ;;
  563. "uninstall")
  564. check_install 0 && uninstall 0
  565. ;;
  566. *) show_usage ;;
  567. esac
  568. else
  569. show_menu
  570. fi