From cadbbc9080b74260108a48bc852c52fc6e8faa7a Mon Sep 17 00:00:00 2001 From: d3m0k1d Date: Mon, 26 Jan 2026 14:04:30 +0300 Subject: [PATCH] feat: improve reason string on db --- cmd/banforge/command/fw.go | 2 +- internal/judge/judge.go | 2 +- internal/storage/ban_db.go | 4 ++-- internal/storage/ban_db_test.go | 14 +++++++------- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cmd/banforge/command/fw.go b/cmd/banforge/command/fw.go index 6e84115..c18e8f5 100644 --- a/cmd/banforge/command/fw.go +++ b/cmd/banforge/command/fw.go @@ -105,7 +105,7 @@ var BanCmd = &cobra.Command{ fmt.Println(err) os.Exit(1) } - err = db.AddBan(ip, ttl_fw) + err = db.AddBan(ip, ttl_fw, "manual ban") if err != nil { fmt.Println(err) os.Exit(1) diff --git a/internal/judge/judge.go b/internal/judge/judge.go index 4171cd9..f474a83 100644 --- a/internal/judge/judge.go +++ b/internal/judge/judge.go @@ -100,7 +100,7 @@ func (j *Judge) Tribunal() { break } - err = j.db_w.AddBan(entry.IP, rule.BanTime) + err = j.db_w.AddBan(entry.IP, rule.BanTime, rule.Name) if err != nil { j.logger.Error( "Failed to add ban to database", diff --git a/internal/storage/ban_db.go b/internal/storage/ban_db.go index 7d1d710..ca5b73c 100644 --- a/internal/storage/ban_db.go +++ b/internal/storage/ban_db.go @@ -41,7 +41,7 @@ func (d *BanWriter) CreateTable() error { return nil } -func (d *BanWriter) AddBan(ip string, ttl string) error { +func (d *BanWriter) AddBan(ip string, ttl string, reason string) error { duration, err := config.ParseDurationWithYears(ttl) if err != nil { d.logger.Error("Invalid duration format", "ttl", ttl, "error", err) @@ -54,7 +54,7 @@ func (d *BanWriter) AddBan(ip string, ttl string) error { _, err = d.db.Exec( "INSERT INTO bans (ip, reason, banned_at, expired_at) VALUES (?, ?, ?, ?)", ip, - "1", + reason, now.Format(time.RFC3339), expiredAt.Format(time.RFC3339), ) diff --git a/internal/storage/ban_db_test.go b/internal/storage/ban_db_test.go index b569730..e62f73d 100644 --- a/internal/storage/ban_db_test.go +++ b/internal/storage/ban_db_test.go @@ -26,7 +26,7 @@ func TestBanWriter_AddBan(t *testing.T) { ip := "192.168.1.1" ttl := "1h" - err = writer.AddBan(ip, ttl) + err = writer.AddBan(ip, ttl, "test") if err != nil { t.Errorf("AddBan failed: %v", err) } @@ -62,7 +62,7 @@ func TestBanWriter_RemoveBan(t *testing.T) { } ip := "192.168.1.2" - err = writer.AddBan(ip, "1h") + err = writer.AddBan(ip, "1h", "test") if err != nil { t.Fatalf("Failed to add ban: %v", err) } @@ -111,13 +111,13 @@ func TestBanWriter_RemoveExpiredBans(t *testing.T) { } expiredIP := "192.168.1.3" - err = writer.AddBan(expiredIP, "-1h") + err = writer.AddBan(expiredIP, "-1h", "tes") if err != nil { t.Fatalf("Failed to add expired ban: %v", err) } activeIP := "192.168.1.4" - err = writer.AddBan(activeIP, "1h") + err = writer.AddBan(activeIP, "1h", "test") if err != nil { t.Fatalf("Failed to add active ban: %v", err) } @@ -181,7 +181,7 @@ func TestBanReader_IsBanned(t *testing.T) { } ip := "192.168.1.5" - err = writer.AddBan(ip, "1h") + err = writer.AddBan(ip, "1h", "test") if err != nil { t.Fatalf("Failed to add ban: %v", err) } @@ -280,7 +280,7 @@ func TestBanWriter_AddBan_InvalidDuration(t *testing.T) { t.Fatalf("Failed to create table: %v", err) } - err = writer.AddBan("192.168.1.7", "invalid_duration") + err = writer.AddBan("192.168.1.7", "invalid_duration", "test") if err == nil { t.Error("Expected error for invalid duration") } else if err.Error() == "" || err.Error() == "" { @@ -306,7 +306,7 @@ func TestMultipleBans(t *testing.T) { ips := []string{"192.168.1.8", "192.168.1.9", "192.168.1.10"} for _, ip := range ips { - err := writer.AddBan(ip, "1h") + err := writer.AddBan(ip, "1h", "test") if err != nil { t.Errorf("Failed to add ban for IP %s: %v", ip, err) }