mirror of https://github.com/bjdgyc/anylink.git
增加锁定状态记录生命周期配置项,优化清理内存的定时器
This commit is contained in:
parent
f195ae2d30
commit
59748fe395
|
@ -98,6 +98,8 @@ type ServerConfig struct {
|
|||
MaxGlobalIPBanCount int `json:"max_global_ip_ban_count"`
|
||||
GlobalIPBanResetTime int `json:"global_ip_ban_reset_time"`
|
||||
GlobalIPLockTime int `json:"global_ip_lock_time"`
|
||||
|
||||
GlobalLockStateExpirationTime int `json:"global_lock_state_expiration_time"`
|
||||
}
|
||||
|
||||
func initServerCfg() {
|
||||
|
|
|
@ -74,17 +74,19 @@ var configs = []config{
|
|||
|
||||
{Typ: cfgBool, Name: "anti_brute_force", Usage: "是否开启防爆功能", ValBool: true},
|
||||
|
||||
{Typ: cfgInt, Name: "max_ban_score", Usage: "单位时间内最大尝试次数,0为关闭防爆功能", ValInt: 5},
|
||||
{Typ: cfgInt, Name: "max_ban_score", Usage: "单位时间内最大尝试次数,0为关闭该功能", ValInt: 5},
|
||||
{Typ: cfgInt, Name: "ban_reset_time", Usage: "设置单位时间(秒),超过则重置计数", ValInt: 10},
|
||||
{Typ: cfgInt, Name: "lock_time", Usage: "超过最大尝试次数后的锁定时长(秒)", ValInt: 300},
|
||||
|
||||
{Typ: cfgInt, Name: "max_global_user_ban_count", Usage: "全局用户单位时间内最大尝试次数", ValInt: 20},
|
||||
{Typ: cfgInt, Name: "max_global_user_ban_count", Usage: "全局用户单位时间内最大尝试次数,0为关闭该功能", ValInt: 20},
|
||||
{Typ: cfgInt, Name: "global_user_ban_reset_time", Usage: "全局用户设置单位时间(秒)", ValInt: 600},
|
||||
{Typ: cfgInt, Name: "global_user_lock_time", Usage: "全局用户锁定时间(秒)", ValInt: 300},
|
||||
|
||||
{Typ: cfgInt, Name: "max_global_ip_ban_count", Usage: "全局IP单位时间内最大尝试次数", ValInt: 40},
|
||||
{Typ: cfgInt, Name: "max_global_ip_ban_count", Usage: "全局IP单位时间内最大尝试次数,0为关闭该功能", ValInt: 40},
|
||||
{Typ: cfgInt, Name: "global_ip_ban_reset_time", Usage: "全局IP设置单位时间(秒)", ValInt: 1200},
|
||||
{Typ: cfgInt, Name: "global_ip_lock_time", Usage: "全局IP锁定时间(秒)", ValInt: 300},
|
||||
|
||||
{Typ: cfgInt, Name: "global_lock_state_expiration_time", Usage: "全局锁定状态的保存生命周期(秒),超过则删除记录", ValInt: 3600},
|
||||
}
|
||||
|
||||
var envs = map[string]string{}
|
||||
|
|
|
@ -53,10 +53,10 @@ ipv4_end = "192.168.90.200"
|
|||
#是否自动添加nat
|
||||
iptables_nat = true
|
||||
|
||||
#防爆全局开关
|
||||
#防爆破全局开关
|
||||
anti_brute_force = true
|
||||
|
||||
#单位时间内最大尝试次数,0为全局关闭防爆功能
|
||||
#单位时间内最大尝试次数,0为关闭该功能
|
||||
max_ban_score = 5
|
||||
#设置单位时间(秒),超过则重置计数
|
||||
ban_reset_time = 10
|
||||
|
@ -77,5 +77,8 @@ global_ip_ban_reset_time = 1200
|
|||
#全局IP锁定时间(秒)
|
||||
global_ip_lock_time = 300
|
||||
|
||||
#全局锁定状态的保存生命周期(秒),超过则删除记录
|
||||
global_lock_state_expiration_time = 3600
|
||||
|
||||
#客户端显示详细错误信息(线上环境慎开启)
|
||||
display_error = true
|
||||
|
|
|
@ -20,7 +20,9 @@ type contextKey string
|
|||
const loginStatusKey contextKey = "login_status"
|
||||
|
||||
func init() {
|
||||
if base.Cfg.AntiBruteForce {
|
||||
lockManager.startCleanupTicker()
|
||||
}
|
||||
}
|
||||
|
||||
// 防爆破中间件
|
||||
|
@ -130,7 +132,7 @@ var lockManager = &LockManager{
|
|||
}
|
||||
|
||||
func (lm *LockManager) startCleanupTicker() {
|
||||
lm.cleanupTicker = time.NewTicker(1 * time.Minute)
|
||||
lm.cleanupTicker = time.NewTicker(5 * time.Minute)
|
||||
go func() {
|
||||
for range lm.cleanupTicker.C {
|
||||
lm.cleanupExpiredLocks()
|
||||
|
@ -140,8 +142,6 @@ func (lm *LockManager) startCleanupTicker() {
|
|||
|
||||
// 定期清理过期的锁定
|
||||
func (lm *LockManager) cleanupExpiredLocks() {
|
||||
go func() {
|
||||
for range time.Tick(5 * time.Minute) {
|
||||
now := time.Now()
|
||||
|
||||
var ipKeys, userKeys []string
|
||||
|
@ -149,20 +149,20 @@ func (lm *LockManager) cleanupExpiredLocks() {
|
|||
|
||||
lm.mu.Lock()
|
||||
for ip, state := range lm.ipLocks {
|
||||
if now.Sub(state.LastAttempt) > time.Duration(base.Cfg.GlobalIPBanResetTime)*time.Second {
|
||||
if now.Sub(state.LastAttempt) > time.Duration(base.Cfg.GlobalLockStateExpirationTime)*time.Second {
|
||||
ipKeys = append(ipKeys, ip)
|
||||
}
|
||||
}
|
||||
|
||||
for user, state := range lm.userLocks {
|
||||
if now.Sub(state.LastAttempt) > time.Duration(base.Cfg.GlobalUserBanResetTime)*time.Second {
|
||||
if now.Sub(state.LastAttempt) > time.Duration(base.Cfg.GlobalLockStateExpirationTime)*time.Second {
|
||||
userKeys = append(userKeys, user)
|
||||
}
|
||||
}
|
||||
|
||||
for user, ipMap := range lm.ipUserLocks {
|
||||
for ip, state := range ipMap {
|
||||
if now.Sub(state.LastAttempt) > time.Duration(base.Cfg.BanResetTime)*time.Second {
|
||||
if now.Sub(state.LastAttempt) > time.Duration(base.Cfg.GlobalLockStateExpirationTime)*time.Second {
|
||||
IPuserKeys = append(IPuserKeys, struct{ user, ip string }{user, ip})
|
||||
}
|
||||
}
|
||||
|
@ -183,8 +183,6 @@ func (lm *LockManager) cleanupExpiredLocks() {
|
|||
}
|
||||
}
|
||||
lm.mu.Unlock()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// 检查全局 IP 锁定
|
||||
|
|
Loading…
Reference in New Issue