mirror of
https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin.git
synced 2026-07-21 11:38:59 +02:00
fix lint
This commit is contained in:
+30
-45
@@ -185,35 +185,16 @@ func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// We are now in none or live mode.
|
// We are now in none or live mode.
|
||||||
noneURL := url.URL{
|
routeURL := url.URL{
|
||||||
Scheme: a.crowdsecScheme,
|
Scheme: a.crowdsecScheme,
|
||||||
Host: a.crowdsecHost,
|
Host: a.crowdsecHost,
|
||||||
Path: crowdsecRoute,
|
Path: crowdsecRoute,
|
||||||
RawQuery: fmt.Sprintf("ip=%v&banned=true", remoteHost),
|
RawQuery: fmt.Sprintf("ip=%v&banned=true", remoteHost),
|
||||||
}
|
}
|
||||||
request, _ := http.NewRequest(http.MethodGet, noneURL.String(), nil)
|
body := crowdsecQuery(a, routeURL.String())
|
||||||
request.Header.Add(crowdsecAuthHeader, a.crowdsecKey)
|
|
||||||
res, err := a.client.Do(request)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("failed to get decision: %s", err)
|
|
||||||
rw.WriteHeader(http.StatusForbidden)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer closeBody(res.Body)
|
|
||||||
if res.StatusCode != 200 {
|
|
||||||
log.Printf("failed to get decision, status code: %d", res.StatusCode)
|
|
||||||
rw.WriteHeader(http.StatusForbidden)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
body, err := ioutil.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("failed to read body: %s", err)
|
|
||||||
rw.WriteHeader(http.StatusForbidden)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if !bytes.Equal(body, []byte("null")) {
|
if !bytes.Equal(body, []byte("null")) {
|
||||||
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)
|
log.Printf("failed to parse body: %s", err)
|
||||||
rw.WriteHeader(http.StatusForbidden)
|
rw.WriteHeader(http.StatusForbidden)
|
||||||
@@ -295,34 +276,15 @@ func handleStreamCache(a *Bouncer, initialized bool) {
|
|||||||
time.AfterFunc(time.Duration(a.updateInterval)*time.Second, func() {
|
time.AfterFunc(time.Duration(a.updateInterval)*time.Second, func() {
|
||||||
handleStreamCache(a, false)
|
handleStreamCache(a, false)
|
||||||
})
|
})
|
||||||
streamURL := url.URL{
|
streamRouteURL := url.URL{
|
||||||
Scheme: a.crowdsecScheme,
|
Scheme: a.crowdsecScheme,
|
||||||
Host: a.crowdsecHost,
|
Host: a.crowdsecHost,
|
||||||
Path: crowdsecStreamRoute,
|
Path: crowdsecStreamRoute,
|
||||||
RawQuery: fmt.Sprintf("startup=%t", initialized),
|
RawQuery: fmt.Sprintf("startup=%t", initialized),
|
||||||
}
|
}
|
||||||
req, _ := http.NewRequest(http.MethodGet, streamURL.String(), nil)
|
body := crowdsecQuery(a, streamRouteURL.String())
|
||||||
req.Header.Add(crowdsecAuthHeader, a.crowdsecKey)
|
|
||||||
res, err := a.client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("error while fetching decisions: %s", err)
|
|
||||||
a.crowdsecStreamHealthy = false
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if res.StatusCode == http.StatusForbidden {
|
|
||||||
log.Printf("error while fetching decisions, status code: %d", res.StatusCode)
|
|
||||||
a.crowdsecStreamHealthy = false
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer closeBody(res.Body)
|
|
||||||
body, err := ioutil.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("error while reading body: %s", err)
|
|
||||||
a.crowdsecStreamHealthy = false
|
|
||||||
return
|
|
||||||
}
|
|
||||||
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)
|
log.Printf("error while parsing body: %s", err)
|
||||||
a.crowdsecStreamHealthy = false
|
a.crowdsecStreamHealthy = false
|
||||||
@@ -340,9 +302,32 @@ func handleStreamCache(a *Bouncer, initialized bool) {
|
|||||||
a.crowdsecStreamHealthy = true
|
a.crowdsecStreamHealthy = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func closeBody(body io.ReadCloser) {
|
func crowdsecQuery(a *Bouncer, stringURL string) ([]byte) {
|
||||||
|
req, _ := http.NewRequest(http.MethodGet, stringURL, nil)
|
||||||
|
req.Header.Add(crowdsecAuthHeader, a.crowdsecKey)
|
||||||
|
res, err := a.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error while fetching %v: %s", stringURL, err)
|
||||||
|
a.crowdsecStreamHealthy = false
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if res.StatusCode == http.StatusForbidden {
|
||||||
|
log.Printf("error while fetching %v, status code: %d", stringURL, res.StatusCode)
|
||||||
|
a.crowdsecStreamHealthy = false
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
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)
|
log.Printf("failed to close body reader: %s", err)
|
||||||
}
|
}
|
||||||
|
}(res.Body)
|
||||||
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error while reading body: %s", err)
|
||||||
|
a.crowdsecStreamHealthy = false
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
return body
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user