DockerEntrypoint.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/sh
  2. # Start fail2ban with the 3x-ipl jail
  3. if [ "$XUI_ENABLE_FAIL2BAN" = "true" ]; then
  4. LOG_FOLDER="${XUI_LOG_FOLDER:-/var/log/x-ui}"
  5. mkdir -p "$LOG_FOLDER"
  6. touch "$LOG_FOLDER/3xipl.log" "$LOG_FOLDER/3xipl-banned.log"
  7. mkdir -p /etc/fail2ban/jail.d /etc/fail2ban/filter.d /etc/fail2ban/action.d
  8. cat > /etc/fail2ban/jail.d/3x-ipl.conf << EOF
  9. [3x-ipl]
  10. enabled=true
  11. backend=auto
  12. filter=3x-ipl
  13. action=3x-ipl
  14. logpath=$LOG_FOLDER/3xipl.log
  15. maxretry=1
  16. findtime=32
  17. bantime=30m
  18. EOF
  19. cat > /etc/fail2ban/filter.d/3x-ipl.conf << 'EOF'
  20. [Definition]
  21. datepattern = ^%%Y/%%m/%%d %%H:%%M:%%S
  22. failregex = \[LIMIT_IP\]\s*Email\s*=\s*<F-USER>.+</F-USER>\s*\|\|\s*Disconnecting OLD IP\s*=\s*<ADDR>\s*\|\|\s*Timestamp\s*=\s*\d+
  23. ignoreregex =
  24. EOF
  25. # Ports to exempt from the ban so an over-limit proxy client can never lock
  26. # the administrator out of SSH or the panel. The ban still covers every other
  27. # TCP port (including all Xray inbounds), so IP-limit keeps working for inbounds
  28. # added later without regenerating these files.
  29. SSH_PORTS=$(grep -oE '^[[:space:]]*Port[[:space:]]+[0-9]+' /etc/ssh/sshd_config 2>/dev/null | grep -oE '[0-9]+' | paste -sd, -)
  30. [ -z "$SSH_PORTS" ] && SSH_PORTS="22"
  31. PANEL_PORT=$(/app/x-ui setting -show true 2>/dev/null | grep -Eo 'port: .+' | awk '{print $2}')
  32. EXEMPT_PORTS="$SSH_PORTS"
  33. [ -n "$PANEL_PORT" ] && EXEMPT_PORTS="$EXEMPT_PORTS,$PANEL_PORT"
  34. cat > /etc/fail2ban/action.d/3x-ipl.conf << EOF
  35. [INCLUDES]
  36. before = iptables-allports.conf
  37. [Definition]
  38. actionstart = <iptables> -N f2b-<name>
  39. <iptables> -A f2b-<name> -j <returntype>
  40. <iptables> -I <chain> -p <protocol> -j f2b-<name>
  41. actionstop = <iptables> -D <chain> -p <protocol> -j f2b-<name>
  42. <actionflush>
  43. <iptables> -X f2b-<name>
  44. actioncheck = <iptables> -n -L <chain> | grep -q 'f2b-<name>[ \t]'
  45. actionban = <iptables> -I f2b-<name> 1 -s <ip> -p <protocol> -m multiport ! --dports <exemptports> -j <blocktype>
  46. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") BAN [Email] = <F-USER> [IP] = <ip> banned for <bantime> seconds." >> $LOG_FOLDER/3xipl-banned.log
  47. actionunban = <iptables> -D f2b-<name> -s <ip> -p <protocol> -m multiport ! --dports <exemptports> -j <blocktype>
  48. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") UNBAN [Email] = <F-USER> [IP] = <ip> unbanned." >> $LOG_FOLDER/3xipl-banned.log
  49. [Init]
  50. name = default
  51. protocol = tcp
  52. chain = INPUT
  53. exemptports = $EXEMPT_PORTS
  54. EOF
  55. fail2ban-client -x start
  56. fi
  57. # Run x-ui
  58. exec /app/x-ui