Skip to content

Commit 0d764f1

Browse files
committed
feat(iplimit): auto-install fail2ban on install and update
IP limit enforcement is gated on fail2ban being present (ce8b1be), but the bare-metal install.sh/update.sh never installed it, so the feature stayed disabled until the user ran the IP Limit menu by hand. Docker already auto-configures it; bare-metal hosts did not. Extract the fail2ban install + jail setup out of install_iplimit into a non-interactive setup_fail2ban_iplimit() (no exit/before_show_menu, returns a status) exposed via 'x-ui setup-fail2ban', and call it from install.sh and update.sh after the panel is up. update.sh is the primary update path (x-ui update and the panel self-updater both run it). Honors XUI_ENABLE_FAIL2BAN (proceed only when unset or true, matching the Go gate) and is non-fatal so a fail2ban failure never aborts the install/update.
1 parent 6836536 commit 0d764f1

3 files changed

Lines changed: 84 additions & 3 deletions

File tree

install.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,32 @@ EOF
13001300
${xui_folder}/x-ui migrate
13011301
}
13021302

1303+
# setup_fail2ban auto-installs and configures fail2ban for the IP Limit feature
1304+
# by invoking the freshly installed x-ui CLI. IP Limit is load-bearing on
1305+
# fail2ban (without it the panel disables the limitIp field and zeroes existing
1306+
# limits), so a fresh install should make it work out of the box, just like the
1307+
# Docker image already does. Non-fatal by design: a fail2ban failure must never
1308+
# abort the panel install.
1309+
setup_fail2ban() {
1310+
if [[ -n "${XUI_ENABLE_FAIL2BAN+x}" && "${XUI_ENABLE_FAIL2BAN}" != "true" ]]; then
1311+
echo -e "${yellow}XUI_ENABLE_FAIL2BAN=${XUI_ENABLE_FAIL2BAN}, skipping Fail2ban auto-setup.${plain}"
1312+
return 0
1313+
fi
1314+
1315+
if [[ ! -x /usr/bin/x-ui ]]; then
1316+
echo -e "${yellow}x-ui CLI not found; skipping Fail2ban auto-setup.${plain}"
1317+
return 0
1318+
fi
1319+
1320+
echo -e "${green}Setting up Fail2ban for the IP Limit feature...${plain}"
1321+
if /usr/bin/x-ui setup-fail2ban; then
1322+
echo -e "${green}Fail2ban setup complete.${plain}"
1323+
else
1324+
echo -e "${yellow}Fail2ban setup did not finish; IP Limit stays disabled until you run 'x-ui' and open the IP Limit menu. Continuing.${plain}"
1325+
fi
1326+
return 0
1327+
}
1328+
13031329
install_x-ui() {
13041330
cd ${xui_folder%/x-ui}/
13051331

@@ -1487,6 +1513,10 @@ install_x-ui() {
14871513
fi
14881514
fi
14891515

1516+
# IP Limit relies on fail2ban; install + configure it now so the feature
1517+
# works out of the box (no-op when XUI_ENABLE_FAIL2BAN=false). Never fatal.
1518+
setup_fail2ban
1519+
14901520
echo -e "${green}x-ui ${tag_version}${plain} installation finished, it is running now..."
14911521
echo -e ""
14921522
echo -e "┌───────────────────────────────────────────────────────┐

update.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,33 @@ config_after_update() {
854854
fi
855855
}
856856

857+
# setup_fail2ban auto-installs and configures fail2ban for the IP Limit feature
858+
# by invoking the freshly downloaded x-ui CLI. IP Limit is load-bearing on
859+
# fail2ban (without it the panel disables the limitIp field and zeroes existing
860+
# limits), so updating an older install should make it work without a manual
861+
# trip through the IP Limit menu. Non-fatal: a fail2ban failure must never abort
862+
# the update. XUI_ENABLE_FAIL2BAN is honored (load_xui_env exports it from the
863+
# persisted env file, so a deliberate opt-out survives updates).
864+
setup_fail2ban() {
865+
if [[ -n "${XUI_ENABLE_FAIL2BAN+x}" && "${XUI_ENABLE_FAIL2BAN}" != "true" ]]; then
866+
echo -e "${yellow}XUI_ENABLE_FAIL2BAN=${XUI_ENABLE_FAIL2BAN}, skipping Fail2ban auto-setup.${plain}"
867+
return 0
868+
fi
869+
870+
if [[ ! -x /usr/bin/x-ui ]]; then
871+
echo -e "${yellow}x-ui CLI not found; skipping Fail2ban auto-setup.${plain}"
872+
return 0
873+
fi
874+
875+
echo -e "${green}Setting up Fail2ban for the IP Limit feature...${plain}"
876+
if /usr/bin/x-ui setup-fail2ban; then
877+
echo -e "${green}Fail2ban setup complete.${plain}"
878+
else
879+
echo -e "${yellow}Fail2ban setup did not finish; IP Limit stays disabled until you run 'x-ui' and open the IP Limit menu. Continuing.${plain}"
880+
fi
881+
return 0
882+
}
883+
857884
update_x-ui() {
858885
cd ${xui_folder%/x-ui}/
859886

@@ -1037,6 +1064,11 @@ update_x-ui() {
10371064

10381065
config_after_update
10391066

1067+
# IP Limit relies on fail2ban; install + configure it now so the feature
1068+
# works out of the box on update too (no-op when XUI_ENABLE_FAIL2BAN=false).
1069+
# Never fatal.
1070+
setup_fail2ban
1071+
10401072
echo -e "${green}x-ui ${tag_version}${plain} updating finished, it is running now..."
10411073
echo -e ""
10421074
echo -e "┌───────────────────────────────────────────────────────┐

x-ui.sh

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,7 +2166,15 @@ iplimit_main() {
21662166
esac
21672167
}
21682168

2169-
install_iplimit() {
2169+
setup_fail2ban_iplimit() {
2170+
# Honor the same toggle the panel uses (isFail2BanEnabled): enabled when the
2171+
# var is unset or exactly "true"; any other explicit value means the operator
2172+
# opted out, so do nothing rather than install a fail2ban the panel ignores.
2173+
if [[ -n "${XUI_ENABLE_FAIL2BAN+x}" && "${XUI_ENABLE_FAIL2BAN}" != "true" ]]; then
2174+
echo -e "${yellow}XUI_ENABLE_FAIL2BAN=${XUI_ENABLE_FAIL2BAN}, skipping Fail2ban setup.${plain}\n"
2175+
return 0
2176+
fi
2177+
21702178
if ! command -v fail2ban-client &> /dev/null; then
21712179
echo -e "${green}Fail2ban is not installed. Installing now...!${plain}\n"
21722180

@@ -2216,13 +2224,13 @@ install_iplimit() {
22162224
;;
22172225
*)
22182226
echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
2219-
exit 1
2227+
return 1
22202228
;;
22212229
esac
22222230

22232231
if ! command -v fail2ban-client &> /dev/null; then
22242232
echo -e "${red}Fail2ban installation failed.${plain}\n"
2225-
exit 1
2233+
return 1
22262234
fi
22272235

22282236
echo -e "${green}Fail2ban installed successfully!${plain}\n"
@@ -2267,6 +2275,14 @@ install_iplimit() {
22672275
fi
22682276

22692277
echo -e "${green}IP Limit installed and configured successfully!${plain}\n"
2278+
return 0
2279+
}
2280+
2281+
# install_iplimit is the interactive (menu) entry point: it runs the shared
2282+
# setup and then returns to the menu. The non-interactive installer path uses
2283+
# setup_fail2ban_iplimit directly via `x-ui setup-fail2ban`.
2284+
install_iplimit() {
2285+
setup_fail2ban_iplimit
22702286
before_show_menu
22712287
}
22722288

@@ -3263,6 +3279,9 @@ if [[ $# > 0 ]]; then
32633279
"banlog")
32643280
check_install 0 && show_banlog 0
32653281
;;
3282+
"setup-fail2ban")
3283+
setup_fail2ban_iplimit
3284+
;;
32663285
"update")
32673286
check_install 0 && update 0
32683287
;;

0 commit comments

Comments
 (0)