mirror of
https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin.git
synced 2026-07-21 11:38:59 +02:00
✨ add one instance of cache for all the service (#19)
* ✨ add one instance of cache for all the service * 🐛 fix the healthy issue * 🐛 fix lint * 🍱 upgrade codebase * 🍱 fix lint * 🐛 when there is master node shut down * Set live mode as default, clean docker and recipes * Restore live mode in readme as default * 🍱 add channel management * 🍱 order funcs * 🍱 revert service in docker compose * ✨ logLevel added + cache put out the bouncer * 🍱 fix * 🍱 fix * 🍱 fix Co-authored-by: MathieuHa <mathieu@hanotaux.fr>
This commit is contained in:
co-authored by
MathieuHa
parent
f22fc2cd09
commit
8696501f61
Vendored
+39
@@ -0,0 +1,39 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
ttl_map "github.com/leprosus/golang-ttl-map"
|
||||
|
||||
logger "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/logger"
|
||||
)
|
||||
const (
|
||||
cacheBannedValue = "t"
|
||||
cacheNoBannedValue = "f"
|
||||
)
|
||||
|
||||
var cache = ttl_map.New()
|
||||
|
||||
// Get Decision check in the cache if the IP has the banned / not banned value.
|
||||
// Otherwise return with an error to add the IP in cache if we are on.
|
||||
func GetDecision(clientIP string) (bool, error) {
|
||||
banned, isCached := cache.Get(clientIP)
|
||||
bannedString, isValid := banned.(string)
|
||||
if isCached && isValid && len(bannedString) > 0 {
|
||||
return bannedString == cacheBannedValue, nil
|
||||
}
|
||||
return false, fmt.Errorf("no cache data")
|
||||
}
|
||||
|
||||
func SetDecision(clientIP string, isBanned bool, duration int64) {
|
||||
if isBanned {
|
||||
logger.Debug(fmt.Sprintf("%v banned", clientIP))
|
||||
cache.Set(clientIP, cacheBannedValue, duration)
|
||||
} else {
|
||||
cache.Set(clientIP, cacheNoBannedValue, duration)
|
||||
}
|
||||
}
|
||||
|
||||
func DeleteDecision(clientIP string) {
|
||||
cache.Del(clientIP)
|
||||
}
|
||||
Reference in New Issue
Block a user