Files
traefik-botfilter/cache.go
T

65 lines
1.3 KiB
Go

package botfilter
import (
"sync"
"time"
)
type scoreEvent struct {
at time.Time
points int
}
type clientState struct {
banUntil time.Time
lastSeen time.Time
requests int
sawHome bool
sawStatic bool
sawFavicon bool
events []scoreEvent
}
type requestObservation struct {
at time.Time
paths []string
points int
forceBan bool
}
type cacheResult struct {
banned bool
banUntil time.Time
score int
}
// clientCache deliberately has no background goroutine. A middleware
// instance can be discarded on a Traefik reload without needing goroutine
// cleanup, and opportunistic cleanup keeps its memory use bounded.
type clientCache struct {
mu sync.Mutex
entries map[string]*clientState
maxEntries int
maxEvents int
scoreWindow time.Duration
banDuration time.Duration
threshold int
randomPaths []string
randomScore int
operations uint64
}
func newClientCache(cfg *compiledConfig) *clientCache {
return &clientCache{
entries: make(map[string]*clientState),
maxEntries: cfg.MaxTrackedIPs,
maxEvents: cfg.MaxScoreEventsPerIP,
scoreWindow: cfg.scoreWindow,
banDuration: cfg.banDuration,
threshold: cfg.ScoreThreshold,
randomPaths: append([]string(nil), cfg.randomPaths...),
randomScore: cfg.RandomArticleScore,
}
}