ipmatch

package
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 17 Imported by: 0

README

IP Match 规则文件示例

IPV4规则

# 内部网络段
10.20.0.0/16

# 办公网络
192.168.100.0/24
192.168.200.0/24

# 特定IP范围
172.20.1.1-172.20.1.50
172.20.2.1-10
172.20.3.*

IPV6规则

# IPv6 内部网络段
2001:db8::/32
fd00::/8

# 特定IPv6地址
2001:db8:1234::1
2001:db8:5678:abcd::1

# IPv6 范围
2001:db8:1000::1-2001:db8:1000::100
2001:db8:2000:1::-2001:db8:2000:1:ffff:ffff:ffff:ffff

# 办公网络IPv6段
2001:db8:aaaa::/48
2001:db8:bbbb::/48

# 开发环境IPv6
2001:db8:cccc:1::/64
2001:db8:cccc:2::/64

# 特定服务IPv6
2001:db8:1111:2222::/64

Documentation

Index

Constants

View Source
const (
	DefaultIPv4MapThreshold = 100000 // IPv4默认阈值:10万IP
	DefaultIPv6MapThreshold = 50000  // IPv6默认阈值:5万IP(IPv6范围通常较大)
)

默认阈值常量

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Dynamic          bool   `json:"dynamic" yaml:"dynamic" ini:"dynamic"`                                  // 是否支持动态添加规则
	IPv4MapThreshold uint64 `json:"ipv4_map_threshold" yaml:"ipv4_map_threshold" ini:"ipv4_map_threshold"` // IPv4 Map模式阈值
	IPv6MapThreshold uint64 `json:"ipv6_map_threshold" yaml:"ipv6_map_threshold" ini:"ipv6_map_threshold"` // IPv6 Map模式阈值

	IPv4RuleSet IPRuleSet `json:"ipv4_rule_set" yaml:"ipv4_rule_set" ini:"ipv4_rule_set"`
	IPv6RuleSet IPRuleSet `json:"ipv6_rule_set" yaml:"ipv6_rule_set" ini:"ipv6_rule_set"`

	IPv4CacheTTL time.Duration `json:"ipv4_cache_ttl" yaml:"ipv4_cache_ttl" ini:"ipv4_cache_ttl"`
	IPv6CacheTTL time.Duration `json:"ipv6_cache_ttl" yaml:"ipv6_cache_ttl" ini:"ipv6_cache_ttl"`
}

Config IP匹配器配置

type IPMatcher

type IPMatcher struct {
	// contains filtered or unexported fields
}

IPMatcher 主匹配器结构

func NewIPMatcher

func NewIPMatcher(ctx context.Context, config *Config) (*IPMatcher, error)

func (*IPMatcher) AddIPv4Rule

func (m *IPMatcher) AddIPv4Rule(ip string) error

AddIPv4Rule 添加ipv4规则 对于单个的ip,都定义为离散ip 对于多个的ip,都定义为连续范围 后面在构建的时候再进行排序合并

func (*IPMatcher) AddIPv6Rule

func (m *IPMatcher) AddIPv6Rule(ip string) error

AddIPv6Rule 添加ipv6规则

func (*IPMatcher) Build

func (m *IPMatcher) Build()

Build 构建

func (*IPMatcher) Contains

func (m *IPMatcher) Contains(ip string) bool

func (*IPMatcher) LoadRule

func (m *IPMatcher) LoadRule() error

type IPRuleSet

type IPRuleSet struct {
	Rule     []string `json:"rule" yaml:"rule" ini:"rule"`                // 规则
	RuleFile []string `json:"rule_file" yaml:"rule_file" ini:"rule_file"` // 规则文件
}

type IPVersion

type IPVersion string

IPVersion IP版本类型

const (
	IPv4 IPVersion = "ipv4"
	IPv6 IPVersion = "ipv6"
)

Jump to

Keyboard shortcuts

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