x-ui.sh 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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 "Are you sure to reset the username and password of the panel?" "n"
  125. if [[ $? != 0 ]]; then
  126. if [[ $# == 0 ]]; then
  127. show_menu
  128. fi
  129. return 0
  130. fi
  131. read -rp "Please set the login username [default is a random username]: " config_account
  132. [[ -z $config_account ]] && config_account=$(date +%s%N | md5sum | cut -c 1-8)
  133. read -rp "Please set the login password [default is a random password]: " config_password
  134. [[ -z $config_password ]] && config_password=$(date +%s%N | md5sum | cut -c 1-8)
  135. /usr/local/x-ui/x-ui setting -username ${config_account} -password ${config_password} >/dev/null 2>&1
  136. /usr/local/x-ui/x-ui setting -remove_secret >/dev/null 2>&1
  137. echo -e "Panel login username has been reset to: ${green} ${config_account} ${plain}"
  138. echo -e "Panel login password has been reset to: ${green} ${config_password} ${plain}"
  139. echo -e "${yellow} Panel login secret token disabled ${plain}"
  140. echo -e "${green} Please use the new login username and password to access the X-UI panel. Also remember them! ${plain}"
  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. enable_bbr() {
  260. if grep -q "net.core.default_qdisc=fq" /etc/sysctl.conf && grep -q "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf; then
  261. echo -e "${green}BBR is already enabled!${plain}"
  262. exit 0
  263. fi
  264. # Check the OS and install necessary packages
  265. if [[ "$(cat /etc/os-release | grep -E '^ID=' | awk -F '=' '{print $2}')" == "ubuntu" ]]; then
  266. sudo apt-get update && sudo apt-get install -yqq --no-install-recommends ca-certificates
  267. elif [[ "$(cat /etc/os-release | grep -E '^ID=' | awk -F '=' '{print $2}')" == "debian" ]]; then
  268. sudo apt-get update && sudo apt-get install -yqq --no-install-recommends ca-certificates
  269. elif [[ "$(cat /etc/os-release | grep -E '^ID=' | awk -F '=' '{print $2}')" == "fedora" ]]; then
  270. sudo dnf -y update && sudo dnf -y install ca-certificates
  271. elif [[ "$(cat /etc/os-release | grep -E '^ID=' | awk -F '=' '{print $2}')" == "centos" ]]; then
  272. sudo yum -y update && sudo yum -y install ca-certificates
  273. else
  274. echo "Unsupported operating system. Please check the script and install the necessary packages manually."
  275. exit 1
  276. fi
  277. # Enable BBR
  278. echo "net.core.default_qdisc=fq" | sudo tee -a /etc/sysctl.conf
  279. echo "net.ipv4.tcp_congestion_control=bbr" | sudo tee -a /etc/sysctl.conf
  280. # Apply changes
  281. sudo sysctl -p
  282. # Verify that BBR is enabled
  283. if [[ $(sysctl net.ipv4.tcp_congestion_control | awk '{print $3}') == "bbr" ]]; then
  284. echo -e "${green}BBR has been enabled successfully.${plain}"
  285. else
  286. echo -e "${red}Failed to enable BBR. Please check your system configuration.${plain}"
  287. fi
  288. }
  289. update_shell() {
  290. wget -O /usr/bin/x-ui -N --no-check-certificate https://github.com/MHSanaei/3x-ui/raw/main/x-ui.sh
  291. if [[ $? != 0 ]]; then
  292. echo ""
  293. LOGE "Failed to download script, Please check whether the machine can connect Github"
  294. before_show_menu
  295. else
  296. chmod +x /usr/bin/x-ui
  297. LOGI "Upgrade script succeeded, Please rerun the script" && exit 0
  298. fi
  299. }
  300. # 0: running, 1: not running, 2: not installed
  301. check_status() {
  302. if [[ ! -f /etc/systemd/system/x-ui.service ]]; then
  303. return 2
  304. fi
  305. temp=$(systemctl status x-ui | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
  306. if [[ x"${temp}" == x"running" ]]; then
  307. return 0
  308. else
  309. return 1
  310. fi
  311. }
  312. check_enabled() {
  313. temp=$(systemctl is-enabled x-ui)
  314. if [[ x"${temp}" == x"enabled" ]]; then
  315. return 0
  316. else
  317. return 1
  318. fi
  319. }
  320. check_uninstall() {
  321. check_status
  322. if [[ $? != 2 ]]; then
  323. echo ""
  324. LOGE "Panel installed, Please do not reinstall"
  325. if [[ $# == 0 ]]; then
  326. before_show_menu
  327. fi
  328. return 1
  329. else
  330. return 0
  331. fi
  332. }
  333. check_install() {
  334. check_status
  335. if [[ $? == 2 ]]; then
  336. echo ""
  337. LOGE "Please install the panel first"
  338. if [[ $# == 0 ]]; then
  339. before_show_menu
  340. fi
  341. return 1
  342. else
  343. return 0
  344. fi
  345. }
  346. show_status() {
  347. check_status
  348. case $? in
  349. 0)
  350. echo -e "Panel state: ${green}Running${plain}"
  351. show_enable_status
  352. ;;
  353. 1)
  354. echo -e "Panel state: ${yellow}Not Running${plain}"
  355. show_enable_status
  356. ;;
  357. 2)
  358. echo -e "Panel state: ${red}Not Installed${plain}"
  359. ;;
  360. esac
  361. show_xray_status
  362. }
  363. show_enable_status() {
  364. check_enabled
  365. if [[ $? == 0 ]]; then
  366. echo -e "Start automatically: ${green}Yes${plain}"
  367. else
  368. echo -e "Start automatically: ${red}No${plain}"
  369. fi
  370. }
  371. check_xray_status() {
  372. count=$(ps -ef | grep "xray-linux" | grep -v "grep" | wc -l)
  373. if [[ count -ne 0 ]]; then
  374. return 0
  375. else
  376. return 1
  377. fi
  378. }
  379. show_xray_status() {
  380. check_xray_status
  381. if [[ $? == 0 ]]; then
  382. echo -e "xray state: ${green}Running${plain}"
  383. else
  384. echo -e "xray state: ${red}Not Running${plain}"
  385. fi
  386. }
  387. #this will be an entrance for ssl cert issue
  388. #here we can provide two different methods to issue cert
  389. #first.standalone mode second.DNS API mode
  390. ssl_cert_issue() {
  391. local method=""
  392. echo -E ""
  393. LOGD "********Usage********"
  394. LOGI "this shell script will use acme to help issue certs."
  395. LOGI "here we provide two methods for issuing certs:"
  396. LOGI "method 1:acme standalone mode,need to keep port:80 open"
  397. LOGI "method 2:acme DNS API mode,need provide Cloudflare Global API Key"
  398. LOGI "recommend method 2 first,if it fails,you can try method 1."
  399. LOGI "certs will be installed in /root/cert directory"
  400. read -p "please choose which method do you want,type 1 or 2": method
  401. LOGI "you choosed method:${method}"
  402. if [ "${method}" == "1" ]; then
  403. ssl_cert_issue_standalone
  404. elif [ "${method}" == "2" ]; then
  405. ssl_cert_issue_by_cloudflare
  406. else
  407. LOGE "invalid input,please check it..."
  408. exit 1
  409. fi
  410. }
  411. open_ports() {
  412. if ! command -v ufw &> /dev/null
  413. then
  414. echo "ufw firewall is not installed. Installing now..."
  415. sudo apt-get update
  416. sudo apt-get install -y ufw
  417. else
  418. echo "ufw firewall is already installed"
  419. fi
  420. # Check if the firewall is inactive
  421. if sudo ufw status | grep -q "Status: active"; then
  422. echo "firewall is already active"
  423. else
  424. # Open the necessary ports
  425. sudo ufw allow ssh
  426. sudo ufw allow http
  427. sudo ufw allow https
  428. sudo ufw allow 2053/tcp
  429. # Enable the firewall
  430. sudo ufw --force enable
  431. fi
  432. # Prompt the user to enter a list of ports
  433. read -p "Enter the ports you want to open (e.g. 80,443,2053 or range 400-500): " ports
  434. # Check if the input is valid
  435. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  436. 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
  437. fi
  438. # Open the specified ports using ufw
  439. IFS=',' read -ra PORT_LIST <<< "$ports"
  440. for port in "${PORT_LIST[@]}"; do
  441. if [[ $port == *-* ]]; then
  442. # Split the range into start and end ports
  443. start_port=$(echo $port | cut -d'-' -f1)
  444. end_port=$(echo $port | cut -d'-' -f2)
  445. # Loop through the range and open each port
  446. for ((i=start_port; i<=end_port; i++)); do
  447. sudo ufw allow $i
  448. done
  449. else
  450. sudo ufw allow "$port"
  451. fi
  452. done
  453. # Confirm that the ports are open
  454. sudo ufw status | grep $ports
  455. }
  456. update_geo() {
  457. local defaultBinFolder="/usr/local/x-ui/bin"
  458. read -p "Please enter x-ui bin folder path. Leave blank for default. (Default: '${defaultBinFolder}')" binFolder
  459. binFolder=${binFolder:-${defaultBinFolder}}
  460. if [[ ! -d ${binFolder} ]]; then
  461. LOGE "Folder ${binFolder} not exists!"
  462. LOGI "making bin folder: ${binFolder}..."
  463. mkdir -p ${binFolder}
  464. fi
  465. systemctl stop x-ui
  466. cd ${binFolder}
  467. rm -f geoip.dat geosite.dat iran.dat
  468. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat
  469. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat
  470. wget -N https://github.com/bootmortis/iran-hosted-domains/releases/latest/download/iran.dat
  471. systemctl start x-ui
  472. echo -e "${green}Geosite.dat + Geoip.dat + Iran.dat have been updated successfully in bin folder '${binfolder}'!${plain}"
  473. before_show_menu
  474. }
  475. install_acme() {
  476. cd ~
  477. LOGI "install acme..."
  478. curl https://get.acme.sh | sh
  479. if [ $? -ne 0 ]; then
  480. LOGE "install acme failed"
  481. return 1
  482. else
  483. LOGI "install acme succeed"
  484. fi
  485. return 0
  486. }
  487. #method for standalone mode
  488. ssl_cert_issue_standalone() {
  489. #check for acme.sh first
  490. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  491. echo "acme.sh could not be found. we will install it"
  492. install_acme
  493. if [ $? -ne 0 ]; then
  494. LOGE "install acme failed, please check logs"
  495. exit 1
  496. fi
  497. fi
  498. #install socat second
  499. if [[ x"${release}" == x"centos" ]]; then
  500. yum install socat -y
  501. else
  502. apt install socat -y
  503. fi
  504. if [ $? -ne 0 ]; then
  505. LOGE "install socat failed,please check logs"
  506. exit 1
  507. else
  508. LOGI "install socat succeed..."
  509. fi
  510. #get the domain here,and we need verify it
  511. local domain=""
  512. read -p "please input your domain:" domain
  513. LOGD "your domain is:${domain},check it..."
  514. #here we need to judge whether there exists cert already
  515. local currentCert=$(~/.acme.sh/acme.sh --list | tail -1 | awk '{print $1}')
  516. if [ ${currentCert} == ${domain} ]; then
  517. local certInfo=$(~/.acme.sh/acme.sh --list)
  518. LOGE "system already have certs here,can not issue again,current certs details:"
  519. LOGI "$certInfo"
  520. exit 1
  521. else
  522. LOGI "your domain is ready for issuing cert now..."
  523. fi
  524. #create a directory for install cert
  525. certPath="/root/cert/${domain}"
  526. if [ ! -d "$certPath" ]; then
  527. mkdir -p "$certPath"
  528. else
  529. rm -rf "$certPath"
  530. mkdir -p "$certPath"
  531. fi
  532. #get needed port here
  533. local WebPort=80
  534. read -p "please choose which port do you use,default will be 80 port:" WebPort
  535. if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
  536. LOGE "your input ${WebPort} is invalid,will use default port"
  537. fi
  538. LOGI "will use port:${WebPort} to issue certs,please make sure this port is open..."
  539. #NOTE:This should be handled by user
  540. #open the port and kill the occupied progress
  541. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  542. ~/.acme.sh/acme.sh --issue -d ${domain} --standalone --httpport ${WebPort}
  543. if [ $? -ne 0 ]; then
  544. LOGE "issue certs failed,please check logs"
  545. rm -rf ~/.acme.sh/${domain}
  546. exit 1
  547. else
  548. LOGE "issue certs succeed,installing certs..."
  549. fi
  550. #install cert
  551. ~/.acme.sh/acme.sh --installcert -d ${domain} \
  552. --key-file /root/cert/${domain}/privkey.pem \
  553. --fullchain-file /root/cert/${domain}/fullchain.pem
  554. if [ $? -ne 0 ]; then
  555. LOGE "install certs failed,exit"
  556. rm -rf ~/.acme.sh/${domain}
  557. exit 1
  558. else
  559. LOGI "install certs succeed,enable auto renew..."
  560. fi
  561. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  562. if [ $? -ne 0 ]; then
  563. LOGE "auto renew failed, certs details:"
  564. ls -lah cert/*
  565. chmod 755 $certPath/*
  566. exit 1
  567. else
  568. LOGI "auto renew succeed, certs details:"
  569. ls -lah cert/*
  570. chmod 755 $certPath/*
  571. fi
  572. }
  573. #method for DNS API mode
  574. ssl_cert_issue_by_cloudflare() {
  575. echo -E ""
  576. LOGD "******Preconditions******"
  577. LOGI "1.need Cloudflare account associated email"
  578. LOGI "2.need Cloudflare Global API Key"
  579. LOGI "3.your domain use Cloudflare as resolver"
  580. confirm "I have confirmed all these info above[y/n]" "y"
  581. if [ $? -eq 0 ]; then
  582. install_acme
  583. if [ $? -ne 0 ]; then
  584. LOGE "install acme failed,please check logs"
  585. exit 1
  586. fi
  587. CF_Domain=""
  588. CF_GlobalKey=""
  589. CF_AccountEmail=""
  590. LOGD "please input your domain:"
  591. read -p "Input your domain here:" CF_Domain
  592. LOGD "your domain is:${CF_Domain},check it..."
  593. #here we need to judge whether there exists cert already
  594. local currentCert=$(~/.acme.sh/acme.sh --list | tail -1 | awk '{print $1}')
  595. if [ ${currentCert} == ${CF_Domain} ]; then
  596. local certInfo=$(~/.acme.sh/acme.sh --list)
  597. LOGE "system already have certs here,can not issue again,current certs details:"
  598. LOGI "$certInfo"
  599. exit 1
  600. else
  601. LOGI "your domain is ready for issuing cert now..."
  602. fi
  603. #create a directory for install cert
  604. certPath="/root/cert/${CF_Domain}"
  605. if [ ! -d "$certPath" ]; then
  606. mkdir -p "$certPath"
  607. else
  608. rm -rf "$certPath"
  609. mkdir -p "$certPath"
  610. fi
  611. LOGD "please inout your cloudflare global API key:"
  612. read -p "Input your key here:" CF_GlobalKey
  613. LOGD "your cloudflare global API key is:${CF_GlobalKey}"
  614. LOGD "please input your cloudflare account email:"
  615. read -p "Input your email here:" CF_AccountEmail
  616. LOGD "your cloudflare account email:${CF_AccountEmail}"
  617. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  618. if [ $? -ne 0 ]; then
  619. LOGE "change the default CA to Lets'Encrypt failed,exit"
  620. exit 1
  621. fi
  622. export CF_Key="${CF_GlobalKey}"
  623. export CF_Email=${CF_AccountEmail}
  624. ~/.acme.sh/acme.sh --issue --dns dns_cf -d ${CF_Domain} -d *.${CF_Domain} --log
  625. if [ $? -ne 0 ]; then
  626. LOGE "issue cert failed,exit"
  627. rm -rf ~/.acme.sh/${CF_Domain}
  628. exit 1
  629. else
  630. LOGI "Certificate issued Successfully, Installing..."
  631. fi
  632. ~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} \
  633. --key-file /root/cert/${CF_Domain}/privkey.pem \
  634. --fullchain-file /root/cert/${CF_Domain}/fullchain.pem
  635. if [ $? -ne 0 ]; then
  636. LOGE "install cert failed,exit"
  637. rm -rf ~/.acme.sh/${CF_Domain}
  638. exit 1
  639. else
  640. LOGI "Certificate installed Successfully,Turning on automatic updates..."
  641. fi
  642. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  643. if [ $? -ne 0 ]; then
  644. LOGE "auto renew failed, certs details:"
  645. ls -lah cert/*
  646. chmod 755 $certPath/*
  647. exit 1
  648. else
  649. LOGI "auto renew succeed, certs details:"
  650. ls -lah cert/*
  651. chmod 755 $certPath/*
  652. fi
  653. else
  654. show_menu
  655. fi
  656. }
  657. warp_fixchatgpt() {
  658. curl -fsSL https://gist.githubusercontent.com/hamid-gh98/dc5dd9b0cc5b0412af927b1ccdb294c7/raw/install_warp_proxy.sh | bash
  659. echo ""
  660. before_show_menu
  661. }
  662. run_speedtest() {
  663. # Check if Speedtest is already installed
  664. if ! command -v speedtest &> /dev/null; then
  665. # If not installed, install it
  666. if command -v dnf &> /dev/null; then
  667. sudo dnf install -y curl
  668. curl -s https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh | sudo bash
  669. sudo dnf install -y speedtest
  670. elif command -v yum &> /dev/null; then
  671. sudo yum install -y curl
  672. curl -s https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh | sudo bash
  673. sudo yum install -y speedtest
  674. elif command -v apt-get &> /dev/null; then
  675. sudo apt-get update && sudo apt-get install -y curl
  676. curl -s https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh | sudo bash
  677. sudo apt-get install -y speedtest
  678. elif command -v apt &> /dev/null; then
  679. sudo apt update && sudo apt install -y curl
  680. curl -s https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh | sudo bash
  681. sudo apt install -y speedtest
  682. else
  683. echo "Error: Package manager not found. You may need to install Speedtest manually."
  684. return 1
  685. fi
  686. fi
  687. # Run Speedtest
  688. speedtest
  689. }
  690. show_usage() {
  691. echo "x-ui control menu usages: "
  692. echo "------------------------------------------"
  693. echo -e "x-ui - Enter control menu"
  694. echo -e "x-ui start - Start x-ui "
  695. echo -e "x-ui stop - Stop x-ui "
  696. echo -e "x-ui restart - Restart x-ui "
  697. echo -e "x-ui status - Show x-ui status"
  698. echo -e "x-ui enable - Enable x-ui on system startup"
  699. echo -e "x-ui disable - Disable x-ui on system startup"
  700. echo -e "x-ui log - Check x-ui logs"
  701. echo -e "x-ui update - Update x-ui "
  702. echo -e "x-ui install - Install x-ui "
  703. echo -e "x-ui uninstall - Uninstall x-ui "
  704. echo "------------------------------------------"
  705. }
  706. show_menu() {
  707. echo -e "
  708. ${green}3X-ui Panel Management Script${plain}
  709. ${green}0.${plain} Exit Script
  710. ————————————————
  711. ${green}1.${plain} Install x-ui
  712. ${green}2.${plain} Update x-ui
  713. ${green}3.${plain} Uninstall x-ui
  714. ————————————————
  715. ${green}4.${plain} Reset Username & Password & Secret Token
  716. ${green}5.${plain} Reset Panel Settings
  717. ${green}6.${plain} Change Panel Port
  718. ${green}7.${plain} View Current Panel Settings
  719. ————————————————
  720. ${green}8.${plain} Start x-ui
  721. ${green}9.${plain} Stop x-ui
  722. ${green}10.${plain} Restart x-ui
  723. ${green}11.${plain} Check x-ui Status
  724. ${green}12.${plain} Check x-ui Logs
  725. ————————————————
  726. ${green}13.${plain} Enable x-ui On System Startup
  727. ${green}14.${plain} Disable x-ui On System Startup
  728. ————————————————
  729. ${green}15.${plain} Enable BBR
  730. ${green}16.${plain} Apply for an SSL Certificate
  731. ${green}17.${plain} Update Geo Files
  732. ${green}18.${plain} Active Firewall and open ports
  733. ${green}19.${plain} Install WARP
  734. ${green}20.${plain} Speedtest by Ookla
  735. "
  736. show_status
  737. echo && read -p "Please enter your selection [0-20]: " num
  738. case "${num}" in
  739. 0)
  740. exit 0
  741. ;;
  742. 1)
  743. check_uninstall && install
  744. ;;
  745. 2)
  746. check_install && update
  747. ;;
  748. 3)
  749. check_install && uninstall
  750. ;;
  751. 4)
  752. check_install && reset_user
  753. ;;
  754. 5)
  755. check_install && reset_config
  756. ;;
  757. 6)
  758. check_install && set_port
  759. ;;
  760. 7)
  761. check_install && check_config
  762. ;;
  763. 8)
  764. check_install && start
  765. ;;
  766. 9)
  767. check_install && stop
  768. ;;
  769. 10)
  770. check_install && restart
  771. ;;
  772. 11)
  773. check_install && status
  774. ;;
  775. 12)
  776. check_install && show_log
  777. ;;
  778. 13)
  779. check_install && enable
  780. ;;
  781. 14)
  782. check_install && disable
  783. ;;
  784. 15)
  785. enable_bbr
  786. ;;
  787. 16)
  788. ssl_cert_issue
  789. ;;
  790. 17)
  791. update_geo
  792. ;;
  793. 18)
  794. open_ports
  795. ;;
  796. 19)
  797. warp_fixchatgpt
  798. ;;
  799. 20)
  800. run_speedtest
  801. ;;
  802. *)
  803. LOGE "Please enter the correct number [0-20]"
  804. ;;
  805. esac
  806. }
  807. if [[ $# > 0 ]]; then
  808. case $1 in
  809. "start")
  810. check_install 0 && start 0
  811. ;;
  812. "stop")
  813. check_install 0 && stop 0
  814. ;;
  815. "restart")
  816. check_install 0 && restart 0
  817. ;;
  818. "status")
  819. check_install 0 && status 0
  820. ;;
  821. "enable")
  822. check_install 0 && enable 0
  823. ;;
  824. "disable")
  825. check_install 0 && disable 0
  826. ;;
  827. "log")
  828. check_install 0 && show_log 0
  829. ;;
  830. "update")
  831. check_install 0 && update 0
  832. ;;
  833. "install")
  834. check_uninstall 0 && install 0
  835. ;;
  836. "uninstall")
  837. check_install 0 && uninstall 0
  838. ;;
  839. *) show_usage ;;
  840. esac
  841. else
  842. show_menu
  843. fi