- 安全:移除 CSP 中 esm.sh/cdnjs.cloudflare.com,highlight.js 主题已本地化 73 个文件 - 安全:StatusBanned 分支补 dummy hash 防时序攻击 - 安全:手写 constantTimeEq 替换为 crypto/subtle.ConstantTimeCompare - Bug:锁定操作消息已删除→已锁定 - YAGNI:删除 12 个空预留模板目录 - KISS:删除 common.CheckPassword 薄封装,统一用 bcrypt 调用 - KISS:删除 tokenCtrl 别名字段,api.go 统一用 authCtrl - RateLimiter:check()+recordFail 闭包模式重构为 try() 原子操作,消除竞态 - RateLimiter:新增 ClearIP() 方法,注册成功时清除 IP 计数 - 文档:修正 audit_service.go 注释编号跳跃(3→5→4) - 文档:修正 deps_core.go 过清理→过期清理
40 lines
946 B
Go
40 lines
946 B
Go
package middleware
|
|
|
|
import "time"
|
|
|
|
// Clear 登录成功后清除该 email 和 IP 的失败计数
|
|
func (rl *RateLimiter) Clear(email, ip string) {
|
|
rl.mu.Lock()
|
|
defer rl.mu.Unlock()
|
|
delete(rl.accountFailures, email)
|
|
delete(rl.ipFailures, ip)
|
|
}
|
|
|
|
// ClearIP 按 IP key 清除失败计数(用于注册等仅 IP 维度的限流)
|
|
func (rl *RateLimiter) ClearIP(ipKey string) {
|
|
rl.mu.Lock()
|
|
defer rl.mu.Unlock()
|
|
delete(rl.ipFailures, ipKey)
|
|
}
|
|
|
|
// cleanupLoop 定期清理过期条目
|
|
func (rl *RateLimiter) cleanupLoop() {
|
|
ticker := time.NewTicker(cleanupInterval)
|
|
defer ticker.Stop()
|
|
for range ticker.C {
|
|
rl.mu.Lock()
|
|
now := time.Now()
|
|
clean := func(m map[string]*windowState) {
|
|
for k, v := range m {
|
|
if (v.blockedUntil.IsZero() || now.After(v.blockedUntil)) &&
|
|
now.Sub(v.windowStart) > accountWindow+ipBlockDur {
|
|
delete(m, k)
|
|
}
|
|
}
|
|
}
|
|
clean(rl.accountFailures)
|
|
clean(rl.ipFailures)
|
|
rl.mu.Unlock()
|
|
}
|
|
}
|