defender: don't return expired hosts/banned ip in GetHost too

This commit is contained in:
Nicola Murino 2021-06-19 18:51:33 +02:00
parent c1b862394d
commit 81aac15a6c
No known key found for this signature in database
GPG key ID: 2F1FB59433D5A8CB
2 changed files with 17 additions and 11 deletions

View file

@ -272,11 +272,19 @@ func (d *memoryDefender) GetHost(ip string) (*DefenderEntry, error) {
}, nil
}
if ev, ok := d.hosts[ip]; ok {
return &DefenderEntry{
IP: ip,
Score: ev.TotalScore,
}, nil
if hs, ok := d.hosts[ip]; ok {
score := 0
for _, event := range hs.Events {
if event.dateTime.Add(time.Duration(d.config.ObservationTime) * time.Minute).After(time.Now()) {
score += event.score
}
}
if score > 0 {
return &DefenderEntry{
IP: ip,
Score: score,
}, nil
}
}
return nil, utils.NewRecordNotFoundError("host not found")

View file

@ -246,12 +246,10 @@ func TestExpiredHostBans(t *testing.T) {
// the recorded scored are too old
res = defender.GetHosts()
assert.Len(t, res, 0)
// the old API still returns the host
entry, err = defender.GetHost(testIP)
assert.NoError(t, err)
assert.Equal(t, testIP, entry.IP)
assert.Empty(t, entry.GetBanTime())
assert.Equal(t, 5, entry.Score)
_, err = defender.GetHost(testIP)
assert.Error(t, err)
_, ok := defender.hosts[testIP]
assert.True(t, ok)
}
func TestLoadHostListFromFile(t *testing.T) {