1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-01-06 01:05:35 -05:00

optimize multi-geoip matcher

This commit is contained in:
Darien Raymond
2018-11-07 23:57:06 +01:00
parent 2cc92920fa
commit 58221ebae1
2 changed files with 68 additions and 14 deletions

View File

@@ -113,7 +113,7 @@ func targetFromContent(ctx context.Context) net.Destination {
type MultiGeoIPMatcher struct {
matchers []*GeoIPMatcher
onSource bool
destFunc func(context.Context) net.Destination
}
func NewMultiGeoIPMatcher(geoips []*GeoIP, onSource bool) (*MultiGeoIPMatcher, error) {
@@ -126,32 +126,33 @@ func NewMultiGeoIPMatcher(geoips []*GeoIP, onSource bool) (*MultiGeoIPMatcher, e
matchers = append(matchers, matcher)
}
var destFunc func(context.Context) net.Destination
if onSource {
destFunc = sourceFromContext
} else {
destFunc = targetFromContent
}
return &MultiGeoIPMatcher{
matchers: matchers,
onSource: onSource,
destFunc: destFunc,
}, nil
}
func (m *MultiGeoIPMatcher) Apply(ctx context.Context) bool {
ips := make([]net.IP, 0, 4)
if resolver, ok := ResolvedIPsFromContext(ctx); ok {
dest := m.destFunc(ctx)
if dest.IsValid() && (dest.Address.Family().IsIPv4() || dest.Address.Family().IsIPv6()) {
ips = append(ips, dest.Address.IP())
} else if resolver, ok := ResolvedIPsFromContext(ctx); ok {
resolvedIPs := resolver.Resolve()
for _, rip := range resolvedIPs {
ips = append(ips, rip.IP())
}
}
var dest net.Destination
if m.onSource {
dest = sourceFromContext(ctx)
} else {
dest = targetFromContent(ctx)
}
if dest.IsValid() && (dest.Address.Family().IsIPv4() || dest.Address.Family().IsIPv6()) {
ips = append(ips, dest.Address.IP())
}
for _, ip := range ips {
for _, matcher := range m.matchers {
if matcher.Match(ip) {