This commit is contained in:
Max Lerebourg
2022-09-29 09:16:03 +02:00
parent 0aff6e1789
commit 1d9bd3fab0
+8 -8
View File
@@ -136,14 +136,14 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
Timeout: 5 * time.Second, Timeout: 5 * time.Second,
}, },
} }
// if we are on a stream mode, we fetch in a go routine every minute the new decisions // if we are on a stream mode, we fetch in a go routine every minute the new decisions.
if config.CrowdsecMode == "stream" { if config.CrowdsecMode == "stream" {
go handleStreamCache(bouncer, true) go handleStreamCache(bouncer, true)
} }
return bouncer, nil return bouncer, nil
} }
// TODO the serve HTTP should be split as it's too long // TODO the serve HTTP should be split as it's too long.
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")
@@ -151,7 +151,7 @@ func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
return return
} }
// 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) log.Printf("failed to extract ip from remote address: %v", err)
@@ -171,7 +171,7 @@ func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
} }
} }
// Right here if we cannot join the stream we forbid the request to go on // Right here if we cannot join the stream we forbid the request to go on.
if a.crowdsecMode == "stream" { if a.crowdsecMode == "stream" {
if a.crowdsecStreamHealthy { if a.crowdsecStreamHealthy {
a.next.ServeHTTP(rw, req) a.next.ServeHTTP(rw, req)
@@ -181,7 +181,7 @@ func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
return return
} }
// We are now in none or live mode // We are now in none or live mode.
noneUrl := url.URL{ noneUrl := url.URL{
Scheme: a.crowdsecScheme, Scheme: a.crowdsecScheme,
Host: a.crowdsecHost, Host: a.crowdsecHost,
@@ -239,8 +239,8 @@ func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
a.next.ServeHTTP(rw, req) a.next.ServeHTTP(rw, req)
} }
// 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 {
@@ -288,7 +288,7 @@ func setDecision(clientIP string, isBanned bool, duration int64) {
} }
func handleStreamCache(a *Bouncer, initialized bool) { func handleStreamCache(a *Bouncer, initialized bool) {
// TODO clean properly on exit // TODO clean properly on exit.
time.AfterFunc(time.Duration(a.updateInterval)*time.Second, func() { time.AfterFunc(time.Duration(a.updateInterval)*time.Second, func() {
handleStreamCache(a, false) handleStreamCache(a, false)
}) })