feat: improve reason string on db
All checks were successful
build / build (push) Successful in 3m8s

This commit is contained in:
d3m0k1d
2026-01-26 14:04:30 +03:00
parent e907fb0b1a
commit cadbbc9080
4 changed files with 11 additions and 11 deletions

View File

@@ -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",

View File

@@ -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),
)

View File

@@ -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() == "<nil>" {
@@ -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)
}