mirror of
https://github.com/swissmakers/fail2ban-ui.git
synced 2026-04-17 05:53:15 +02:00
Add hook, if no jail.local was precreated and fix bug on server start
This commit is contained in:
@@ -45,8 +45,8 @@ func main() {
|
|||||||
log.Println("Server listening on port", serverPort, ".")
|
log.Println("Server listening on port", serverPort, ".")
|
||||||
|
|
||||||
// Start the server on port 8080.
|
// Start the server on port 8080.
|
||||||
if err := router.Run(":", serverPort); err != nil {
|
if err := router.Run(":" + serverPort); err != nil {
|
||||||
log.Fatalf("Server crashed: %v", err)
|
log.Fatalf("Could not start server: %v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -58,8 +59,8 @@ type AppSettings struct {
|
|||||||
|
|
||||||
// init paths to key-files
|
// init paths to key-files
|
||||||
const (
|
const (
|
||||||
settingsFile = "fail2ban-ui-settings.json" // this is relative to where the app was started
|
settingsFile = "fail2ban-ui-settings.json" // this file is created, relatively to where the app was started
|
||||||
defaultjailFile = "/etc/fail2ban/jail.conf"
|
defaultJailFile = "/etc/fail2ban/jail.conf"
|
||||||
jailFile = "/etc/fail2ban/jail.local" // Path to jail.local (to override conf-values from jail.conf)
|
jailFile = "/etc/fail2ban/jail.local" // Path to jail.local (to override conf-values from jail.conf)
|
||||||
jailDFile = "/etc/fail2ban/jail.d/ui-custom-action.conf"
|
jailDFile = "/etc/fail2ban/jail.d/ui-custom-action.conf"
|
||||||
actionFile = "/etc/fail2ban/action.d/ui-custom-action.conf"
|
actionFile = "/etc/fail2ban/action.d/ui-custom-action.conf"
|
||||||
@@ -206,7 +207,19 @@ func initializeFail2banAction() error {
|
|||||||
func setupGeoCustomAction() error {
|
func setupGeoCustomAction() error {
|
||||||
file, err := os.Open(jailFile)
|
file, err := os.Open(jailFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err // File not found or inaccessible
|
// Fallback: Copy default file if jail.local is not found
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
if err := copyFile(defaultJailFile, jailFile); err != nil {
|
||||||
|
return fmt.Errorf("failed to copy default jail.conf to jail.local: %w", err)
|
||||||
|
}
|
||||||
|
fmt.Println("Successfully created jail.local from jail.conf.")
|
||||||
|
file, err = os.Open(jailFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return err // Other error
|
||||||
|
}
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
@@ -251,6 +264,24 @@ func setupGeoCustomAction() error {
|
|||||||
return os.WriteFile(jailFile, []byte(output), 0644)
|
return os.WriteFile(jailFile, []byte(output), 0644)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// copyFile copies a file from src to dst. If the destination file does not exist, it will be created.
|
||||||
|
func copyFile(src, dst string) error {
|
||||||
|
sourceFile, err := os.Open(src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer sourceFile.Close()
|
||||||
|
|
||||||
|
destFile, err := os.Create(dst)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer destFile.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(destFile, sourceFile)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// ensureJailDConfig checks if the jail.d file exists and creates it if necessary
|
// ensureJailDConfig checks if the jail.d file exists and creates it if necessary
|
||||||
func ensureJailDConfig() error {
|
func ensureJailDConfig() error {
|
||||||
// Check if the file already exists
|
// Check if the file already exists
|
||||||
|
|||||||
Reference in New Issue
Block a user