🐛 Support range decision on stream mode

This commit is contained in:
maxlerebourg
2026-07-31 14:08:59 +02:00
parent bef5dfaadb
commit 6b0518859d
9 changed files with 301 additions and 17 deletions
+36
View File
@@ -10,6 +10,8 @@ import (
ttl_map "github.com/leprosus/golang-ttl-map"
simpleredis "github.com/maxlerebourg/simpleredis"
ip "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/ip"
)
const (
@@ -25,6 +27,8 @@ const (
CacheMiss = "cache:miss"
// CacheUnreachable error string when cache is unreachable.
CacheUnreachable = "cache:unreachable"
// cidrPrefix store all cidr within the same key
cidrPrefix = "cidr:"
)
//nolint:gochecknoglobals
@@ -144,3 +148,35 @@ func (c *Client) Set(key string, value string, duration int64) {
c.log.Debug(fmt.Sprintf("cache:Set key:%v value:%v duration:%vs", key, value, duration))
c.cache.set(key, value, duration)
}
func (c *Client) DeleteCIDR(cidr string) {
cidr = ip.NormalizeCIDR(cidr)
if cidr == "" {
return
}
c.cache.delete(cidrPrefix + cidr)
c.log.Debug(fmt.Sprintf("cache:DeleteCIDR cidr:%v", cidr))
}
func (c *Client) GetCIDR(ipStr string) (string, error) {
keys := ip.CIDRKeys(ipStr)
if keys == nil {
return "", errors.New(CacheMiss)
}
for _, key := range keys {
value, err := c.cache.get(cidrPrefix + key)
if err == nil {
return value, nil
}
}
return "", errors.New(CacheMiss)
}
func (c *Client) SetCIDR(cidr, value string, duration int64) {
cidr = ip.NormalizeCIDR(cidr)
if cidr == "" {
return
}
c.cache.set(cidrPrefix+cidr, value, duration)
c.log.Debug(fmt.Sprintf("cache:SetCIDR cidr:%v value:%v duration:%vs", cidr, value, duration))
}