Add headers bot rule (#300)

* Closes #291: add headers support to bot policy rules

* Fix config validator
This commit is contained in:
Neur0toxine 2025-04-21 01:18:21 +03:00 committed by GitHub
parent 1add24b907
commit 7dc545cfa9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 125 additions and 21 deletions

View file

@ -58,8 +58,9 @@ func ParseConfig(fin io.Reader, fname string, defaultDifficulty int) (*ParsedCon
}
parsedBot := Bot{
Name: b.Name,
Action: b.Action,
Name: b.Name,
Action: b.Action,
Headers: map[string]*regexp.Regexp{},
}
if len(b.RemoteAddr) > 0 {
@ -95,6 +96,22 @@ func ParseConfig(fin io.Reader, fname string, defaultDifficulty int) (*ParsedCon
}
}
if len(b.HeadersRegex) > 0 {
for name, expr := range b.HeadersRegex {
if name == "" {
continue
}
header, err := regexp.Compile(expr)
if err != nil {
validationErrs = append(validationErrs, fmt.Errorf("while compiling header regexp: %w", err))
continue
} else {
parsedBot.Headers[name] = header
}
}
}
if b.Challenge == nil {
parsedBot.Challenge = &config.ChallengeRules{
Difficulty: defaultDifficulty,