42 lines
919 B
Go
42 lines
919 B
Go
// Package traefik_botfilter is a Traefik middleware plugin that blocks known scans
|
|
// and applies a bounded, in-memory per-IP suspicion score.
|
|
package botfilter
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/netip"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type botFilter struct {
|
|
next http.Handler
|
|
config *compiledConfig
|
|
cache *clientCache
|
|
logger filterLogger
|
|
}
|
|
|
|
// New constructs a Traefik middleware. Its signature is the interface used
|
|
// by Traefik's Go plugin runtime.
|
|
func New(_ context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
|
|
if next == nil {
|
|
return nil, fmt.Errorf("botfilter: next handler is nil")
|
|
}
|
|
compiled, err := compileConfig(config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &botFilter{
|
|
next: next,
|
|
config: compiled,
|
|
cache: newClientCache(compiled),
|
|
logger: filterLogger{name: name, enabled: compiled.LogBlockedRequests},
|
|
}, nil
|
|
}
|
|
|