ipfilter

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2025 License: MPL-2.0 Imports: 5 Imported by: 0

README

ipfilter

基于IP进行黑白名单处理的中间件

Documentation

Overview

ipfilter 提供了一个用于 Touka 框架的 IP 黑白名单过滤中间件. 它允许根据 IP 地址或 CIDR 范围来允许或拒绝请求.

过滤优先级: 如果同时启用了白名单和黑名单:

  1. 首先检查客户端 IP 是否在白名单中. - 如果 IP 不在白名单中,请求将被拒绝.
  2. 如果 IP 在白名单中 (即通过了白名单检查),则继续检查黑名单. - 如果 IP 在黑名单中,请求将被拒绝.
  3. 如果通过了所有启用的规则,请求将被允许.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewIPFilter

func NewIPFilter(config IPFilterConfig) (touka.HandlerFunc, error)

NewIPFilter 是 IP 过滤中间件的构造函数. 它接收一个 IPFilterConfig 配置对象,并在初始化时解析并验证 IP 地址列表. 返回一个 Touka HandlerFunc (可直接用于 Touka.Use()) 和可能发生的错误.

Types

type IPFilterConfig

type IPFilterConfig struct {
	// EnableAllowList 启用白名单模式.
	// 如果启用,只有列出的 IP 地址或 CIDR 范围才被允许 (在检查黑名单之前).
	EnableAllowList bool
	// EnableBlockList 启用黑名单模式.
	// 如果启用,列出的 IP 地址或 CIDR 范围将被拒绝 (在检查白名单之后).
	EnableBlockList bool

	// AllowList 是一个字符串切片,包含要允许的 IP 地址或 CIDR 范围
	// (例如 "192.168.1.10", "10.0.0.0/8", "2001:db8::/32").
	AllowList []string
	// BlockList 是一个字符串切片,包含要拒绝的 IP 地址或 CIDR 范围.
	BlockList []string
}

IPFilterConfig 配置 IP 过滤中间件的行为.

Jump to

Keyboard shortcuts

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