blocklist

package
v0.1.27 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrGeoDBNotInitialized 表示GeoIP数据库未初始化
	ErrGeoDBNotInitialized = errors.New("GeoIP数据库未初始化")
	// ErrCountryNotFound 表示未找到国家信息
	ErrCountryNotFound = errors.New("无法确定IP的国家信息")
	// ErrIPBlocked 表示IP因地理位置限制被封禁
	ErrIPBlocked = errors.New("IP受到地理位置限制")
)

地理位置限制相关错误

View Source
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) BlockIP

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

BlockIP 手动封禁IP

func (*Manager) GetIPInfo added in v0.1.24

func (m *Manager) GetIPInfo(ip string) (*IPRecord, error)

GetIPInfo 获取IP详细信息

func (*Manager) IsBlocked

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

IsBlocked 检查IP是否被封禁

func (*Manager) ListBlockedIPs added in v0.1.24

func (m *Manager) ListBlockedIPs() ([]*IPRecord, error)

ListBlockedIPs 列出所有被封禁的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框架的中间件

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 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) Close added in v0.1.24

func (s *RedisStorage) Close() error

Close 关闭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 定义黑名单存储接口

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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