mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-27 09:23:01 +00:00
58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package geoip
|
|
|
|
import (
|
|
"github.com/xtls/xray-core/common/net"
|
|
"github.com/xtls/xray-core/features/routing"
|
|
)
|
|
|
|
type MultiGeoIPMatcher struct {
|
|
matchers []*GeoIPMatcher
|
|
onSource bool
|
|
}
|
|
|
|
func NewMultiGeoIPMatcher(geoips []*GeoIP, onSource bool) (*MultiGeoIPMatcher, error) {
|
|
var matchers []*GeoIPMatcher
|
|
for _, geoip := range geoips {
|
|
matcher, err := GlobalGeoIPContainer.Add(geoip)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
matchers = append(matchers, matcher)
|
|
}
|
|
|
|
matcher := &MultiGeoIPMatcher{
|
|
matchers: matchers,
|
|
onSource: onSource,
|
|
}
|
|
|
|
return matcher, nil
|
|
}
|
|
|
|
// Apply implements Condition.
|
|
func (m *MultiGeoIPMatcher) Apply(ctx routing.Context) bool {
|
|
var ips []net.IP
|
|
if m.onSource {
|
|
ips = ctx.GetSourceIPs()
|
|
} else {
|
|
ips = ctx.GetTargetIPs()
|
|
}
|
|
for _, ip := range ips {
|
|
for _, matcher := range m.matchers {
|
|
if matcher.Match(ip) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// MatchIP match given ip.
|
|
func (m *MultiGeoIPMatcher) MatchIP(ip net.IP) bool {
|
|
for _, matcher := range m.matchers {
|
|
if matcher.Match(ip) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|