From 46dc54f5a7b3a1fc0e17ad4a73479640d54f3979 Mon Sep 17 00:00:00 2001 From: d3m0k1d Date: Mon, 19 Jan 2026 18:57:11 +0300 Subject: [PATCH] chore: add new formatter to .golangci.yml --- .golangci.yml | 2 ++ cmd/banforge/command/daemon.go | 10 +++++++++- cmd/banforge/command/rule.go | 9 ++++++++- internal/config/appconf.go | 21 +++++++++++++++++++-- internal/judge/judge.go | 19 +++++++++++++++++-- internal/parser/NginxParser.go | 12 +++++++++++- internal/parser/parser.go | 4 +++- internal/storage/db.go | 14 +++++++++++--- 8 files changed, 80 insertions(+), 11 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index ae7edd9..fb9f9ed 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -12,10 +12,12 @@ linters: - govet - staticcheck - gosec + - nilerr formatters: enable: - gofmt - goimports + - golines diff --git a/cmd/banforge/command/daemon.go b/cmd/banforge/command/daemon.go index 1a17193..1aa85df 100644 --- a/cmd/banforge/command/daemon.go +++ b/cmd/banforge/command/daemon.go @@ -62,7 +62,15 @@ var DaemonCmd = &cobra.Command{ }() for _, svc := range cfg.Service { - log.Info("Processing service", "name", svc.Name, "enabled", svc.Enabled, "path", svc.LogPath) + log.Info( + "Processing service", + "name", + svc.Name, + "enabled", + svc.Enabled, + "path", + svc.LogPath, + ) if !svc.Enabled { log.Info("Service disabled, skipping", "name", svc.Name) diff --git a/cmd/banforge/command/rule.go b/cmd/banforge/command/rule.go index ac200aa..3ee513a 100644 --- a/cmd/banforge/command/rule.go +++ b/cmd/banforge/command/rule.go @@ -60,7 +60,14 @@ var ListCmd = &cobra.Command{ os.Exit(1) } for _, rule := range r { - fmt.Printf("Name: %s\nService: %s\nPath: %s\nStatus: %s\nMethod: %s\n\n", rule.Name, rule.ServiceName, rule.Path, rule.Status, rule.Method) + fmt.Printf( + "Name: %s\nService: %s\nPath: %s\nStatus: %s\nMethod: %s\n\n", + rule.Name, + rule.ServiceName, + rule.Path, + rule.Status, + rule.Method, + ) } }, } diff --git a/internal/config/appconf.go b/internal/config/appconf.go index f7a6a1b..69933ee 100644 --- a/internal/config/appconf.go +++ b/internal/config/appconf.go @@ -25,7 +25,14 @@ func LoadRuleConfig() ([]Rule, error) { return cfg.Rules, nil } -func NewRule(Name string, ServiceName string, Path string, Status string, Method string, ttl string) error { +func NewRule( + Name string, + ServiceName string, + Path string, + Status string, + Method string, + ttl string, +) error { r, err := LoadRuleConfig() if err != nil { r = []Rule{} @@ -34,7 +41,17 @@ func NewRule(Name string, ServiceName string, Path string, Status string, Method fmt.Printf("Rule name can't be empty\n") return nil } - r = append(r, Rule{Name: Name, ServiceName: ServiceName, Path: Path, Status: Status, Method: Method, BanTime: ttl}) + r = append( + r, + Rule{ + Name: Name, + ServiceName: ServiceName, + Path: Path, + Status: Status, + Method: Method, + BanTime: ttl, + }, + ) file, err := os.Create("/etc/banforge/rules.toml") if err != nil { return err diff --git a/internal/judge/judge.go b/internal/judge/judge.go index 14d896e..b55ba21 100644 --- a/internal/judge/judge.go +++ b/internal/judge/judge.go @@ -51,7 +51,16 @@ func (j *Judge) ProcessUnviewed() error { }() 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) + err = rows.Scan( + &entry.ID, + &entry.Service, + &entry.IP, + &entry.Path, + &entry.Status, + &entry.Method, + &entry.IsViewed, + &entry.CreatedAt, + ) if err != nil { j.logger.Error(fmt.Sprintf("Failed to scan database row: %v", err)) continue @@ -64,7 +73,13 @@ func (j *Judge) ProcessUnviewed() error { (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.logger.Info( + fmt.Sprintf( + "Rule matched for IP: %s, Service: %s", + entry.IP, + entry.Service, + ), + ) ban_status, err := j.db.IsBanned(entry.IP) if err != nil { j.logger.Error(fmt.Sprintf("Failed to check ban status: %v", err)) diff --git a/internal/parser/NginxParser.go b/internal/parser/NginxParser.go index e652d3c..f4c6541 100644 --- a/internal/parser/NginxParser.go +++ b/internal/parser/NginxParser.go @@ -42,7 +42,17 @@ func (p *NginxParser) Parse(eventCh <-chan Event, resultCh chan<- *storage.LogEn Method: method, IsViewed: false, } - p.logger.Info("Parsed nginx log entry", "ip", matches[1], "path", path, "status", status, "method", method) + p.logger.Info( + "Parsed nginx log entry", + "ip", + matches[1], + "path", + path, + "status", + status, + "method", + method, + ) } }() } diff --git a/internal/parser/parser.go b/internal/parser/parser.go index 7f37c5b..e4b062c 100644 --- a/internal/parser/parser.go +++ b/internal/parser/parser.go @@ -22,7 +22,9 @@ type Scanner struct { } func NewScanner(path string) (*Scanner, error) { - file, err := os.Open(path) // #nosec G304 -- admin tool, runs as root, path controlled by operator + file, err := os.Open( + path, + ) // #nosec G304 -- admin tool, runs as root, path controlled by operator if err != nil { return nil, err } diff --git a/internal/storage/db.go b/internal/storage/db.go index ad92170..2dff4eb 100644 --- a/internal/storage/db.go +++ b/internal/storage/db.go @@ -19,7 +19,10 @@ type DB struct { } func NewDB() (*DB, error) { - db, err := sql.Open("sqlite3", "/var/lib/banforge/storage.db?mode=rwc&_journal_mode=WAL&_busy_timeout=10000&cache=shared") + db, err := sql.Open( + "sqlite3", + "/var/lib/banforge/storage.db?mode=rwc&_journal_mode=WAL&_busy_timeout=10000&cache=shared", + ) if err != nil { return nil, err } @@ -52,7 +55,9 @@ func (d *DB) CreateTable() error { } func (d *DB) SearchUnViewed() (*sql.Rows, error) { - rows, err := d.db.Query("SELECT id, service, ip, path, status, method, viewed, created_at FROM requests WHERE viewed = 0") + rows, err := d.db.Query( + "SELECT id, service, ip, path, status, method, viewed, created_at FROM requests WHERE viewed = 0", + ) if err != nil { d.logger.Error("Failed to query database") return nil, err @@ -136,7 +141,10 @@ func (d *DB) BanList() error { func (d *DB) CheckExpiredBans() ([]string, error) { var ips []string - rows, err := d.db.Query("SELECT ip FROM bans WHERE expired_at < ?", time.Now().Format(time.RFC3339)) + rows, err := d.db.Query( + "SELECT ip FROM bans WHERE expired_at < ?", + time.Now().Format(time.RFC3339), + ) if err != nil { d.logger.Error("Failed to get ban list", "error", err) return nil, err