x-ui.sh 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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 and set release variable
  19. if [[ -f /etc/os-release ]]; then
  20. source /etc/os-release
  21. release=$ID
  22. elif [[ -f /usr/lib/os-release ]]; then
  23. source /usr/lib/os-release
  24. release=$ID
  25. else
  26. echo "Failed to check the system OS, please contact the author!" >&2
  27. exit 1
  28. fi
  29. echo "The OS release is: $release"
  30. os_version=""
  31. os_version=$(grep -i version_id /etc/os-release | cut -d \" -f2 | cut -d . -f1)
  32. if [[ "${release}" == "centos" ]]; then
  33. if [[ ${os_version} -lt 8 ]]; then
  34. echo -e "${red} Please use CentOS 8 or higher ${plain}\n" && exit 1
  35. fi
  36. elif [[ "${release}" == "ubuntu" ]]; then
  37. if [[ ${os_version} -lt 20 ]]; then
  38. echo -e "${red}please use Ubuntu 20 or higher version!${plain}\n" && exit 1
  39. fi
  40. elif [[ "${release}" == "fedora" ]]; then
  41. if [[ ${os_version} -lt 36 ]]; then
  42. echo -e "${red}please use Fedora 36 or higher version!${plain}\n" && exit 1
  43. fi
  44. elif [[ "${release}" == "debian" ]]; then
  45. if [[ ${os_version} -lt 10 ]]; then
  46. echo -e "${red} Please use Debian 10 or higher ${plain}\n" && exit 1
  47. fi
  48. fi
  49. confirm() {
  50. if [[ $# > 1 ]]; then
  51. echo && read -p "$1 [Default$2]: " temp
  52. if [[ x"${temp}" == x"" ]]; then
  53. temp=$2
  54. fi
  55. else
  56. read -p "$1 [y/n]: " temp
  57. fi
  58. if [[ x"${temp}" == x"y" || x"${temp}" == x"Y" ]]; then
  59. return 0
  60. else
  61. return 1
  62. fi
  63. }
  64. confirm_restart() {
  65. confirm "Restart the panel, Attention: Restarting the panel will also restart xray" "y"
  66. if [[ $? == 0 ]]; then
  67. restart
  68. else
  69. show_menu
  70. fi
  71. }
  72. before_show_menu() {
  73. echo && echo -n -e "${yellow}Press enter to return to the main menu: ${plain}" && read temp
  74. show_menu
  75. }
  76. install() {
  77. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  78. if [[ $? == 0 ]]; then
  79. if [[ $# == 0 ]]; then
  80. start
  81. else
  82. start 0
  83. fi
  84. fi
  85. }
  86. update() {
  87. confirm "This function will forcefully reinstall the latest version, and the data will not be lost. Do you want to continue?" "n"
  88. if [[ $? != 0 ]]; then
  89. LOGE "Cancelled"
  90. if [[ $# == 0 ]]; then
  91. before_show_menu
  92. fi
  93. return 0
  94. fi
  95. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  96. if [[ $? == 0 ]]; then
  97. LOGI "Update is complete, Panel has automatically restarted "
  98. exit 0
  99. fi
  100. }
  101. uninstall() {
  102. confirm "Are you sure you want to uninstall the panel? xray will also uninstalled!" "n"
  103. if [[ $? != 0 ]]; then
  104. if [[ $# == 0 ]]; then
  105. show_menu
  106. fi
  107. return 0
  108. fi
  109. systemctl stop x-ui
  110. systemctl disable x-ui
  111. rm /etc/systemd/system/x-ui.service -f
  112. systemctl daemon-reload
  113. systemctl reset-failed
  114. rm /etc/x-ui/ -rf
  115. rm /usr/local/x-ui/ -rf
  116. echo ""
  117. 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."
  118. echo ""
  119. if [[ $# == 0 ]]; then
  120. before_show_menu
  121. fi
  122. }
  123. reset_user() {
  124. confirm "Reset your username and password to admin?" "n"
  125. if [[ $? != 0 ]]; then
  126. if [[ $# == 0 ]]; then
  127. show_menu
  128. fi
  129. return 0
  130. fi
  131. /usr/local/x-ui/x-ui setting -username admin -password admin
  132. echo -e "Username and password have been reset to ${green}admin${plain},Please restart the panel now."
  133. confirm_restart
  134. }
  135. reset_config() {
  136. confirm "Are you sure you want to reset all panel settings,Account data will not be lost,Username and password will not change" "n"
  137. if [[ $? != 0 ]]; then
  138. if [[ $# == 0 ]]; then
  139. show_menu
  140. fi
  141. return 0
  142. fi
  143. /usr/local/x-ui/x-ui setting -reset
  144. 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"
  145. confirm_restart
  146. }
  147. check_config() {
  148. info=$(/usr/local/x-ui/x-ui setting -show true)
  149. if [[ $? != 0 ]]; then
  150. LOGE "get current settings error,please check logs"
  151. show_menu
  152. fi
  153. LOGI "${info}"
  154. }
  155. set_port() {
  156. echo && echo -n -e "Enter port number[1-65535]: " && read port
  157. if [[ -z "${port}" ]]; then
  158. LOGD "Cancelled"
  159. before_show_menu
  160. else
  161. /usr/local/x-ui/x-ui setting -port ${port}
  162. echo -e "The port is set,Please restart the panel now,and use the new port ${green}${port}${plain} to access web panel"
  163. confirm_restart
  164. fi
  165. }
  166. start() {
  167. check_status
  168. if [[ $? == 0 ]]; then
  169. echo ""
  170. LOGI "Panel is running,No need to start again,If you need to restart, please select restart"
  171. else
  172. systemctl start x-ui
  173. sleep 2
  174. check_status
  175. if [[ $? == 0 ]]; then
  176. LOGI "x-ui Started Successfully"
  177. else
  178. LOGE "panel Failed to start,Probably because it takes longer than two seconds to start,Please check the log information later"
  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 "Panel stopped,No need to stop again!"
  190. else
  191. systemctl stop x-ui
  192. sleep 2
  193. check_status
  194. if [[ $? == 1 ]]; then
  195. LOGI "x-ui and xray stopped successfully"
  196. else
  197. LOGE "Panel stop failed,Probably because the stop time exceeds two seconds,Please check the log information later"
  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 and xray Restarted successfully"
  210. else
  211. LOGE "Panel restart failed,Probably because it takes longer than two seconds to start,Please check the log information later"
  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 Set to boot automatically on startup successfully"
  227. else
  228. LOGE "x-ui Failed to set Autostart"
  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 Autostart Cancelled successfully"
  238. else
  239. LOGE "x-ui Failed to cancel autostart"
  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. enable_bbr() {
  252. if grep -q "net.core.default_qdisc=fq" /etc/sysctl.conf && grep -q "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf; then
  253. echo -e "${green}BBR is already enabled!${plain}"
  254. exit 0
  255. fi
  256. # Check the OS and install necessary packages
  257. if [[ "$(cat /etc/os-release | grep -E '^ID=' | awk -F '=' '{print $2}')" == "ubuntu" ]]; then
  258. sudo apt-get update && sudo apt-get install -yqq --no-install-recommends ca-certificates
  259. elif [[ "$(cat /etc/os-release | grep -E '^ID=' | awk -F '=' '{print $2}')" == "debian" ]]; then
  260. sudo apt-get update && sudo apt-get install -yqq --no-install-recommends ca-certificates
  261. elif [[ "$(cat /etc/os-release | grep -E '^ID=' | awk -F '=' '{print $2}')" == "fedora" ]]; then
  262. sudo dnf -y update && sudo dnf -y install ca-certificates
  263. elif [[ "$(cat /etc/os-release | grep -E '^ID=' | awk -F '=' '{print $2}')" == "centos" ]]; then
  264. sudo yum -y update && sudo yum -y install ca-certificates
  265. else
  266. echo "Unsupported operating system. Please check the script and install the necessary packages manually."
  267. exit 1
  268. fi
  269. # Enable BBR
  270. echo "net.core.default_qdisc=fq" | sudo tee -a /etc/sysctl.conf
  271. echo "net.ipv4.tcp_congestion_control=bbr" | sudo tee -a /etc/sysctl.conf
  272. # Apply changes
  273. sudo sysctl -p
  274. # Verify that BBR is enabled
  275. if [[ $(sysctl net.ipv4.tcp_congestion_control | awk '{print $3}') == "bbr" ]]; then
  276. echo -e "${green}BBR has been enabled successfully.${plain}"
  277. else
  278. echo -e "${red}Failed to enable BBR. Please check your system configuration.${plain}"
  279. fi
  280. }
  281. update_shell() {
  282. wget -O /usr/bin/x-ui -N --no-check-certificate https://github.com/MHSanaei/3x-ui/raw/main/x-ui.sh
  283. if [[ $? != 0 ]]; then
  284. echo ""
  285. LOGE "Failed to download script,Please check whether the machine can connect Github"
  286. before_show_menu
  287. else
  288. chmod +x /usr/bin/x-ui
  289. LOGI "Upgrade script succeeded,Please rerun the script" && exit 0
  290. fi
  291. }
  292. # 0: running, 1: not running, 2: not installed
  293. check_status() {
  294. if [[ ! -f /etc/systemd/system/x-ui.service ]]; then
  295. return 2
  296. fi
  297. temp=$(systemctl status x-ui | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
  298. if [[ x"${temp}" == x"running" ]]; then
  299. return 0
  300. else
  301. return 1
  302. fi
  303. }
  304. check_enabled() {
  305. temp=$(systemctl is-enabled x-ui)
  306. if [[ x"${temp}" == x"enabled" ]]; then
  307. return 0
  308. else
  309. return 1
  310. fi
  311. }
  312. check_uninstall() {
  313. check_status
  314. if [[ $? != 2 ]]; then
  315. echo ""
  316. LOGE "Panel installed,Please do not reinstall"
  317. if [[ $# == 0 ]]; then
  318. before_show_menu
  319. fi
  320. return 1
  321. else
  322. return 0
  323. fi
  324. }
  325. check_install() {
  326. check_status
  327. if [[ $? == 2 ]]; then
  328. echo ""
  329. LOGE "Please install the panel first"
  330. if [[ $# == 0 ]]; then
  331. before_show_menu
  332. fi
  333. return 1
  334. else
  335. return 0
  336. fi
  337. }
  338. show_status() {
  339. check_status
  340. case $? in
  341. 0)
  342. echo -e "Panel state: ${green}Running${plain}"
  343. show_enable_status
  344. ;;
  345. 1)
  346. echo -e "Panel state: ${yellow}Not Running${plain}"
  347. show_enable_status
  348. ;;
  349. 2)
  350. echo -e "Panel state: ${red}Not Installed${plain}"
  351. ;;
  352. esac
  353. show_xray_status
  354. }
  355. show_enable_status() {
  356. check_enabled
  357. if [[ $? == 0 ]]; then
  358. echo -e "Start automatically: ${green}Yes${plain}"
  359. else
  360. echo -e "Start automatically: ${red}No${plain}"
  361. fi
  362. }
  363. check_xray_status() {
  364. count=$(ps -ef | grep "xray-linux" | grep -v "grep" | wc -l)
  365. if [[ count -ne 0 ]]; then
  366. return 0
  367. else
  368. return 1
  369. fi
  370. }
  371. show_xray_status() {
  372. check_xray_status
  373. if [[ $? == 0 ]]; then
  374. echo -e "xray state: ${green}Running${plain}"
  375. else
  376. echo -e "xray state: ${red}Not Running${plain}"
  377. fi
  378. }
  379. #this will be an entrance for ssl cert issue
  380. #here we can provide two different methods to issue cert
  381. #first.standalone mode second.DNS API mode
  382. ssl_cert_issue() {
  383. local method=""
  384. echo -E ""
  385. LOGD "********Usage********"
  386. LOGI "this shell script will use acme to help issue certs."
  387. LOGI "here we provide two methods for issuing certs:"
  388. LOGI "method 1:acme standalone mode,need to keep port:80 open"
  389. LOGI "method 2:acme DNS API mode,need provide Cloudflare Global API Key"
  390. LOGI "recommend method 2 first,if it fails,you can try method 1."
  391. LOGI "certs will be installed in /root/cert directory"
  392. read -p "please choose which method do you want,type 1 or 2": method
  393. LOGI "you choosed method:${method}"
  394. if [ "${method}" == "1" ]; then
  395. ssl_cert_issue_standalone
  396. elif [ "${method}" == "2" ]; then
  397. ssl_cert_issue_by_cloudflare
  398. else
  399. LOGE "invalid input,please check it..."
  400. exit 1
  401. fi
  402. }
  403. open_ports() {
  404. if ! command -v ufw &> /dev/null
  405. then
  406. echo "ufw firewall is not installed. Installing now..."
  407. sudo apt-get update
  408. sudo apt-get install -y ufw
  409. else
  410. echo "ufw firewall is already installed"
  411. fi
  412. # Check if the firewall is inactive
  413. if sudo ufw status | grep -q "Status: active"; then
  414. echo "firewall is already active"
  415. else
  416. # Open the necessary ports
  417. sudo ufw allow ssh
  418. sudo ufw allow http
  419. sudo ufw allow https
  420. sudo ufw allow 2053/tcp
  421. # Enable the firewall
  422. sudo ufw --force enable
  423. fi
  424. # Prompt the user to enter a list of ports
  425. read -p "Enter the ports you want to open (e.g. 80,443,2053 or range 400-500): " ports
  426. # Check if the input is valid
  427. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  428. echo "Error: Invalid input. Please enter a comma-separated list of ports or a range of ports (e.g. 80,443,2053 or 400-500)." >&2; exit 1
  429. fi
  430. # Open the specified ports using ufw
  431. IFS=',' read -ra PORT_LIST <<< "$ports"
  432. for port in "${PORT_LIST[@]}"; do
  433. if [[ $port == *-* ]]; then
  434. # Split the range into start and end ports
  435. start_port=$(echo $port | cut -d'-' -f1)
  436. end_port=$(echo $port | cut -d'-' -f2)
  437. # Loop through the range and open each port
  438. for ((i=start_port; i<=end_port; i++)); do
  439. sudo ufw allow $i
  440. done
  441. else
  442. sudo ufw allow "$port"
  443. fi
  444. done
  445. # Confirm that the ports are open
  446. sudo ufw status | grep $ports
  447. }
  448. update_geo(){
  449. systemctl stop x-ui
  450. cd /usr/local/x-ui/bin
  451. rm -f geoip.dat geosite.dat iran.dat
  452. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat
  453. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat
  454. wget -N https://github.com/bootmortis/iran-hosted-domains/releases/latest/download/iran.dat
  455. systemctl start x-ui
  456. echo -e "${green}Geosite and Geoip have been updated successfully!${plain}"
  457. before_show_menu
  458. }
  459. install_acme() {
  460. cd ~
  461. LOGI "install acme..."
  462. curl https://get.acme.sh | sh
  463. if [ $? -ne 0 ]; then
  464. LOGE "install acme failed"
  465. return 1
  466. else
  467. LOGI "install acme succeed"
  468. fi
  469. return 0
  470. }
  471. #method for standalone mode
  472. ssl_cert_issue_standalone() {
  473. #check for acme.sh first
  474. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  475. echo "acme.sh could not be found. we will install it"
  476. install_acme
  477. if [ $? -ne 0 ]; then
  478. LOGE "install acme failed, please check logs"
  479. exit 1
  480. fi
  481. fi
  482. #install socat second
  483. if [[ x"${release}" == x"centos" ]]; then
  484. yum install socat -y
  485. else
  486. apt install socat -y
  487. fi
  488. if [ $? -ne 0 ]; then
  489. LOGE "install socat failed,please check logs"
  490. exit 1
  491. else
  492. LOGI "install socat succeed..."
  493. fi
  494. #get the domain here,and we need verify it
  495. local domain=""
  496. read -p "please input your domain:" domain
  497. LOGD "your domain is:${domain},check it..."
  498. #here we need to judge whether there exists cert already
  499. local currentCert=$(~/.acme.sh/acme.sh --list | tail -1 | awk '{print $1}')
  500. if [ ${currentCert} == ${domain} ]; then
  501. local certInfo=$(~/.acme.sh/acme.sh --list)
  502. LOGE "system already have certs here,can not issue again,current certs details:"
  503. LOGI "$certInfo"
  504. exit 1
  505. else
  506. LOGI "your domain is ready for issuing cert now..."
  507. fi
  508. #create a directory for install cert
  509. certPath="/root/cert/${domain}"
  510. if [ ! -d "$certPath" ]; then
  511. mkdir -p "$certPath"
  512. else
  513. rm -rf "$certPath"
  514. mkdir -p "$certPath"
  515. fi
  516. #get needed port here
  517. local WebPort=80
  518. read -p "please choose which port do you use,default will be 80 port:" WebPort
  519. if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
  520. LOGE "your input ${WebPort} is invalid,will use default port"
  521. fi
  522. LOGI "will use port:${WebPort} to issue certs,please make sure this port is open..."
  523. #NOTE:This should be handled by user
  524. #open the port and kill the occupied progress
  525. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  526. ~/.acme.sh/acme.sh --issue -d ${domain} --standalone --httpport ${WebPort}
  527. if [ $? -ne 0 ]; then
  528. LOGE "issue certs failed,please check logs"
  529. rm -rf ~/.acme.sh/${domain}
  530. exit 1
  531. else
  532. LOGE "issue certs succeed,installing certs..."
  533. fi
  534. #install cert
  535. ~/.acme.sh/acme.sh --installcert -d ${domain} \
  536. --key-file /root/cert/${domain}/privkey.pem \
  537. --fullchain-file /root/cert/${domain}/fullchain.pem
  538. if [ $? -ne 0 ]; then
  539. LOGE "install certs failed,exit"
  540. rm -rf ~/.acme.sh/${domain}
  541. exit 1
  542. else
  543. LOGI "install certs succeed,enable auto renew..."
  544. fi
  545. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  546. if [ $? -ne 0 ]; then
  547. LOGE "auto renew failed, certs details:"
  548. ls -lah cert/*
  549. chmod 755 $certPath/*
  550. exit 1
  551. else
  552. LOGI "auto renew succeed, certs details:"
  553. ls -lah cert/*
  554. chmod 755 $certPath/*
  555. fi
  556. }
  557. #method for DNS API mode
  558. ssl_cert_issue_by_cloudflare() {
  559. echo -E ""
  560. LOGD "******Preconditions******"
  561. LOGI "1.need Cloudflare account associated email"
  562. LOGI "2.need Cloudflare Global API Key"
  563. LOGI "3.your domain use Cloudflare as resolver"
  564. confirm "I have confirmed all these info above[y/n]" "y"
  565. if [ $? -eq 0 ]; then
  566. install_acme
  567. if [ $? -ne 0 ]; then
  568. LOGE "install acme failed,please check logs"
  569. exit 1
  570. fi
  571. CF_Domain=""
  572. CF_GlobalKey=""
  573. CF_AccountEmail=""
  574. LOGD "please input your domain:"
  575. read -p "Input your domain here:" CF_Domain
  576. LOGD "your domain is:${CF_Domain},check it..."
  577. #here we need to judge whether there exists cert already
  578. local currentCert=$(~/.acme.sh/acme.sh --list | tail -1 | awk '{print $1}')
  579. if [ ${currentCert} == ${CF_Domain} ]; then
  580. local certInfo=$(~/.acme.sh/acme.sh --list)
  581. LOGE "system already have certs here,can not issue again,current certs details:"
  582. LOGI "$certInfo"
  583. exit 1
  584. else
  585. LOGI "your domain is ready for issuing cert now..."
  586. fi
  587. #create a directory for install cert
  588. certPath="/root/cert/${CF_Domain}"
  589. if [ ! -d "$certPath" ]; then
  590. mkdir -p "$certPath"
  591. else
  592. rm -rf "$certPath"
  593. mkdir -p "$certPath"
  594. fi
  595. LOGD "please inout your cloudflare global API key:"
  596. read -p "Input your key here:" CF_GlobalKey
  597. LOGD "your cloudflare global API key is:${CF_GlobalKey}"
  598. LOGD "please input your cloudflare account email:"
  599. read -p "Input your email here:" CF_AccountEmail
  600. LOGD "your cloudflare account email:${CF_AccountEmail}"
  601. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  602. if [ $? -ne 0 ]; then
  603. LOGE "change the default CA to Lets'Encrypt failed,exit"
  604. exit 1
  605. fi
  606. export CF_Key="${CF_GlobalKey}"
  607. export CF_Email=${CF_AccountEmail}
  608. ~/.acme.sh/acme.sh --issue --dns dns_cf -d ${CF_Domain} -d *.${CF_Domain} --log
  609. if [ $? -ne 0 ]; then
  610. LOGE "issue cert failed,exit"
  611. rm -rf ~/.acme.sh/${CF_Domain}
  612. exit 1
  613. else
  614. LOGI "Certificate issued Successfully, Installing..."
  615. fi
  616. ~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} \
  617. --key-file /root/cert/${CF_Domain}/privkey.pem \
  618. --fullchain-file /root/cert/${CF_Domain}/fullchain.pem
  619. if [ $? -ne 0 ]; then
  620. LOGE "install cert failed,exit"
  621. rm -rf ~/.acme.sh/${CF_Domain}
  622. exit 1
  623. else
  624. LOGI "Certificate installed Successfully,Turning on automatic updates..."
  625. fi
  626. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  627. if [ $? -ne 0 ]; then
  628. LOGE "auto renew failed, certs details:"
  629. ls -lah cert/*
  630. chmod 755 $certPath/*
  631. exit 1
  632. else
  633. LOGI "auto renew succeed, certs details:"
  634. ls -lah cert/*
  635. chmod 755 $certPath/*
  636. fi
  637. else
  638. show_menu
  639. fi
  640. }
  641. google_recaptcha() {
  642. curl -O https://raw.githubusercontent.com/jinwyp/one_click_script/master/install_kernel.sh && chmod +x ./install_kernel.sh && ./install_kernel.sh
  643. echo ""
  644. before_show_menu
  645. }
  646. run_speedtest() {
  647. # Check if Speedtest is already installed
  648. if ! command -v speedtest &> /dev/null; then
  649. # If not installed, install it
  650. if command -v dnf &> /dev/null; then
  651. sudo dnf install -y curl
  652. curl -s https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh | sudo bash
  653. sudo dnf install -y speedtest
  654. elif command -v yum &> /dev/null; then
  655. sudo yum install -y curl
  656. curl -s https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh | sudo bash
  657. sudo yum install -y speedtest
  658. elif command -v apt-get &> /dev/null; then
  659. sudo apt-get update && sudo apt-get install -y curl
  660. curl -s https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh | sudo bash
  661. sudo apt-get install -y speedtest
  662. elif command -v apt &> /dev/null; then
  663. sudo apt update && sudo apt install -y curl
  664. curl -s https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh | sudo bash
  665. sudo apt install -y speedtest
  666. else
  667. echo "Error: Package manager not found. You may need to install Speedtest manually."
  668. return 1
  669. fi
  670. fi
  671. # Run Speedtest
  672. speedtest
  673. }
  674. show_usage() {
  675. echo "x-ui control menu usages: "
  676. echo "------------------------------------------"
  677. echo -e "x-ui - Enter control menu"
  678. echo -e "x-ui start - Start x-ui "
  679. echo -e "x-ui stop - Stop x-ui "
  680. echo -e "x-ui restart - Restart x-ui "
  681. echo -e "x-ui status - Show x-ui status"
  682. echo -e "x-ui enable - Enable x-ui on system startup"
  683. echo -e "x-ui disable - Disable x-ui on system startup"
  684. echo -e "x-ui log - Check x-ui logs"
  685. echo -e "x-ui update - Update x-ui "
  686. echo -e "x-ui install - Install x-ui "
  687. echo -e "x-ui uninstall - Uninstall x-ui "
  688. echo "------------------------------------------"
  689. }
  690. show_menu() {
  691. echo -e "
  692. ${green}3X-ui Panel Management Script${plain}
  693. ${green}0.${plain} Exit Script
  694. ————————————————
  695. ${green}1.${plain} Install x-ui
  696. ${green}2.${plain} Update x-ui
  697. ${green}3.${plain} Uninstall x-ui
  698. ————————————————
  699. ${green}4.${plain} Reset Username And Password
  700. ${green}5.${plain} Reset Panel Settings
  701. ${green}6.${plain} Change Panel Port
  702. ${green}7.${plain} View Current Panel Settings
  703. ————————————————
  704. ${green}8.${plain} Start x-ui
  705. ${green}9.${plain} Stop x-ui
  706. ${green}10.${plain} Restart x-ui
  707. ${green}11.${plain} Check x-ui Status
  708. ${green}12.${plain} Check x-ui Logs
  709. ————————————————
  710. ${green}13.${plain} Enable x-ui On System Startup
  711. ${green}14.${plain} Disabel x-ui On System Startup
  712. ————————————————
  713. ${green}15.${plain} Enable BBR
  714. ${green}16.${plain} Apply for an SSL Certificate
  715. ${green}17.${plain} Update Geo Files
  716. ${green}18.${plain} Active Firewall and open ports
  717. ${green}19.${plain} Fixing Google reCAPTCHA
  718. ${green}20.${plain} Speedtest by Ookla
  719. "
  720. show_status
  721. echo && read -p "Please enter your selection [0-20]: " num
  722. case "${num}" in
  723. 0)
  724. exit 0
  725. ;;
  726. 1)
  727. check_uninstall && install
  728. ;;
  729. 2)
  730. check_install && update
  731. ;;
  732. 3)
  733. check_install && uninstall
  734. ;;
  735. 4)
  736. check_install && reset_user
  737. ;;
  738. 5)
  739. check_install && reset_config
  740. ;;
  741. 6)
  742. check_install && set_port
  743. ;;
  744. 7)
  745. check_install && check_config
  746. ;;
  747. 8)
  748. check_install && start
  749. ;;
  750. 9)
  751. check_install && stop
  752. ;;
  753. 10)
  754. check_install && restart
  755. ;;
  756. 11)
  757. check_install && status
  758. ;;
  759. 12)
  760. check_install && show_log
  761. ;;
  762. 13)
  763. check_install && enable
  764. ;;
  765. 14)
  766. check_install && disable
  767. ;;
  768. 15)
  769. enable_bbr
  770. ;;
  771. 16)
  772. ssl_cert_issue
  773. ;;
  774. 17)
  775. update_geo
  776. ;;
  777. 18)
  778. open_ports
  779. ;;
  780. 19)
  781. google_recaptcha
  782. ;;
  783. 20)
  784. run_speedtest
  785. ;;
  786. *)
  787. LOGE "Please enter the correct number [0-20]"
  788. ;;
  789. esac
  790. }
  791. if [[ $# > 0 ]]; then
  792. case $1 in
  793. "start")
  794. check_install 0 && start 0
  795. ;;
  796. "stop")
  797. check_install 0 && stop 0
  798. ;;
  799. "restart")
  800. check_install 0 && restart 0
  801. ;;
  802. "status")
  803. check_install 0 && status 0
  804. ;;
  805. "enable")
  806. check_install 0 && enable 0
  807. ;;
  808. "disable")
  809. check_install 0 && disable 0
  810. ;;
  811. "log")
  812. check_install 0 && show_log 0
  813. ;;
  814. "update")
  815. check_install 0 && update 0
  816. ;;
  817. "install")
  818. check_uninstall 0 && install 0
  819. ;;
  820. "uninstall")
  821. check_install 0 && uninstall 0
  822. ;;
  823. *) show_usage ;;
  824. esac
  825. else
  826. show_menu
  827. fi