From 2db45393cf6d41b9b336eb2f6b862b97b161db01 Mon Sep 17 00:00:00 2001 From: Sanaei Date: Sun, 29 Nov 2026 11:02:51 +0330 Subject: [PATCH] fix(iplimit): install the iptables tools the ban action needs The 3x-ipl Fail2ban action bans with iptables and ip6tables, but the setup installed only fail2ban and nftables. Debian 12/13 and Ubuntu 22.04/24.04 do not pull iptables in with fail2ban, so on a host without it every ban logged "iptables: not found", showed up in the jail's banned list and applied no rule, while the setup reported success. A configured 3x-ipl jail now always has its ban tools. Writing the action installs them when they are missing, and so does an update over a jail an earlier release set up, whether the choice is stored or not. A jail that is already running is restarted after the install so bans it recorded without a rule get one. When the tools cannot be installed the setup fails with the reason instead of writing a jail that enforces nothing. The lifecycle fixture now models the tools as present, so its cases no longer depend on the host running the tests. --- scripts_fail2ban_test.go | 100 ++++++++++++++++++++++++++++++++++++++- x-ui.sh | 33 ++++++++++++- 2 files changed, 130 insertions(+), 3 deletions(-) diff --git a/scripts_fail2ban_test.go b/scripts_fail2ban_test.go index d99bde7af..ca7088848 100644 --- a/scripts_fail2ban_test.go +++ b/scripts_fail2ban_test.go @@ -107,7 +107,11 @@ systemctl() { } apt-get() { rm -f "$TEST_DIR/installed"; } command() { - if [[ $* == '-v fail2ban-client' ]]; then [[ -f "$TEST_DIR/installed" ]]; else builtin command "$@"; fi + case "$*" in + '-v fail2ban-client') [[ -f "$TEST_DIR/installed" ]] ;; + '-v iptables' | '-v ip6tables') [[ -f "$TEST_DIR/$2" ]] ;; + *) builtin command "$@" ;; + esac } fail2ban-client() { case "$*" in @@ -139,7 +143,7 @@ setup_fail2ban_iplimit() { "confirm", "read_fail2ban_preference", "persist_fail2ban_preference", "iplimit_jail_configured", "setup_fail2ban_for_lifecycle", "apply_iplimit_preference", "iplimit_remove_conflicts", "iplimit_can_uninstall", "remove_iplimit_jail", - "install_iplimit", "remove_iplimit", + "install_iplimit", "remove_iplimit", "iplimit_ban_tools_present", "ensure_iplimit_ban_tools", } { if strings.Contains(string(source), "\n"+name+"() {\n") { driver += strings.ReplaceAll(scriptFunction(t, "x-ui.sh", name), "/etc/fail2ban", filepath.Join(dir, "fail2ban")) @@ -159,9 +163,101 @@ setup_fail2ban_iplimit() { driver += wrapper } driver += "\nexport NONINTERACTIVE=1\n" + writeFail2banFixture(t, dir, "iptables", "") + writeFail2banFixture(t, dir, "ip6tables", "") return driver } +func TestIPLimitBanToolsAreEnsured(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("requires a POSIX shell") + } + bash, err := exec.LookPath("bash") + if err != nil { + t.Skip("requires bash") + } + const legacyJail = "[3x-ipl]\nenabled = true\nbantime = 47m\n" + const install = "update\ninstall -y iptables\n" + for _, tc := range []struct { + name, command, env, wantPackages, wantRestarts string + present, unavailable, jail, wantFailure bool + }{ + {name: "writing the action installs the missing tools", command: "create_iplimit_jails", wantPackages: install}, + {name: "present tools need no package and no restart", command: "create_iplimit_jails", present: true, jail: true}, + {name: "an update repairs a running legacy jail", command: "setup_fail2ban_for_lifecycle update", jail: true, wantPackages: install, wantRestarts: "restart 3x-ipl\n"}, + {name: "an update repairs a stored opt-in", command: "setup_fail2ban_for_lifecycle update", env: "XUI_ENABLE_FAIL2BAN=true\n", jail: true, wantPackages: install, wantRestarts: "restart 3x-ipl\n"}, + {name: "tools that cannot be installed fail the setup", command: "create_iplimit_jails", unavailable: true, wantPackages: install, wantFailure: true}, + } { + t.Run(tc.name, func(t *testing.T) { + dir := t.TempDir() + driver := setupFail2banScriptFixture(t, dir) + ` +iplimit_log_path="$TEST_DIR/3xipl.log" iplimit_banned_log_path="$TEST_DIR/3xipl-banned.log" xui_folder="$TEST_DIR/app" +apt-get() { + printf '%s\n' "$*" >> "$TEST_DIR/packages" + if [[ $* == *iptables* && ! -f "$TEST_DIR/unavailable" ]]; then touch "$TEST_DIR/iptables" "$TEST_DIR/ip6tables"; fi +} +fail2ban-client() { + case "$*" in + 'status 3x-ipl') [[ -f "$TEST_DIR/managed-running" ]] ;; + 'restart 3x-ipl') printf '%s\n' "$*" >> "$TEST_DIR/restarts" ;; + *) return 1 ;; + esac +} +` + strings.ReplaceAll(scriptFunction(t, "x-ui.sh", "create_iplimit_jails"), "/etc/fail2ban", filepath.Join(dir, "fail2ban")) + for _, sub := range []string{"jail.d", "filter.d", "action.d"} { + if err := os.MkdirAll(filepath.Join(dir, "fail2ban", sub), 0o755); err != nil { + t.Fatal(err) + } + } + if !tc.present { + for _, tool := range []string{"iptables", "ip6tables"} { + if err := os.Remove(filepath.Join(dir, tool)); err != nil { + t.Fatal(err) + } + } + } + if tc.unavailable { + writeFail2banFixture(t, dir, "unavailable", "") + } + if tc.jail { + writeFail2banFixture(t, dir, "fail2ban/jail.d/3x-ipl.conf", legacyJail) + writeFail2banFixture(t, dir, "installed", "") + writeFail2banFixture(t, dir, "managed-running", "") + } + writeFail2banFixture(t, dir, "env", tc.env) + writeFail2banFixture(t, dir, "packages", "") + writeFail2banFixture(t, dir, "restarts", "") + cmd := exec.CommandContext(t.Context(), bash, "-c", driver+"\n"+tc.command) + for _, e := range os.Environ() { + if !strings.HasPrefix(e, "XUI_") && !strings.HasPrefix(e, "TEST_DIR=") { + cmd.Env = append(cmd.Env, e) + } + } + cmd.Env = append(cmd.Env, "TEST_DIR="+dir) + out, err := cmd.CombinedOutput() + if (err != nil) != tc.wantFailure { + t.Fatalf("shell flow error = %v, want failure %t\n%s", err, tc.wantFailure, out) + } + for _, tool := range []string{"iptables", "ip6tables"} { + _, statErr := os.Stat(filepath.Join(dir, tool)) + if (statErr == nil) == tc.wantFailure { + t.Errorf("%s present = %t after the flow, want %t", tool, statErr == nil, !tc.wantFailure) + } + } + if got := readScriptState(t, dir, "packages"); got != tc.wantPackages { + t.Errorf("package manager calls = %q, want %q", got, tc.wantPackages) + } + if got := readScriptState(t, dir, "restarts"); got != tc.wantRestarts { + t.Errorf("jail restarts = %q, want %q", got, tc.wantRestarts) + } + _, actionErr := os.Stat(filepath.Join(dir, "fail2ban", "action.d", "3x-ipl.conf")) + if tc.wantFailure && (actionErr == nil || !strings.Contains(string(out), "iptables and ip6tables")) { + t.Errorf("setup without ban tools wrote the action (stat %v) or hid the reason: %s", actionErr, out) + } + }) + } +} + func TestNativeFail2banStockSSHBackend(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("requires a POSIX shell") diff --git a/x-ui.sh b/x-ui.sh index d47d53a9b..2eeee3a38 100644 --- a/x-ui.sh +++ b/x-ui.sh @@ -2415,7 +2415,8 @@ setup_fail2ban_for_lifecycle() { choice=$(read_fail2ban_preference) || return 1 if [[ -z "$choice" ]] && iplimit_jail_configured; then # Preserve legacy jails, including later overrides to their enabled state. - return 0 + ensure_iplimit_ban_tools + return fi if [[ -z "$choice" ]]; then choice=false @@ -2432,6 +2433,7 @@ setup_fail2ban_for_lifecycle() { return 0 fi if command -v fail2ban-client > /dev/null 2>&1 && iplimit_jail_configured; then + ensure_iplimit_ban_tools || return 1 fail2ban-client status 3x-ipl > /dev/null 2>&1 || start_iplimit_jail return fi @@ -2709,10 +2711,39 @@ show_banlog() { fail2ban-client status 3x-ipl || echo -e "${yellow}Unable to get jail status${plain}" } +iplimit_ban_tools_present() { + command -v iptables > /dev/null 2>&1 && command -v ip6tables > /dev/null 2>&1 +} + +# The 3x-ipl action bans with iptables and ip6tables, which Debian and Ubuntu do not +# install with fail2ban; without them a ban is listed by fail2ban and never applied. +ensure_iplimit_ban_tools() { + iplimit_ban_tools_present && return 0 + case "${release}" in + ubuntu | debian | armbian) apt-get update && apt-get install -y iptables ;; + fedora | amzn | virtuozzo | rhel | almalinux | rocky | ol) dnf -y install iptables ;; + centos) + if [[ "${VERSION_ID}" =~ ^7 ]]; then yum -y install iptables; else dnf -y install iptables; fi + ;; + arch | manjaro | parch) pacman -Sy --noconfirm iptables ;; + alpine) apk add iptables ;; + esac + if ! iplimit_ban_tools_present; then + LOGE "IP Limit bans need iptables and ip6tables, which could not be installed." + return 1 + fi + # Bans recorded while the tools were missing get their rules when the jail restarts. + if fail2ban-client status 3x-ipl > /dev/null 2>&1; then + fail2ban-client restart 3x-ipl > /dev/null || return 1 + fi +} + create_iplimit_jails() { # Use default bantime if not passed => 30 minutes local bantime="${1:-30}" + ensure_iplimit_ban_tools || return 1 + cat << EOF > /etc/fail2ban/jail.d/3x-ipl.conf || return 1 [3x-ipl] enabled=true