x-ui.sh 28 KB

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