Documentation
¶
Index ¶
- Variables
- func WithAllowPrivateIPs(allow bool) func(*BlocklistConfig)
- func WithBlockDuration(duration time.Duration) func(*BlocklistConfig)
- func WithClearInterval(interval time.Duration) func(*BlocklistConfig)
- func WithGeoRestriction(geo *GeoRestriction) 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 WithProgressiveBlocking(enable bool, factor float64, maxDuration time.Duration) func(*BlocklistConfig)
- func WithRecordExpiry(expiry time.Duration) func(*BlocklistConfig)
- func WithStorage(storage Storage) func(*BlocklistConfig)
- func WithWhitelistIPs(ips []string) func(*BlocklistConfig)
- type BlocklistConfig
- type CountryInfo
- type GeoRestriction
- func (g *GeoRestriction) AddCountry(countryCode string)
- func (g *GeoRestriction) Close() error
- func (g *GeoRestriction) GeoMiddleware(next http.Handler) http.Handler
- func (g *GeoRestriction) GetCountryCode(ip string) (string, error)
- func (g *GeoRestriction) GetIPInfo(ip string) (*CountryInfo, error)
- func (g *GeoRestriction) InitDBFromFile(dbPath string) error
- func (g *GeoRestriction) InitDBFromURL(url, savePath string) error
- func (g *GeoRestriction) IsIPRestricted(ip string) (bool, error)
- func (g *GeoRestriction) MistGeoMiddleware(onRestricted func(*mist.Context)) mist.Middleware
- func (g *GeoRestriction) RemoveCountry(countryCode string)
- type GeoRestrictionMode
- type IPRecord
- type Manager
- func (m *Manager) BlockIP(ip string, duration time.Duration)
- func (m *Manager) GetIPInfo(ip string) (*IPRecord, error)
- func (m *Manager) IsBlocked(ip string) bool
- func (m *Manager) ListBlockedIPs() ([]*IPRecord, error)
- 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 MemoryStorage
- func (s *MemoryStorage) Close() error
- func (s *MemoryStorage) DeleteIPRecord(ip string) error
- func (s *MemoryStorage) GetIPRecord(ip string) (*IPRecord, error)
- func (s *MemoryStorage) ListBlockedIPs() ([]*IPRecord, error)
- func (s *MemoryStorage) ListIPRecords() ([]*IPRecord, error)
- func (s *MemoryStorage) SaveIPRecord(record *IPRecord) error
- type MistBlocklistConfig
- type RedisStorage
- func (s *RedisStorage) Close() error
- func (s *RedisStorage) DeleteIPRecord(ip string) error
- func (s *RedisStorage) GetIPRecord(ip string) (*IPRecord, error)
- func (s *RedisStorage) ListBlockedIPs() ([]*IPRecord, error)
- func (s *RedisStorage) ListIPRecords() ([]*IPRecord, error)
- func (s *RedisStorage) SaveIPRecord(record *IPRecord) error
- type Storage
Constants ¶
This section is empty.
Variables ¶
var ( // ErrGeoDBNotInitialized 表示GeoIP数据库未初始化 ErrGeoDBNotInitialized = errors.New("GeoIP数据库未初始化") // ErrCountryNotFound 表示未找到国家信息 ErrCountryNotFound = errors.New("无法确定IP的国家信息") // ErrIPBlocked 表示IP因地理位置限制被封禁 ErrIPBlocked = errors.New("IP受到地理位置限制") )
地理位置限制相关错误
var ( // ErrStorageOperationFailed 表示存储操作失败 ErrStorageOperationFailed = errors.New("存储操作失败") )
错误定义
Functions ¶
func WithAllowPrivateIPs ¶ added in v0.1.24
func WithAllowPrivateIPs(allow bool) func(*BlocklistConfig)
WithAllowPrivateIPs 设置是否允许私有IP
func WithBlockDuration ¶
func WithBlockDuration(duration time.Duration) func(*BlocklistConfig)
WithBlockDuration 设置封禁时长
func WithClearInterval ¶
func WithClearInterval(interval time.Duration) func(*BlocklistConfig)
WithClearInterval 设置清理间隔
func WithGeoRestriction ¶ added in v0.1.24
func WithGeoRestriction(geo *GeoRestriction) func(*BlocklistConfig)
WithGeoRestriction 设置地理位置限制
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 WithProgressiveBlocking ¶ added in v0.1.24
func WithProgressiveBlocking(enable bool, factor float64, maxDuration time.Duration) func(*BlocklistConfig)
WithProgressiveBlocking 设置是否使用递增封禁时长
func WithRecordExpiry ¶
func WithRecordExpiry(expiry time.Duration) func(*BlocklistConfig)
WithRecordExpiry 设置记录过期时间
func WithStorage ¶ added in v0.1.24
func WithStorage(storage Storage) func(*BlocklistConfig)
WithStorage 设置存储实现
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
// Storage 存储实现,默认为内存存储
Storage Storage
// UseProgressiveBlocking 是否使用递增封禁时长
UseProgressiveBlocking bool
// ProgressiveBlockingFactor 递增封禁时长因子
ProgressiveBlockingFactor float64
// MaxBlockDuration 最大封禁时长
MaxBlockDuration time.Duration
// GeoRestriction 地理位置限制,可选
GeoRestriction *GeoRestriction
// AllowPrivateIPs 是否允许私有IP地址
AllowPrivateIPs bool
// contains filtered or unexported fields
}
BlocklistConfig 黑名单配置
type CountryInfo ¶ added in v0.1.24
type CountryInfo struct {
// Code 国家代码
Code string `json:"code"`
// Name 国家名称
Name string `json:"name"`
// Continent 大陆
Continent string `json:"continent"`
}
CountryInfo 国家信息结构体
type GeoRestriction ¶ added in v0.1.24
type GeoRestriction struct {
// Mode 限制模式
Mode GeoRestrictionMode
// Countries 国家代码列表
Countries []string
// DB GeoIP2数据库
DB *geoip2.Reader
// contains filtered or unexported fields
}
GeoRestriction 地理位置限制配置
func NewGeoRestriction ¶ added in v0.1.24
func NewGeoRestriction(mode GeoRestrictionMode, countries []string) *GeoRestriction
NewGeoRestriction 创建地理位置限制实例
func (*GeoRestriction) AddCountry ¶ added in v0.1.24
func (g *GeoRestriction) AddCountry(countryCode string)
AddCountry 添加国家到限制列表
func (*GeoRestriction) Close ¶ added in v0.1.24
func (g *GeoRestriction) Close() error
Close 关闭GeoIP2数据库
func (*GeoRestriction) GeoMiddleware ¶ added in v0.1.24
func (g *GeoRestriction) GeoMiddleware(next http.Handler) http.Handler
GeoMiddleware 创建基于地理位置的中间件
func (*GeoRestriction) GetCountryCode ¶ added in v0.1.24
func (g *GeoRestriction) GetCountryCode(ip string) (string, error)
GetCountryCode 获取IP地址的国家代码
func (*GeoRestriction) GetIPInfo ¶ added in v0.1.24
func (g *GeoRestriction) GetIPInfo(ip string) (*CountryInfo, error)
GetIPInfo 获取IP详细信息
func (*GeoRestriction) InitDBFromFile ¶ added in v0.1.24
func (g *GeoRestriction) InitDBFromFile(dbPath string) error
InitDBFromFile 从文件初始化GeoIP2数据库
func (*GeoRestriction) InitDBFromURL ¶ added in v0.1.24
func (g *GeoRestriction) InitDBFromURL(url, savePath string) error
InitDBFromURL 从URL下载并初始化GeoIP2数据库
func (*GeoRestriction) IsIPRestricted ¶ added in v0.1.24
func (g *GeoRestriction) IsIPRestricted(ip string) (bool, error)
IsIPRestricted 检查IP是否受到地理位置限制
func (*GeoRestriction) MistGeoMiddleware ¶ added in v0.1.24
func (g *GeoRestriction) MistGeoMiddleware(onRestricted func(*mist.Context)) mist.Middleware
MistGeoMiddleware 创建基于地理位置的Mist中间件
func (*GeoRestriction) RemoveCountry ¶ added in v0.1.24
func (g *GeoRestriction) RemoveCountry(countryCode string)
RemoveCountry 从限制列表移除国家
type GeoRestrictionMode ¶ added in v0.1.24
type GeoRestrictionMode int
GeoRestrictionMode 地理位置限制模式
const ( // AllowListMode 白名单模式 - 只允许指定国家/地区 AllowListMode GeoRestrictionMode = iota // BlockListMode 黑名单模式 - 禁止指定国家/地区 BlockListMode )
type IPRecord ¶
type IPRecord struct {
// IP地址
IP string `json:"ip"`
// LastActivity 最后活动时间
LastActivity time.Time `json:"last_activity"`
// FailedAttempts 失败尝试次数
FailedAttempts int `json:"failed_attempts"`
// BlockedUntil 封禁解除时间
BlockedUntil time.Time `json:"blocked_until"`
// BlockCount 封禁次数,用于递增封禁时长
BlockCount int `json:"block_count"`
// CountryCode 国家/地区代码
CountryCode string `json:"country_code,omitempty"`
}
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) ListBlockedIPs ¶ added in v0.1.24
ListBlockedIPs 列出所有被封禁的IP
func (*Manager) Middleware ¶
Middleware 创建IP黑名单中间件
func (*Manager) MistMiddleware ¶
func (m *Manager) MistMiddleware(options ...func(*MistBlocklistConfig)) mist.Middleware
MistMiddleware 创建适用于Mist框架的中间件
func (*Manager) RecordFailure ¶
RecordFailure 记录失败的尝试
func (*Manager) RecordSuccess ¶
RecordSuccess 记录成功的尝试,重置失败计数
type MemoryStorage ¶ added in v0.1.24
type MemoryStorage struct {
// contains filtered or unexported fields
}
MemoryStorage 内存存储实现
func NewMemoryStorage ¶ added in v0.1.24
func NewMemoryStorage() *MemoryStorage
NewMemoryStorage 创建新的内存存储
func (*MemoryStorage) Close ¶ added in v0.1.24
func (s *MemoryStorage) Close() error
Close 关闭内存存储(无操作)
func (*MemoryStorage) DeleteIPRecord ¶ added in v0.1.24
func (s *MemoryStorage) DeleteIPRecord(ip string) error
DeleteIPRecord 从内存删除IP记录
func (*MemoryStorage) GetIPRecord ¶ added in v0.1.24
func (s *MemoryStorage) GetIPRecord(ip string) (*IPRecord, error)
GetIPRecord 从内存获取IP记录
func (*MemoryStorage) ListBlockedIPs ¶ added in v0.1.24
func (s *MemoryStorage) ListBlockedIPs() ([]*IPRecord, error)
ListBlockedIPs 列出所有被封禁的IP
func (*MemoryStorage) ListIPRecords ¶ added in v0.1.24
func (s *MemoryStorage) ListIPRecords() ([]*IPRecord, error)
ListIPRecords 列出所有内存中的IP记录
func (*MemoryStorage) SaveIPRecord ¶ added in v0.1.24
func (s *MemoryStorage) SaveIPRecord(record *IPRecord) error
SaveIPRecord 保存IP记录到内存
type MistBlocklistConfig ¶
type MistBlocklistConfig struct {
// 原始黑名单配置
Config *BlocklistConfig
// 当IP被封禁时的处理函数(适用于Mist框架)
OnBlocked func(*mist.Context)
}
MistBlocklistConfig Mist框架的黑名单配置
type RedisStorage ¶ added in v0.1.24
type RedisStorage struct {
// contains filtered or unexported fields
}
RedisStorage Redis存储实现
func NewRedisStorage ¶ added in v0.1.24
func NewRedisStorage(addr, password string, db int, prefix string) (*RedisStorage, error)
NewRedisStorage 创建新的Redis存储
func (*RedisStorage) DeleteIPRecord ¶ added in v0.1.24
func (s *RedisStorage) DeleteIPRecord(ip string) error
DeleteIPRecord 从Redis删除IP记录
func (*RedisStorage) GetIPRecord ¶ added in v0.1.24
func (s *RedisStorage) GetIPRecord(ip string) (*IPRecord, error)
GetIPRecord 从Redis获取IP记录
func (*RedisStorage) ListBlockedIPs ¶ added in v0.1.24
func (s *RedisStorage) ListBlockedIPs() ([]*IPRecord, error)
ListBlockedIPs 列出Redis中所有被封禁的IP
func (*RedisStorage) ListIPRecords ¶ added in v0.1.24
func (s *RedisStorage) ListIPRecords() ([]*IPRecord, error)
ListIPRecords 列出Redis中的所有IP记录
func (*RedisStorage) SaveIPRecord ¶ added in v0.1.24
func (s *RedisStorage) SaveIPRecord(record *IPRecord) error
SaveIPRecord 保存IP记录到Redis
type Storage ¶ added in v0.1.24
type Storage interface {
// SaveIPRecord 保存IP记录
SaveIPRecord(record *IPRecord) error
// GetIPRecord 获取IP记录
GetIPRecord(ip string) (*IPRecord, error)
// DeleteIPRecord 删除IP记录
DeleteIPRecord(ip string) error
// ListIPRecords 列出所有IP记录
ListIPRecords() ([]*IPRecord, error)
// ListBlockedIPs 列出所有被封禁的IP
ListBlockedIPs() ([]*IPRecord, error)
// Close 关闭存储连接
Close() error
}
Storage 定义黑名单存储接口