From f0180b4bbe352b5924b3f1a3fc474180b27ca40f Mon Sep 17 00:00:00 2001 From: d3m0k1d Date: Tue, 13 Jan 2026 21:03:10 +0300 Subject: [PATCH] feat: fix db and recode judge --- internal/judge/judge.go | 35 ++++++++++++++++++++++++++-------- internal/parser/NginxParser.go | 11 ++++++----- internal/storage/db.go | 9 +++++++++ internal/storage/models.go | 24 +++++++++++------------ internal/storage/writer.go | 21 +++----------------- 5 files changed, 57 insertions(+), 43 deletions(-) diff --git a/internal/judge/judge.go b/internal/judge/judge.go index 9b36257..099f1fe 100644 --- a/internal/judge/judge.go +++ b/internal/judge/judge.go @@ -12,7 +12,7 @@ import ( type Judge struct { db *storage.DB logger *logger.Logger - Blocker *blocker.BlockerEngine + Blocker blocker.BlockerEngine rulesByService map[string][]config.Rule } @@ -35,11 +35,11 @@ func (j *Judge) LoadRules(rules []config.Rule) { j.logger.Info("Rules loaded and indexed by service") } -func (j *Judge) ProcessUnviewed() ([]storage.LogEntry, error) { +func (j *Judge) ProcessUnviewed() error { rows, err := j.db.SearchUnViewed() if err != nil { j.logger.Error(fmt.Sprintf("Failed to query database: %v", err)) - return nil, err + return err } defer func() { err = rows.Close() @@ -48,8 +48,6 @@ func (j *Judge) ProcessUnviewed() ([]storage.LogEntry, error) { } }() - var entries []storage.LogEntry - for rows.Next() { var entry storage.LogEntry err = rows.Scan(&entry.ID, &entry.Service, &entry.IP, &entry.Path, &entry.Status, &entry.Method, &entry.IsViewed, &entry.CreatedAt) @@ -57,13 +55,34 @@ func (j *Judge) ProcessUnviewed() ([]storage.LogEntry, error) { j.logger.Error(fmt.Sprintf("Failed to scan database row: %v", err)) continue } - entries = append(entries, entry) + + rules, serviceExists := j.rulesByService[entry.Service] + if serviceExists { + for _, rule := range rules { + if (rule.Method == "" || entry.Method == rule.Method) && + (rule.Status == "" || entry.Status == rule.Status) && + (rule.Path == "" || entry.Path == rule.Path) { + + j.logger.Info(fmt.Sprintf("Rule matched for IP: %s, Service: %s", entry.IP, entry.Service)) + j.Blocker.Ban(entry.IP) + j.logger.Info(fmt.Sprintf("IP banned: %s", entry.IP)) + break + } + } + } + + err = j.db.MarkAsViewed(entry.ID) + if err != nil { + j.logger.Error(fmt.Sprintf("Failed to mark entry as viewed: %v", err)) + } else { + j.logger.Info(fmt.Sprintf("Entry marked as viewed: ID=%d", entry.ID)) + } } if err = rows.Err(); err != nil { j.logger.Error(fmt.Sprintf("Error iterating rows: %v", err)) - return nil, err + return err } - return entries, nil + return nil } diff --git a/internal/parser/NginxParser.go b/internal/parser/NginxParser.go index 62a90b9..26e6ecf 100644 --- a/internal/parser/NginxParser.go +++ b/internal/parser/NginxParser.go @@ -35,11 +35,12 @@ func (p *NginxParser) Parse(eventCh <-chan Event, resultCh chan<- *storage.LogEn method := matches[3] resultCh <- &storage.LogEntry{ - Service: "nginx", - IP: matches[1], - Path: &path, - Status: &status, - Method: &method, + Service: "nginx", + IP: matches[1], + Path: path, + Status: status, + Method: method, + IsViewed: false, } } }() diff --git a/internal/storage/db.go b/internal/storage/db.go index 1368134..aeafee8 100644 --- a/internal/storage/db.go +++ b/internal/storage/db.go @@ -49,3 +49,12 @@ func (d *DB) SearchUnViewed() (*sql.Rows, error) { } return rows, nil } + +func (d *DB) MarkAsViewed(id int) error { + _, err := d.db.Exec("UPDATE requests SET viewed = 1 WHERE id = ?", id) + if err != nil { + d.logger.Error("Failed to mark as viewed", "error", err) + return err + } + return nil +} diff --git a/internal/storage/models.go b/internal/storage/models.go index a1730fe..013a106 100644 --- a/internal/storage/models.go +++ b/internal/storage/models.go @@ -1,19 +1,19 @@ package storage type LogEntry struct { - ID int `db:"id"` - Service string `db:"service"` - IP string `db:"ip"` - Path *string `db:"path"` - Status *string `db:"status"` - Method *string `db:"method"` - IsViewed *bool `db:"viewed"` - CreatedAt string `db:"created_at"` + ID int `db:"id"` + Service string `db:"service"` + IP string `db:"ip"` + Path string `db:"path"` + Status string `db:"status"` + Method string `db:"method"` + IsViewed bool `db:"viewed"` + CreatedAt string `db:"created_at"` } type Ban struct { - ID int `db:"id"` - IP string `db:"ip"` - Reason *string `db:"reason"` - BannedAt string `db:"banned_at"` + ID int `db:"id"` + IP string `db:"ip"` + Reason string `db:"reason"` + BannedAt string `db:"banned_at"` } diff --git a/internal/storage/writer.go b/internal/storage/writer.go index a1110eb..479843b 100644 --- a/internal/storage/writer.go +++ b/internal/storage/writer.go @@ -6,28 +6,13 @@ import ( func Write(db *DB, resultCh <-chan *LogEntry) { for result := range resultCh { - path := "" - if result.Path != nil { - path = *result.Path - } - - status := "" - if result.Status != nil { - status = *result.Status - } - - method := "" - if result.Method != nil { - method = *result.Method - } - _, err := db.db.Exec( "INSERT INTO requests (service, ip, path, method, status, created_at) VALUES (?, ?, ?, ?, ?, ?)", result.Service, result.IP, - path, - method, - status, + result.Path, + result.Method, + result.Status, time.Now().Format(time.RFC3339), ) if err != nil {