Dedublicate banner stuff and add missing init-vars for banactions

This commit is contained in:
2025-12-04 20:36:23 +01:00
parent 0f2a3a1e32
commit def440611a
3 changed files with 75 additions and 35 deletions

View File

@@ -132,6 +132,24 @@ const (
actionServerIDPlaceholder = "__SERVER_ID__"
)
// jailLocalBanner is the standard banner for jail.local files
const jailLocalBanner = `################################################################################
# Fail2Ban-UI Managed Configuration
#
# WARNING: This file is automatically managed by Fail2Ban-UI.
# DO NOT EDIT THIS FILE MANUALLY - your changes will be overwritten.
#
# This file overrides settings from /etc/fail2ban/jail.conf
# Custom jail configurations should be placed in /etc/fail2ban/jail.d/
################################################################################
`
// JailLocalBanner returns the standard banner for jail.local files
func JailLocalBanner() string {
return jailLocalBanner
}
const fail2banActionTemplate = `[INCLUDES]
before = sendmail-common.conf
@@ -771,18 +789,8 @@ func ensureJailLocalStructure() error {
return updateJailLocalDefaultSection(settings)
}
// Build the banner
banner := `################################################################################
# Fail2Ban-UI Managed Configuration
#
# WARNING: This file is automatically managed by Fail2Ban-UI.
# DO NOT EDIT THIS FILE MANUALLY - your changes will be overwritten.
#
# This file overrides settings from /etc/fail2ban/jail.conf
# Custom jail configurations should be placed in /etc/fail2ban/jail.d/
################################################################################
`
// Use the standard banner
banner := jailLocalBanner
// Build [DEFAULT] section
// Convert IgnoreIPs array to space-separated string
@@ -846,7 +854,8 @@ func updateJailLocalDefaultSection(settings AppSettings) error {
return fmt.Errorf("failed to read jail.local: %w", err)
}
lines := strings.Split(string(content), "\n")
contentStr := string(content)
lines := strings.Split(contentStr, "\n")
var outputLines []string
inDefault := false
defaultUpdated := false
@@ -878,9 +887,23 @@ func updateJailLocalDefaultSection(settings AppSettings) error {
}
keysUpdated := make(map[string]bool)
// Always add the full banner at the start
outputLines = append(outputLines, strings.Split(strings.TrimRight(jailLocalBanner, "\n"), "\n")...)
// Skip everything before [DEFAULT] section (old banner, comments, empty lines)
foundSection := false
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "[") && strings.HasSuffix(trimmed, "]") {
// Found a section - stop skipping and process this line
foundSection = true
}
if !foundSection {
// Skip lines before any section (old banner, comments, empty lines)
continue
}
// Process lines after we found a section
if strings.HasPrefix(trimmed, "[") && strings.HasSuffix(trimmed, "]") {
sectionName := strings.Trim(trimmed, "[]")
if sectionName == "DEFAULT" {