diff options
Diffstat (limited to 'cmd/anubis/internal/config/config.go')
| -rw-r--r-- | cmd/anubis/internal/config/config.go | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/cmd/anubis/internal/config/config.go b/cmd/anubis/internal/config/config.go index efd8496..56975af 100644 --- a/cmd/anubis/internal/config/config.go +++ b/cmd/anubis/internal/config/config.go @@ -3,6 +3,7 @@ package config import ( "errors" "fmt" + "net" "regexp" ) @@ -28,17 +29,19 @@ type Bot struct { UserAgentRegex *string `json:"user_agent_regex"` PathRegex *string `json:"path_regex"` Action Rule `json:"action"` + RemoteAddr []string `json:"remote_addresses"` Challenge *ChallengeRules `json:"challenge,omitempty"` } var ( ErrNoBotRulesDefined = errors.New("config: must define at least one (1) bot rule") ErrBotMustHaveName = errors.New("config.Bot: must set name") - ErrBotMustHaveUserAgentOrPath = errors.New("config.Bot: must set either user_agent_regex, path_regex") + ErrBotMustHaveUserAgentOrPath = errors.New("config.Bot: must set either user_agent_regex, path_regex, or remote_addresses") ErrBotMustHaveUserAgentOrPathNotBoth = errors.New("config.Bot: must set either user_agent_regex, path_regex, and not both") ErrUnknownAction = errors.New("config.Bot: unknown action") ErrInvalidUserAgentRegex = errors.New("config.Bot: invalid user agent regex") ErrInvalidPathRegex = errors.New("config.Bot: invalid path regex") + ErrInvalidCIDR = errors.New("config.Bot: invalid CIDR") ) func (b Bot) Valid() error { @@ -48,7 +51,7 @@ func (b Bot) Valid() error { errs = append(errs, ErrBotMustHaveName) } - if b.UserAgentRegex == nil && b.PathRegex == nil { + if b.UserAgentRegex == nil && b.PathRegex == nil && (b.RemoteAddr == nil || len(b.RemoteAddr) == 0) { errs = append(errs, ErrBotMustHaveUserAgentOrPath) } @@ -68,6 +71,14 @@ func (b Bot) Valid() error { } } + if b.RemoteAddr != nil && len(b.RemoteAddr) > 0 { + for _, cidr := range b.RemoteAddr { + if _, _, err := net.ParseCIDR(cidr); err != nil { + errs = append(errs, ErrInvalidCIDR, err) + } + } + } + switch b.Action { case RuleAllow, RuleChallenge, RuleDeny: // okay |
