feat: recode NginxParser, add writer to db
All checks were successful
CI.yml / build (push) Successful in 1m54s
All checks were successful
CI.yml / build (push) Successful in 1m54s
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package parser
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"github.com/d3m0k1d/BanForge/internal/logger"
|
"github.com/d3m0k1d/BanForge/internal/logger"
|
||||||
@@ -23,19 +22,26 @@ func NewNginxParser() *NginxParser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *NginxParser) Parse(line string) (*storage.LogEntry, error) {
|
func (p *NginxParser) Parse(eventCh <-chan Event, resultCh chan<- *storage.LogEntry) {
|
||||||
// Group 1: IP, Group 2: Timestamp, Group 3: Method, Group 4: Path, Group 5: Status
|
// Group 1: IP, Group 2: Timestamp, Group 3: Method, Group 4: Path, Group 5: Status
|
||||||
matches := p.pattern.FindStringSubmatch(line)
|
go func() {
|
||||||
|
for event := range eventCh {
|
||||||
|
matches := p.pattern.FindStringSubmatch(event.Data)
|
||||||
if matches == nil {
|
if matches == nil {
|
||||||
return nil, fmt.Errorf("invalid log format")
|
continue
|
||||||
}
|
}
|
||||||
|
path := matches[4]
|
||||||
|
status := matches[5]
|
||||||
|
method := matches[3]
|
||||||
|
|
||||||
return &storage.LogEntry{
|
resultCh <- &storage.LogEntry{
|
||||||
Service: "nginx",
|
Service: "nginx",
|
||||||
IP: matches[1],
|
IP: matches[1],
|
||||||
Path: &matches[4],
|
Path: &path,
|
||||||
Status: &matches[5],
|
Status: &status,
|
||||||
Method: &matches[3],
|
Method: &method,
|
||||||
Reason: nil,
|
Reason: nil,
|
||||||
}, nil
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ type DB struct {
|
|||||||
db *sql.DB
|
db *sql.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDB(path string) (*DB, error) {
|
func NewDB() (*DB, error) {
|
||||||
db, err := sql.Open("sqlite3", "/var/lib/banforge/storage.db")
|
db, err := sql.Open("sqlite3", "/var/lib/banforge/storage.db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
43
internal/storage/writer.go
Normal file
43
internal/storage/writer.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
reason := ""
|
||||||
|
if result.Reason != nil {
|
||||||
|
reason = *result.Reason
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := db.db.Exec(
|
||||||
|
"INSERT INTO requests (service, ip, path, method, status, reason, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||||
|
result.Service,
|
||||||
|
result.IP,
|
||||||
|
path,
|
||||||
|
method,
|
||||||
|
status,
|
||||||
|
reason,
|
||||||
|
time.Now().Format(time.RFC3339),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
db.logger.Error("Failed to write to database", "error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user