mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-24 01:21:29 +00:00
48 lines
907 B
Go
48 lines
907 B
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
|
||
|
}
|