perf(GeoIPMatcher): faster heuristic matching with reduced memory usage (#5289)
Some checks failed
Build and Release for Windows 7 / check-assets (push) Has been cancelled
Build and Release for Windows 7 / build (win7-32, 386, windows) (push) Has been cancelled
Build and Release for Windows 7 / build (win7-64, amd64, windows) (push) Has been cancelled
Build and Release / check-assets (push) Has been cancelled
Build and Release / build (386, freebsd, ) (push) Has been cancelled
Build and Release / build (386, linux, ) (push) Has been cancelled
Build and Release / build (386, openbsd, ) (push) Has been cancelled
Build and Release / build (386, windows, ) (push) Has been cancelled
Build and Release / build (amd64, android, android-amd64) (push) Has been cancelled
Build and Release / build (amd64, darwin, ) (push) Has been cancelled
Build and Release / build (amd64, freebsd, ) (push) Has been cancelled
Build and Release / build (amd64, linux, ) (push) Has been cancelled
Build and Release / build (amd64, openbsd, ) (push) Has been cancelled
Build and Release / build (amd64, windows, ) (push) Has been cancelled
Build and Release / build (arm, 5, linux) (push) Has been cancelled
Build and Release / build (arm, 6, linux) (push) Has been cancelled
Build and Release / build (arm, 7, freebsd) (push) Has been cancelled
Build and Release / build (arm, 7, linux) (push) Has been cancelled
Build and Release / build (arm, 7, openbsd) (push) Has been cancelled
Build and Release / build (arm, 7, windows) (push) Has been cancelled
Build and Release / build (arm64, android) (push) Has been cancelled
Build and Release / build (arm64, darwin) (push) Has been cancelled
Build and Release / build (arm64, freebsd) (push) Has been cancelled
Build and Release / build (arm64, linux) (push) Has been cancelled
Build and Release / build (arm64, openbsd) (push) Has been cancelled
Build and Release / build (arm64, windows) (push) Has been cancelled
Build and Release / build (loong64, linux) (push) Has been cancelled
Build and Release / build (mips, linux) (push) Has been cancelled
Build and Release / build (mips64, linux) (push) Has been cancelled
Build and Release / build (mips64le, linux) (push) Has been cancelled
Build and Release / build (mipsle, linux) (push) Has been cancelled
Build and Release / build (ppc64, linux) (push) Has been cancelled
Build and Release / build (ppc64le, linux) (push) Has been cancelled
Build and Release / build (riscv64, linux) (push) Has been cancelled
Build and Release / build (s390x, linux) (push) Has been cancelled
Test / check-assets (push) Has been cancelled
Test / test (macos-latest) (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
Test / test (windows-latest) (push) Has been cancelled

This commit is contained in:
Meow
2025-11-21 10:54:01 +08:00
committed by GitHub
parent b40bf56e4e
commit fcfb0a302a
6 changed files with 996 additions and 203 deletions

View File

@@ -29,8 +29,8 @@ type Client struct {
server Server server Server
skipFallback bool skipFallback bool
domains []string domains []string
expectedIPs []*router.GeoIPMatcher expectedIPs router.GeoIPMatcher
unexpectedIPs []*router.GeoIPMatcher unexpectedIPs router.GeoIPMatcher
actPrior bool actPrior bool
actUnprior bool actUnprior bool
tag string tag string
@@ -154,23 +154,21 @@ func NewClient(
} }
// Establish expected IPs // Establish expected IPs
var expectedMatchers []*router.GeoIPMatcher var expectedMatcher router.GeoIPMatcher
for _, geoip := range ns.ExpectedGeoip { if len(ns.ExpectedGeoip) > 0 {
matcher, err := router.GlobalGeoIPContainer.Add(geoip) expectedMatcher, err = router.BuildOptimizedGeoIPMatcher(ns.ExpectedGeoip...)
if err != nil { if err != nil {
return errors.New("failed to create expected ip matcher").Base(err).AtWarning() return errors.New("failed to create expected ip matcher").Base(err).AtWarning()
} }
expectedMatchers = append(expectedMatchers, matcher)
} }
// Establish unexpected IPs // Establish unexpected IPs
var unexpectedMatchers []*router.GeoIPMatcher var unexpectedMatcher router.GeoIPMatcher
for _, geoip := range ns.UnexpectedGeoip { if len(ns.UnexpectedGeoip) > 0 {
matcher, err := router.GlobalGeoIPContainer.Add(geoip) unexpectedMatcher, err = router.BuildOptimizedGeoIPMatcher(ns.UnexpectedGeoip...)
if err != nil { if err != nil {
return errors.New("failed to create unexpected ip matcher").Base(err).AtWarning() return errors.New("failed to create unexpected ip matcher").Base(err).AtWarning()
} }
unexpectedMatchers = append(unexpectedMatchers, matcher)
} }
if len(clientIP) > 0 { if len(clientIP) > 0 {
@@ -192,8 +190,8 @@ func NewClient(
client.server = server client.server = server
client.skipFallback = ns.SkipFallback client.skipFallback = ns.SkipFallback
client.domains = rules client.domains = rules
client.expectedIPs = expectedMatchers client.expectedIPs = expectedMatcher
client.unexpectedIPs = unexpectedMatchers client.unexpectedIPs = unexpectedMatcher
client.actPrior = ns.ActPrior client.actPrior = ns.ActPrior
client.actUnprior = ns.ActUnprior client.actUnprior = ns.ActUnprior
client.tag = tag client.tag = tag
@@ -243,32 +241,32 @@ func (c *Client) QueryIP(ctx context.Context, domain string, option dns.IPOption
return nil, 0, dns.ErrEmptyResponse return nil, 0, dns.ErrEmptyResponse
} }
if len(c.expectedIPs) > 0 && !c.actPrior { if c.expectedIPs != nil && !c.actPrior {
ips = router.MatchIPs(c.expectedIPs, ips, false) ips, _ = c.expectedIPs.FilterIPs(ips)
errors.LogDebug(context.Background(), "domain ", domain, " expectedIPs ", ips, " matched at server ", c.Name()) errors.LogDebug(context.Background(), "domain ", domain, " expectedIPs ", ips, " matched at server ", c.Name())
if len(ips) == 0 { if len(ips) == 0 {
return nil, 0, dns.ErrEmptyResponse return nil, 0, dns.ErrEmptyResponse
} }
} }
if len(c.unexpectedIPs) > 0 && !c.actUnprior { if c.unexpectedIPs != nil && !c.actUnprior {
ips = router.MatchIPs(c.unexpectedIPs, ips, true) _, ips = c.unexpectedIPs.FilterIPs(ips)
errors.LogDebug(context.Background(), "domain ", domain, " unexpectedIPs ", ips, " matched at server ", c.Name()) errors.LogDebug(context.Background(), "domain ", domain, " unexpectedIPs ", ips, " matched at server ", c.Name())
if len(ips) == 0 { if len(ips) == 0 {
return nil, 0, dns.ErrEmptyResponse return nil, 0, dns.ErrEmptyResponse
} }
} }
if len(c.expectedIPs) > 0 && c.actPrior { if c.expectedIPs != nil && c.actPrior {
ipsNew := router.MatchIPs(c.expectedIPs, ips, false) ipsNew, _ := c.expectedIPs.FilterIPs(ips)
if len(ipsNew) > 0 { if len(ipsNew) > 0 {
ips = ipsNew ips = ipsNew
errors.LogDebug(context.Background(), "domain ", domain, " priorIPs ", ips, " matched at server ", c.Name()) errors.LogDebug(context.Background(), "domain ", domain, " priorIPs ", ips, " matched at server ", c.Name())
} }
} }
if len(c.unexpectedIPs) > 0 && c.actUnprior { if c.unexpectedIPs != nil && c.actUnprior {
ipsNew := router.MatchIPs(c.unexpectedIPs, ips, true) _, ipsNew := c.unexpectedIPs.FilterIPs(ips)
if len(ipsNew) > 0 { if len(ipsNew) > 0 {
ips = ipsNew ips = ipsNew
errors.LogDebug(context.Background(), "domain ", domain, " unpriorIPs ", ips, " matched at server ", c.Name()) errors.LogDebug(context.Background(), "domain ", domain, " unpriorIPs ", ips, " matched at server ", c.Name())

View File

@@ -96,61 +96,53 @@ func (m *DomainMatcher) Apply(ctx routing.Context) bool {
return m.ApplyDomain(domain) return m.ApplyDomain(domain)
} }
type MultiGeoIPMatcher struct { type MatcherAsType byte
matchers []*GeoIPMatcher
asType string // local, source, target const (
MatcherAsType_Local MatcherAsType = iota
MatcherAsType_Source
MatcherAsType_Target
MatcherAsType_VlessRoute // for port
)
type IPMatcher struct {
matcher GeoIPMatcher
asType MatcherAsType
} }
func NewMultiGeoIPMatcher(geoips []*GeoIP, asType string) (*MultiGeoIPMatcher, error) { func NewIPMatcher(geoips []*GeoIP, asType MatcherAsType) (*IPMatcher, error) {
var matchers []*GeoIPMatcher matcher, err := BuildOptimizedGeoIPMatcher(geoips...)
for _, geoip := range geoips {
matcher, err := GlobalGeoIPContainer.Add(geoip)
if err != nil { if err != nil {
return nil, err return nil, err
} }
matchers = append(matchers, matcher) return &IPMatcher{matcher: matcher, asType: asType}, nil
}
matcher := &MultiGeoIPMatcher{
matchers: matchers,
asType: asType,
}
return matcher, nil
} }
// Apply implements Condition. // Apply implements Condition.
func (m *MultiGeoIPMatcher) Apply(ctx routing.Context) bool { func (m *IPMatcher) Apply(ctx routing.Context) bool {
var ips []net.IP var ips []net.IP
switch m.asType { switch m.asType {
case "local": case MatcherAsType_Local:
ips = ctx.GetLocalIPs() ips = ctx.GetLocalIPs()
case "source": case MatcherAsType_Source:
ips = ctx.GetSourceIPs() ips = ctx.GetSourceIPs()
case "target": case MatcherAsType_Target:
ips = ctx.GetTargetIPs() ips = ctx.GetTargetIPs()
default: default:
panic("unreachable, asType should be local or source or target") panic("unk asType")
} }
for _, ip := range ips { return m.matcher.AnyMatch(ips)
for _, matcher := range m.matchers {
if matcher.Match(ip) {
return true
}
}
}
return false
} }
type PortMatcher struct { type PortMatcher struct {
port net.MemoryPortList port net.MemoryPortList
asType string // local, source, target asType MatcherAsType
} }
// NewPortMatcher create a new port matcher that can match source or local or destination port // NewPortMatcher create a new port matcher that can match source or local or destination port
func NewPortMatcher(list *net.PortList, asType string) *PortMatcher { func NewPortMatcher(list *net.PortList, asType MatcherAsType) *PortMatcher {
return &PortMatcher{ return &PortMatcher{
port: net.PortListFromProto(list), port: net.PortListFromProto(list),
asType: asType, asType: asType,
@@ -160,18 +152,17 @@ func NewPortMatcher(list *net.PortList, asType string) *PortMatcher {
// Apply implements Condition. // Apply implements Condition.
func (v *PortMatcher) Apply(ctx routing.Context) bool { func (v *PortMatcher) Apply(ctx routing.Context) bool {
switch v.asType { switch v.asType {
case "local": case MatcherAsType_Local:
return v.port.Contains(ctx.GetLocalPort()) return v.port.Contains(ctx.GetLocalPort())
case "source": case MatcherAsType_Source:
return v.port.Contains(ctx.GetSourcePort()) return v.port.Contains(ctx.GetSourcePort())
case "target": case MatcherAsType_Target:
return v.port.Contains(ctx.GetTargetPort()) return v.port.Contains(ctx.GetTargetPort())
case "vlessRoute": case MatcherAsType_VlessRoute:
return v.port.Contains(ctx.GetVlessRoute()) return v.port.Contains(ctx.GetVlessRoute())
default: default:
panic("unreachable, asType should be local or source or target") panic("unk asType")
} }
} }
type NetworkMatcher struct { type NetworkMatcher struct {

File diff suppressed because it is too large Load Diff

View File

@@ -35,33 +35,6 @@ func getAssetPath(file string) (string, error) {
return path, nil return path, nil
} }
func TestGeoIPMatcherContainer(t *testing.T) {
container := &router.GeoIPMatcherContainer{}
m1, err := container.Add(&router.GeoIP{
CountryCode: "CN",
})
common.Must(err)
m2, err := container.Add(&router.GeoIP{
CountryCode: "US",
})
common.Must(err)
m3, err := container.Add(&router.GeoIP{
CountryCode: "CN",
})
common.Must(err)
if m1 != m3 {
t.Error("expect same matcher for same geoip, but not")
}
if m1 == m2 {
t.Error("expect different matcher for different geoip, but actually same")
}
}
func TestGeoIPMatcher(t *testing.T) { func TestGeoIPMatcher(t *testing.T) {
cidrList := []*router.CIDR{ cidrList := []*router.CIDR{
{Ip: []byte{0, 0, 0, 0}, Prefix: 8}, {Ip: []byte{0, 0, 0, 0}, Prefix: 8},
@@ -80,8 +53,10 @@ func TestGeoIPMatcher(t *testing.T) {
{Ip: []byte{91, 108, 4, 0}, Prefix: 16}, {Ip: []byte{91, 108, 4, 0}, Prefix: 16},
} }
matcher := &router.GeoIPMatcher{} matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{
common.Must(matcher.Init(cidrList)) Cidr: cidrList,
})
common.Must(err)
testCases := []struct { testCases := []struct {
Input string Input string
@@ -140,8 +115,10 @@ func TestGeoIPMatcherRegression(t *testing.T) {
{Ip: []byte{98, 108, 20, 0}, Prefix: 23}, {Ip: []byte{98, 108, 20, 0}, Prefix: 23},
} }
matcher := &router.GeoIPMatcher{} matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{
common.Must(matcher.Init(cidrList)) Cidr: cidrList,
})
common.Must(err)
testCases := []struct { testCases := []struct {
Input string Input string
@@ -171,9 +148,11 @@ func TestGeoIPReverseMatcher(t *testing.T) {
{Ip: []byte{8, 8, 8, 8}, Prefix: 32}, {Ip: []byte{8, 8, 8, 8}, Prefix: 32},
{Ip: []byte{91, 108, 4, 0}, Prefix: 16}, {Ip: []byte{91, 108, 4, 0}, Prefix: 16},
} }
matcher := &router.GeoIPMatcher{} matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{
matcher.SetReverseMatch(true) // Reverse match Cidr: cidrList,
common.Must(matcher.Init(cidrList)) })
common.Must(err)
matcher.SetReverse(true) // Reverse match
testCases := []struct { testCases := []struct {
Input string Input string
@@ -206,8 +185,10 @@ func TestGeoIPMatcher4CN(t *testing.T) {
ips, err := loadGeoIP("CN") ips, err := loadGeoIP("CN")
common.Must(err) common.Must(err)
matcher := &router.GeoIPMatcher{} matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{
common.Must(matcher.Init(ips)) Cidr: ips,
})
common.Must(err)
if matcher.Match([]byte{8, 8, 8, 8}) { if matcher.Match([]byte{8, 8, 8, 8}) {
t.Error("expect CN geoip doesn't contain 8.8.8.8, but actually does") t.Error("expect CN geoip doesn't contain 8.8.8.8, but actually does")
@@ -218,8 +199,10 @@ func TestGeoIPMatcher6US(t *testing.T) {
ips, err := loadGeoIP("US") ips, err := loadGeoIP("US")
common.Must(err) common.Must(err)
matcher := &router.GeoIPMatcher{} matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{
common.Must(matcher.Init(ips)) Cidr: ips,
})
common.Must(err)
if !matcher.Match(net.ParseAddress("2001:4860:4860::8888").IP()) { if !matcher.Match(net.ParseAddress("2001:4860:4860::8888").IP()) {
t.Error("expect US geoip contain 2001:4860:4860::8888, but actually not") t.Error("expect US geoip contain 2001:4860:4860::8888, but actually not")
@@ -254,8 +237,10 @@ func BenchmarkGeoIPMatcher4CN(b *testing.B) {
ips, err := loadGeoIP("CN") ips, err := loadGeoIP("CN")
common.Must(err) common.Must(err)
matcher := &router.GeoIPMatcher{} matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{
common.Must(matcher.Init(ips)) Cidr: ips,
})
common.Must(err)
b.ResetTimer() b.ResetTimer()
@@ -268,8 +253,10 @@ func BenchmarkGeoIPMatcher6US(b *testing.B) {
ips, err := loadGeoIP("US") ips, err := loadGeoIP("US")
common.Must(err) common.Must(err)
matcher := &router.GeoIPMatcher{} matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{
common.Must(matcher.Init(ips)) Cidr: ips,
})
common.Must(err)
b.ResetTimer() b.ResetTimer()

View File

@@ -447,7 +447,7 @@ func BenchmarkMultiGeoIPMatcher(b *testing.B) {
}) })
} }
matcher, err := NewMultiGeoIPMatcher(geoips, "target") matcher, err := NewIPMatcher(geoips, MatcherAsType_Target)
common.Must(err) common.Must(err)
ctx := withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)}) ctx := withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)})

View File

@@ -46,7 +46,7 @@ func (rr *RoutingRule) BuildCondition() (Condition, error) {
} }
if rr.VlessRouteList != nil { if rr.VlessRouteList != nil {
conds.Add(NewPortMatcher(rr.VlessRouteList, "vlessRoute")) conds.Add(NewPortMatcher(rr.VlessRouteList, MatcherAsType_VlessRoute))
} }
if len(rr.InboundTag) > 0 { if len(rr.InboundTag) > 0 {
@@ -54,15 +54,15 @@ func (rr *RoutingRule) BuildCondition() (Condition, error) {
} }
if rr.PortList != nil { if rr.PortList != nil {
conds.Add(NewPortMatcher(rr.PortList, "target")) conds.Add(NewPortMatcher(rr.PortList, MatcherAsType_Target))
} }
if rr.SourcePortList != nil { if rr.SourcePortList != nil {
conds.Add(NewPortMatcher(rr.SourcePortList, "source")) conds.Add(NewPortMatcher(rr.SourcePortList, MatcherAsType_Source))
} }
if rr.LocalPortList != nil { if rr.LocalPortList != nil {
conds.Add(NewPortMatcher(rr.LocalPortList, "local")) conds.Add(NewPortMatcher(rr.LocalPortList, MatcherAsType_Local))
} }
if len(rr.Networks) > 0 { if len(rr.Networks) > 0 {
@@ -70,7 +70,7 @@ func (rr *RoutingRule) BuildCondition() (Condition, error) {
} }
if len(rr.Geoip) > 0 { if len(rr.Geoip) > 0 {
cond, err := NewMultiGeoIPMatcher(rr.Geoip, "target") cond, err := NewIPMatcher(rr.Geoip, MatcherAsType_Target)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -78,7 +78,7 @@ func (rr *RoutingRule) BuildCondition() (Condition, error) {
} }
if len(rr.SourceGeoip) > 0 { if len(rr.SourceGeoip) > 0 {
cond, err := NewMultiGeoIPMatcher(rr.SourceGeoip, "source") cond, err := NewIPMatcher(rr.SourceGeoip, MatcherAsType_Source)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -86,7 +86,7 @@ func (rr *RoutingRule) BuildCondition() (Condition, error) {
} }
if len(rr.LocalGeoip) > 0 { if len(rr.LocalGeoip) > 0 {
cond, err := NewMultiGeoIPMatcher(rr.LocalGeoip, "local") cond, err := NewIPMatcher(rr.LocalGeoip, MatcherAsType_Local)
if err != nil { if err != nil {
return nil, err return nil, err
} }