2 Commits

Author SHA1 Message Date
d3m0k1d
17faaa5c27 Fix errchecl
All checks were successful
CI.yml / build (push) Successful in 1m45s
2026-01-13 21:03:50 +03:00
d3m0k1d
f0180b4bbe feat: fix db and recode judge 2026-01-13 21:03:10 +03:00
5 changed files with 60 additions and 43 deletions

View File

@@ -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,37 @@ 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))
err = j.Blocker.Ban(entry.IP)
if err != nil {
j.logger.Error(fmt.Sprintf("Failed to ban IP: %v", err))
}
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
}

View File

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

View File

@@ -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
}

View File

@@ -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"`
}

View File

@@ -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 {