x-ui.sh 43 KB

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