Merge remote-tracking branch 'origin' into 28-bypassing-crowdsec-for-local-ips

This commit is contained in:
MathieuHa
2022-11-19 12:22:56 +01:00
7 changed files with 30 additions and 20 deletions
+12 -5
View File
@@ -15,6 +15,7 @@ import (
cache "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/cache"
ip "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/ip"
logger "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/logger"
simpleredis "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/redis"
)
const (
@@ -187,10 +188,16 @@ func (bouncer *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}
logger.Debug(fmt.Sprintf("ServeHTTP ip:%v", remoteHost))
healthy := crowdsecStreamHealthy
if bouncer.crowdsecMode != noneMode {
isBanned, err := cache.GetDecision(remoteHost)
if err == nil {
logger.Debug(fmt.Sprintf("ServeHTTP cacheHit isBanned:%v", isBanned))
if err != nil {
logger.Debug(err.Error())
if err.Error() == simpleredis.RedisUnreachable {
healthy = false
}
} else {
logger.Debug(fmt.Sprintf("ServeHTTP cache:hit isBanned:%v", isBanned))
if isBanned {
rw.WriteHeader(http.StatusForbidden)
} else {
@@ -202,7 +209,7 @@ func (bouncer *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// Right here if we cannot join the stream we forbid the request to go on.
if bouncer.crowdsecMode == streamMode {
if crowdsecStreamHealthy {
if healthy {
bouncer.next.ServeHTTP(rw, req)
} else {
rw.WriteHeader(http.StatusForbidden)
@@ -269,7 +276,7 @@ func handleNoStreamCache(bouncer *Bouncer, rw http.ResponseWriter, req *http.Req
}
body, err := crowdsecQuery(bouncer, routeURL.String())
if err != nil {
logger.Info(fmt.Sprintf("%w", err))
logger.Info(err.Error())
rw.WriteHeader(http.StatusForbidden)
return
}
@@ -326,7 +333,7 @@ func handleStreamCache(bouncer *Bouncer) {
}
body, err := crowdsecQuery(bouncer, streamRouteURL.String())
if err != nil {
logger.Info(fmt.Sprintf("%w", err))
logger.Info(err.Error())
crowdsecStreamHealthy = false
return
}