1
0

x-ui.sh 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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. elif [[ "${release}" == "almalinux" ]]; then
  49. if [[ ${os_version} -lt 9 ]]; then
  50. echo -e "${red} Please use Almalinux 9 or higher ${plain}\n" && exit 1
  51. fi
  52. elif [[ "${release}" == "arch" ]]; then
  53. echo "Your OS is ArchLinux"
  54. elif [[ "${release}" == "manjaro" ]]; then
  55. echo "Your OS is Manjaro"
  56. elif [[ "${release}" == "armbian" ]]; then
  57. echo "Your OS is Armbian"
  58. fi
  59. # Declare Variables
  60. log_folder="${XUI_LOG_FOLDER:=/var/log}"
  61. iplimit_log_path="${log_folder}/3xipl.log"
  62. iplimit_banned_log_path="${log_folder}/3xipl-banned.log"
  63. confirm() {
  64. if [[ $# > 1 ]]; then
  65. echo && read -p "$1 [Default $2]: " temp
  66. if [[ "${temp}" == "" ]]; then
  67. temp=$2
  68. fi
  69. else
  70. read -p "$1 [y/n]: " temp
  71. fi
  72. if [[ "${temp}" == "y" || "${temp}" == "Y" ]]; then
  73. return 0
  74. else
  75. return 1
  76. fi
  77. }
  78. confirm_restart() {
  79. confirm "Restart the panel, Attention: Restarting the panel will also restart xray" "y"
  80. if [[ $? == 0 ]]; then
  81. restart
  82. else
  83. show_menu
  84. fi
  85. }
  86. before_show_menu() {
  87. echo && echo -n -e "${yellow}Press enter to return to the main menu: ${plain}" && read temp
  88. show_menu
  89. }
  90. install() {
  91. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  92. if [[ $? == 0 ]]; then
  93. if [[ $# == 0 ]]; then
  94. start
  95. else
  96. start 0
  97. fi
  98. fi
  99. }
  100. update() {
  101. confirm "This function will forcefully reinstall the latest version, and the data will not be lost. Do you want to continue?" "n"
  102. if [[ $? != 0 ]]; then
  103. LOGE "Cancelled"
  104. if [[ $# == 0 ]]; then
  105. before_show_menu
  106. fi
  107. return 0
  108. fi
  109. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  110. if [[ $? == 0 ]]; then
  111. LOGI "Update is complete, Panel has automatically restarted "
  112. exit 0
  113. fi
  114. }
  115. uninstall() {
  116. confirm "Are you sure you want to uninstall the panel? xray will also uninstalled!" "n"
  117. if [[ $? != 0 ]]; then
  118. if [[ $# == 0 ]]; then
  119. show_menu
  120. fi
  121. return 0
  122. fi
  123. systemctl stop x-ui
  124. systemctl disable x-ui
  125. rm /etc/systemd/system/x-ui.service -f
  126. systemctl daemon-reload
  127. systemctl reset-failed
  128. rm /etc/x-ui/ -rf
  129. rm /usr/local/x-ui/ -rf
  130. echo ""
  131. 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."
  132. echo ""
  133. if [[ $# == 0 ]]; then
  134. before_show_menu
  135. fi
  136. }
  137. reset_user() {
  138. confirm "Are you sure to reset the username and password of the panel?" "n"
  139. if [[ $? != 0 ]]; then
  140. if [[ $# == 0 ]]; then
  141. show_menu
  142. fi
  143. return 0
  144. fi
  145. read -rp "Please set the login username [default is a random username]: " config_account
  146. [[ -z $config_account ]] && config_account=$(date +%s%N | md5sum | cut -c 1-8)
  147. read -rp "Please set the login password [default is a random password]: " config_password
  148. [[ -z $config_password ]] && config_password=$(date +%s%N | md5sum | cut -c 1-8)
  149. /usr/local/x-ui/x-ui setting -username ${config_account} -password ${config_password} >/dev/null 2>&1
  150. /usr/local/x-ui/x-ui setting -remove_secret >/dev/null 2>&1
  151. echo -e "Panel login username has been reset to: ${green} ${config_account} ${plain}"
  152. echo -e "Panel login password has been reset to: ${green} ${config_password} ${plain}"
  153. echo -e "${yellow} Panel login secret token disabled ${plain}"
  154. echo -e "${green} Please use the new login username and password to access the X-UI panel. Also remember them! ${plain}"
  155. confirm_restart
  156. }
  157. reset_config() {
  158. confirm "Are you sure you want to reset all panel settings, Account data will not be lost, Username and password will not change" "n"
  159. if [[ $? != 0 ]]; then
  160. if [[ $# == 0 ]]; then
  161. show_menu
  162. fi
  163. return 0
  164. fi
  165. /usr/local/x-ui/x-ui setting -reset
  166. 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"
  167. confirm_restart
  168. }
  169. check_config() {
  170. info=$(/usr/local/x-ui/x-ui setting -show true)
  171. if [[ $? != 0 ]]; then
  172. LOGE "get current settings error, please check logs"
  173. show_menu
  174. fi
  175. LOGI "${info}"
  176. }
  177. set_port() {
  178. echo && echo -n -e "Enter port number[1-65535]: " && read port
  179. if [[ -z "${port}" ]]; then
  180. LOGD "Cancelled"
  181. before_show_menu
  182. else
  183. /usr/local/x-ui/x-ui setting -port ${port}
  184. echo -e "The port is set, Please restart the panel now, and use the new port ${green}${port}${plain} to access web panel"
  185. confirm_restart
  186. fi
  187. }
  188. start() {
  189. check_status
  190. if [[ $? == 0 ]]; then
  191. echo ""
  192. LOGI "Panel is running, No need to start again, If you need to restart, please select restart"
  193. else
  194. systemctl start x-ui
  195. sleep 2
  196. check_status
  197. if [[ $? == 0 ]]; then
  198. LOGI "x-ui Started Successfully"
  199. else
  200. LOGE "panel Failed to start, Probably because it takes longer than two seconds to start, Please check the log information later"
  201. fi
  202. fi
  203. if [[ $# == 0 ]]; then
  204. before_show_menu
  205. fi
  206. }
  207. stop() {
  208. check_status
  209. if [[ $? == 1 ]]; then
  210. echo ""
  211. LOGI "Panel stopped, No need to stop again!"
  212. else
  213. systemctl stop x-ui
  214. sleep 2
  215. check_status
  216. if [[ $? == 1 ]]; then
  217. LOGI "x-ui and xray stopped successfully"
  218. else
  219. LOGE "Panel stop failed, Probably because the stop time exceeds two seconds, Please check the log information later"
  220. fi
  221. fi
  222. if [[ $# == 0 ]]; then
  223. before_show_menu
  224. fi
  225. }
  226. restart() {
  227. systemctl restart x-ui
  228. sleep 2
  229. check_status
  230. if [[ $? == 0 ]]; then
  231. LOGI "x-ui and xray Restarted successfully"
  232. else
  233. LOGE "Panel restart failed, Probably because it takes longer than two seconds to start, Please check the log information later"
  234. fi
  235. if [[ $# == 0 ]]; then
  236. before_show_menu
  237. fi
  238. }
  239. status() {
  240. systemctl status x-ui -l
  241. if [[ $# == 0 ]]; then
  242. before_show_menu
  243. fi
  244. }
  245. enable() {
  246. systemctl enable x-ui
  247. if [[ $? == 0 ]]; then
  248. LOGI "x-ui Set to boot automatically on startup successfully"
  249. else
  250. LOGE "x-ui Failed to set Autostart"
  251. fi
  252. if [[ $# == 0 ]]; then
  253. before_show_menu
  254. fi
  255. }
  256. disable() {
  257. systemctl disable x-ui
  258. if [[ $? == 0 ]]; then
  259. LOGI "x-ui Autostart Cancelled successfully"
  260. else
  261. LOGE "x-ui Failed to cancel autostart"
  262. fi
  263. if [[ $# == 0 ]]; then
  264. before_show_menu
  265. fi
  266. }
  267. show_log() {
  268. journalctl -u x-ui.service -e --no-pager -f
  269. if [[ $# == 0 ]]; then
  270. before_show_menu
  271. fi
  272. }
  273. show_banlog() {
  274. if test -f "${iplimit_banned_log_path}"; then
  275. if [[ -s "${iplimit_banned_log_path}" ]]; then
  276. cat ${iplimit_banned_log_path}
  277. else
  278. echo -e "${red}Log file is empty.${plain}\n"
  279. fi
  280. else
  281. echo -e "${red}Log file not found. Please Install Fail2ban and IP Limit first.${plain}\n"
  282. fi
  283. }
  284. enable_bbr() {
  285. if grep -q "net.core.default_qdisc=fq" /etc/sysctl.conf && grep -q "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf; then
  286. echo -e "${green}BBR is already enabled!${plain}"
  287. exit 0
  288. fi
  289. # Check the OS and install necessary packages
  290. case "${release}" in
  291. ubuntu|debian)
  292. apt-get update && apt-get install -yqq --no-install-recommends ca-certificates
  293. ;;
  294. centos)
  295. yum -y update && yum -y install ca-certificates
  296. ;;
  297. fedora)
  298. dnf -y update && dnf -y install ca-certificates
  299. ;;
  300. *)
  301. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  302. exit 1
  303. ;;
  304. esac
  305. # Enable BBR
  306. echo "net.core.default_qdisc=fq" | tee -a /etc/sysctl.conf
  307. echo "net.ipv4.tcp_congestion_control=bbr" | tee -a /etc/sysctl.conf
  308. # Apply changes
  309. sysctl -p
  310. # Verify that BBR is enabled
  311. if [[ $(sysctl net.ipv4.tcp_congestion_control | awk '{print $3}') == "bbr" ]]; then
  312. echo -e "${green}BBR has been enabled successfully.${plain}"
  313. else
  314. echo -e "${red}Failed to enable BBR. Please check your system configuration.${plain}"
  315. fi
  316. }
  317. update_shell() {
  318. wget -O /usr/bin/x-ui -N --no-check-certificate https://github.com/MHSanaei/3x-ui/raw/main/x-ui.sh
  319. if [[ $? != 0 ]]; then
  320. echo ""
  321. LOGE "Failed to download script, Please check whether the machine can connect Github"
  322. before_show_menu
  323. else
  324. chmod +x /usr/bin/x-ui
  325. LOGI "Upgrade script succeeded, Please rerun the script" && exit 0
  326. fi
  327. }
  328. # 0: running, 1: not running, 2: not installed
  329. check_status() {
  330. if [[ ! -f /etc/systemd/system/x-ui.service ]]; then
  331. return 2
  332. fi
  333. temp=$(systemctl status x-ui | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
  334. if [[ "${temp}" == "running" ]]; then
  335. return 0
  336. else
  337. return 1
  338. fi
  339. }
  340. check_enabled() {
  341. temp=$(systemctl is-enabled x-ui)
  342. if [[ "${temp}" == "enabled" ]]; then
  343. return 0
  344. else
  345. return 1
  346. fi
  347. }
  348. check_uninstall() {
  349. check_status
  350. if [[ $? != 2 ]]; then
  351. echo ""
  352. LOGE "Panel installed, Please do not reinstall"
  353. if [[ $# == 0 ]]; then
  354. before_show_menu
  355. fi
  356. return 1
  357. else
  358. return 0
  359. fi
  360. }
  361. check_install() {
  362. check_status
  363. if [[ $? == 2 ]]; then
  364. echo ""
  365. LOGE "Please install the panel first"
  366. if [[ $# == 0 ]]; then
  367. before_show_menu
  368. fi
  369. return 1
  370. else
  371. return 0
  372. fi
  373. }
  374. show_status() {
  375. check_status
  376. case $? in
  377. 0)
  378. echo -e "Panel state: ${green}Running${plain}"
  379. show_enable_status
  380. ;;
  381. 1)
  382. echo -e "Panel state: ${yellow}Not Running${plain}"
  383. show_enable_status
  384. ;;
  385. 2)
  386. echo -e "Panel state: ${red}Not Installed${plain}"
  387. ;;
  388. esac
  389. show_xray_status
  390. }
  391. show_enable_status() {
  392. check_enabled
  393. if [[ $? == 0 ]]; then
  394. echo -e "Start automatically: ${green}Yes${plain}"
  395. else
  396. echo -e "Start automatically: ${red}No${plain}"
  397. fi
  398. }
  399. check_xray_status() {
  400. count=$(ps -ef | grep "xray-linux" | grep -v "grep" | wc -l)
  401. if [[ count -ne 0 ]]; then
  402. return 0
  403. else
  404. return 1
  405. fi
  406. }
  407. show_xray_status() {
  408. check_xray_status
  409. if [[ $? == 0 ]]; then
  410. echo -e "xray state: ${green}Running${plain}"
  411. else
  412. echo -e "xray state: ${red}Not Running${plain}"
  413. fi
  414. }
  415. open_ports() {
  416. if ! command -v ufw &>/dev/null; then
  417. echo "ufw firewall is not installed. Installing now..."
  418. apt-get update
  419. apt-get install -y ufw
  420. else
  421. echo "ufw firewall is already installed"
  422. fi
  423. # Check if the firewall is inactive
  424. if ufw status | grep -q "Status: active"; then
  425. echo "firewall is already active"
  426. else
  427. # Open the necessary ports
  428. ufw allow ssh
  429. ufw allow http
  430. ufw allow https
  431. ufw allow 2053/tcp
  432. # Enable the firewall
  433. ufw --force enable
  434. fi
  435. # Prompt the user to enter a list of ports
  436. read -p "Enter the ports you want to open (e.g. 80,443,2053 or range 400-500): " ports
  437. # Check if the input is valid
  438. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  439. 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
  440. exit 1
  441. fi
  442. # Open the specified ports using ufw
  443. IFS=',' read -ra PORT_LIST <<<"$ports"
  444. for port in "${PORT_LIST[@]}"; do
  445. if [[ $port == *-* ]]; then
  446. # Split the range into start and end ports
  447. start_port=$(echo $port | cut -d'-' -f1)
  448. end_port=$(echo $port | cut -d'-' -f2)
  449. # Loop through the range and open each port
  450. for ((i = start_port; i <= end_port; i++)); do
  451. ufw allow $i
  452. done
  453. else
  454. ufw allow "$port"
  455. fi
  456. done
  457. # Confirm that the ports are open
  458. ufw status | grep $ports
  459. }
  460. update_geo() {
  461. local defaultBinFolder="/usr/local/x-ui/bin"
  462. read -p "Please enter x-ui bin folder path. Leave blank for default. (Default: '${defaultBinFolder}')" binFolder
  463. binFolder=${binFolder:-${defaultBinFolder}}
  464. if [[ ! -d ${binFolder} ]]; then
  465. LOGE "Folder ${binFolder} not exists!"
  466. LOGI "making bin folder: ${binFolder}..."
  467. mkdir -p ${binFolder}
  468. fi
  469. systemctl stop x-ui
  470. cd ${binFolder}
  471. rm -f geoip.dat geosite.dat geoip_IR.dat geosite_IR.dat geoip_VN.dat geosite_VN.dat
  472. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat
  473. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat
  474. wget -O geoip_IR.dat -N https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geoip.dat
  475. wget -O geosite_IR.dat -N https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geosite.dat
  476. wget -O geoip_VN.dat https://github.com/vuong2023/vn-v2ray-rules/releases/latest/download/geoip.dat
  477. wget -O geosite_VN.dat https://github.com/vuong2023/vn-v2ray-rules/releases/latest/download/geosite.dat
  478. systemctl start x-ui
  479. echo -e "${green}Geosite.dat + Geoip.dat + geoip_IR.dat + geosite_IR.dat have been updated successfully in bin folder '${binfolder}'!${plain}"
  480. before_show_menu
  481. }
  482. install_acme() {
  483. cd ~
  484. LOGI "install acme..."
  485. curl https://get.acme.sh | sh
  486. if [ $? -ne 0 ]; then
  487. LOGE "install acme failed"
  488. return 1
  489. else
  490. LOGI "install acme succeed"
  491. fi
  492. return 0
  493. }
  494. ssl_cert_issue_main() {
  495. echo -e "${green}\t1.${plain} Get SSL"
  496. echo -e "${green}\t2.${plain} Revoke"
  497. echo -e "${green}\t3.${plain} Force Renew"
  498. echo -e "${green}\t0.${plain} Back to Main Menu"
  499. read -p "Choose an option: " choice
  500. case "$choice" in
  501. 0)
  502. show_menu ;;
  503. 1)
  504. ssl_cert_issue ;;
  505. 2)
  506. local domain=""
  507. read -p "Please enter your domain name to revoke the certificate: " domain
  508. ~/.acme.sh/acme.sh --revoke -d ${domain}
  509. LOGI "Certificate revoked"
  510. ;;
  511. 3)
  512. local domain=""
  513. read -p "Please enter your domain name to forcefully renew an SSL certificate: " domain
  514. ~/.acme.sh/acme.sh --renew -d ${domain} --force ;;
  515. *) echo "Invalid choice" ;;
  516. esac
  517. }
  518. ssl_cert_issue() {
  519. # check for acme.sh first
  520. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  521. echo "acme.sh could not be found. we will install it"
  522. install_acme
  523. if [ $? -ne 0 ]; then
  524. LOGE "install acme failed, please check logs"
  525. exit 1
  526. fi
  527. fi
  528. # install socat second
  529. case "${release}" in
  530. ubuntu|debian|armbian)
  531. apt update && apt install socat -y ;;
  532. centos)
  533. yum -y update && yum -y install socat ;;
  534. fedora)
  535. dnf -y update && dnf -y install socat ;;
  536. *)
  537. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  538. exit 1 ;;
  539. esac
  540. if [ $? -ne 0 ]; then
  541. LOGE "install socat failed, please check logs"
  542. exit 1
  543. else
  544. LOGI "install socat succeed..."
  545. fi
  546. # get the domain here,and we need verify it
  547. local domain=""
  548. read -p "Please enter your domain name:" domain
  549. LOGD "your domain is:${domain},check it..."
  550. # here we need to judge whether there exists cert already
  551. local currentCert=$(~/.acme.sh/acme.sh --list | tail -1 | awk '{print $1}')
  552. if [ ${currentCert} == ${domain} ]; then
  553. local certInfo=$(~/.acme.sh/acme.sh --list)
  554. LOGE "system already has certs here,can not issue again,current certs details:"
  555. LOGI "$certInfo"
  556. exit 1
  557. else
  558. LOGI "your domain is ready for issuing cert now..."
  559. fi
  560. # create a directory for install cert
  561. certPath="/root/cert/${domain}"
  562. if [ ! -d "$certPath" ]; then
  563. mkdir -p "$certPath"
  564. else
  565. rm -rf "$certPath"
  566. mkdir -p "$certPath"
  567. fi
  568. # get needed port here
  569. local WebPort=80
  570. read -p "please choose which port do you use,default will be 80 port:" WebPort
  571. if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
  572. LOGE "your input ${WebPort} is invalid,will use default port"
  573. fi
  574. LOGI "will use port:${WebPort} to issue certs,please make sure this port is open..."
  575. # NOTE:This should be handled by user
  576. # open the port and kill the occupied progress
  577. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  578. ~/.acme.sh/acme.sh --issue -d ${domain} --standalone --httpport ${WebPort}
  579. if [ $? -ne 0 ]; then
  580. LOGE "issue certs failed,please check logs"
  581. rm -rf ~/.acme.sh/${domain}
  582. exit 1
  583. else
  584. LOGE "issue certs succeed,installing certs..."
  585. fi
  586. # install cert
  587. ~/.acme.sh/acme.sh --installcert -d ${domain} \
  588. --key-file /root/cert/${domain}/privkey.pem \
  589. --fullchain-file /root/cert/${domain}/fullchain.pem
  590. if [ $? -ne 0 ]; then
  591. LOGE "install certs failed,exit"
  592. rm -rf ~/.acme.sh/${domain}
  593. exit 1
  594. else
  595. LOGI "install certs succeed,enable auto renew..."
  596. fi
  597. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  598. if [ $? -ne 0 ]; then
  599. LOGE "auto renew failed, certs details:"
  600. ls -lah cert/*
  601. chmod 755 $certPath/*
  602. exit 1
  603. else
  604. LOGI "auto renew succeed, certs details:"
  605. ls -lah cert/*
  606. chmod 755 $certPath/*
  607. fi
  608. }
  609. ssl_cert_issue_CF() {
  610. echo -E ""
  611. LOGD "******Instructions for use******"
  612. LOGI "This Acme script requires the following data:"
  613. LOGI "1.Cloudflare Registered e-mail"
  614. LOGI "2.Cloudflare Global API Key"
  615. LOGI "3.The domain name that has been resolved dns to the current server by Cloudflare"
  616. LOGI "4.The script applies for a certificate. The default installation path is /root/cert "
  617. confirm "Confirmed?[y/n]" "y"
  618. if [ $? -eq 0 ]; then
  619. # check for acme.sh first
  620. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  621. echo "acme.sh could not be found. we will install it"
  622. install_acme
  623. if [ $? -ne 0 ]; then
  624. LOGE "install acme failed, please check logs"
  625. exit 1
  626. fi
  627. fi
  628. CF_Domain=""
  629. CF_GlobalKey=""
  630. CF_AccountEmail=""
  631. certPath=/root/cert
  632. if [ ! -d "$certPath" ]; then
  633. mkdir $certPath
  634. else
  635. rm -rf $certPath
  636. mkdir $certPath
  637. fi
  638. LOGD "Please set a domain name:"
  639. read -p "Input your domain here:" CF_Domain
  640. LOGD "Your domain name is set to:${CF_Domain}"
  641. LOGD "Please set the API key:"
  642. read -p "Input your key here:" CF_GlobalKey
  643. LOGD "Your API key is:${CF_GlobalKey}"
  644. LOGD "Please set up registered email:"
  645. read -p "Input your email here:" CF_AccountEmail
  646. LOGD "Your registered email address is:${CF_AccountEmail}"
  647. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  648. if [ $? -ne 0 ]; then
  649. LOGE "Default CA, Lets'Encrypt fail, script exiting..."
  650. exit 1
  651. fi
  652. export CF_Key="${CF_GlobalKey}"
  653. export CF_Email=${CF_AccountEmail}
  654. ~/.acme.sh/acme.sh --issue --dns dns_cf -d ${CF_Domain} -d *.${CF_Domain} --log
  655. if [ $? -ne 0 ]; then
  656. LOGE "Certificate issuance failed, script exiting..."
  657. exit 1
  658. else
  659. LOGI "Certificate issued Successfully, Installing..."
  660. fi
  661. ~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} --ca-file /root/cert/ca.cer \
  662. --cert-file /root/cert/${CF_Domain}.cer --key-file /root/cert/${CF_Domain}.key \
  663. --fullchain-file /root/cert/fullchain.cer
  664. if [ $? -ne 0 ]; then
  665. LOGE "Certificate installation failed, script exiting..."
  666. exit 1
  667. else
  668. LOGI "Certificate installed Successfully,Turning on automatic updates..."
  669. fi
  670. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  671. if [ $? -ne 0 ]; then
  672. LOGE "Auto update setup Failed, script exiting..."
  673. ls -lah cert
  674. chmod 755 $certPath
  675. exit 1
  676. else
  677. LOGI "The certificate is installed and auto-renewal is turned on, Specific information is as follows"
  678. ls -lah cert
  679. chmod 755 $certPath
  680. fi
  681. else
  682. show_menu
  683. fi
  684. }
  685. warp_cloudflare() {
  686. echo -e "${green}\t1.${plain} Install WARP socks5 proxy"
  687. echo -e "${green}\t2.${plain} Account Type (free, plus, team)"
  688. echo -e "${green}\t3.${plain} Turn on/off WireProxy"
  689. echo -e "${green}\t4.${plain} Uninstall WARP"
  690. echo -e "${green}\t0.${plain} Back to Main Menu"
  691. read -p "Choose an option: " choice
  692. case "$choice" in
  693. 0)
  694. show_menu ;;
  695. 1)
  696. bash <(curl -sSL https://raw.githubusercontent.com/hamid-gh98/x-ui-scripts/main/install_warp_proxy.sh)
  697. ;;
  698. 2)
  699. warp a
  700. ;;
  701. 3)
  702. warp y
  703. ;;
  704. 4)
  705. warp u
  706. ;;
  707. *) echo "Invalid choice" ;;
  708. esac
  709. }
  710. run_speedtest() {
  711. # Check if Speedtest is already installed
  712. if ! command -v speedtest &> /dev/null; then
  713. # If not installed, install it
  714. local pkg_manager=""
  715. local speedtest_install_script=""
  716. if command -v dnf &> /dev/null; then
  717. pkg_manager="dnf"
  718. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  719. elif command -v yum &> /dev/null; then
  720. pkg_manager="yum"
  721. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  722. elif command -v apt-get &> /dev/null; then
  723. pkg_manager="apt-get"
  724. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  725. elif command -v apt &> /dev/null; then
  726. pkg_manager="apt"
  727. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  728. fi
  729. if [[ -z $pkg_manager ]]; then
  730. echo "Error: Package manager not found. You may need to install Speedtest manually."
  731. return 1
  732. else
  733. curl -s $speedtest_install_script | bash
  734. $pkg_manager install -y speedtest
  735. fi
  736. fi
  737. # Run Speedtest
  738. speedtest
  739. }
  740. create_iplimit_jails() {
  741. # Use default bantime if not passed => 5 minutes
  742. local bantime="${1:-5}"
  743. cat << EOF > /etc/fail2ban/jail.d/3x-ipl.conf
  744. [3x-ipl]
  745. enabled=true
  746. filter=3x-ipl
  747. action=3x-ipl
  748. logpath=${iplimit_log_path}
  749. maxretry=4
  750. findtime=60
  751. bantime=${bantime}m
  752. EOF
  753. cat << EOF > /etc/fail2ban/filter.d/3x-ipl.conf
  754. [Definition]
  755. datepattern = ^%%Y/%%m/%%d %%H:%%M:%%S
  756. failregex = \[LIMIT_IP\]\s*Email\s*=\s*<F-USER>.+</F-USER>\s*\|\|\s*SRC\s*=\s*<ADDR>
  757. ignoreregex =
  758. EOF
  759. cat << EOF > /etc/fail2ban/action.d/3x-ipl.conf
  760. [INCLUDES]
  761. before = iptables-common.conf
  762. [Definition]
  763. actionstart = <iptables> -N f2b-<name>
  764. <iptables> -A f2b-<name> -j <returntype>
  765. <iptables> -I <chain> -p <protocol> -j f2b-<name>
  766. actionstop = <iptables> -D <chain> -p <protocol> -j f2b-<name>
  767. <actionflush>
  768. <iptables> -X f2b-<name>
  769. actioncheck = <iptables> -n -L <chain> | grep -q 'f2b-<name>[ \t]'
  770. actionban = <iptables> -I f2b-<name> 1 -s <ip> -j <blocktype>
  771. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") BAN [Email] = <F-USER> [IP] = <ip> banned for <bantime> seconds." >> ${iplimit_banned_log_path}
  772. actionunban = <iptables> -D f2b-<name> -s <ip> -j <blocktype>
  773. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") UNBAN [Email] = <F-USER> [IP] = <ip> unbanned." >> ${iplimit_banned_log_path}
  774. [Init]
  775. EOF
  776. echo -e "${green}Created Ip Limit jail files with a bantime of ${bantime} minutes.${plain}"
  777. }
  778. iplimit_remove_conflicts() {
  779. local jail_files=(
  780. /etc/fail2ban/jail.conf
  781. /etc/fail2ban/jail.local
  782. )
  783. for file in "${jail_files[@]}"; do
  784. # Check for [3x-ipl] config in jail file then remove it
  785. if test -f "${file}" && grep -qw '3x-ipl' ${file}; then
  786. sed -i "/\[3x-ipl\]/,/^$/d" ${file}
  787. echo -e "${yellow}Removing conflicts of [3x-ipl] in jail (${file})!${plain}\n"
  788. fi
  789. done
  790. }
  791. iplimit_main() {
  792. echo -e "\n${green}\t1.${plain} Install Fail2ban and configure IP Limit"
  793. echo -e "${green}\t2.${plain} Change Ban Duration"
  794. echo -e "${green}\t3.${plain} Unban Everyone"
  795. echo -e "${green}\t4.${plain} Check Logs"
  796. echo -e "${green}\t5.${plain} fail2ban status"
  797. echo -e "${green}\t6.${plain} Uninstall IP Limit"
  798. echo -e "${green}\t0.${plain} Back to Main Menu"
  799. read -p "Choose an option: " choice
  800. case "$choice" in
  801. 0)
  802. show_menu ;;
  803. 1)
  804. confirm "Proceed with installation of Fail2ban & IP Limit?" "y"
  805. if [[ $? == 0 ]]; then
  806. install_iplimit
  807. else
  808. iplimit_main
  809. fi ;;
  810. 2)
  811. read -rp "Please enter new Ban Duration in Minutes [default 5]: " NUM
  812. if [[ $NUM =~ ^[0-9]+$ ]]; then
  813. create_iplimit_jails ${NUM}
  814. systemctl restart fail2ban
  815. else
  816. echo -e "${red}${NUM} is not a number! Please, try again.${plain}"
  817. fi
  818. iplimit_main ;;
  819. 3)
  820. confirm "Proceed with Unbanning everyone from IP Limit jail?" "y"
  821. if [[ $? == 0 ]]; then
  822. fail2ban-client reload --restart --unban 3x-ipl
  823. echo -e "${green}All users Unbanned successfully.${plain}"
  824. iplimit_main
  825. else
  826. echo -e "${yellow}Cancelled.${plain}"
  827. fi
  828. iplimit_main ;;
  829. 4)
  830. show_banlog
  831. ;;
  832. 5)
  833. service fail2ban status
  834. ;;
  835. 6)
  836. remove_iplimit ;;
  837. *) echo "Invalid choice" ;;
  838. esac
  839. }
  840. install_iplimit() {
  841. if ! command -v fail2ban-client &>/dev/null; then
  842. echo -e "${green}Fail2ban is not installed. Installing now...!${plain}\n"
  843. # Check the OS and install necessary packages
  844. case "${release}" in
  845. ubuntu|debian)
  846. apt update && apt install fail2ban -y ;;
  847. centos)
  848. yum -y update && yum -y install fail2ban ;;
  849. fedora)
  850. dnf -y update && dnf -y install fail2ban ;;
  851. *)
  852. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  853. exit 1 ;;
  854. esac
  855. echo -e "${green}Fail2ban installed successfully!${plain}\n"
  856. else
  857. echo -e "${yellow}Fail2ban is already installed.${plain}\n"
  858. fi
  859. echo -e "${green}Configuring IP Limit...${plain}\n"
  860. # make sure there's no conflict for jail files
  861. iplimit_remove_conflicts
  862. # Check if log file exists
  863. if ! test -f "${iplimit_banned_log_path}"; then
  864. touch ${iplimit_banned_log_path}
  865. fi
  866. # Check if service log file exists so fail2ban won't return error
  867. if ! test -f "${iplimit_log_path}"; then
  868. touch ${iplimit_log_path}
  869. fi
  870. # Create the iplimit jail files
  871. # we didn't pass the bantime here to use the default value
  872. create_iplimit_jails
  873. # Launching fail2ban
  874. if ! systemctl is-active --quiet fail2ban; then
  875. systemctl start fail2ban
  876. else
  877. systemctl restart fail2ban
  878. fi
  879. systemctl enable fail2ban
  880. echo -e "${green}IP Limit installed and configured successfully!${plain}\n"
  881. before_show_menu
  882. }
  883. remove_iplimit(){
  884. echo -e "${green}\t1.${plain} Only remove IP Limit configurations"
  885. echo -e "${green}\t2.${plain} Uninstall Fail2ban and IP Limit"
  886. echo -e "${green}\t0.${plain} Abort"
  887. read -p "Choose an option: " num
  888. case "$num" in
  889. 1)
  890. rm -f /etc/fail2ban/filter.d/3x-ipl.conf
  891. rm -f /etc/fail2ban/action.d/3x-ipl.conf
  892. rm -f /etc/fail2ban/jail.d/3x-ipl.conf
  893. systemctl restart fail2ban
  894. echo -e "${green}IP Limit removed successfully!${plain}\n"
  895. before_show_menu ;;
  896. 2)
  897. rm -rf /etc/fail2ban
  898. systemctl stop fail2ban
  899. case "${release}" in
  900. ubuntu|debian)
  901. apt-get purge fail2ban -y;;
  902. centos)
  903. yum remove fail2ban -y;;
  904. fedora)
  905. dnf remove fail2ban -y;;
  906. *)
  907. echo -e "${red}Unsupported operating system. Please uninstall Fail2ban manually.${plain}\n"
  908. exit 1 ;;
  909. esac
  910. echo -e "${green}Fail2ban and IP Limit removed successfully!${plain}\n"
  911. before_show_menu ;;
  912. 0)
  913. echo -e "${yellow}Cancelled.${plain}\n"
  914. iplimit_main ;;
  915. *)
  916. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  917. remove_iplimit ;;
  918. esac
  919. }
  920. show_usage() {
  921. echo "x-ui control menu usages: "
  922. echo "------------------------------------------"
  923. echo -e "x-ui - Enter control menu"
  924. echo -e "x-ui start - Start x-ui "
  925. echo -e "x-ui stop - Stop x-ui "
  926. echo -e "x-ui restart - Restart x-ui "
  927. echo -e "x-ui status - Show x-ui status"
  928. echo -e "x-ui enable - Enable x-ui on system startup"
  929. echo -e "x-ui disable - Disable x-ui on system startup"
  930. echo -e "x-ui log - Check x-ui logs"
  931. echo -e "x-ui banlog - Check Fail2ban ban logs"
  932. echo -e "x-ui update - Update x-ui "
  933. echo -e "x-ui install - Install x-ui "
  934. echo -e "x-ui uninstall - Uninstall x-ui "
  935. echo "------------------------------------------"
  936. }
  937. show_menu() {
  938. echo -e "
  939. ${green}3X-ui Panel Management Script${plain}
  940. ${green}0.${plain} Exit Script
  941. ————————————————
  942. ${green}1.${plain} Install x-ui
  943. ${green}2.${plain} Update x-ui
  944. ${green}3.${plain} Uninstall x-ui
  945. ————————————————
  946. ${green}4.${plain} Reset Username & Password & Secret Token
  947. ${green}5.${plain} Reset Panel Settings
  948. ${green}6.${plain} Change Panel Port
  949. ${green}7.${plain} View Current Panel Settings
  950. ————————————————
  951. ${green}8.${plain} Start x-ui
  952. ${green}9.${plain} Stop x-ui
  953. ${green}10.${plain} Restart x-ui
  954. ${green}11.${plain} Check x-ui Status
  955. ${green}12.${plain} Check x-ui Logs
  956. ————————————————
  957. ${green}13.${plain} Enable x-ui On System Startup
  958. ${green}14.${plain} Disable x-ui On System Startup
  959. ————————————————
  960. ${green}15.${plain} SSL Certificate Management
  961. ${green}16.${plain} Cloudflare SSL Certificate
  962. ${green}17.${plain} IP Limit Management
  963. ${green}18.${plain} WARP Management
  964. ————————————————
  965. ${green}19.${plain} Enable BBR
  966. ${green}20.${plain} Update Geo Files
  967. ${green}21.${plain} Active Firewall and open ports
  968. ${green}22.${plain} Speedtest by Ookla
  969. "
  970. show_status
  971. echo && read -p "Please enter your selection [0-22]: " num
  972. case "${num}" in
  973. 0)
  974. exit 0
  975. ;;
  976. 1)
  977. check_uninstall && install
  978. ;;
  979. 2)
  980. check_install && update
  981. ;;
  982. 3)
  983. check_install && uninstall
  984. ;;
  985. 4)
  986. check_install && reset_user
  987. ;;
  988. 5)
  989. check_install && reset_config
  990. ;;
  991. 6)
  992. check_install && set_port
  993. ;;
  994. 7)
  995. check_install && check_config
  996. ;;
  997. 8)
  998. check_install && start
  999. ;;
  1000. 9)
  1001. check_install && stop
  1002. ;;
  1003. 10)
  1004. check_install && restart
  1005. ;;
  1006. 11)
  1007. check_install && status
  1008. ;;
  1009. 12)
  1010. check_install && show_log
  1011. ;;
  1012. 13)
  1013. check_install && enable
  1014. ;;
  1015. 14)
  1016. check_install && disable
  1017. ;;
  1018. 15)
  1019. ssl_cert_issue_main
  1020. ;;
  1021. 16)
  1022. ssl_cert_issue_CF
  1023. ;;
  1024. 17)
  1025. iplimit_main
  1026. ;;
  1027. 18)
  1028. warp_cloudflare
  1029. ;;
  1030. 19)
  1031. enable_bbr
  1032. ;;
  1033. 20)
  1034. update_geo
  1035. ;;
  1036. 21)
  1037. open_ports
  1038. ;;
  1039. 22)
  1040. run_speedtest
  1041. ;;
  1042. *)
  1043. LOGE "Please enter the correct number [0-22]"
  1044. ;;
  1045. esac
  1046. }
  1047. if [[ $# > 0 ]]; then
  1048. case $1 in
  1049. "start")
  1050. check_install 0 && start 0
  1051. ;;
  1052. "stop")
  1053. check_install 0 && stop 0
  1054. ;;
  1055. "restart")
  1056. check_install 0 && restart 0
  1057. ;;
  1058. "status")
  1059. check_install 0 && status 0
  1060. ;;
  1061. "enable")
  1062. check_install 0 && enable 0
  1063. ;;
  1064. "disable")
  1065. check_install 0 && disable 0
  1066. ;;
  1067. "log")
  1068. check_install 0 && show_log 0
  1069. ;;
  1070. "banlog")
  1071. check_install 0 && show_banlog 0
  1072. ;;
  1073. "update")
  1074. check_install 0 && update 0
  1075. ;;
  1076. "install")
  1077. check_uninstall 0 && install 0
  1078. ;;
  1079. "uninstall")
  1080. check_install 0 && uninstall 0
  1081. ;;
  1082. *) show_usage ;;
  1083. esac
  1084. else
  1085. show_menu
  1086. fi