defender: fix GetHost for blocklist entries too

This commit is contained in:
Nicola Murino 2021-06-20 21:57:19 +02:00
parent ac8d8a3da1
commit e09bdd43d4
No known key found for this signature in database
GPG key ID: 2F1FB59433D5A8CB
2 changed files with 12 additions and 10 deletions

View file

@ -266,11 +266,13 @@ func (d *memoryDefender) GetHost(ip string) (*DefenderEntry, error) {
defer d.RUnlock()
if banTime, ok := d.banned[ip]; ok {
if banTime.After(time.Now()) {
return &DefenderEntry{
IP: ip,
BanTime: banTime,
}, nil
}
}
if hs, ok := d.hosts[ip]; ok {
score := 0

View file

@ -206,14 +206,14 @@ func TestExpiredHostBans(t *testing.T) {
assert.Len(t, res, 0)
assert.False(t, defender.IsBanned(testIP))
entry, err := defender.GetHost(testIP)
assert.NoError(t, err)
assert.Equal(t, testIP, entry.IP)
assert.NotEmpty(t, entry.GetBanTime())
_, err = defender.GetHost(testIP)
assert.Error(t, err)
_, ok := defender.banned[testIP]
assert.True(t, ok)
// now add an event for an expired banned ip, it should be removed
defender.AddEvent(testIP, HostEventLoginFailed)
assert.False(t, defender.IsBanned(testIP))
entry, err = defender.GetHost(testIP)
entry, err := defender.GetHost(testIP)
assert.NoError(t, err)
assert.Equal(t, testIP, entry.IP)
assert.Empty(t, entry.GetBanTime())
@ -248,7 +248,7 @@ func TestExpiredHostBans(t *testing.T) {
assert.Len(t, res, 0)
_, err = defender.GetHost(testIP)
assert.Error(t, err)
_, ok := defender.hosts[testIP]
_, ok = defender.hosts[testIP]
assert.True(t, ok)
}