91 lines
5.4 KiB
Go
91 lines
5.4 KiB
Go
package botfilter
|
|
|
|
import (
|
|
"fmt"
|
|
"net/netip"
|
|
"net/textproto"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Config is the plugin configuration exposed by Traefik's dynamic file
|
|
// provider. All durations are expressed as integers because Traefik plugin
|
|
// configuration is deliberately kept YAML-friendly.
|
|
type Config struct {
|
|
StatusCode int `json:"statusCode,omitempty" yaml:"statusCode,omitempty" toml:"statusCode,omitempty"`
|
|
RequireUserAgent bool `json:"requireUserAgent,omitempty" yaml:"requireUserAgent,omitempty" toml:"requireUserAgent,omitempty"`
|
|
RequireAccept bool `json:"requireAccept,omitempty" yaml:"requireAccept,omitempty" toml:"requireAccept,omitempty"`
|
|
RequireHost bool `json:"requireHost,omitempty" yaml:"requireHost,omitempty" toml:"requireHost,omitempty"`
|
|
BrowserValidation bool `json:"browserValidation,omitempty" yaml:"browserValidation,omitempty" toml:"browserValidation,omitempty"`
|
|
WhitelistCIDRs []string `json:"whitelistCIDRs,omitempty" yaml:"whitelistCIDRs,omitempty" toml:"whitelistCIDRs,omitempty"`
|
|
TemporaryBanMinutes int `json:"temporaryBanMinutes,omitempty" yaml:"temporaryBanMinutes,omitempty" toml:"temporaryBanMinutes,omitempty"`
|
|
BlockedUserAgents []string `json:"blockedUserAgents,omitempty" yaml:"blockedUserAgents,omitempty" toml:"blockedUserAgents,omitempty"`
|
|
BlockedPaths []string `json:"blockedPaths,omitempty" yaml:"blockedPaths,omitempty" toml:"blockedPaths,omitempty"`
|
|
BlockedExtensions []string `json:"blockedExtensions,omitempty" yaml:"blockedExtensions,omitempty" toml:"blockedExtensions,omitempty"`
|
|
ScoreThreshold int `json:"scoreThreshold,omitempty" yaml:"scoreThreshold,omitempty" toml:"scoreThreshold,omitempty"`
|
|
ScoreWindowMinutes int `json:"scoreWindowMinutes,omitempty" yaml:"scoreWindowMinutes,omitempty" toml:"scoreWindowMinutes,omitempty"`
|
|
MaxTrackedIPs int `json:"maxTrackedIPs,omitempty" yaml:"maxTrackedIPs,omitempty" toml:"maxTrackedIPs,omitempty"`
|
|
MaxScoreEventsPerIP int `json:"maxScoreEventsPerIP,omitempty" yaml:"maxScoreEventsPerIP,omitempty" toml:"maxScoreEventsPerIP,omitempty"`
|
|
EmptyUserAgentScore int `json:"emptyUserAgentScore,omitempty" yaml:"emptyUserAgentScore,omitempty" toml:"emptyUserAgentScore,omitempty"`
|
|
MissingAcceptScore int `json:"missingAcceptScore,omitempty" yaml:"missingAcceptScore,omitempty" toml:"missingAcceptScore,omitempty"`
|
|
BlockedUserAgentScore int `json:"blockedUserAgentScore,omitempty" yaml:"blockedUserAgentScore,omitempty" toml:"blockedUserAgentScore,omitempty"`
|
|
BadPathScore int `json:"badPathScore,omitempty" yaml:"badPathScore,omitempty" toml:"badPathScore,omitempty"`
|
|
RandomArticleScore int `json:"randomArticleScore,omitempty" yaml:"randomArticleScore,omitempty" toml:"randomArticleScore,omitempty"`
|
|
NotFoundScore int `json:"notFoundScore,omitempty" yaml:"notFoundScore,omitempty" toml:"notFoundScore,omitempty"`
|
|
FakeBrowserScore int `json:"fakeBrowserScore,omitempty" yaml:"fakeBrowserScore,omitempty" toml:"fakeBrowserScore,omitempty"`
|
|
RandomArticlePatterns []string `json:"randomArticlePatterns,omitempty" yaml:"randomArticlePatterns,omitempty" toml:"randomArticlePatterns,omitempty"`
|
|
ClientIPHeader string `json:"clientIPHeader,omitempty" yaml:"clientIPHeader,omitempty" toml:"clientIPHeader,omitempty"`
|
|
TrustedProxyCIDRs []string `json:"trustedProxyCIDRs,omitempty" yaml:"trustedProxyCIDRs,omitempty" toml:"trustedProxyCIDRs,omitempty"`
|
|
LogBlockedRequests bool `json:"logBlockedRequests,omitempty" yaml:"logBlockedRequests,omitempty" toml:"logBlockedRequests,omitempty"`
|
|
}
|
|
|
|
// CreateConfig creates the default configuration. The defaults protect common
|
|
// public HTTP services without requiring a third-party dependency.
|
|
func CreateConfig() *Config {
|
|
return &Config{
|
|
StatusCode: 403,
|
|
TemporaryBanMinutes: 15,
|
|
ScoreThreshold: 100,
|
|
ScoreWindowMinutes: 15,
|
|
MaxTrackedIPs: 50000,
|
|
MaxScoreEventsPerIP: 16,
|
|
EmptyUserAgentScore: 40,
|
|
MissingAcceptScore: 20,
|
|
BlockedUserAgentScore: 80,
|
|
BadPathScore: 50,
|
|
RandomArticleScore: 15,
|
|
NotFoundScore: 40,
|
|
FakeBrowserScore: 40,
|
|
RandomArticlePatterns: []string{"/content/"},
|
|
}
|
|
}
|
|
|
|
type compiledConfig struct {
|
|
Config
|
|
banDuration time.Duration
|
|
scoreWindow time.Duration
|
|
whitelist []netip.Prefix
|
|
trustedProxies []netip.Prefix
|
|
blockedAgents []string
|
|
blockedPaths []string
|
|
blockedExts []string
|
|
randomPaths []string
|
|
clientIPHeader string
|
|
}
|
|
|
|
func compileConfig(input *Config) (*compiledConfig, error) {
|
|
if input == nil {
|
|
return nil, fmt.Errorf("botfilter: configuration is nil")
|
|
}
|
|
|
|
// Copy scalar fields and slices so a later configuration reload cannot
|
|
// mutate an already-running middleware instance.
|
|
cfg := *input
|
|
cfg.WhitelistCIDRs = append([]string(nil), input.WhitelistCIDRs...)
|
|
cfg.TrustedProxyCIDRs = append([]string(nil), input.TrustedProxyCIDRs...)
|
|
cfg.BlockedUserAgents = append([]string(nil), input.BlockedUserAgents...)
|
|
cfg.BlockedPaths = append([]string(nil), input.BlockedPaths...)
|
|
cfg.BlockedExtensions = append([]string(nil), input.BlockedExtensions...)
|
|
cfg.RandomArticlePatterns = append([]string(nil), input.RandomArticlePatterns...)
|
|
|