feat: add fail2ban service
This commit is contained in:
71
internal/services/fail2ban/fail.go
Normal file
71
internal/services/fail2ban/fail.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package fail2ban
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
FailedAttempts int
|
||||
BlockedUntil time.Time
|
||||
}
|
||||
|
||||
type Fail2Ban struct {
|
||||
mu sync.Mutex
|
||||
clients map[string]*Client
|
||||
}
|
||||
|
||||
func NewFail2Ban() *Fail2Ban {
|
||||
return &Fail2Ban{
|
||||
clients: make(map[string]*Client),
|
||||
mu: sync.Mutex{},
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Fail2Ban) FailedAttempt(ip string) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||
client, ok := f.clients[ip]
|
||||
if !ok {
|
||||
client = &Client{}
|
||||
f.clients[ip] = client
|
||||
}
|
||||
|
||||
client.FailedAttempts++
|
||||
if client.FailedAttempts >= 3 {
|
||||
client.BlockedUntil = time.Now().Add(10 * time.Minute)
|
||||
client.FailedAttempts = 0
|
||||
|
||||
// Automatically unblock the client after 1 hour
|
||||
time.AfterFunc(1*time.Hour, func() {
|
||||
f.unblockClient(ip)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Fail2Ban) unblockClient(ip string) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||
client, ok := f.clients[ip]
|
||||
if ok {
|
||||
client.BlockedUntil = time.Time{}
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Fail2Ban) CanChangePassword(ip string) bool {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||
client, ok := f.clients[ip]
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
|
||||
if time.Now().Before(client.BlockedUntil) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user