feat: add new method and for db req and add to template max retry
Some checks failed
build / build (push) Failing after 1m48s

This commit is contained in:
d3m0k1d
2026-02-19 10:53:55 +03:00
parent 2e9b307194
commit 7f54db0cd4
5 changed files with 44 additions and 27 deletions

View File

@@ -12,11 +12,13 @@ config = "/etc/nftables.conf"
name = "nginx" name = "nginx"
logging = "file" logging = "file"
log_path = "/var/log/nginx/access.log" log_path = "/var/log/nginx/access.log"
max_retry = 3
enabled = true enabled = true
[[service]] [[service]]
name = "nginx" name = "nginx"
logging = "journald" logging = "journald"
log_path = "/var/log/nginx/access.log" log_path = "/var/log/nginx/access.log"
max_retry = 3
enabled = false enabled = false
` `

View File

@@ -28,5 +28,6 @@ type Rule struct {
Path string `toml:"path"` Path string `toml:"path"`
Status string `toml:"status"` Status string `toml:"status"`
Method string `toml:"method"` Method string `toml:"method"`
MaxRetry int `toml:"max_retry"`
BanTime string `toml:"ban_time"` BanTime string `toml:"ban_time"`
} }

View File

@@ -76,14 +76,6 @@ func (j *Judge) Tribunal() {
statusMatch := rule.Status == "" || entry.Status == rule.Status statusMatch := rule.Status == "" || entry.Status == rule.Status
pathMatch := matchPath(entry.Path, rule.Path) pathMatch := matchPath(entry.Path, rule.Path)
j.logger.Debug(
"Testing rule",
"rule", rule.Name,
"method_match", methodMatch,
"status_match", statusMatch,
"path_match", pathMatch,
)
if methodMatch && statusMatch && pathMatch { if methodMatch && statusMatch && pathMatch {
ruleMatched = true ruleMatched = true
j.logger.Info("Rule matched", "rule", rule.Name, "ip", entry.IP) j.logger.Info("Rule matched", "rule", rule.Name, "ip", entry.IP)
@@ -93,7 +85,6 @@ func (j *Judge) Tribunal() {
j.logger.Error("Failed to check ban status", "ip", entry.IP, "error", err) j.logger.Error("Failed to check ban status", "ip", entry.IP, "error", err)
break break
} }
if banned { if banned {
j.logger.Info("IP already banned", "ip", entry.IP) j.logger.Info("IP already banned", "ip", entry.IP)
j.resultCh <- entry j.resultCh <- entry

View File

@@ -28,3 +28,44 @@ func NewRequestsWr() (*RequestWriter, error) {
db: db, db: db,
}, nil }, nil
} }
type RequestReader struct {
logger *logger.Logger
db *sql.DB
}
func NewRequestsRd() (*RequestReader, error) {
db, err := sql.Open(
"sqlite",
buildSqliteDsn(ReqDBPath, pragmas),
)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(1)
db.SetMaxIdleConns(1)
db.SetConnMaxLifetime(0)
return &RequestReader{
logger: logger.New(false),
db: db,
}, nil
}
func (r *RequestReader) IsMaxRetryExceeded(ip string, max_retry int) (bool, error) {
row, err := r.db.Query("SELECT COUNT(*) FROM requests WHERE ip = ?", ip)
if err != nil {
r.logger.Error("error scan" + err.Error())
return false, err
}
if row.Next() {
var count int
if err := row.Scan(&count); err != nil {
r.logger.Error("error scan" + err.Error())
return false, err
}
if count >= max_retry {
return true, nil
}
}
return true, nil
}

View File

@@ -299,21 +299,3 @@ func (w *RequestWriter) CreateTable() error {
w.logger.Info("Created requests table") w.logger.Info("Created requests table")
return nil return nil
} }
func (w *RequestWriter) Close() error {
w.logger.Info("Closing request database connection")
err := w.db.Close()
if err != nil {
return err
}
return nil
}
func (w *RequestWriter) GetRequestCount() (int, error) {
var count int
err := w.db.QueryRow("SELECT COUNT(*) FROM requests").Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}