mirror of
https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin.git
synced 2026-07-21 11:38:59 +02:00
✨ fix log
This commit is contained in:
+14
-10
@@ -140,7 +140,7 @@ func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|||||||
// TODO Make sur remote address does not include the port.
|
// TODO Make sur remote address does not include the port.
|
||||||
remoteHost, _, err := net.SplitHostPort(req.RemoteAddr)
|
remoteHost, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to extract ip from remote address: %v", err)
|
logger(fmt.Sprintf("failed to extract ip from remote address: %v", err))
|
||||||
a.next.ServeHTTP(rw, req)
|
a.next.ServeHTTP(rw, req)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -197,6 +197,10 @@ type Login struct {
|
|||||||
Expire string `json:"expire"`
|
Expire string `json:"expire"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func logger(str string) {
|
||||||
|
log.Printf("Crowdsec Bouncer Traefik Plugin - %s", str)
|
||||||
|
}
|
||||||
|
|
||||||
func contains(source []string, target string) bool {
|
func contains(source []string, target string) bool {
|
||||||
for _, a := range source {
|
for _, a := range source {
|
||||||
if a == target {
|
if a == target {
|
||||||
@@ -219,7 +223,7 @@ func getDecision(cache *ttl_map.Heap, clientIP string) (bool, error) {
|
|||||||
|
|
||||||
func setDecision(cache *ttl_map.Heap, clientIP string, isBanned bool, duration int64) {
|
func setDecision(cache *ttl_map.Heap, clientIP string, isBanned bool, duration int64) {
|
||||||
if isBanned {
|
if isBanned {
|
||||||
log.Printf("%v banned", clientIP)
|
logger(fmt.Sprintf("%v banned", clientIP))
|
||||||
cache.Set(clientIP, cacheBannedValue, duration)
|
cache.Set(clientIP, cacheBannedValue, duration)
|
||||||
} else {
|
} else {
|
||||||
cache.Set(clientIP, cacheNoBannedValue, duration)
|
cache.Set(clientIP, cacheNoBannedValue, duration)
|
||||||
@@ -247,7 +251,7 @@ func handleNoStreamCache(a *Bouncer, rw http.ResponseWriter, req *http.Request,
|
|||||||
var decisions []Decision
|
var decisions []Decision
|
||||||
err := json.Unmarshal(body, &decisions)
|
err := json.Unmarshal(body, &decisions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to parse body: %s", err)
|
logger(fmt.Sprintf("failed to parse body: %s", err))
|
||||||
rw.WriteHeader(http.StatusForbidden)
|
rw.WriteHeader(http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -261,7 +265,7 @@ func handleNoStreamCache(a *Bouncer, rw http.ResponseWriter, req *http.Request,
|
|||||||
rw.WriteHeader(http.StatusForbidden)
|
rw.WriteHeader(http.StatusForbidden)
|
||||||
duration, err := time.ParseDuration(decisions[0].Duration)
|
duration, err := time.ParseDuration(decisions[0].Duration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to parse duration: %s", err)
|
logger(fmt.Sprintf("failed to parse duration: %s", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
setDecision(a.cache, remoteHost, true, int64(duration.Seconds()))
|
setDecision(a.cache, remoteHost, true, int64(duration.Seconds()))
|
||||||
@@ -288,7 +292,7 @@ func handleStreamCache(a *Bouncer) {
|
|||||||
var stream Stream
|
var stream Stream
|
||||||
err := json.Unmarshal(body, &stream)
|
err := json.Unmarshal(body, &stream)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error while parsing body: %s", err)
|
logger(fmt.Sprintf("error while parsing body: %s", err))
|
||||||
a.crowdsecStreamHealthy = false
|
a.crowdsecStreamHealthy = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -314,7 +318,7 @@ func getToken(a *Bouncer) {
|
|||||||
var login Login
|
var login Login
|
||||||
err := json.Unmarshal(body, &login)
|
err := json.Unmarshal(body, &login)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error while parsing body: %s", err)
|
logger(fmt.Sprintf("error while parsing body: %s", err))
|
||||||
a.crowdsecStreamHealthy = false
|
a.crowdsecStreamHealthy = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -343,7 +347,7 @@ func crowdsecQuery(a *Bouncer, stringURL string, isPost bool) []byte {
|
|||||||
}
|
}
|
||||||
res, err := a.client.Do(req)
|
res, err := a.client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error while fetching %v: %s", stringURL, err)
|
logger(fmt.Sprintf("error while fetching %v: %s", stringURL, err))
|
||||||
a.crowdsecStreamHealthy = false
|
a.crowdsecStreamHealthy = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -357,19 +361,19 @@ func crowdsecQuery(a *Bouncer, stringURL string, isPost bool) []byte {
|
|||||||
return crowdsecQuery(a, stringURL, false)
|
return crowdsecQuery(a, stringURL, false)
|
||||||
}
|
}
|
||||||
if res.StatusCode != http.StatusOK {
|
if res.StatusCode != http.StatusOK {
|
||||||
log.Printf("error while fetching %v, status code: %d", stringURL, res.StatusCode)
|
logger(fmt.Sprintf("error while fetching %v, status code: %d", stringURL, res.StatusCode))
|
||||||
a.crowdsecStreamHealthy = false
|
a.crowdsecStreamHealthy = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
defer func(body io.ReadCloser) {
|
defer func(body io.ReadCloser) {
|
||||||
err = body.Close()
|
err = body.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to close body reader: %s", err)
|
logger(fmt.Sprintf("failed to close body reader: %s", err))
|
||||||
}
|
}
|
||||||
}(res.Body)
|
}(res.Body)
|
||||||
body, err := ioutil.ReadAll(res.Body)
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error while reading body: %s", err)
|
logger(fmt.Sprintf("error while reading body: %s", err))
|
||||||
a.crowdsecStreamHealthy = false
|
a.crowdsecStreamHealthy = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user