From 7f54db0cd45ed8de1f4d217b1fc74822c8e5a04c Mon Sep 17 00:00:00 2001 From: d3m0k1d Date: Thu, 19 Feb 2026 10:53:55 +0300 Subject: [PATCH] feat: add new method and for db req and add to template max retry --- internal/config/template.go | 2 ++ internal/config/types.go | 1 + internal/judge/judge.go | 9 -------- internal/storage/requests_db.go | 41 +++++++++++++++++++++++++++++++++ internal/storage/writer_test.go | 18 --------------- 5 files changed, 44 insertions(+), 27 deletions(-) diff --git a/internal/config/template.go b/internal/config/template.go index 21a7fcc..4df0948 100644 --- a/internal/config/template.go +++ b/internal/config/template.go @@ -12,11 +12,13 @@ config = "/etc/nftables.conf" name = "nginx" logging = "file" log_path = "/var/log/nginx/access.log" +max_retry = 3 enabled = true [[service]] name = "nginx" logging = "journald" log_path = "/var/log/nginx/access.log" +max_retry = 3 enabled = false ` diff --git a/internal/config/types.go b/internal/config/types.go index 5ad8353..73828d1 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -28,5 +28,6 @@ type Rule struct { Path string `toml:"path"` Status string `toml:"status"` Method string `toml:"method"` + MaxRetry int `toml:"max_retry"` BanTime string `toml:"ban_time"` } diff --git a/internal/judge/judge.go b/internal/judge/judge.go index f474a83..df82567 100644 --- a/internal/judge/judge.go +++ b/internal/judge/judge.go @@ -76,14 +76,6 @@ func (j *Judge) Tribunal() { statusMatch := rule.Status == "" || entry.Status == rule.Status pathMatch := matchPath(entry.Path, rule.Path) - j.logger.Debug( - "Testing rule", - "rule", rule.Name, - "method_match", methodMatch, - "status_match", statusMatch, - "path_match", pathMatch, - ) - if methodMatch && statusMatch && pathMatch { ruleMatched = true j.logger.Info("Rule matched", "rule", rule.Name, "ip", entry.IP) @@ -93,7 +85,6 @@ func (j *Judge) Tribunal() { j.logger.Error("Failed to check ban status", "ip", entry.IP, "error", err) break } - if banned { j.logger.Info("IP already banned", "ip", entry.IP) j.resultCh <- entry diff --git a/internal/storage/requests_db.go b/internal/storage/requests_db.go index 379a592..eee3718 100644 --- a/internal/storage/requests_db.go +++ b/internal/storage/requests_db.go @@ -28,3 +28,44 @@ func NewRequestsWr() (*RequestWriter, error) { db: db, }, nil } + +type RequestReader struct { + logger *logger.Logger + db *sql.DB +} + +func NewRequestsRd() (*RequestReader, error) { + db, err := sql.Open( + "sqlite", + buildSqliteDsn(ReqDBPath, pragmas), + ) + if err != nil { + return nil, err + } + db.SetMaxOpenConns(1) + db.SetMaxIdleConns(1) + db.SetConnMaxLifetime(0) + return &RequestReader{ + logger: logger.New(false), + db: db, + }, nil +} + +func (r *RequestReader) IsMaxRetryExceeded(ip string, max_retry int) (bool, error) { + row, err := r.db.Query("SELECT COUNT(*) FROM requests WHERE ip = ?", ip) + if err != nil { + r.logger.Error("error scan" + err.Error()) + return false, err + } + if row.Next() { + var count int + if err := row.Scan(&count); err != nil { + r.logger.Error("error scan" + err.Error()) + return false, err + } + if count >= max_retry { + return true, nil + } + } + return true, nil +} diff --git a/internal/storage/writer_test.go b/internal/storage/writer_test.go index 68b31e5..1509817 100644 --- a/internal/storage/writer_test.go +++ b/internal/storage/writer_test.go @@ -299,21 +299,3 @@ func (w *RequestWriter) CreateTable() error { w.logger.Info("Created requests table") return nil } - -func (w *RequestWriter) Close() error { - w.logger.Info("Closing request database connection") - err := w.db.Close() - if err != nil { - return err - } - return nil -} - -func (w *RequestWriter) GetRequestCount() (int, error) { - var count int - err := w.db.QueryRow("SELECT COUNT(*) FROM requests").Scan(&count) - if err != nil { - return 0, err - } - return count, nil -}