This commit is contained in:
Max Lerebourg
2022-09-29 09:33:52 +02:00
parent 75267a1746
commit c7714cb793
+14 -6
View File
@@ -5,6 +5,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"log" "log"
"net" "net"
@@ -144,7 +145,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
} }
// TODO the serve HTTP should be split as it's too long. // TODO the serve HTTP should be split as it's too long.
// ServeHTTP principal function of plugin // ServeHTTP principal function of plugin.
func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if !a.enabled { if !a.enabled {
log.Printf("Crowdsec Bouncer not enabled") log.Printf("Crowdsec Bouncer not enabled")
@@ -197,7 +198,7 @@ func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusForbidden) rw.WriteHeader(http.StatusForbidden)
return return
} }
defer res.Body.Close() defer closeBody(res.Body)
if res.StatusCode != 200 { if res.StatusCode != 200 {
log.Printf("failed to get decision, status code: %d", res.StatusCode) log.Printf("failed to get decision, status code: %d", res.StatusCode)
rw.WriteHeader(http.StatusForbidden) rw.WriteHeader(http.StatusForbidden)
@@ -243,7 +244,7 @@ func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// CUSTOM CODE. // CUSTOM CODE.
// TODO place in another file. // TODO place in another file.
// Decision: Body returned from Crowdsec LAPI. // Decision Body returned from Crowdsec LAPI.
type Decision struct { type Decision struct {
ID int `json:"id"` ID int `json:"id"`
Origin string `json:"origin"` Origin string `json:"origin"`
@@ -255,7 +256,7 @@ type Decision struct {
Simulated bool `json:"simulated"` Simulated bool `json:"simulated"`
} }
// Stream: Body returned from Crowdsec Stream LAPI. // Stream Body returned from Crowdsec Stream LAPI.
type Stream struct { type Stream struct {
Deleted []Decision `json:"deleted"` Deleted []Decision `json:"deleted"`
New []Decision `json:"new"` New []Decision `json:"new"`
@@ -302,7 +303,7 @@ func handleStreamCache(a *Bouncer, initialized bool) {
req, _ := http.NewRequest(http.MethodGet, streamURL.String(), nil) req, _ := http.NewRequest(http.MethodGet, streamURL.String(), nil)
req.Header.Add(crowdsecAuthHeader, a.crowdsecKey) req.Header.Add(crowdsecAuthHeader, a.crowdsecKey)
res, err := a.client.Do(req) res, err := a.client.Do(req)
if err != nil || res.StatusCode == http.StatusForbidden { if err != nil {
log.Printf("error while fetching decisions: %s", err) log.Printf("error while fetching decisions: %s", err)
a.crowdsecStreamHealthy = false a.crowdsecStreamHealthy = false
return return
@@ -312,7 +313,7 @@ func handleStreamCache(a *Bouncer, initialized bool) {
a.crowdsecStreamHealthy = false a.crowdsecStreamHealthy = false
return return
} }
defer res.Body.Close() defer closeBody(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) log.Printf("error while reading body: %s", err)
@@ -337,3 +338,10 @@ func handleStreamCache(a *Bouncer, initialized bool) {
} }
a.crowdsecStreamHealthy = true a.crowdsecStreamHealthy = true
} }
func closeBody(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Printf("failed to close body reader: %s", err)
}
}