mirror of
https://github.com/swissmakers/fail2ban-ui.git
synced 2026-04-17 14:03:15 +02:00
Fix console logger file and reorder routes, add comments for interlnal/external APIs
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
// Fail2ban UI - A Swiss made, management interface for Fail2ban.
|
// Fail2ban UI - A Swiss made, management interface for Fail2ban.
|
||||||
//
|
//
|
||||||
// Copyright (C) 2025 Swissmakers GmbH (https://swissmakers.ch)
|
// Copyright (C) 2026 Swissmakers GmbH (https://swissmakers.ch)
|
||||||
//
|
//
|
||||||
// Licensed under the GNU General Public License, Version 3 (GPL-3.0)
|
// Licensed under the GNU General Public License, Version 3 (GPL-3.0)
|
||||||
// You may not use this file except in compliance with the License.
|
// You may not use this file except in compliance with the License.
|
||||||
@@ -25,8 +25,11 @@ import (
|
|||||||
"github.com/swissmakers/fail2ban-ui/internal/config"
|
"github.com/swissmakers/fail2ban-ui/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConsoleLogWriter is a multi-writer that writes to both the original log output
|
// =========================================================================
|
||||||
// and broadcasts to WebSocket clients when console output is enabled
|
// Console log writer that mirrors log output to the WebSocket hub
|
||||||
|
// so the browser can display server logs in real time.
|
||||||
|
// =========================================================================
|
||||||
|
|
||||||
type ConsoleLogWriter struct {
|
type ConsoleLogWriter struct {
|
||||||
originalWriter io.Writer
|
originalWriter io.Writer
|
||||||
hub *Hub
|
hub *Hub
|
||||||
@@ -34,7 +37,6 @@ type ConsoleLogWriter struct {
|
|||||||
enabled bool
|
enabled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConsoleLogWriter creates a new console log writer
|
|
||||||
func NewConsoleLogWriter(hub *Hub, originalWriter io.Writer) *ConsoleLogWriter {
|
func NewConsoleLogWriter(hub *Hub, originalWriter io.Writer) *ConsoleLogWriter {
|
||||||
return &ConsoleLogWriter{
|
return &ConsoleLogWriter{
|
||||||
originalWriter: originalWriter,
|
originalWriter: originalWriter,
|
||||||
@@ -43,25 +45,22 @@ func NewConsoleLogWriter(hub *Hub, originalWriter io.Writer) *ConsoleLogWriter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetEnabled enables or disables console output broadcasting
|
|
||||||
func (c *ConsoleLogWriter) SetEnabled(enabled bool) {
|
func (c *ConsoleLogWriter) SetEnabled(enabled bool) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
c.enabled = enabled
|
c.enabled = enabled
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write implements io.Writer interface
|
// Write sends bytes to the original writer and, when enabled,
|
||||||
|
// broadcasts the trimmed line to WebSocket clients.
|
||||||
func (c *ConsoleLogWriter) Write(p []byte) (n int, err error) {
|
func (c *ConsoleLogWriter) Write(p []byte) (n int, err error) {
|
||||||
// Always write to original writer
|
|
||||||
n, err = c.originalWriter.Write(p)
|
n, err = c.originalWriter.Write(p)
|
||||||
|
|
||||||
// Broadcast to WebSocket if enabled
|
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
enabled := c.enabled
|
enabled := c.enabled
|
||||||
c.mu.RUnlock()
|
c.mu.RUnlock()
|
||||||
|
|
||||||
if enabled && c.hub != nil {
|
if enabled && c.hub != nil {
|
||||||
// Remove trailing newline for cleaner display
|
|
||||||
message := string(p)
|
message := string(p)
|
||||||
if len(message) > 0 && message[len(message)-1] == '\n' {
|
if len(message) > 0 && message[len(message)-1] == '\n' {
|
||||||
message = message[:len(message)-1]
|
message = message[:len(message)-1]
|
||||||
@@ -70,26 +69,23 @@ func (c *ConsoleLogWriter) Write(p []byte) (n int, err error) {
|
|||||||
c.hub.BroadcastConsoleLog(message)
|
c.hub.BroadcastConsoleLog(message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// =========================================================================
|
||||||
|
// Global Setup
|
||||||
|
// =========================================================================
|
||||||
|
|
||||||
var globalConsoleLogWriter *ConsoleLogWriter
|
var globalConsoleLogWriter *ConsoleLogWriter
|
||||||
var consoleLogWriterOnce sync.Once
|
var consoleLogWriterOnce sync.Once
|
||||||
|
|
||||||
// SetupConsoleLogWriter sets up the console log writer and replaces the standard log output
|
|
||||||
// This captures all log.Printf, log.Println, etc. output
|
|
||||||
func SetupConsoleLogWriter(hub *Hub) {
|
func SetupConsoleLogWriter(hub *Hub) {
|
||||||
consoleLogWriterOnce.Do(func() {
|
consoleLogWriterOnce.Do(func() {
|
||||||
// Create a multi-writer that writes to both original stdout and our console writer
|
|
||||||
globalConsoleLogWriter = NewConsoleLogWriter(hub, os.Stdout)
|
globalConsoleLogWriter = NewConsoleLogWriter(hub, os.Stdout)
|
||||||
|
|
||||||
// Replace log output - this captures all log.Printf, log.Println, etc.
|
|
||||||
log.SetOutput(globalConsoleLogWriter)
|
log.SetOutput(globalConsoleLogWriter)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateConsoleLogEnabled updates the enabled state based on settings
|
|
||||||
func UpdateConsoleLogEnabled() {
|
func UpdateConsoleLogEnabled() {
|
||||||
if globalConsoleLogWriter != nil {
|
if globalConsoleLogWriter != nil {
|
||||||
settings := config.GetSettings()
|
settings := config.GetSettings()
|
||||||
@@ -97,7 +93,6 @@ func UpdateConsoleLogEnabled() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetConsoleLogEnabled directly sets the enabled state
|
|
||||||
func SetConsoleLogEnabled(enabled bool) {
|
func SetConsoleLogEnabled(enabled bool) {
|
||||||
if globalConsoleLogWriter != nil {
|
if globalConsoleLogWriter != nil {
|
||||||
globalConsoleLogWriter.SetEnabled(enabled)
|
globalConsoleLogWriter.SetEnabled(enabled)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Fail2ban UI - A Swiss made, management interface for Fail2ban.
|
// Fail2ban UI - A Swiss made, management interface for Fail2ban.
|
||||||
//
|
//
|
||||||
// Copyright (C) 2025 Swissmakers GmbH (https://swissmakers.ch)
|
// Copyright (C) 2026 Swissmakers GmbH (https://swissmakers.ch)
|
||||||
//
|
//
|
||||||
// Licensed under the GNU General Public License, Version 3 (GPL-3.0)
|
// Licensed under the GNU General Public License, Version 3 (GPL-3.0)
|
||||||
// You may not use this file except in compliance with the License.
|
// You may not use this file except in compliance with the License.
|
||||||
@@ -20,12 +20,14 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RegisterRoutes sets up the routes for the Fail2ban UI.
|
// =========================================================================
|
||||||
|
// Route Registration
|
||||||
|
// =========================================================================
|
||||||
|
|
||||||
func RegisterRoutes(r *gin.Engine, hub *Hub) {
|
func RegisterRoutes(r *gin.Engine, hub *Hub) {
|
||||||
// Set the global WebSocket hub
|
|
||||||
SetWebSocketHub(hub)
|
SetWebSocketHub(hub)
|
||||||
|
|
||||||
// Public authentication routes (no auth required)
|
// Public routes; do not require authentication
|
||||||
authRoutes := r.Group("/auth")
|
authRoutes := r.Group("/auth")
|
||||||
{
|
{
|
||||||
authRoutes.GET("/login", LoginHandler)
|
authRoutes.GET("/login", LoginHandler)
|
||||||
@@ -35,40 +37,52 @@ func RegisterRoutes(r *gin.Engine, hub *Hub) {
|
|||||||
authRoutes.GET("/user", UserInfoHandler)
|
authRoutes.GET("/user", UserInfoHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply authentication middleware to all routes
|
// Initialize authentication middleware; all routes below here require authentication
|
||||||
r.Use(AuthMiddleware())
|
r.Use(AuthMiddleware())
|
||||||
|
|
||||||
// Render the dashboard
|
// Default currently "/" renders the dashboard -> TODO: To run f2b-UI on a different (sub)-path, we need to prefix that.
|
||||||
r.GET("/", renderIndexPage)
|
r.GET("/", renderIndexPage)
|
||||||
|
|
||||||
|
// API routes group
|
||||||
api := r.Group("/api")
|
api := r.Group("/api")
|
||||||
{
|
{
|
||||||
|
// Internal call from frontend to the Fail2ban-UI backend to get the summary of the servers (banned IPs per active jail)
|
||||||
api.GET("/summary", SummaryHandler)
|
api.GET("/summary", SummaryHandler)
|
||||||
|
|
||||||
|
// External API calls from Fail2ban servers that notify Fail2Ban-UI backend about ban/unban events that where triggered.
|
||||||
|
api.POST("/ban", BanNotificationHandler)
|
||||||
|
api.POST("/unban", UnbanNotificationHandler)
|
||||||
|
|
||||||
|
// Internal API calls from frontend (e.g. manual actions) to backend to execute Ban / Unban
|
||||||
api.POST("/jails/:jail/unban/:ip", UnbanIPHandler)
|
api.POST("/jails/:jail/unban/:ip", UnbanIPHandler)
|
||||||
api.POST("/jails/:jail/ban/:ip", BanIPHandler)
|
api.POST("/jails/:jail/ban/:ip", BanIPHandler)
|
||||||
|
|
||||||
// Routes for jail-filter management (TODO: rename API-call)
|
// Internal API calls for jail-filter management (TODO: rename API-call)
|
||||||
api.GET("/jails/:jail/config", GetJailFilterConfigHandler)
|
api.GET("/jails/:jail/config", GetJailFilterConfigHandler)
|
||||||
api.POST("/jails/:jail/config", SetJailFilterConfigHandler)
|
api.POST("/jails/:jail/config", SetJailFilterConfigHandler)
|
||||||
api.POST("/jails/:jail/logpath/test", TestLogpathHandler)
|
api.POST("/jails/:jail/logpath/test", TestLogpathHandler)
|
||||||
|
|
||||||
// Routes for jail management
|
|
||||||
api.GET("/jails/manage", ManageJailsHandler)
|
api.GET("/jails/manage", ManageJailsHandler)
|
||||||
api.POST("/jails/manage", UpdateJailManagementHandler)
|
api.POST("/jails/manage", UpdateJailManagementHandler)
|
||||||
api.POST("/jails", CreateJailHandler)
|
api.POST("/jails", CreateJailHandler)
|
||||||
api.DELETE("/jails/:jail", DeleteJailHandler)
|
api.DELETE("/jails/:jail", DeleteJailHandler)
|
||||||
|
|
||||||
// Version and update check (only on page load; UPDATE_CHECK=false disables GitHub request)
|
// Internal API calls for filter management
|
||||||
api.GET("/version", GetVersionHandler)
|
api.GET("/filters", ListFiltersHandler)
|
||||||
|
api.GET("/filters/:filter/content", GetFilterContentHandler)
|
||||||
|
api.POST("/filters/test", TestFilterHandler)
|
||||||
|
api.POST("/filters", CreateFilterHandler)
|
||||||
|
api.DELETE("/filters/:filter", DeleteFilterHandler)
|
||||||
|
|
||||||
// Settings endpoints
|
// Internal API calls for Fail2ban-UI settings
|
||||||
api.GET("/settings", GetSettingsHandler)
|
api.GET("/settings", GetSettingsHandler)
|
||||||
api.POST("/settings", UpdateSettingsHandler)
|
api.POST("/settings", UpdateSettingsHandler)
|
||||||
api.POST("/settings/test-email", TestEmailHandler)
|
api.POST("/settings/test-email", TestEmailHandler)
|
||||||
|
|
||||||
|
// Internal API calls for advanced actions
|
||||||
api.GET("/advanced-actions/blocks", ListPermanentBlocksHandler)
|
api.GET("/advanced-actions/blocks", ListPermanentBlocksHandler)
|
||||||
api.POST("/advanced-actions/test", AdvancedActionsTestHandler)
|
api.POST("/advanced-actions/test", AdvancedActionsTestHandler)
|
||||||
|
|
||||||
// Fail2ban servers management
|
// Internal API calls for Fail2ban-UI server management
|
||||||
api.GET("/servers", ListServersHandler)
|
api.GET("/servers", ListServersHandler)
|
||||||
api.POST("/servers", UpsertServerHandler)
|
api.POST("/servers", UpsertServerHandler)
|
||||||
api.DELETE("/servers/:id", DeleteServerHandler)
|
api.DELETE("/servers/:id", DeleteServerHandler)
|
||||||
@@ -76,26 +90,18 @@ func RegisterRoutes(r *gin.Engine, hub *Hub) {
|
|||||||
api.GET("/ssh/keys", ListSSHKeysHandler)
|
api.GET("/ssh/keys", ListSSHKeysHandler)
|
||||||
api.POST("/servers/:id/test", TestServerHandler)
|
api.POST("/servers/:id/test", TestServerHandler)
|
||||||
|
|
||||||
// Filter debugger endpoints
|
// Internal API to restart Fail2ban
|
||||||
api.GET("/filters", ListFiltersHandler)
|
|
||||||
api.GET("/filters/:filter/content", GetFilterContentHandler)
|
|
||||||
api.POST("/filters/test", TestFilterHandler)
|
|
||||||
api.POST("/filters", CreateFilterHandler)
|
|
||||||
api.DELETE("/filters/:filter", DeleteFilterHandler)
|
|
||||||
|
|
||||||
// Restart endpoint
|
|
||||||
api.POST("/fail2ban/restart", RestartFail2banHandler)
|
api.POST("/fail2ban/restart", RestartFail2banHandler)
|
||||||
|
|
||||||
// Handle Fail2Ban notifications
|
// Internal API calls to get the stats of the bans
|
||||||
api.POST("/ban", BanNotificationHandler)
|
|
||||||
api.POST("/unban", UnbanNotificationHandler)
|
|
||||||
|
|
||||||
// Internal database overview
|
|
||||||
api.GET("/events/bans", ListBanEventsHandler)
|
api.GET("/events/bans", ListBanEventsHandler)
|
||||||
api.GET("/events/bans/stats", BanStatisticsHandler)
|
api.GET("/events/bans/stats", BanStatisticsHandler)
|
||||||
api.GET("/events/bans/insights", BanInsightsHandler)
|
api.GET("/events/bans/insights", BanInsightsHandler)
|
||||||
|
|
||||||
// WebSocket endpoint
|
// WebSocket endpoint
|
||||||
api.GET("/ws", WebSocketHandler(hub))
|
api.GET("/ws", WebSocketHandler(hub))
|
||||||
|
|
||||||
|
// Internal & external API to get the version of the Fail2ban-UI and check for updates
|
||||||
|
api.GET("/version", GetVersionHandler)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user