From 4132445a7999257bae9480908a1b9912ea10be2f Mon Sep 17 00:00:00 2001 From: maxlerebourg Date: Wed, 18 Jan 2023 21:03:09 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20cache=20decision=20in=20live=20mode?= =?UTF-8?q?=20for=20maximum=20defaultDecisionTimeout=20params=20(#79)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- bouncer.go | 6 +++++- pkg/cache/cache.go | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5eb193a..7eecf4b 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ make run - DefaultDecisionSeconds - int64 - default: 60 - - Used only in `live` mode, decision duration of accepted IPs + - Used only in `live` mode, maximum decision duration - CrowdsecCapiMachineId - string - Used only in `alone` mode, login for Crowdsec CAPI diff --git a/bouncer.go b/bouncer.go index 9fabd54..26cd6db 100644 --- a/bouncer.go +++ b/bouncer.go @@ -313,7 +313,11 @@ func handleNoStreamCache(bouncer *Bouncer, remoteIP string) error { return fmt.Errorf("handleNoStreamCache:parseDuration %w", err) } if isLiveMode { - bouncer.cacheClient.SetDecision(remoteIP, true, int64(duration.Seconds())) + durationSecond := int64(duration.Seconds()) + if bouncer.defaultDecisionTimeout < durationSecond { + durationSecond = bouncer.defaultDecisionTimeout + } + bouncer.cacheClient.SetDecision(remoteIP, true, durationSecond) } return fmt.Errorf("handleNoStreamCache:banned") } diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index c92445e..bd9ed9c 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -101,7 +101,7 @@ func (client *Client) GetDecision(clientIP string) (bool, error) { // SetDecision update the cache with the IP as key and the value banned / not banned. func (client *Client) SetDecision(clientIP string, isBanned bool, duration int64) { - logger.Debug(fmt.Sprintf("cache:SetDecision ip:%v isBanned:%v", clientIP, isBanned)) + logger.Debug(fmt.Sprintf("cache:SetDecision ip:%v isBanned:%v duration:%vs", clientIP, isBanned, duration)) var value string if isBanned { value = cacheBannedValue