Documentation
¶
Index ¶
- func IPRangeMiddleware(config IPRangeConfig) mist.Middleware
- func ParseCIDR(cidrs []string) ([]*net.IPNet, error)
- func WithBlockDuration(duration time.Duration) func(*BlocklistConfig)
- func WithClearInterval(interval time.Duration) func(*BlocklistConfig)
- func WithMaxFailedAttempts(max int) func(*BlocklistConfig)
- func WithMistOnBlocked(handler func(*mist.Context)) func(*MistBlocklistConfig)
- func WithOnBlocked(handler func(w http.ResponseWriter, r *http.Request)) func(*BlocklistConfig)
- func WithRecordExpiry(expiry time.Duration) func(*BlocklistConfig)
- func WithWhitelistIPs(ips []string) func(*BlocklistConfig)
- type BlocklistConfig
- type IPRangeConfig
- type IPRecord
- type Manager
- func (m *Manager) BlockIP(ip string, duration time.Duration)
- func (m *Manager) IsBlocked(ip string) bool
- func (m *Manager) Middleware() func(http.Handler) http.Handler
- func (m *Manager) MistMiddleware(options ...func(*MistBlocklistConfig)) mist.Middleware
- func (m *Manager) RecordFailure(ip string) bool
- func (m *Manager) RecordSuccess(ip string)
- func (m *Manager) Stop()
- func (m *Manager) UnblockIP(ip string)
- type MistBlocklistConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IPRangeMiddleware ¶
func IPRangeMiddleware(config IPRangeConfig) mist.Middleware
IPRangeMiddleware 基于IP范围的中间件
func WithBlockDuration ¶
func WithBlockDuration(duration time.Duration) func(*BlocklistConfig)
WithBlockDuration 设置封禁时长
func WithClearInterval ¶
func WithClearInterval(interval time.Duration) func(*BlocklistConfig)
WithClearInterval 设置清理间隔
func WithMaxFailedAttempts ¶
func WithMaxFailedAttempts(max int) func(*BlocklistConfig)
WithMaxFailedAttempts 设置最大失败尝试次数
func WithMistOnBlocked ¶
func WithMistOnBlocked(handler func(*mist.Context)) func(*MistBlocklistConfig)
WithMistOnBlocked 设置Mist框架中IP封禁时的处理函数
func WithOnBlocked ¶
func WithOnBlocked(handler func(w http.ResponseWriter, r *http.Request)) func(*BlocklistConfig)
WithOnBlocked 设置封禁处理函数
func WithRecordExpiry ¶
func WithRecordExpiry(expiry time.Duration) func(*BlocklistConfig)
WithRecordExpiry 设置记录过期时间
func WithWhitelistIPs ¶
func WithWhitelistIPs(ips []string) func(*BlocklistConfig)
WithWhitelistIPs 设置白名单IP
Types ¶
type BlocklistConfig ¶
type BlocklistConfig struct {
// MaxFailedAttempts 最大失败尝试次数,超过则封禁
MaxFailedAttempts int
// BlockDuration 封禁时长
BlockDuration time.Duration
// ClearInterval 清理间隔,定期清理过期的记录
ClearInterval time.Duration
// OnBlocked 封禁时的处理函数
OnBlocked func(w http.ResponseWriter, r *http.Request)
// RecordExpiry 记录过期时间,过期后失败次数重置
RecordExpiry time.Duration
// WhitelistIPs 白名单IP,这些IP不会被封禁
WhitelistIPs []string
// contains filtered or unexported fields
}
BlocklistConfig 黑名单配置
type IPRangeConfig ¶
type IPRangeConfig struct {
// AllowedNetworks 允许的IP网段
AllowedNetworks []*net.IPNet
// DeniedNetworks 拒绝的IP网段
DeniedNetworks []*net.IPNet
// DefaultAllow 默认允许策略(如果为true,则除了明确拒绝的IP外都允许)
DefaultAllow bool
// OnDenied 拒绝访问时的处理函数
OnDenied func(*mist.Context)
}
IPRangeConfig IP范围配置
type IPRecord ¶
type IPRecord struct {
// IP地址
IP string
// LastActivity 最后活动时间
LastActivity time.Time
// FailedAttempts 失败尝试次数
FailedAttempts int
// BlockedUntil 封禁解除时间
BlockedUntil time.Time
}
IPRecord 表示IP访问记录
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager IP黑名单管理器
func NewManager ¶
func NewManager(options ...func(*BlocklistConfig)) *Manager
NewManager 创建一个新的黑名单管理器
func (*Manager) Middleware ¶
Middleware 创建IP黑名单中间件
func (*Manager) MistMiddleware ¶
func (m *Manager) MistMiddleware(options ...func(*MistBlocklistConfig)) mist.Middleware
MistMiddleware 创建适用于Mist框架的中间件,支持自定义封禁处理函数 已废弃: 请使用 security/blocklist/middleware 包中的 New 函数
func (*Manager) RecordFailure ¶
RecordFailure 记录失败的尝试
func (*Manager) RecordSuccess ¶
RecordSuccess 记录成功的尝试,重置失败计数
type MistBlocklistConfig ¶
type MistBlocklistConfig struct {
// 原始黑名单配置
Config *BlocklistConfig
// 当IP被封禁时的处理函数(适用于Mist框架)
OnBlocked func(*mist.Context)
}
MistBlocklistConfig Mist框架的IP黑名单配置
Click to show internal directories.
Click to hide internal directories.