Compare commits
2 Commits
b2d03a4008
...
17faaa5c27
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
17faaa5c27 | ||
|
|
f0180b4bbe |
@@ -12,7 +12,7 @@ import (
|
|||||||
type Judge struct {
|
type Judge struct {
|
||||||
db *storage.DB
|
db *storage.DB
|
||||||
logger *logger.Logger
|
logger *logger.Logger
|
||||||
Blocker *blocker.BlockerEngine
|
Blocker blocker.BlockerEngine
|
||||||
rulesByService map[string][]config.Rule
|
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")
|
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()
|
rows, err := j.db.SearchUnViewed()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
j.logger.Error(fmt.Sprintf("Failed to query database: %v", err))
|
j.logger.Error(fmt.Sprintf("Failed to query database: %v", err))
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
err = rows.Close()
|
err = rows.Close()
|
||||||
@@ -48,8 +48,6 @@ func (j *Judge) ProcessUnviewed() ([]storage.LogEntry, error) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var entries []storage.LogEntry
|
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var entry storage.LogEntry
|
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)
|
||||||
@@ -57,13 +55,37 @@ func (j *Judge) ProcessUnviewed() ([]storage.LogEntry, error) {
|
|||||||
j.logger.Error(fmt.Sprintf("Failed to scan database row: %v", err))
|
j.logger.Error(fmt.Sprintf("Failed to scan database row: %v", err))
|
||||||
continue
|
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 {
|
if err = rows.Err(); err != nil {
|
||||||
j.logger.Error(fmt.Sprintf("Error iterating rows: %v", err))
|
j.logger.Error(fmt.Sprintf("Error iterating rows: %v", err))
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return entries, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,11 +35,12 @@ func (p *NginxParser) Parse(eventCh <-chan Event, resultCh chan<- *storage.LogEn
|
|||||||
method := matches[3]
|
method := matches[3]
|
||||||
|
|
||||||
resultCh <- &storage.LogEntry{
|
resultCh <- &storage.LogEntry{
|
||||||
Service: "nginx",
|
Service: "nginx",
|
||||||
IP: matches[1],
|
IP: matches[1],
|
||||||
Path: &path,
|
Path: path,
|
||||||
Status: &status,
|
Status: status,
|
||||||
Method: &method,
|
Method: method,
|
||||||
|
IsViewed: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
@@ -49,3 +49,12 @@ func (d *DB) SearchUnViewed() (*sql.Rows, error) {
|
|||||||
}
|
}
|
||||||
return rows, nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,19 +1,19 @@
|
|||||||
package storage
|
package storage
|
||||||
|
|
||||||
type LogEntry struct {
|
type LogEntry struct {
|
||||||
ID int `db:"id"`
|
ID int `db:"id"`
|
||||||
Service string `db:"service"`
|
Service string `db:"service"`
|
||||||
IP string `db:"ip"`
|
IP string `db:"ip"`
|
||||||
Path *string `db:"path"`
|
Path string `db:"path"`
|
||||||
Status *string `db:"status"`
|
Status string `db:"status"`
|
||||||
Method *string `db:"method"`
|
Method string `db:"method"`
|
||||||
IsViewed *bool `db:"viewed"`
|
IsViewed bool `db:"viewed"`
|
||||||
CreatedAt string `db:"created_at"`
|
CreatedAt string `db:"created_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Ban struct {
|
type Ban struct {
|
||||||
ID int `db:"id"`
|
ID int `db:"id"`
|
||||||
IP string `db:"ip"`
|
IP string `db:"ip"`
|
||||||
Reason *string `db:"reason"`
|
Reason string `db:"reason"`
|
||||||
BannedAt string `db:"banned_at"`
|
BannedAt string `db:"banned_at"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,28 +6,13 @@ import (
|
|||||||
|
|
||||||
func Write(db *DB, resultCh <-chan *LogEntry) {
|
func Write(db *DB, resultCh <-chan *LogEntry) {
|
||||||
for result := range resultCh {
|
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(
|
_, err := db.db.Exec(
|
||||||
"INSERT INTO requests (service, ip, path, method, status, created_at) VALUES (?, ?, ?, ?, ?, ?)",
|
"INSERT INTO requests (service, ip, path, method, status, created_at) VALUES (?, ?, ?, ?, ?, ?)",
|
||||||
result.Service,
|
result.Service,
|
||||||
result.IP,
|
result.IP,
|
||||||
path,
|
result.Path,
|
||||||
method,
|
result.Method,
|
||||||
status,
|
result.Status,
|
||||||
time.Now().Format(time.RFC3339),
|
time.Now().Format(time.RFC3339),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user