x-ui.sh 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  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 "^VERSION_ID" /etc/os-release | cut -d '=' -f2 | tr -d '"')
  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}" == "amzn" ]]; then
  55. if [[ ${os_version} != "2023" ]]; then
  56. echo -e "${red} Please use Amazon Linux 2023!${plain}\n" && exit 1
  57. fi
  58. elif [[ "${release}" == "debian" ]]; then
  59. if [[ ${os_version} -lt 11 ]]; then
  60. echo -e "${red} Please use Debian 11 or higher ${plain}\n" && exit 1
  61. fi
  62. elif [[ "${release}" == "almalinux" ]]; then
  63. if [[ ${os_version} -lt 9 ]]; then
  64. echo -e "${red} Please use AlmaLinux 9 or higher ${plain}\n" && exit 1
  65. fi
  66. elif [[ "${release}" == "rocky" ]]; then
  67. if [[ ${os_version} -lt 9 ]]; then
  68. echo -e "${red} Please use Rocky Linux 9 or higher ${plain}\n" && exit 1
  69. fi
  70. elif [[ "${release}" == "oracle" ]]; then
  71. if [[ ${os_version} -lt 8 ]]; then
  72. echo -e "${red} Please use Oracle Linux 8 or higher ${plain}\n" && exit 1
  73. fi
  74. else
  75. echo -e "${red}Your operating system is not supported by this script.${plain}\n"
  76. echo "Please ensure you are using one of the following supported operating systems:"
  77. echo "- Ubuntu 20.04+"
  78. echo "- Debian 11+"
  79. echo "- CentOS 8+"
  80. echo "- Fedora 36+"
  81. echo "- Arch Linux"
  82. echo "- Parch Linux"
  83. echo "- Manjaro"
  84. echo "- Armbian"
  85. echo "- AlmaLinux 9+"
  86. echo "- Rocky Linux 9+"
  87. echo "- Oracle Linux 8+"
  88. echo "- OpenSUSE Tumbleweed"
  89. echo "- Amazon Linux 2023"
  90. exit 1
  91. fi
  92. # Declare Variables
  93. log_folder="${XUI_LOG_FOLDER:=/var/log}"
  94. iplimit_log_path="${log_folder}/3xipl.log"
  95. iplimit_banned_log_path="${log_folder}/3xipl-banned.log"
  96. confirm() {
  97. if [[ $# > 1 ]]; then
  98. echo && read -p "$1 [Default $2]: " temp
  99. if [[ "${temp}" == "" ]]; then
  100. temp=$2
  101. fi
  102. else
  103. read -p "$1 [y/n]: " temp
  104. fi
  105. if [[ "${temp}" == "y" || "${temp}" == "Y" ]]; then
  106. return 0
  107. else
  108. return 1
  109. fi
  110. }
  111. confirm_restart() {
  112. confirm "Restart the panel, Attention: Restarting the panel will also restart xray" "y"
  113. if [[ $? == 0 ]]; then
  114. restart
  115. else
  116. show_menu
  117. fi
  118. }
  119. before_show_menu() {
  120. echo && echo -n -e "${yellow}Press enter to return to the main menu: ${plain}" && read temp
  121. show_menu
  122. }
  123. install() {
  124. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  125. if [[ $? == 0 ]]; then
  126. if [[ $# == 0 ]]; then
  127. start
  128. else
  129. start 0
  130. fi
  131. fi
  132. }
  133. update() {
  134. confirm "This function will forcefully reinstall the latest version, and the data will not be lost. Do you want to continue?" "y"
  135. if [[ $? != 0 ]]; then
  136. LOGE "Cancelled"
  137. if [[ $# == 0 ]]; then
  138. before_show_menu
  139. fi
  140. return 0
  141. fi
  142. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  143. if [[ $? == 0 ]]; then
  144. LOGI "Update is complete, Panel has automatically restarted "
  145. exit 0
  146. fi
  147. }
  148. update_menu() {
  149. echo -e "${yellow}Updating Menu${plain}"
  150. confirm "This function will update the menu to the latest changes." "y"
  151. if [[ $? != 0 ]]; then
  152. LOGE "Cancelled"
  153. if [[ $# == 0 ]]; then
  154. before_show_menu
  155. fi
  156. return 0
  157. fi
  158. wget --no-check-certificate -O /usr/bin/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.sh
  159. chmod +x /usr/local/x-ui/x-ui.sh
  160. chmod +x /usr/bin/x-ui
  161. if [[ $? == 0 ]]; then
  162. echo -e "${green}Update successful. The panel has automatically restarted.${plain}"
  163. exit 0
  164. else
  165. echo -e "${red}Failed to update the menu.${plain}"
  166. return 1
  167. fi
  168. }
  169. custom_version() {
  170. echo "Enter the panel version (like 2.0.0):"
  171. read panel_version
  172. if [ -z "$panel_version" ]; then
  173. echo "Panel version cannot be empty. Exiting."
  174. exit 1
  175. fi
  176. download_link="https://raw.githubusercontent.com/mhsanaei/3x-ui/master/install.sh"
  177. # Use the entered panel version in the download link
  178. install_command="bash <(curl -Ls $download_link) v$panel_version"
  179. echo "Downloading and installing panel version $panel_version..."
  180. eval $install_command
  181. }
  182. # Function to handle the deletion of the script file
  183. delete_script() {
  184. rm "$0" # Remove the script file itself
  185. exit 1
  186. }
  187. uninstall() {
  188. confirm "Are you sure you want to uninstall the panel? xray will also uninstalled!" "n"
  189. if [[ $? != 0 ]]; then
  190. if [[ $# == 0 ]]; then
  191. show_menu
  192. fi
  193. return 0
  194. fi
  195. systemctl stop x-ui
  196. systemctl disable x-ui
  197. rm /etc/systemd/system/x-ui.service -f
  198. systemctl daemon-reload
  199. systemctl reset-failed
  200. rm /etc/x-ui/ -rf
  201. rm /usr/local/x-ui/ -rf
  202. echo ""
  203. echo -e "Uninstalled Successfully.\n"
  204. echo "If you need to install this panel again, you can use below command:"
  205. echo -e "${green}bash <(curl -Ls https://raw.githubusercontent.com/mhsanaei/3x-ui/master/install.sh)${plain}"
  206. echo ""
  207. # Trap the SIGTERM signal
  208. trap delete_script SIGTERM
  209. delete_script
  210. }
  211. reset_user() {
  212. confirm "Are you sure to reset the username and password of the panel?" "n"
  213. if [[ $? != 0 ]]; then
  214. if [[ $# == 0 ]]; then
  215. show_menu
  216. fi
  217. return 0
  218. fi
  219. read -rp "Please set the login username [default is a random username]: " config_account
  220. [[ -z $config_account ]] && config_account=$(date +%s%N | md5sum | cut -c 1-8)
  221. read -rp "Please set the login password [default is a random password]: " config_password
  222. [[ -z $config_password ]] && config_password=$(date +%s%N | md5sum | cut -c 1-8)
  223. /usr/local/x-ui/x-ui setting -username ${config_account} -password ${config_password} >/dev/null 2>&1
  224. /usr/local/x-ui/x-ui setting -remove_secret >/dev/null 2>&1
  225. echo -e "Panel login username has been reset to: ${green} ${config_account} ${plain}"
  226. echo -e "Panel login password has been reset to: ${green} ${config_password} ${plain}"
  227. echo -e "${yellow} Panel login secret token disabled ${plain}"
  228. echo -e "${green} Please use the new login username and password to access the X-UI panel. Also remember them! ${plain}"
  229. confirm_restart
  230. }
  231. gen_random_string() {
  232. local length="$1"
  233. local random_string=$(LC_ALL=C tr -dc 'a-zA-Z0-9' </dev/urandom | fold -w "$length" | head -n 1)
  234. echo "$random_string"
  235. }
  236. reset_webbasepath() {
  237. echo -e "${yellow}Resetting Web Base Path${plain}"
  238. # Prompt user to set a new web base path
  239. read -rp "Please set the new web base path [press 'y' for a random path]: " config_webBasePath
  240. if [[ $config_webBasePath == "y" ]]; then
  241. config_webBasePath=$(gen_random_string 10)
  242. fi
  243. # Apply the new web base path setting
  244. /usr/local/x-ui/x-ui setting -webBasePath "${config_webBasePath}" >/dev/null 2>&1
  245. systemctl restart x-ui
  246. # Display confirmation message
  247. echo -e "Web base path has been reset to: ${green}${config_webBasePath}${plain}"
  248. echo -e "${green}Please use the new web base path to access the panel.${plain}"
  249. }
  250. reset_config() {
  251. confirm "Are you sure you want to reset all panel settings, Account data will not be lost, Username and password will not change" "n"
  252. if [[ $? != 0 ]]; then
  253. if [[ $# == 0 ]]; then
  254. show_menu
  255. fi
  256. return 0
  257. fi
  258. /usr/local/x-ui/x-ui setting -reset
  259. 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"
  260. confirm_restart
  261. }
  262. check_config() {
  263. info=$(/usr/local/x-ui/x-ui setting -show true)
  264. if [[ $? != 0 ]]; then
  265. LOGE "get current settings error, please check logs"
  266. show_menu
  267. fi
  268. LOGI "${info}"
  269. }
  270. set_port() {
  271. echo && echo -n -e "Enter port number[1-65535]: " && read port
  272. if [[ -z "${port}" ]]; then
  273. LOGD "Cancelled"
  274. before_show_menu
  275. else
  276. /usr/local/x-ui/x-ui setting -port ${port}
  277. echo -e "The port is set, Please restart the panel now, and use the new port ${green}${port}${plain} to access web panel"
  278. confirm_restart
  279. fi
  280. }
  281. start() {
  282. check_status
  283. if [[ $? == 0 ]]; then
  284. echo ""
  285. LOGI "Panel is running, No need to start again, If you need to restart, please select restart"
  286. else
  287. systemctl start x-ui
  288. sleep 2
  289. check_status
  290. if [[ $? == 0 ]]; then
  291. LOGI "x-ui Started Successfully"
  292. else
  293. LOGE "panel Failed to start, Probably because it takes longer than two seconds to start, Please check the log information later"
  294. fi
  295. fi
  296. if [[ $# == 0 ]]; then
  297. before_show_menu
  298. fi
  299. }
  300. stop() {
  301. check_status
  302. if [[ $? == 1 ]]; then
  303. echo ""
  304. LOGI "Panel stopped, No need to stop again!"
  305. else
  306. systemctl stop x-ui
  307. sleep 2
  308. check_status
  309. if [[ $? == 1 ]]; then
  310. LOGI "x-ui and xray stopped successfully"
  311. else
  312. LOGE "Panel stop failed, Probably because the stop time exceeds two seconds, Please check the log information later"
  313. fi
  314. fi
  315. if [[ $# == 0 ]]; then
  316. before_show_menu
  317. fi
  318. }
  319. restart() {
  320. systemctl restart x-ui
  321. sleep 2
  322. check_status
  323. if [[ $? == 0 ]]; then
  324. LOGI "x-ui and xray Restarted successfully"
  325. else
  326. LOGE "Panel restart failed, Probably because it takes longer than two seconds to start, Please check the log information later"
  327. fi
  328. if [[ $# == 0 ]]; then
  329. before_show_menu
  330. fi
  331. }
  332. status() {
  333. systemctl status x-ui -l
  334. if [[ $# == 0 ]]; then
  335. before_show_menu
  336. fi
  337. }
  338. enable() {
  339. systemctl enable x-ui
  340. if [[ $? == 0 ]]; then
  341. LOGI "x-ui Set to boot automatically on startup successfully"
  342. else
  343. LOGE "x-ui Failed to set Autostart"
  344. fi
  345. if [[ $# == 0 ]]; then
  346. before_show_menu
  347. fi
  348. }
  349. disable() {
  350. systemctl disable x-ui
  351. if [[ $? == 0 ]]; then
  352. LOGI "x-ui Autostart Cancelled successfully"
  353. else
  354. LOGE "x-ui Failed to cancel autostart"
  355. fi
  356. if [[ $# == 0 ]]; then
  357. before_show_menu
  358. fi
  359. }
  360. show_log() {
  361. journalctl -u x-ui.service -e --no-pager -f
  362. if [[ $# == 0 ]]; then
  363. before_show_menu
  364. fi
  365. }
  366. show_banlog() {
  367. if test -f "${iplimit_banned_log_path}"; then
  368. if [[ -s "${iplimit_banned_log_path}" ]]; then
  369. cat ${iplimit_banned_log_path}
  370. else
  371. echo -e "${red}Log file is empty.${plain}\n"
  372. fi
  373. else
  374. echo -e "${red}Log file not found. Please Install Fail2ban and IP Limit first.${plain}\n"
  375. fi
  376. }
  377. bbr_menu() {
  378. echo -e "${green}\t1.${plain} Enable BBR"
  379. echo -e "${green}\t2.${plain} Disable BBR"
  380. echo -e "${green}\t0.${plain} Back to Main Menu"
  381. read -p "Choose an option: " choice
  382. case "$choice" in
  383. 0)
  384. show_menu
  385. ;;
  386. 1)
  387. enable_bbr
  388. ;;
  389. 2)
  390. disable_bbr
  391. ;;
  392. *) echo "Invalid choice" ;;
  393. esac
  394. }
  395. disable_bbr() {
  396. if ! grep -q "net.core.default_qdisc=fq" /etc/sysctl.conf || ! grep -q "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf; then
  397. echo -e "${yellow}BBR is not currently enabled.${plain}"
  398. exit 0
  399. fi
  400. # Replace BBR with CUBIC configurations
  401. sed -i 's/net.core.default_qdisc=fq/net.core.default_qdisc=pfifo_fast/' /etc/sysctl.conf
  402. sed -i 's/net.ipv4.tcp_congestion_control=bbr/net.ipv4.tcp_congestion_control=cubic/' /etc/sysctl.conf
  403. # Apply changes
  404. sysctl -p
  405. # Verify that BBR is replaced with CUBIC
  406. if [[ $(sysctl net.ipv4.tcp_congestion_control | awk '{print $3}') == "cubic" ]]; then
  407. echo -e "${green}BBR has been replaced with CUBIC successfully.${plain}"
  408. else
  409. echo -e "${red}Failed to replace BBR with CUBIC. Please check your system configuration.${plain}"
  410. fi
  411. }
  412. enable_bbr() {
  413. if grep -q "net.core.default_qdisc=fq" /etc/sysctl.conf && grep -q "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf; then
  414. echo -e "${green}BBR is already enabled!${plain}"
  415. exit 0
  416. fi
  417. # Check the OS and install necessary packages
  418. case "${release}" in
  419. ubuntu | debian | armbian)
  420. apt-get update && apt-get install -yqq --no-install-recommends ca-certificates
  421. ;;
  422. centos | almalinux | rocky | oracle)
  423. yum -y update && yum -y install ca-certificates
  424. ;;
  425. fedora)
  426. dnf -y update && dnf -y install ca-certificates
  427. ;;
  428. arch | manjaro | parch)
  429. pacman -Sy --noconfirm ca-certificates
  430. ;;
  431. *)
  432. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  433. exit 1
  434. ;;
  435. esac
  436. # Enable BBR
  437. echo "net.core.default_qdisc=fq" | tee -a /etc/sysctl.conf
  438. echo "net.ipv4.tcp_congestion_control=bbr" | tee -a /etc/sysctl.conf
  439. # Apply changes
  440. sysctl -p
  441. # Verify that BBR is enabled
  442. if [[ $(sysctl net.ipv4.tcp_congestion_control | awk '{print $3}') == "bbr" ]]; then
  443. echo -e "${green}BBR has been enabled successfully.${plain}"
  444. else
  445. echo -e "${red}Failed to enable BBR. Please check your system configuration.${plain}"
  446. fi
  447. }
  448. update_shell() {
  449. wget -O /usr/bin/x-ui -N --no-check-certificate https://github.com/MHSanaei/3x-ui/raw/main/x-ui.sh
  450. if [[ $? != 0 ]]; then
  451. echo ""
  452. LOGE "Failed to download script, Please check whether the machine can connect Github"
  453. before_show_menu
  454. else
  455. chmod +x /usr/bin/x-ui
  456. LOGI "Upgrade script succeeded, Please rerun the script" && exit 0
  457. fi
  458. }
  459. # 0: running, 1: not running, 2: not installed
  460. check_status() {
  461. if [[ ! -f /etc/systemd/system/x-ui.service ]]; then
  462. return 2
  463. fi
  464. temp=$(systemctl status x-ui | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
  465. if [[ "${temp}" == "running" ]]; then
  466. return 0
  467. else
  468. return 1
  469. fi
  470. }
  471. check_enabled() {
  472. temp=$(systemctl is-enabled x-ui)
  473. if [[ "${temp}" == "enabled" ]]; then
  474. return 0
  475. else
  476. return 1
  477. fi
  478. }
  479. check_uninstall() {
  480. check_status
  481. if [[ $? != 2 ]]; then
  482. echo ""
  483. LOGE "Panel installed, Please do not reinstall"
  484. if [[ $# == 0 ]]; then
  485. before_show_menu
  486. fi
  487. return 1
  488. else
  489. return 0
  490. fi
  491. }
  492. check_install() {
  493. check_status
  494. if [[ $? == 2 ]]; then
  495. echo ""
  496. LOGE "Please install the panel first"
  497. if [[ $# == 0 ]]; then
  498. before_show_menu
  499. fi
  500. return 1
  501. else
  502. return 0
  503. fi
  504. }
  505. show_status() {
  506. check_status
  507. case $? in
  508. 0)
  509. echo -e "Panel state: ${green}Running${plain}"
  510. show_enable_status
  511. ;;
  512. 1)
  513. echo -e "Panel state: ${yellow}Not Running${plain}"
  514. show_enable_status
  515. ;;
  516. 2)
  517. echo -e "Panel state: ${red}Not Installed${plain}"
  518. ;;
  519. esac
  520. show_xray_status
  521. }
  522. show_enable_status() {
  523. check_enabled
  524. if [[ $? == 0 ]]; then
  525. echo -e "Start automatically: ${green}Yes${plain}"
  526. else
  527. echo -e "Start automatically: ${red}No${plain}"
  528. fi
  529. }
  530. check_xray_status() {
  531. count=$(ps -ef | grep "xray-linux" | grep -v "grep" | wc -l)
  532. if [[ count -ne 0 ]]; then
  533. return 0
  534. else
  535. return 1
  536. fi
  537. }
  538. show_xray_status() {
  539. check_xray_status
  540. if [[ $? == 0 ]]; then
  541. echo -e "xray state: ${green}Running${plain}"
  542. else
  543. echo -e "xray state: ${red}Not Running${plain}"
  544. fi
  545. }
  546. firewall_menu() {
  547. echo -e "${green}\t1.${plain} Install Firewall & open ports"
  548. echo -e "${green}\t2.${plain} Allowed List"
  549. echo -e "${green}\t3.${plain} Delete Ports from List"
  550. echo -e "${green}\t4.${plain} Disable Firewall"
  551. echo -e "${green}\t0.${plain} Back to Main Menu"
  552. read -p "Choose an option: " choice
  553. case "$choice" in
  554. 0)
  555. show_menu
  556. ;;
  557. 1)
  558. open_ports
  559. ;;
  560. 2)
  561. sudo ufw status
  562. ;;
  563. 3)
  564. delete_ports
  565. ;;
  566. 4)
  567. sudo ufw disable
  568. ;;
  569. *) echo "Invalid choice" ;;
  570. esac
  571. }
  572. open_ports() {
  573. if ! command -v ufw &>/dev/null; then
  574. echo "ufw firewall is not installed. Installing now..."
  575. apt-get update
  576. apt-get install -y ufw
  577. else
  578. echo "ufw firewall is already installed"
  579. fi
  580. # Check if the firewall is inactive
  581. if ufw status | grep -q "Status: active"; then
  582. echo "Firewall is already active"
  583. else
  584. echo "Activating firewall..."
  585. # Open the necessary ports
  586. ufw allow ssh
  587. ufw allow http
  588. ufw allow https
  589. ufw allow 2053/tcp
  590. # Enable the firewall
  591. ufw --force enable
  592. fi
  593. # Prompt the user to enter a list of ports
  594. read -p "Enter the ports you want to open (e.g. 80,443,2053 or range 400-500): " ports
  595. # Check if the input is valid
  596. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  597. 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
  598. exit 1
  599. fi
  600. # Open the specified ports using ufw
  601. IFS=',' read -ra PORT_LIST <<<"$ports"
  602. for port in "${PORT_LIST[@]}"; do
  603. if [[ $port == *-* ]]; then
  604. # Split the range into start and end ports
  605. start_port=$(echo $port | cut -d'-' -f1)
  606. end_port=$(echo $port | cut -d'-' -f2)
  607. ufw allow $start_port:$end_port/tcp
  608. ufw allow $start_port:$end_port/udp
  609. else
  610. ufw allow "$port"
  611. fi
  612. done
  613. # Confirm that the ports are open
  614. echo "The following ports are now open:"
  615. ufw status | grep "ALLOW" | grep -Eo "[0-9]+(/[a-z]+)?"
  616. echo "Firewall status:"
  617. ufw status verbose
  618. }
  619. delete_ports() {
  620. # Prompt the user to enter the ports they want to delete
  621. read -p "Enter the ports you want to delete (e.g. 80,443,2053 or range 400-500): " ports
  622. # Check if the input is valid
  623. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  624. 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
  625. exit 1
  626. fi
  627. # Delete the specified ports using ufw
  628. IFS=',' read -ra PORT_LIST <<<"$ports"
  629. for port in "${PORT_LIST[@]}"; do
  630. if [[ $port == *-* ]]; then
  631. # Split the range into start and end ports
  632. start_port=$(echo $port | cut -d'-' -f1)
  633. end_port=$(echo $port | cut -d'-' -f2)
  634. # Delete the port range
  635. ufw delete allow $start_port:$end_port/tcp
  636. ufw delete allow $start_port:$end_port/udp
  637. else
  638. ufw delete allow "$port"
  639. fi
  640. done
  641. # Confirm that the ports are deleted
  642. echo "Deleted the specified ports:"
  643. for port in "${PORT_LIST[@]}"; do
  644. if [[ $port == *-* ]]; then
  645. start_port=$(echo $port | cut -d'-' -f1)
  646. end_port=$(echo $port | cut -d'-' -f2)
  647. # Check if the port range has been successfully deleted
  648. (ufw status | grep -q "$start_port:$end_port") || echo "$start_port-$end_port"
  649. else
  650. # Check if the individual port has been successfully deleted
  651. (ufw status | grep -q "$port") || echo "$port"
  652. fi
  653. done
  654. }
  655. update_geo() {
  656. local defaultBinFolder="/usr/local/x-ui/bin"
  657. read -p "Please enter x-ui bin folder path. Leave blank for default. (Default: '${defaultBinFolder}')" binFolder
  658. binFolder=${binFolder:-${defaultBinFolder}}
  659. if [[ ! -d ${binFolder} ]]; then
  660. LOGE "Folder ${binFolder} not exists!"
  661. LOGI "making bin folder: ${binFolder}..."
  662. mkdir -p ${binFolder}
  663. fi
  664. systemctl stop x-ui
  665. cd ${binFolder}
  666. rm -f geoip.dat geosite.dat geoip_IR.dat geosite_IR.dat geoip_VN.dat geosite_VN.dat
  667. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat
  668. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat
  669. wget -O geoip_IR.dat -N https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geoip.dat
  670. wget -O geosite_IR.dat -N https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geosite.dat
  671. wget -O geoip_VN.dat https://github.com/vuong2023/vn-v2ray-rules/releases/latest/download/geoip.dat
  672. wget -O geosite_VN.dat https://github.com/vuong2023/vn-v2ray-rules/releases/latest/download/geosite.dat
  673. systemctl start x-ui
  674. echo -e "${green}Geosite.dat + Geoip.dat + geoip_IR.dat + geosite_IR.dat have been updated successfully in bin folder '${binfolder}'!${plain}"
  675. before_show_menu
  676. }
  677. install_acme() {
  678. cd ~
  679. LOGI "install acme..."
  680. curl https://get.acme.sh | sh
  681. if [ $? -ne 0 ]; then
  682. LOGE "install acme failed"
  683. return 1
  684. else
  685. LOGI "install acme succeed"
  686. fi
  687. return 0
  688. }
  689. ssl_cert_issue_main() {
  690. echo -e "${green}\t1.${plain} Get SSL"
  691. echo -e "${green}\t2.${plain} Revoke"
  692. echo -e "${green}\t3.${plain} Force Renew"
  693. echo -e "${green}\t0.${plain} Back to Main Menu"
  694. read -p "Choose an option: " choice
  695. case "$choice" in
  696. 0)
  697. show_menu
  698. ;;
  699. 1)
  700. ssl_cert_issue
  701. ;;
  702. 2)
  703. local domain=""
  704. read -p "Please enter your domain name to revoke the certificate: " domain
  705. ~/.acme.sh/acme.sh --revoke -d ${domain}
  706. LOGI "Certificate revoked"
  707. ;;
  708. 3)
  709. local domain=""
  710. read -p "Please enter your domain name to forcefully renew an SSL certificate: " domain
  711. ~/.acme.sh/acme.sh --renew -d ${domain} --force
  712. ;;
  713. *) echo "Invalid choice" ;;
  714. esac
  715. }
  716. ssl_cert_issue() {
  717. # check for acme.sh first
  718. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  719. echo "acme.sh could not be found. we will install it"
  720. install_acme
  721. if [ $? -ne 0 ]; then
  722. LOGE "install acme failed, please check logs"
  723. exit 1
  724. fi
  725. fi
  726. # install socat second
  727. case "${release}" in
  728. ubuntu | debian | armbian)
  729. apt update && apt install socat -y
  730. ;;
  731. centos | almalinux | rocky | oracle)
  732. yum -y update && yum -y install socat
  733. ;;
  734. fedora)
  735. dnf -y update && dnf -y install socat
  736. ;;
  737. arch | manjaro | parch)
  738. pacman -Sy --noconfirm socat
  739. ;;
  740. *)
  741. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  742. exit 1
  743. ;;
  744. esac
  745. if [ $? -ne 0 ]; then
  746. LOGE "install socat failed, please check logs"
  747. exit 1
  748. else
  749. LOGI "install socat succeed..."
  750. fi
  751. # get the domain here,and we need verify it
  752. local domain=""
  753. read -p "Please enter your domain name:" domain
  754. LOGD "your domain is:${domain},check it..."
  755. # here we need to judge whether there exists cert already
  756. local currentCert=$(~/.acme.sh/acme.sh --list | tail -1 | awk '{print $1}')
  757. if [ ${currentCert} == ${domain} ]; then
  758. local certInfo=$(~/.acme.sh/acme.sh --list)
  759. LOGE "system already has certs here,can not issue again,current certs details:"
  760. LOGI "$certInfo"
  761. exit 1
  762. else
  763. LOGI "your domain is ready for issuing cert now..."
  764. fi
  765. # create a directory for install cert
  766. certPath="/root/cert/${domain}"
  767. if [ ! -d "$certPath" ]; then
  768. mkdir -p "$certPath"
  769. else
  770. rm -rf "$certPath"
  771. mkdir -p "$certPath"
  772. fi
  773. # get needed port here
  774. local WebPort=80
  775. read -p "please choose which port do you use,default will be 80 port:" WebPort
  776. if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
  777. LOGE "your input ${WebPort} is invalid,will use default port"
  778. fi
  779. LOGI "will use port:${WebPort} to issue certs,please make sure this port is open..."
  780. # NOTE:This should be handled by user
  781. # open the port and kill the occupied progress
  782. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  783. ~/.acme.sh/acme.sh --issue -d ${domain} --listen-v6 --standalone --httpport ${WebPort}
  784. if [ $? -ne 0 ]; then
  785. LOGE "issue certs failed,please check logs"
  786. rm -rf ~/.acme.sh/${domain}
  787. exit 1
  788. else
  789. LOGE "issue certs succeed,installing certs..."
  790. fi
  791. # install cert
  792. ~/.acme.sh/acme.sh --installcert -d ${domain} \
  793. --key-file /root/cert/${domain}/privkey.pem \
  794. --fullchain-file /root/cert/${domain}/fullchain.pem
  795. if [ $? -ne 0 ]; then
  796. LOGE "install certs failed,exit"
  797. rm -rf ~/.acme.sh/${domain}
  798. exit 1
  799. else
  800. LOGI "install certs succeed,enable auto renew..."
  801. fi
  802. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  803. if [ $? -ne 0 ]; then
  804. LOGE "auto renew failed, certs details:"
  805. ls -lah cert/*
  806. chmod 755 $certPath/*
  807. exit 1
  808. else
  809. LOGI "auto renew succeed, certs details:"
  810. ls -lah cert/*
  811. chmod 755 $certPath/*
  812. fi
  813. }
  814. ssl_cert_issue_CF() {
  815. echo -E ""
  816. LOGD "******Instructions for use******"
  817. LOGI "This Acme script requires the following data:"
  818. LOGI "1.Cloudflare Registered e-mail"
  819. LOGI "2.Cloudflare Global API Key"
  820. LOGI "3.The domain name that has been resolved dns to the current server by Cloudflare"
  821. LOGI "4.The script applies for a certificate. The default installation path is /root/cert "
  822. confirm "Confirmed?[y/n]" "y"
  823. if [ $? -eq 0 ]; then
  824. # check for acme.sh first
  825. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  826. echo "acme.sh could not be found. we will install it"
  827. install_acme
  828. if [ $? -ne 0 ]; then
  829. LOGE "install acme failed, please check logs"
  830. exit 1
  831. fi
  832. fi
  833. CF_Domain=""
  834. CF_GlobalKey=""
  835. CF_AccountEmail=""
  836. certPath=/root/cert
  837. if [ ! -d "$certPath" ]; then
  838. mkdir $certPath
  839. else
  840. rm -rf $certPath
  841. mkdir $certPath
  842. fi
  843. LOGD "Please set a domain name:"
  844. read -p "Input your domain here:" CF_Domain
  845. LOGD "Your domain name is set to:${CF_Domain}"
  846. LOGD "Please set the API key:"
  847. read -p "Input your key here:" CF_GlobalKey
  848. LOGD "Your API key is:${CF_GlobalKey}"
  849. LOGD "Please set up registered email:"
  850. read -p "Input your email here:" CF_AccountEmail
  851. LOGD "Your registered email address is:${CF_AccountEmail}"
  852. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  853. if [ $? -ne 0 ]; then
  854. LOGE "Default CA, Lets'Encrypt fail, script exiting..."
  855. exit 1
  856. fi
  857. export CF_Key="${CF_GlobalKey}"
  858. export CF_Email=${CF_AccountEmail}
  859. ~/.acme.sh/acme.sh --issue --dns dns_cf -d ${CF_Domain} -d *.${CF_Domain} --log
  860. if [ $? -ne 0 ]; then
  861. LOGE "Certificate issuance failed, script exiting..."
  862. exit 1
  863. else
  864. LOGI "Certificate issued Successfully, Installing..."
  865. fi
  866. ~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} --ca-file /root/cert/ca.cer \
  867. --cert-file /root/cert/${CF_Domain}.cer --key-file /root/cert/${CF_Domain}.key \
  868. --fullchain-file /root/cert/fullchain.cer
  869. if [ $? -ne 0 ]; then
  870. LOGE "Certificate installation failed, script exiting..."
  871. exit 1
  872. else
  873. LOGI "Certificate installed Successfully,Turning on automatic updates..."
  874. fi
  875. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  876. if [ $? -ne 0 ]; then
  877. LOGE "Auto update setup Failed, script exiting..."
  878. ls -lah cert
  879. chmod 755 $certPath
  880. exit 1
  881. else
  882. LOGI "The certificate is installed and auto-renewal is turned on, Specific information is as follows"
  883. ls -lah cert
  884. chmod 755 $certPath
  885. fi
  886. else
  887. show_menu
  888. fi
  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 Fail2ban"
  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} Update Menu
  1180. ${green}4.${plain} Custom Version
  1181. ${green}5.${plain} Uninstall
  1182. ————————————————
  1183. ${green}6.${plain} Reset Username & Password & Secret Token
  1184. ${green}7.${plain} Reset Web Base Path
  1185. ${green}8.${plain} Reset Settings
  1186. ${green}9.${plain} Change Port
  1187. ${green}10.${plain} View Current Settings
  1188. ————————————————
  1189. ${green}11.${plain} Start
  1190. ${green}12.${plain} Stop
  1191. ${green}13.${plain} Restart
  1192. ${green}14.${plain} Check Status
  1193. ${green}15.${plain} Check Logs
  1194. ————————————————
  1195. ${green}16.${plain} Enable Autostart
  1196. ${green}17.${plain} Disable Autostart
  1197. ————————————————
  1198. ${green}18.${plain} SSL Certificate Management
  1199. ${green}19.${plain} Cloudflare SSL Certificate
  1200. ${green}20.${plain} IP Limit 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 && update_menu
  1221. ;;
  1222. 4)
  1223. check_install && custom_version
  1224. ;;
  1225. 5)
  1226. check_install && uninstall
  1227. ;;
  1228. 6)
  1229. check_install && reset_user
  1230. ;;
  1231. 7)
  1232. check_install && reset_webbasepath
  1233. ;;
  1234. 8)
  1235. check_install && reset_config
  1236. ;;
  1237. 9)
  1238. check_install && set_port
  1239. ;;
  1240. 10)
  1241. check_install && check_config
  1242. ;;
  1243. 11)
  1244. check_install && start
  1245. ;;
  1246. 12)
  1247. check_install && stop
  1248. ;;
  1249. 13)
  1250. check_install && restart
  1251. ;;
  1252. 14)
  1253. check_install && status
  1254. ;;
  1255. 15)
  1256. check_install && show_log
  1257. ;;
  1258. 16)
  1259. check_install && enable
  1260. ;;
  1261. 17)
  1262. check_install && disable
  1263. ;;
  1264. 18)
  1265. ssl_cert_issue_main
  1266. ;;
  1267. 19)
  1268. ssl_cert_issue_CF
  1269. ;;
  1270. 20)
  1271. iplimit_main
  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