mirror of
https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin.git
synced 2026-12-02 07:29:14 +01:00
⚡ build each trusted IP list once instead of once per router (#441)
* ⚡ build each trusted IP list once instead of once per router Traefik builds an entry-point middleware once per router, and every instance parsed forwardedHeadersTrustedIps and clientTrustedIps twice, in ValidateParams and then for the pool strategy. NewChecker now returns the Checker already built for the same list. A Checker is read-only once built, so the bouncers can share it. The cache is keyed by the comma-joined list. A list with an entry that contains a comma is never valid, so it skips the cache and still fails validation. The cache is emptied when it reaches 64 lists, so lists replaced over many reloads do not pile up. Fixes #440. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * 🐛 name the invalid entry in the trusted IPs error net.ParseCIDR returns a nil network on failure, so the error named <nil> instead of the entry that failed to parse. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * ⚡ keep at most 16 trusted IP lists in the cache A configuration uses one to three lists, so 16 still leaves room, and lists replaced over many reloads hold a quarter of the memory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * 🐛 print the invalid entry once in the trusted IPs error The error from net.ParseCIDR already names the entry, so naming it again printed it twice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d47336857a
commit
0ef9358b79
1 file changed
+34
-2
+34
-2
@@ -9,6 +9,15 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const maxCheckers = 16
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var (
|
||||
checkersMu sync.Mutex
|
||||
checkers = map[string]*Checker{}
|
||||
)
|
||||
|
||||
// CHECKER
|
||||
@@ -19,8 +28,31 @@ type Checker struct {
|
||||
authorizedIPsNet []*net.IPNet
|
||||
}
|
||||
|
||||
// NewChecker builds a new Checker given a list of CIDR-Strings to trusted IPs.
|
||||
// NewChecker returns the Checker of a list of CIDR-Strings to trusted IPs, shared between calls with the same list.
|
||||
func NewChecker(log *slog.Logger, trustedIPs []string) (*Checker, error) {
|
||||
key := strings.Join(trustedIPs, ",")
|
||||
if strings.Count(key, ",") != len(trustedIPs)-1 {
|
||||
return newChecker(log, trustedIPs)
|
||||
}
|
||||
|
||||
checkersMu.Lock()
|
||||
defer checkersMu.Unlock()
|
||||
if checker, ok := checkers[key]; ok {
|
||||
return checker, nil
|
||||
}
|
||||
|
||||
checker, err := newChecker(log, trustedIPs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(checkers) >= maxCheckers {
|
||||
checkers = map[string]*Checker{}
|
||||
}
|
||||
checkers[key] = checker
|
||||
return checker, nil
|
||||
}
|
||||
|
||||
func newChecker(log *slog.Logger, trustedIPs []string) (*Checker, error) {
|
||||
checker := &Checker{}
|
||||
|
||||
for _, ipMaskRaw := range trustedIPs {
|
||||
@@ -33,7 +65,7 @@ func NewChecker(log *slog.Logger, trustedIPs []string) (*Checker, error) {
|
||||
|
||||
_, ipAddr, err := net.ParseCIDR(ipMask)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parsing CIDR trusted IPs %s: %w", ipAddr, err)
|
||||
return nil, fmt.Errorf("parsing CIDR trusted IPs: %w", err)
|
||||
}
|
||||
checker.authorizedIPsNet = append(checker.authorizedIPsNet, ipAddr)
|
||||
log.Debug(fmt.Sprintf("IP network %v is trusted", ipAddr))
|
||||
|
||||
Reference in new issue
Block a user