feat(admin): 增加登录限流、审计与令牌退出 (#42)
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
package adminhandler
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type loginLimiter struct {
|
||||
mu sync.Mutex
|
||||
ipFailures map[string][]time.Time
|
||||
usernameFailures map[string][]time.Time
|
||||
ipLimit int
|
||||
usernameLimit int
|
||||
window time.Duration
|
||||
lastPrune time.Time
|
||||
}
|
||||
|
||||
func newLoginLimiter(ipLimit, usernameLimit int, window time.Duration) *loginLimiter {
|
||||
return &loginLimiter{
|
||||
ipFailures: make(map[string][]time.Time),
|
||||
usernameFailures: make(map[string][]time.Time),
|
||||
ipLimit: ipLimit,
|
||||
usernameLimit: usernameLimit,
|
||||
window: window,
|
||||
}
|
||||
}
|
||||
|
||||
func loginUsernameKey(username string) string {
|
||||
return strings.ToLower(strings.TrimSpace(username))
|
||||
}
|
||||
|
||||
func pruneLoginFailures(failures []time.Time, cutoff time.Time) []time.Time {
|
||||
kept := failures[:0]
|
||||
for _, failure := range failures {
|
||||
if failure.After(cutoff) {
|
||||
kept = append(kept, failure)
|
||||
}
|
||||
}
|
||||
return kept
|
||||
}
|
||||
|
||||
func (l *loginLimiter) pruneExpired(now time.Time) {
|
||||
if !l.lastPrune.IsZero() && now.Sub(l.lastPrune) < l.window {
|
||||
return
|
||||
}
|
||||
cutoff := now.Add(-l.window)
|
||||
for key, failures := range l.ipFailures {
|
||||
kept := pruneLoginFailures(failures, cutoff)
|
||||
if len(kept) == 0 {
|
||||
delete(l.ipFailures, key)
|
||||
} else {
|
||||
l.ipFailures[key] = kept
|
||||
}
|
||||
}
|
||||
for key, failures := range l.usernameFailures {
|
||||
kept := pruneLoginFailures(failures, cutoff)
|
||||
if len(kept) == 0 {
|
||||
delete(l.usernameFailures, key)
|
||||
} else {
|
||||
l.usernameFailures[key] = kept
|
||||
}
|
||||
}
|
||||
l.lastPrune = now
|
||||
}
|
||||
|
||||
func retryAfter(failures []time.Time, limit int, now time.Time, window time.Duration) time.Duration {
|
||||
if len(failures) < limit || len(failures) == 0 {
|
||||
return 0
|
||||
}
|
||||
remaining := failures[0].Add(window).Sub(now)
|
||||
if remaining < 0 {
|
||||
return 0
|
||||
}
|
||||
return remaining
|
||||
}
|
||||
|
||||
func (l *loginLimiter) blocked(ip, username string) (bool, time.Duration) {
|
||||
now := time.Now()
|
||||
cutoff := now.Add(-l.window)
|
||||
username = loginUsernameKey(username)
|
||||
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
l.pruneExpired(now)
|
||||
|
||||
ipFailures := pruneLoginFailures(l.ipFailures[ip], cutoff)
|
||||
if len(ipFailures) == 0 {
|
||||
delete(l.ipFailures, ip)
|
||||
} else {
|
||||
l.ipFailures[ip] = ipFailures
|
||||
}
|
||||
userFailures := pruneLoginFailures(l.usernameFailures[username], cutoff)
|
||||
if len(userFailures) == 0 {
|
||||
delete(l.usernameFailures, username)
|
||||
} else {
|
||||
l.usernameFailures[username] = userFailures
|
||||
}
|
||||
|
||||
ipRetry := retryAfter(ipFailures, l.ipLimit, now, l.window)
|
||||
userRetry := retryAfter(userFailures, l.usernameLimit, now, l.window)
|
||||
if userRetry > ipRetry {
|
||||
ipRetry = userRetry
|
||||
}
|
||||
return ipRetry > 0, ipRetry
|
||||
}
|
||||
|
||||
func (l *loginLimiter) recordFailure(ip, username string) {
|
||||
now := time.Now()
|
||||
cutoff := now.Add(-l.window)
|
||||
username = loginUsernameKey(username)
|
||||
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
l.pruneExpired(now)
|
||||
|
||||
ipFailures := pruneLoginFailures(l.ipFailures[ip], cutoff)
|
||||
l.ipFailures[ip] = append(ipFailures, now)
|
||||
userFailures := pruneLoginFailures(l.usernameFailures[username], cutoff)
|
||||
l.usernameFailures[username] = append(userFailures, now)
|
||||
}
|
||||
|
||||
func (l *loginLimiter) clearIP(ip string) {
|
||||
l.mu.Lock()
|
||||
delete(l.ipFailures, ip)
|
||||
l.mu.Unlock()
|
||||
}
|
||||
Reference in New Issue
Block a user