Feat: add storage block(first methods to db, migrations, models) add nginx parser with regular expression, add to deps sqlite driver
All checks were successful
CI.yml / build (push) Successful in 1m51s
All checks were successful
CI.yml / build (push) Successful in 1m51s
This commit is contained in:
41
internal/parser/NginxParser.go
Normal file
41
internal/parser/NginxParser.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"github.com/d3m0k1d/BanForge/internal/logger"
|
||||
"github.com/d3m0k1d/BanForge/internal/storage"
|
||||
)
|
||||
|
||||
type NginxParser struct {
|
||||
pattern *regexp.Regexp
|
||||
logger *logger.Logger
|
||||
}
|
||||
|
||||
func NewNginxParser() *NginxParser {
|
||||
pattern := regexp.MustCompile(
|
||||
`^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*\[(.*?)\]\s+"(\w+)\s+(.*?)\s+HTTP.*"\s+(\d+)`,
|
||||
)
|
||||
return &NginxParser{
|
||||
pattern: pattern,
|
||||
logger: logger.New(false),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *NginxParser) Parse(line string) (*storage.LogEntry, error) {
|
||||
// Group 1: IP, Group 2: Timestamp, Group 3: Method, Group 4: Path, Group 5: Status
|
||||
matches := p.pattern.FindStringSubmatch(line)
|
||||
if matches == nil {
|
||||
return nil, fmt.Errorf("invalid log format")
|
||||
}
|
||||
|
||||
return &storage.LogEntry{
|
||||
Service: "nginx",
|
||||
IP: matches[1],
|
||||
Path: &matches[4],
|
||||
Status: &matches[5],
|
||||
Method: &matches[3],
|
||||
Reason: nil,
|
||||
}, nil
|
||||
}
|
||||
42
internal/storage/db.go
Normal file
42
internal/storage/db.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/d3m0k1d/BanForge/internal/logger"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
logger *logger.Logger
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewDB(path string) (*DB, error) {
|
||||
db, err := sql.Open("sqlite3", "/var/lib/banforge/storage.db")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &DB{
|
||||
logger: logger.New(false),
|
||||
db: db,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (d *DB) Close() error {
|
||||
d.logger.Info("Closing database connection")
|
||||
err := d.db.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DB) CreateTable() error {
|
||||
_, err := d.db.Exec(CreateTables)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.logger.Info("Created tables")
|
||||
return nil
|
||||
}
|
||||
27
internal/storage/migrations.go
Normal file
27
internal/storage/migrations.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package storage
|
||||
|
||||
const CreateTables = `
|
||||
|
||||
CREATE TABLE IF NOT EXISTS requests (
|
||||
id INTEGER PRIMARY KEY,
|
||||
service TEXT NOT NULL,
|
||||
ip TEXT NOT NULL,
|
||||
path TEXT,
|
||||
method TEXT,
|
||||
status TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS bans (
|
||||
id INTEGER PRIMARY KEY,
|
||||
ip TEXT UNIQUE NOT NULL,
|
||||
reason TEXT,
|
||||
banned_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_service ON requests(service);
|
||||
CREATE INDEX IF NOT EXISTS idx_ip ON requests(ip);
|
||||
CREATE INDEX IF NOT EXISTS idx_status ON requests(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_created_at ON requests(created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_ban_ip ON bans(ip);
|
||||
`
|
||||
15
internal/storage/models.go
Normal file
15
internal/storage/models.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package storage
|
||||
|
||||
type LogEntry struct {
|
||||
Service string
|
||||
IP string
|
||||
Path *string
|
||||
Status *string
|
||||
Method *string
|
||||
Reason *string
|
||||
}
|
||||
|
||||
type Ban struct {
|
||||
IP string
|
||||
Reason *string
|
||||
}
|
||||
Reference in New Issue
Block a user