x-ui.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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 "ERROR: You must be root to run this script! \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 "check system OS failed,please contact with author! \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 "please use CentOS 7 or higher version! \n" && exit 1
  47. fi
  48. elif [[ x"${release}" == x"ubuntu" ]]; then
  49. if [[ ${os_version} -lt 16 ]]; then
  50. LOGE "please use Ubuntu 16 or higher version!\n" && exit 1
  51. fi
  52. elif [[ x"${release}" == x"debian" ]]; then
  53. if [[ ${os_version} -lt 8 ]]; then
  54. LOGE "please use Debian 8 or higher version!\n" && exit 1
  55. fi
  56. fi
  57. confirm() {
  58. if [[ $# > 1 ]]; then
  59. echo && read -p "$1 [Default$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 "Restart the panel, Attention: Restarting the panel will also restart 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}Press enter to return to the main menu: ${plain}" && read temp
  82. show_menu
  83. }
  84. install() {
  85. bash <(curl -Ls https://raw.githubusercontent.com/mhsanaei/3x-ui/main/install.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 "This function will forcefully reinstall the latest version, and the data will not be lost. Do you want to continue?" "n"
  96. if [[ $? != 0 ]]; then
  97. LOGE "Cancelled"
  98. if [[ $# == 0 ]]; then
  99. before_show_menu
  100. fi
  101. return 0
  102. fi
  103. bash <(curl -Ls https://raw.githubusercontent.com/mhsanaei/3x-ui/main/install.sh)
  104. if [[ $? == 0 ]]; then
  105. LOGI "Update is complete, Panel has automatically restarted "
  106. exit 0
  107. fi
  108. }
  109. uninstall() {
  110. confirm "Are you sure you want to uninstall the panel? xray will also uninstalled!" "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 "Uninstalled Successfully,If you want to remove this script,then after exiting the script run ${green}rm /usr/bin/x-ui -f${plain} to delete it."
  126. echo ""
  127. if [[ $# == 0 ]]; then
  128. before_show_menu
  129. fi
  130. }
  131. reset_user() {
  132. confirm "Reset your username and password to 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 "Username and password have been reset to ${green}admin${plain},Please restart the panel now."
  141. confirm_restart
  142. }
  143. reset_config() {
  144. confirm "Are you sure you want to reset all panel settings,Account data will not be lost,Username and password will not change" "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 "All panel settings have been reset to default,Please restart the panel now,and use the default ${green}2053${plain} Port to Access the web Panel"
  153. confirm_restart
  154. }
  155. check_config() {
  156. info=$(/usr/local/x-ui/x-ui setting -show true)
  157. if [[ $? != 0 ]]; then
  158. LOGE "get current settings error,please check logs"
  159. show_menu
  160. fi
  161. LOGI "${info}"
  162. }
  163. set_port() {
  164. echo && echo -n -e "Enter port number[1-65535]: " && read port
  165. if [[ -z "${port}" ]]; then
  166. LOGD "Cancelled"
  167. before_show_menu
  168. else
  169. /usr/local/x-ui/x-ui setting -port ${port}
  170. echo -e "The port is set,Please restart the panel now,and use the new port ${green}${port}${plain} to access web panel"
  171. confirm_restart
  172. fi
  173. }
  174. start() {
  175. check_status
  176. if [[ $? == 0 ]]; then
  177. echo ""
  178. LOGI "Panel is running,No need to start again,If you need to restart, please select restart"
  179. else
  180. systemctl start x-ui
  181. sleep 2
  182. check_status
  183. if [[ $? == 0 ]]; then
  184. LOGI "x-ui Started Successfully"
  185. else
  186. LOGE "panel Failed to start,Probably because it takes longer than two seconds to start,Please check the log information later"
  187. fi
  188. fi
  189. if [[ $# == 0 ]]; then
  190. before_show_menu
  191. fi
  192. }
  193. stop() {
  194. check_status
  195. if [[ $? == 1 ]]; then
  196. echo ""
  197. LOGI "Panel stopped,No need to stop again!"
  198. else
  199. systemctl stop x-ui
  200. sleep 2
  201. check_status
  202. if [[ $? == 1 ]]; then
  203. LOGI "x-ui and xray stopped successfully"
  204. else
  205. LOGE "Panel stop failed,Probably because the stop time exceeds two seconds,Please check the log information later"
  206. fi
  207. fi
  208. if [[ $# == 0 ]]; then
  209. before_show_menu
  210. fi
  211. }
  212. restart() {
  213. systemctl restart x-ui
  214. sleep 2
  215. check_status
  216. if [[ $? == 0 ]]; then
  217. LOGI "x-ui and xray Restarted successfully"
  218. else
  219. LOGE "Panel restart failed,Probably because it takes longer than two seconds to start,Please check the log information later"
  220. fi
  221. if [[ $# == 0 ]]; then
  222. before_show_menu
  223. fi
  224. }
  225. status() {
  226. systemctl status x-ui -l
  227. if [[ $# == 0 ]]; then
  228. before_show_menu
  229. fi
  230. }
  231. enable() {
  232. systemctl enable x-ui
  233. if [[ $? == 0 ]]; then
  234. LOGI "x-ui Set to boot automatically on startup successfully"
  235. else
  236. LOGE "x-ui Failed to set Autostart"
  237. fi
  238. if [[ $# == 0 ]]; then
  239. before_show_menu
  240. fi
  241. }
  242. disable() {
  243. systemctl disable x-ui
  244. if [[ $? == 0 ]]; then
  245. LOGI "x-ui Autostart Cancelled successfully"
  246. else
  247. LOGE "x-ui Failed to cancel autostart"
  248. fi
  249. if [[ $# == 0 ]]; then
  250. before_show_menu
  251. fi
  252. }
  253. show_log() {
  254. journalctl -u x-ui.service -e --no-pager -f
  255. if [[ $# == 0 ]]; then
  256. before_show_menu
  257. fi
  258. }
  259. migrate_v2_ui() {
  260. /usr/local/x-ui/x-ui v2-ui
  261. before_show_menu
  262. }
  263. install_bbr() {
  264. # temporary workaround for installing bbr
  265. bash <(curl -L -s https://raw.githubusercontent.com/teddysun/across/master/bbr.sh)
  266. echo ""
  267. before_show_menu
  268. }
  269. update_shell() {
  270. wget -O /usr/bin/x-ui -N --no-check-certificate https://github.com/mhsanaei/3x-ui/raw/main/x-ui.sh
  271. if [[ $? != 0 ]]; then
  272. echo ""
  273. LOGE "Failed to download script,Please check whether the machine can connect Github"
  274. before_show_menu
  275. else
  276. chmod +x /usr/bin/x-ui
  277. LOGI "Upgrade script succeeded,Please rerun the script" && exit 0
  278. fi
  279. }
  280. # 0: running, 1: not running, 2: not installed
  281. check_status() {
  282. if [[ ! -f /etc/systemd/system/x-ui.service ]]; then
  283. return 2
  284. fi
  285. temp=$(systemctl status x-ui | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
  286. if [[ x"${temp}" == x"running" ]]; then
  287. return 0
  288. else
  289. return 1
  290. fi
  291. }
  292. check_enabled() {
  293. temp=$(systemctl is-enabled x-ui)
  294. if [[ x"${temp}" == x"enabled" ]]; then
  295. return 0
  296. else
  297. return 1
  298. fi
  299. }
  300. check_uninstall() {
  301. check_status
  302. if [[ $? != 2 ]]; then
  303. echo ""
  304. LOGE "Panel installed,Please do not reinstall"
  305. if [[ $# == 0 ]]; then
  306. before_show_menu
  307. fi
  308. return 1
  309. else
  310. return 0
  311. fi
  312. }
  313. check_install() {
  314. check_status
  315. if [[ $? == 2 ]]; then
  316. echo ""
  317. LOGE "Please install the panel first"
  318. if [[ $# == 0 ]]; then
  319. before_show_menu
  320. fi
  321. return 1
  322. else
  323. return 0
  324. fi
  325. }
  326. show_status() {
  327. check_status
  328. case $? in
  329. 0)
  330. echo -e "Panel state: ${green}Runing${plain}"
  331. show_enable_status
  332. ;;
  333. 1)
  334. echo -e "Panel state: ${yellow}Not Running${plain}"
  335. show_enable_status
  336. ;;
  337. 2)
  338. echo -e "Panel state: ${red}Not Installed${plain}"
  339. ;;
  340. esac
  341. show_xray_status
  342. }
  343. show_enable_status() {
  344. check_enabled
  345. if [[ $? == 0 ]]; then
  346. echo -e "Start automatically: ${green}Yes${plain}"
  347. else
  348. echo -e "Start automatically: ${red}No${plain}"
  349. fi
  350. }
  351. check_xray_status() {
  352. count=$(ps -ef | grep "xray-linux" | grep -v "grep" | wc -l)
  353. if [[ count -ne 0 ]]; then
  354. return 0
  355. else
  356. return 1
  357. fi
  358. }
  359. show_xray_status() {
  360. check_xray_status
  361. if [[ $? == 0 ]]; then
  362. echo -e "xray state: ${green}Runing${plain}"
  363. else
  364. echo -e "xray state: ${red}Not Running${plain}"
  365. fi
  366. }
  367. ssl_cert_issue() {
  368. echo -E ""
  369. LOGD "******Instructions for use******"
  370. LOGI "This Acme script requires the following data:"
  371. LOGI "1.Cloudflare Registered e-mail"
  372. LOGI "2.Cloudflare Global API Key"
  373. LOGI "3.The domain name that has been resolved dns to the current server by Cloudflare"
  374. LOGI "4.The script applies for a certificate. The default installation path is /root/cert "
  375. confirm "Confirmed?[y/n]" "y"
  376. if [ $? -eq 0 ]; then
  377. cd ~
  378. LOGI "Install Acme-Script"
  379. curl https://get.acme.sh | sh
  380. if [ $? -ne 0 ]; then
  381. LOGE "Failed to install acme script"
  382. exit 1
  383. fi
  384. CF_Domain=""
  385. CF_GlobalKey=""
  386. CF_AccountEmail=""
  387. certPath=/root/cert
  388. if [ ! -d "$certPath" ]; then
  389. mkdir $certPath
  390. else
  391. rm -rf $certPath
  392. mkdir $certPath
  393. fi
  394. LOGD "Please set a domain name:"
  395. read -p "Input your domain here:" CF_Domain
  396. LOGD "Your domain name is set to:${CF_Domain}"
  397. LOGD "Please set the API key:"
  398. read -p "Input your key here:" CF_GlobalKey
  399. LOGD "Your API key is:${CF_GlobalKey}"
  400. LOGD "Please set up registered email:"
  401. read -p "Input your email here:" CF_AccountEmail
  402. LOGD "Your registered email address is:${CF_AccountEmail}"
  403. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  404. if [ $? -ne 0 ]; then
  405. LOGE "Default CA, Lets'Encrypt fail, script exiting..."
  406. exit 1
  407. fi
  408. export CF_Key="${CF_GlobalKey}"
  409. export CF_Email=${CF_AccountEmail}
  410. ~/.acme.sh/acme.sh --issue --dns dns_cf -d ${CF_Domain} -d *.${CF_Domain} --log
  411. if [ $? -ne 0 ]; then
  412. LOGE "Certificate issuance failed, script exiting..."
  413. exit 1
  414. else
  415. LOGI "Certificate issued Successfully, Installing..."
  416. fi
  417. ~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} --ca-file /root/cert/ca.cer \
  418. --cert-file /root/cert/${CF_Domain}.cer --key-file /root/cert/${CF_Domain}.key \
  419. --fullchain-file /root/cert/fullchain.cer
  420. if [ $? -ne 0 ]; then
  421. LOGE "Certificate installation failed, script exiting..."
  422. exit 1
  423. else
  424. LOGI "Certificate installed Successfully,Turning on automatic updates..."
  425. fi
  426. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  427. if [ $? -ne 0 ]; then
  428. LOGE "Auto update setup Failed, script exiting..."
  429. ls -lah cert
  430. chmod 755 $certPath
  431. exit 1
  432. else
  433. LOGI "The certificate is installed and auto-renewal is turned on, Specific information is as follows"
  434. ls -lah cert
  435. chmod 755 $certPath
  436. fi
  437. else
  438. show_menu
  439. fi
  440. }
  441. show_usage() {
  442. echo "x-ui control menu usages: "
  443. echo "------------------------------------------"
  444. echo "x-ui - Enter Admin menu"
  445. echo "x-ui start - Start x-ui"
  446. echo "x-ui stop - Stop x-ui"
  447. echo "x-ui restart - Restart x-ui"
  448. echo "x-ui status - Show x-ui status"
  449. echo "x-ui enable - Enable x-ui on system startup"
  450. echo "x-ui disable - Disable x-ui on system startup"
  451. echo "x-ui log - Check x-ui logs"
  452. echo "x-ui v2-ui - Migrate v2-ui Account data to x-ui"
  453. echo "x-ui update - Update x-ui"
  454. echo "x-ui install - Install x-ui"
  455. echo "x-ui uninstall - Uninstall x-ui"
  456. echo "------------------------------------------"
  457. }
  458. show_menu() {
  459. echo -e "
  460. ${green}x-ui Panel Management Script${plain}
  461. ${green}0.${plain} exit script
  462. ————————————————
  463. ${green}1.${plain} Install x-ui
  464. ${green}2.${plain} Update x-ui
  465. ${green}3.${plain} Uninstall x-ui
  466. ————————————————
  467. ${green}4.${plain} Reset username and password
  468. ${green}5.${plain} Reset panel settings
  469. ${green}6.${plain} Set panel port
  470. ${green}7.${plain} View current panel settings
  471. ————————————————
  472. ${green}8.${plain} Start x-ui
  473. ${green}9.${plain} stop x-ui
  474. ${green}10.${plain} Reboot x-ui
  475. ${green}11.${plain} Check x-ui state
  476. ${green}12.${plain} Check x-ui logs
  477. ————————————————
  478. ${green}13.${plain} set x-ui Autostart
  479. ${green}14.${plain} Cancel x-ui Autostart
  480. ————————————————
  481. ${green}15.${plain} 一A key installation bbr (latest kernel)
  482. ${green}16.${plain} 一Apply for an SSL certificate with one click(acme script)
  483. "
  484. show_status
  485. echo && read -p "Please enter your selection [0-16]: " num
  486. case "${num}" in
  487. 0)
  488. exit 0
  489. ;;
  490. 1)
  491. check_uninstall && install
  492. ;;
  493. 2)
  494. check_install && update
  495. ;;
  496. 3)
  497. check_install && uninstall
  498. ;;
  499. 4)
  500. check_install && reset_user
  501. ;;
  502. 5)
  503. check_install && reset_config
  504. ;;
  505. 6)
  506. check_install && set_port
  507. ;;
  508. 7)
  509. check_install && check_config
  510. ;;
  511. 8)
  512. check_install && start
  513. ;;
  514. 9)
  515. check_install && stop
  516. ;;
  517. 10)
  518. check_install && restart
  519. ;;
  520. 11)
  521. check_install && status
  522. ;;
  523. 12)
  524. check_install && show_log
  525. ;;
  526. 13)
  527. check_install && enable
  528. ;;
  529. 14)
  530. check_install && disable
  531. ;;
  532. 15)
  533. install_bbr
  534. ;;
  535. 16)
  536. ssl_cert_issue
  537. ;;
  538. *)
  539. LOGE "Please enter the correct number [0-16]"
  540. ;;
  541. esac
  542. }
  543. if [[ $# > 0 ]]; then
  544. case $1 in
  545. "start")
  546. check_install 0 && start 0
  547. ;;
  548. "stop")
  549. check_install 0 && stop 0
  550. ;;
  551. "restart")
  552. check_install 0 && restart 0
  553. ;;
  554. "status")
  555. check_install 0 && status 0
  556. ;;
  557. "enable")
  558. check_install 0 && enable 0
  559. ;;
  560. "disable")
  561. check_install 0 && disable 0
  562. ;;
  563. "log")
  564. check_install 0 && show_log 0
  565. ;;
  566. "v2-ui")
  567. check_install 0 && migrate_v2_ui 0
  568. ;;
  569. "update")
  570. check_install 0 && update 0
  571. ;;
  572. "install")
  573. check_uninstall 0 && install 0
  574. ;;
  575. "uninstall")
  576. check_install 0 && uninstall 0
  577. ;;
  578. *) show_usage ;;
  579. esac
  580. else
  581. show_menu
  582. fi