not working...

This commit is contained in:
Max Lerebourg
2022-10-19 08:21:01 +02:00
parent 44e329cd57
commit a197194591
20 changed files with 315 additions and 4189 deletions
+53 -52
View File
@@ -7,6 +7,7 @@ import (
ttl_map "github.com/leprosus/golang-ttl-map"
logger "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/logger"
redis "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/redis"
)
const (
@@ -17,17 +18,9 @@ const (
var ctx = context.Background()
var cache = ttl_map.New()
var redisEnabled = false
// 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) {
if redisEnabled {
return getDecisionRedisCache(clientIP)
} else {
return getDecisionLocalCache(clientIP)
}
}
func getDecisionLocalCache(clientIP string) (bool, error) {
banned, isCached := cache.Get(clientIP)
@@ -38,8 +31,16 @@ func getDecisionLocalCache(clientIP string) (bool, error) {
return false, fmt.Errorf("no cache data")
}
func SetDecisionLocalCache(clientIP string, value string, duration int64) {
cache.Set(clientIP, value, duration)
}
func DeleteDecisionLocalCache(clientIP string) {
cache.Del(clientIP)
}
func getDecisionRedisCache(clientIP string) (bool, error) {
// banned, err := rdb.Get(ctx, clientIP).Result()
// banned, err := redis.Do(ctx, redis.B().Get().Key("key").Build()).ToString()
// if err != nil {
// return false, fmt.Errorf("no cache data")
// } else {
@@ -48,6 +49,39 @@ func getDecisionRedisCache(clientIP string) (bool, error) {
return false, nil
}
func SetDecisionRedisCache(clientIP string, value string, duration int64) {
// ctx := context.Background()
// redis.Do(ctx, redis.B().Set().Key("key").Value("val").Nx().Build()).Error()
}
func DeleteDecisionRedisCache(clientIP string) {
// err := rdb.Del(ctx, clientIP).Err()
// if err != nil {
// logger.Info("Error, could not delete in redis cache IP")
// }
// errors are not handled
}
func DeleteDecision(clientIP string) {
if redisEnabled {
DeleteDecisionRedisCache(clientIP)
} else {
DeleteDecisionLocalCache(clientIP)
}
}
// 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) {
if redisEnabled {
return getDecisionRedisCache(clientIP)
} else {
return getDecisionLocalCache(clientIP)
}
}
func SetDecision(clientIP string, isBanned bool, duration int64) {
var value string
if isBanned {
@@ -63,48 +97,15 @@ func SetDecision(clientIP string, isBanned bool, duration int64) {
}
}
func SetDecisionRedisCache(clientIP string, value string, duration int64) {
// err := rdb.Set(ctx, clientIP, value, time.Duration(duration)).Err()
func InitRedisClient(host string, password string) error {
// redis, err := rueidis.NewClient(rueidis.ClientOption{
// InitAddress: []string{host},
// })
// if err != nil {
// logger.Info("Error, could not insert in redis cache IP")
// return fmt.Errorf("error instanciate redis: %w", err)
// }
// errors are not handled
}
func SetDecisionLocalCache(clientIP string, value string, duration int64) {
cache.Set(clientIP, value, duration)
}
func DeleteDecision(clientIP string) {
if redisEnabled {
DeleteDecisionRedisCache(clientIP)
} else {
DeleteDecisionLocalCache(clientIP)
}
}
func DeleteDecisionRedisCache(clientIP string) {
// err := rdb.Del(ctx, clientIP).Err()
// if err != nil {
// logger.Info("Error, could not delete in redis cache IP")
// }
// errors are not handled
}
func DeleteDecisionLocalCache(clientIP string) {
cache.Del(clientIP)
}
func InitRedisClient(addr string, port int, password string) {
// c, _ := redis.DialContext(ctx, "tcp", ":6379")
// defer c.Close()
// c, err := redis.DialURL(fmt.Sprintf("%v:%d", addr, port))
// if err != nil && c != nil{
// // handle error
// // c.Do("Set", "key", "value")
// logger.Debug("test")
// }
// TODO defer c.Close()
redisEnabled = true
// redisEnabled = true
writter, reader := redis.Init(host, password)
// writter.PrintfLine("[caca, true, caca]")
return nil
}