x-ui.sh 43 KB

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