Skip to content

Commit 58221eb

Browse files
committed
optimize multi-geoip matcher
1 parent 2cc9292 commit 58221eb

File tree

2 files changed

+68
-14
lines changed

2 files changed

+68
-14
lines changed

app/router/condition.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func targetFromContent(ctx context.Context) net.Destination {
113113

114114
type MultiGeoIPMatcher struct {
115115
matchers []*GeoIPMatcher
116-
onSource bool
116+
destFunc func(context.Context) net.Destination
117117
}
118118

119119
func NewMultiGeoIPMatcher(geoips []*GeoIP, onSource bool) (*MultiGeoIPMatcher, error) {
@@ -126,30 +126,31 @@ func NewMultiGeoIPMatcher(geoips []*GeoIP, onSource bool) (*MultiGeoIPMatcher, e
126126
matchers = append(matchers, matcher)
127127
}
128128

129+
var destFunc func(context.Context) net.Destination
130+
if onSource {
131+
destFunc = sourceFromContext
132+
} else {
133+
destFunc = targetFromContent
134+
}
135+
129136
return &MultiGeoIPMatcher{
130137
matchers: matchers,
131-
onSource: onSource,
138+
destFunc: destFunc,
132139
}, nil
133140
}
134141

135142
func (m *MultiGeoIPMatcher) Apply(ctx context.Context) bool {
136143
ips := make([]net.IP, 0, 4)
137-
if resolver, ok := ResolvedIPsFromContext(ctx); ok {
138-
resolvedIPs := resolver.Resolve()
139-
for _, rip := range resolvedIPs {
140-
ips = append(ips, rip.IP())
141-
}
142-
}
143144

144-
var dest net.Destination
145-
if m.onSource {
146-
dest = sourceFromContext(ctx)
147-
} else {
148-
dest = targetFromContent(ctx)
149-
}
145+
dest := m.destFunc(ctx)
150146

151147
if dest.IsValid() && (dest.Address.Family().IsIPv4() || dest.Address.Family().IsIPv6()) {
152148
ips = append(ips, dest.Address.IP())
149+
} else if resolver, ok := ResolvedIPsFromContext(ctx); ok {
150+
resolvedIPs := resolver.Resolve()
151+
for _, rip := range resolvedIPs {
152+
ips = append(ips, rip.IP())
153+
}
153154
}
154155

155156
for _, ip := range ips {

app/router/condition_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,56 @@ func TestChinaSites(t *testing.T) {
285285
assert(matcher.ApplyDomain(strconv.Itoa(i)+".not-exists.com"), IsFalse)
286286
}
287287
}
288+
289+
func BenchmarkMultiGeoIPMatcher(b *testing.B) {
290+
common.Must(sysio.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "release", "config", "geoip.dat")))
291+
292+
var geoips []*GeoIP
293+
294+
{
295+
ips, err := loadGeoIP("CN")
296+
common.Must(err)
297+
geoips = append(geoips, &GeoIP{
298+
CountryCode: "CN",
299+
Cidr: ips,
300+
})
301+
}
302+
303+
{
304+
ips, err := loadGeoIP("JP")
305+
common.Must(err)
306+
geoips = append(geoips, &GeoIP{
307+
CountryCode: "JP",
308+
Cidr: ips,
309+
})
310+
}
311+
312+
{
313+
ips, err := loadGeoIP("CA")
314+
common.Must(err)
315+
geoips = append(geoips, &GeoIP{
316+
CountryCode: "CA",
317+
Cidr: ips,
318+
})
319+
}
320+
321+
{
322+
ips, err := loadGeoIP("US")
323+
common.Must(err)
324+
geoips = append(geoips, &GeoIP{
325+
CountryCode: "US",
326+
Cidr: ips,
327+
})
328+
}
329+
330+
matcher, err := NewMultiGeoIPMatcher(geoips, false)
331+
common.Must(err)
332+
333+
ctx := withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)})
334+
335+
b.ResetTimer()
336+
337+
for i := 0; i < b.N; i++ {
338+
_ = matcher.Apply(ctx)
339+
}
340+
}

0 commit comments

Comments
 (0)