mirror of
https://github.com/TecharoHQ/anubis.git
synced 2026-04-14 12:38:45 +00:00
@@ -12,6 +12,7 @@ type Bot struct {
|
||||
Challenge *config.ChallengeRules
|
||||
Name string
|
||||
Action config.Rule
|
||||
Weight *config.Weight
|
||||
}
|
||||
|
||||
func (b Bot) Hash() string {
|
||||
|
||||
@@ -7,12 +7,15 @@ import (
|
||||
)
|
||||
|
||||
type CheckResult struct {
|
||||
Name string
|
||||
Rule config.Rule
|
||||
Name string
|
||||
Rule config.Rule
|
||||
Weight int
|
||||
}
|
||||
|
||||
func (cr CheckResult) LogValue() slog.Value {
|
||||
return slog.GroupValue(
|
||||
slog.String("name", cr.Name),
|
||||
slog.String("rule", string(cr.Rule)))
|
||||
slog.String("rule", string(cr.Rule)),
|
||||
slog.Int("weight", cr.Weight),
|
||||
)
|
||||
}
|
||||
|
||||
47
lib/policy/passratechecker.go
Normal file
47
lib/policy/passratechecker.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package policy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/TecharoHQ/anubis/internal"
|
||||
"github.com/TecharoHQ/anubis/internal/store/valkey"
|
||||
)
|
||||
|
||||
type PassRateChecker struct {
|
||||
store *valkey.Store
|
||||
header string
|
||||
rate float64
|
||||
}
|
||||
|
||||
func NewPassRateChecker(store *valkey.Store, rate float64) Checker {
|
||||
return &PassRateChecker{
|
||||
store: store,
|
||||
rate: rate,
|
||||
header: "User-Agent",
|
||||
}
|
||||
}
|
||||
|
||||
func (prc *PassRateChecker) Hash() string {
|
||||
return internal.SHA256sum(fmt.Sprintf("pass rate checker::%s", prc.header))
|
||||
}
|
||||
|
||||
func (prc *PassRateChecker) Check(r *http.Request) (bool, error) {
|
||||
data, err := prc.store.MultiGetInt(r.Context(), [][]string{
|
||||
{"pass_rate", prc.header, r.Header.Get(prc.header), "pass"},
|
||||
{"pass_rate", prc.header, r.Header.Get(prc.header), "challenges_issued"},
|
||||
{"pass_rate", prc.header, r.Header.Get(prc.header), "fail"},
|
||||
})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
passCount, challengeCount, failCount := data[0], data[1], data[2]
|
||||
passRate := float64(passCount-failCount) / float64(challengeCount)
|
||||
|
||||
if passRate >= prc.rate {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
@@ -116,6 +116,10 @@ func ParseConfig(fin io.Reader, fname string, defaultDifficulty int) (*ParsedCon
|
||||
}
|
||||
}
|
||||
|
||||
if b.Weight != nil {
|
||||
parsedBot.Weight = b.Weight
|
||||
}
|
||||
|
||||
parsedBot.Rules = cl
|
||||
|
||||
result.Bots = append(result.Bots, parsedBot)
|
||||
|
||||
Reference in New Issue
Block a user