feat: upgrade max_retry logic and change version
All checks were successful
build / build (push) Successful in 2m9s

This commit is contained in:
d3m0k1d
2026-02-22 18:27:21 +03:00
parent 97eb626237
commit 7bba444522
6 changed files with 18 additions and 9 deletions

View File

@@ -9,12 +9,13 @@ import (
)
var (
name string
service string
path string
status string
method string
ttl string
name string
service string
path string
status string
method string
ttl string
max_retry int
)
var RuleCmd = &cobra.Command{
@@ -41,7 +42,7 @@ var AddCmd = &cobra.Command{
if ttl == "" {
ttl = "1y"
}
err := config.NewRule(name, service, path, status, method, ttl)
err := config.NewRule(name, service, path, status, method, ttl, max_retry)
if err != nil {
fmt.Println(err)
os.Exit(1)
@@ -82,4 +83,5 @@ func RuleRegister() {
AddCmd.Flags().StringVarP(&status, "status", "c", "", "status code")
AddCmd.Flags().StringVarP(&method, "method", "m", "", "method")
AddCmd.Flags().StringVarP(&ttl, "ttl", "t", "", "ban time")
AddCmd.Flags().IntVarP(&max_retry, "max_retry", "r", 0, "max retry")
}

View File

@@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)
var version = "0.5.0"
var version = "0.5.2"
var VersionCmd = &cobra.Command{
Use: "version",

View File

@@ -79,5 +79,6 @@ These command help you to create and manage detection rules in CLI interface.
| -m -method | - |
| -c -status | - |
| -t -ttl | -(if not used default ban 1 year) |
| -r -max_retry | - |
You must specify at least 1 of the optional flags to create a rule.

View File

@@ -47,4 +47,5 @@ Example:
**Description**
The [[rule]] section require name and one of the following parameters: service, path, status, method. To add a rule, create a [[rule]] block and specify the parameters.
ban_time require in format "1m", "1h", "1d", "1M", "1y".
If you want to ban all requests to PHP files (e.g., path = "*.php") or requests to the admin panel (e.g., path = "/admin/*")
If you want to ban all requests to PHP files (e.g., path = "*.php") or requests to the admin panel (e.g., path = "/admin/*").
If max_retry = 0 ban on first request.

View File

@@ -33,6 +33,7 @@ func NewRule(
Status string,
Method string,
ttl string,
max_retry int,
) error {
r, err := LoadRuleConfig()
if err != nil {
@@ -51,6 +52,7 @@ func NewRule(
Status: Status,
Method: Method,
BanTime: ttl,
MaxRetry: max_retry,
},
)
file, err := os.Create("/etc/banforge/rules.toml")

View File

@@ -53,6 +53,9 @@ func NewRequestsRd() (*RequestReader, error) {
func (r *RequestReader) IsMaxRetryExceeded(ip string, maxRetry int) (bool, error) {
var count int
if maxRetry == 0 {
return true, nil
}
err := r.db.QueryRow("SELECT COUNT(*) FROM requests WHERE ip = ?", ip).Scan(&count)
if err != nil {
r.logger.Error("error query count: " + err.Error())