First version not working, unsafe library

This commit is contained in:
MathieuHa
2022-10-16 23:08:21 +02:00
parent 59268ee33d
commit 395c80dccf
4 changed files with 50 additions and 2 deletions
+6
View File
@@ -3,3 +3,9 @@ module github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin
go 1.17 go 1.17
require github.com/leprosus/golang-ttl-map v1.1.7 require github.com/leprosus/golang-ttl-map v1.1.7
require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-redis/redis/v9 v9.0.0-rc.1 // indirect
)
+6
View File
@@ -1,2 +1,8 @@
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/go-redis/redis/v9 v9.0.0-rc.1 h1:/+bS+yeUnanqAbuD3QwlejzQZ+4eqgfUtFTG4b+QnXs=
github.com/go-redis/redis/v9 v9.0.0-rc.1/go.mod h1:8et+z03j0l8N+DvsVnclzjf3Dl/pFHgRk+2Ct1qw66A=
github.com/leprosus/golang-ttl-map v1.1.7 h1:cF4AAFDDnJTFSV+/42sKLhmMluvLdRlCGS2UaifH6UM= github.com/leprosus/golang-ttl-map v1.1.7 h1:cF4AAFDDnJTFSV+/42sKLhmMluvLdRlCGS2UaifH6UM=
github.com/leprosus/golang-ttl-map v1.1.7/go.mod h1:4QWHJPeVBbrkhOhXdhCv9IEiyj/YzkO04/iexy4vSe0= github.com/leprosus/golang-ttl-map v1.1.7/go.mod h1:4QWHJPeVBbrkhOhXdhCv9IEiyj/YzkO04/iexy4vSe0=
+30
View File
@@ -1,17 +1,20 @@
package cache package cache
import ( import (
"context"
"fmt" "fmt"
ttl_map "github.com/leprosus/golang-ttl-map" ttl_map "github.com/leprosus/golang-ttl-map"
logger "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/logger" logger "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/logger"
) )
const ( const (
cacheBannedValue = "t" cacheBannedValue = "t"
cacheNoBannedValue = "f" cacheNoBannedValue = "f"
) )
var ctx = context.Background()
var cache = ttl_map.New() var cache = ttl_map.New()
// Get Decision check in the cache if the IP has the banned / not banned value. // Get Decision check in the cache if the IP has the banned / not banned value.
@@ -25,6 +28,33 @@ func GetDecision(clientIP string) (bool, error) {
return false, fmt.Errorf("no cache data") return false, fmt.Errorf("no cache data")
} }
func getDecisionLocalCache(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 getDecisionRedisCache(clientIP string) (bool, error) {
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
banned, err := rdb.Get(ctx, "key").Result()
if err != nil {
panic(err)
}
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) { func SetDecision(clientIP string, isBanned bool, duration int64) {
if isBanned { if isBanned {
logger.Debug(fmt.Sprintf("%v banned", clientIP)) logger.Debug(fmt.Sprintf("%v banned", clientIP))
+6
View File
@@ -1,3 +1,9 @@
# github.com/cespare/xxhash/v2 v2.1.2
## explicit; go 1.11
# github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f
## explicit
# github.com/go-redis/redis/v9 v9.0.0-rc.1
## explicit; go 1.17
# github.com/leprosus/golang-ttl-map v1.1.7 # github.com/leprosus/golang-ttl-map v1.1.7
## explicit; go 1.15 ## explicit; go 1.15
github.com/leprosus/golang-ttl-map github.com/leprosus/golang-ttl-map