blocklist

package
v0.1.23 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IPRangeMiddleware

func IPRangeMiddleware(config IPRangeConfig) mist.Middleware

IPRangeMiddleware 基于IP范围的中间件

func ParseCIDR

func ParseCIDR(cidrs []string) ([]*net.IPNet, error)

ParseCIDR 解析CIDR字符串为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) BlockIP

func (m *Manager) BlockIP(ip string, duration time.Duration)

BlockIP 手动封禁IP

func (*Manager) IsBlocked

func (m *Manager) IsBlocked(ip string) bool

IsBlocked 检查IP是否被封禁

func (*Manager) Middleware

func (m *Manager) Middleware() func(http.Handler) http.Handler

Middleware 创建IP黑名单中间件

func (*Manager) MistMiddleware

func (m *Manager) MistMiddleware(options ...func(*MistBlocklistConfig)) mist.Middleware

MistMiddleware 创建适用于Mist框架的中间件,支持自定义封禁处理函数 已废弃: 请使用 security/blocklist/middleware 包中的 New 函数

func (*Manager) RecordFailure

func (m *Manager) RecordFailure(ip string) bool

RecordFailure 记录失败的尝试

func (*Manager) RecordSuccess

func (m *Manager) RecordSuccess(ip string)

RecordSuccess 记录成功的尝试,重置失败计数

func (*Manager) Stop

func (m *Manager) Stop()

Stop 停止黑名单管理器

func (*Manager) UnblockIP

func (m *Manager) UnblockIP(ip string)

UnblockIP 手动解除IP封禁

type MistBlocklistConfig

type MistBlocklistConfig struct {
	// 原始黑名单配置
	Config *BlocklistConfig
	// 当IP被封禁时的处理函数(适用于Mist框架)
	OnBlocked func(*mist.Context)
}

MistBlocklistConfig Mist框架的IP黑名单配置

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL