mirror of
https://github.com/swissmakers/fail2ban-ui.git
synced 2026-04-11 13:47:05 +02:00
implemented bind address environment variable.
This commit is contained in:
@@ -19,6 +19,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -59,6 +60,10 @@ func main() {
|
|||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
serverPort := strconv.Itoa(int(settings.Port))
|
serverPort := strconv.Itoa(int(settings.Port))
|
||||||
|
|
||||||
|
// Get bind address from environment variable, defaulting to 0.0.0.0
|
||||||
|
bindAddress, _ := config.GetBindAddressFromEnv()
|
||||||
|
serverAddr := net.JoinHostPort(bindAddress, serverPort)
|
||||||
|
|
||||||
// Load HTML templates depending on whether the application is running inside a container.
|
// Load HTML templates depending on whether the application is running inside a container.
|
||||||
_, container := os.LookupEnv("CONTAINER")
|
_, container := os.LookupEnv("CONTAINER")
|
||||||
if container {
|
if container {
|
||||||
@@ -82,17 +87,17 @@ func main() {
|
|||||||
|
|
||||||
// Check if LOTR mode is active
|
// Check if LOTR mode is active
|
||||||
isLOTRMode := isLOTRModeActive(settings.AlertCountries)
|
isLOTRMode := isLOTRModeActive(settings.AlertCountries)
|
||||||
printWelcomeBanner(serverPort, isLOTRMode)
|
printWelcomeBanner(bindAddress, serverPort, isLOTRMode)
|
||||||
if isLOTRMode {
|
if isLOTRMode {
|
||||||
log.Println("--- Middle-earth Security Realm activated ---")
|
log.Println("--- Middle-earth Security Realm activated ---")
|
||||||
log.Println("🎭 LOTR Mode: The guardians of Middle-earth stand ready!")
|
log.Println("🎭 LOTR Mode: The guardians of Middle-earth stand ready!")
|
||||||
} else {
|
} else {
|
||||||
log.Println("--- Fail2Ban-UI started in", gin.Mode(), "mode ---")
|
log.Println("--- Fail2Ban-UI started in", gin.Mode(), "mode ---")
|
||||||
}
|
}
|
||||||
log.Println("Server listening on port", serverPort, ".")
|
log.Printf("Server listening on %s:%s.\n", bindAddress, serverPort)
|
||||||
|
|
||||||
// Start the server on port 8080.
|
// Start the server on the configured address and port.
|
||||||
if err := router.Run(":" + serverPort); err != nil {
|
if err := router.Run(serverAddr); err != nil {
|
||||||
log.Fatalf("Could not start server: %v\n", err)
|
log.Fatalf("Could not start server: %v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -110,7 +115,7 @@ func isLOTRModeActive(alertCountries []string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// printWelcomeBanner prints the Tux banner with startup info.
|
// printWelcomeBanner prints the Tux banner with startup info.
|
||||||
func printWelcomeBanner(appPort string, isLOTRMode bool) {
|
func printWelcomeBanner(bindAddress, appPort string, isLOTRMode bool) {
|
||||||
greeting := getGreeting()
|
greeting := getGreeting()
|
||||||
|
|
||||||
if isLOTRMode {
|
if isLOTRMode {
|
||||||
@@ -128,11 +133,11 @@ Middle-earth Security Realm - LOTR Mode Activated
|
|||||||
⚔️ The guardians of Middle-earth stand ready! ⚔️
|
⚔️ The guardians of Middle-earth stand ready! ⚔️
|
||||||
Developers: https://swissmakers.ch
|
Developers: https://swissmakers.ch
|
||||||
Mode: %s
|
Mode: %s
|
||||||
Listening on: http://0.0.0.0:%s
|
Listening on: http://%s:%s
|
||||||
══════════════════════════════════════════════════
|
══════════════════════════════════════════════════
|
||||||
|
|
||||||
`
|
`
|
||||||
fmt.Printf(lotrBanner, greeting, gin.Mode(), appPort)
|
fmt.Printf(lotrBanner, greeting, gin.Mode(), bindAddress, appPort)
|
||||||
} else {
|
} else {
|
||||||
const tuxBanner = `
|
const tuxBanner = `
|
||||||
.--.
|
.--.
|
||||||
@@ -147,11 +152,11 @@ Fail2Ban UI - A Swissmade Management Interface
|
|||||||
----------------------------------------------
|
----------------------------------------------
|
||||||
Developers: https://swissmakers.ch
|
Developers: https://swissmakers.ch
|
||||||
Mode: %s
|
Mode: %s
|
||||||
Listening on: http://0.0.0.0:%s
|
Listening on: http://%s:%s
|
||||||
----------------------------------------------
|
----------------------------------------------
|
||||||
|
|
||||||
`
|
`
|
||||||
fmt.Printf(tuxBanner, greeting, gin.Mode(), appPort)
|
fmt.Printf(tuxBanner, greeting, gin.Mode(), bindAddress, appPort)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -222,6 +222,7 @@ The Fail2Ban UI container requires several volume mounts to function properly. B
|
|||||||
| Variable | Default | Description |
|
| Variable | Default | Description |
|
||||||
|----------|---------|-------------|
|
|----------|---------|-------------|
|
||||||
| `PORT` | `8080` | Port number for the web interface |
|
| `PORT` | `8080` | Port number for the web interface |
|
||||||
|
| `BIND_ADDRESS` | `0.0.0.0` | IP address to bind the web interface to. Useful when running with host networking to prevent exposing the web UI to unprotected networks. Set to a specific IP (e.g., `127.0.0.1` or a specific interface IP) to restrict access. |
|
||||||
| `CONTAINER` | `true` | Automatically set by the container (do not override) |
|
| `CONTAINER` | `true` | Automatically set by the container (do not override) |
|
||||||
|
|
||||||
### First Launch Configuration
|
### First Launch Configuration
|
||||||
|
|||||||
@@ -32,7 +32,13 @@ services:
|
|||||||
privileged: true # needed because the fail2ban-ui container needs to modify the fail2ban config owned by root inside the linuxserver-fail2ban container
|
privileged: true # needed because the fail2ban-ui container needs to modify the fail2ban config owned by root inside the linuxserver-fail2ban container
|
||||||
network_mode: host
|
network_mode: host
|
||||||
environment:
|
environment:
|
||||||
|
# Optional: Change this to use a different port for the web interface (defaults is 8080)
|
||||||
- PORT=3080
|
- PORT=3080
|
||||||
|
# Optional: Bind to a specific IP address (default: 0.0.0.0)
|
||||||
|
# This is useful when running with host networking to prevent exposing
|
||||||
|
# the web UI to unprotected networks. Set to a specific IP (e.g., 127.0.0.1
|
||||||
|
# or a specific interface IP) to restrict access.
|
||||||
|
# - BIND_ADDRESS=127.0.0.1
|
||||||
volumes:
|
volumes:
|
||||||
# Required for fail2ban-ui: Stores SQLite database, application settings, and SSH keys of the fail2ban-ui container
|
# Required for fail2ban-ui: Stores SQLite database, application settings, and SSH keys of the fail2ban-ui container
|
||||||
- ./config:/config:Z
|
- ./config:/config:Z
|
||||||
|
|||||||
@@ -14,9 +14,13 @@ services:
|
|||||||
network_mode: host
|
network_mode: host
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
# Change this to use a different port for the web interface (defaults is 8080)
|
# Optional: Change this to use a different port for the web interface (defaults is 8080)
|
||||||
- PORT=8080
|
- PORT=8080
|
||||||
|
# Optional: Bind to a specific IP address (default: 0.0.0.0)
|
||||||
|
# This is useful when running with host networking to prevent exposing
|
||||||
|
# the web UI to unprotected networks. Set to a specific IP (e.g., 127.0.0.1
|
||||||
|
# or a specific interface IP) to restrict access.
|
||||||
|
# - BIND_ADDRESS=127.0.0.1
|
||||||
volumes:
|
volumes:
|
||||||
# Required for fail2ban-ui: Stores SQLite database, application settings, and SSH keys of the fail2ban-ui container
|
# Required for fail2ban-ui: Stores SQLite database, application settings, and SSH keys of the fail2ban-ui container
|
||||||
- /opt/podman-fail2ban-ui:/config:Z
|
- /opt/podman-fail2ban-ui:/config:Z
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
@@ -1396,6 +1397,22 @@ func GetPortFromEnv() (int, bool) {
|
|||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetBindAddressFromEnv returns the BIND_ADDRESS environment variable value if set, and whether it's set
|
||||||
|
// If not set, returns "0.0.0.0" as the default bind address
|
||||||
|
// Validates that the address is a valid IP address format
|
||||||
|
func GetBindAddressFromEnv() (string, bool) {
|
||||||
|
bindAddrEnv := os.Getenv("BIND_ADDRESS")
|
||||||
|
if bindAddrEnv == "" {
|
||||||
|
return "0.0.0.0", false
|
||||||
|
}
|
||||||
|
// Validate that it's a valid IP address format using net.ParseIP
|
||||||
|
if ip := net.ParseIP(bindAddrEnv); ip != nil {
|
||||||
|
return bindAddrEnv, true
|
||||||
|
}
|
||||||
|
// Invalid format, return default
|
||||||
|
return "0.0.0.0", false
|
||||||
|
}
|
||||||
|
|
||||||
func GetSettings() AppSettings {
|
func GetSettings() AppSettings {
|
||||||
settingsLock.RLock()
|
settingsLock.RLock()
|
||||||
defer settingsLock.RUnlock()
|
defer settingsLock.RUnlock()
|
||||||
|
|||||||
Reference in New Issue
Block a user