From 9247ad2dd5afef9096a2a524bd33c8b2b385494a Mon Sep 17 00:00:00 2001 From: Michael Reber Date: Thu, 30 Jan 2025 12:35:16 +0100 Subject: [PATCH] Improve JSON parsing and error-handling --- internal/config/settings.go | 13 +++-- pkg/web/handlers.go | 113 +++++++++++++++++++++++++----------- 2 files changed, 85 insertions(+), 41 deletions(-) diff --git a/internal/config/settings.go b/internal/config/settings.go index 7a05604..1225a91 100644 --- a/internal/config/settings.go +++ b/internal/config/settings.go @@ -291,12 +291,13 @@ norestored = 1 actionban = /usr/bin/curl -X POST http://127.0.0.1:8080/api/ban \ -H "Content-Type: application/json" \ - -d "{\"ip\": \"\", \ - \"jail\": \"\", \ - \"hostname\": \"\", \ - \"failures\": \"\", \ - \"whois\": \"%%(_whois_command)s\", \ - \"logs\": \"%%(_grep_logs)s\"}" + -d "$(jq -n --arg ip '' \ + --arg jail '' \ + --arg hostname '' \ + --arg failures '' \ + --arg whois "$(whois || echo 'missing whois program')" \ + --arg logs "$(grep -wF | )" \ + '{ip: $ip, jail: $jail, hostname: $hostname, failures: $failures, whois: $whois, logs: $logs}')" [Init] diff --git a/pkg/web/handlers.go b/pkg/web/handlers.go index 61ee32d..e6111f7 100644 --- a/pkg/web/handlers.go +++ b/pkg/web/handlers.go @@ -17,9 +17,11 @@ package web import ( + "bytes" "crypto/tls" "errors" "fmt" + "io" "log" "net" "net/http" @@ -108,14 +110,27 @@ func BanNotificationHandler(c *gin.Context) { Logs string `json:"logs"` } + // **DEBUGGING: Log Raw JSON Body** + body, _ := io.ReadAll(c.Request.Body) + log.Printf("📩 Incoming Ban Notification: %s\n", string(body)) + + // Rebind body so Gin can parse it again (important!) + c.Request.Body = io.NopCloser(bytes.NewBuffer(body)) + // Parse JSON request body if err := c.ShouldBindJSON(&request); err != nil { + log.Printf("❌ Invalid request: %v\n", err) c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request: " + err.Error()}) return } + // **DEBUGGING: Log Parsed Request** + log.Printf("✅ Parsed Ban Request - IP: %s, Jail: %s, Hostname: %s, Failures: %s", + request.IP, request.Jail, request.Hostname, request.Failures) + // Handle the Fail2Ban notification if err := HandleBanNotification(request.IP, request.Jail, request.Hostname, request.Failures, request.Whois, request.Logs); err != nil { + log.Printf("❌ Failed to process ban notification: %v\n", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to process ban notification: " + err.Error()}) return } @@ -498,43 +513,71 @@ func sendSMTPMessage(client *smtp.Client, from, to string, msg []byte) error { // * sendBanAlert Function : * // ******************************************************************* func sendBanAlert(ip, jail, hostname, failures, whois, logs, country string, settings config.AppSettings) error { - subject := fmt.Sprintf("[Fail2Ban] %s: banned %s from %s", jail, ip, hostname) + subject := fmt.Sprintf("[Fail2Ban] %s: Banned %s from %s", jail, ip, hostname) - // Ensure HTML email format + // Improved Responsive HTML Email body := fmt.Sprintf(` - - - - Fail2Ban Alert - - - -
-

🚨 Fail2Ban Alert

-

A new IP has been banned due to excessive failed login attempts.

-
-

📌 Banned IP: %s

-

🛡️ Jail Name: %s

-

🏠 Hostname: %s

-

🚫 Failed Attempts: %s

-

🌍 Country: %s

-
-

🔍 Whois Information:

-
%s
-

📄 Log Entries:

-
%s
- -
- - `, ip, jail, hostname, failures, country, whois, logs) + + + + +Fail2Ban Alert + + + +
+ +
+ Swissmakers GmbH +

🚨 Security Alert from Fail2Ban

+
+ + +
+

A new IP has been banned due to excessive failed login attempts.

+ +
+

📌 Banned IP: %s

+

🛡️ Jail Name: %s

+

🏠 Hostname: %s

+

🚫 Failed Attempts: %s

+

🌍 Country: %s

+
+ +

🔍 Whois Information:

+
%s
+ +

📄 Log Entries:

+
%s
+
+ + + +
+ +`, ip, jail, hostname, failures, country, whois, logs, time.Now().Year()) // Send the email return sendEmail(settings.Destemail, subject, body, settings)