2020-11-25 11:01:53 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2023-08-26 15:11:37 +00:00
|
|
|
"net/netip"
|
|
|
|
"strconv"
|
2020-11-25 11:01:53 +00:00
|
|
|
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
2023-08-26 15:11:37 +00:00
|
|
|
"go4.org/netipx"
|
2020-11-25 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type GeoIPMatcher struct {
|
2021-04-08 05:19:25 +00:00
|
|
|
countryCode string
|
|
|
|
reverseMatch bool
|
2023-08-26 15:11:37 +00:00
|
|
|
ip4 *netipx.IPSet
|
|
|
|
ip6 *netipx.IPSet
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *GeoIPMatcher) Init(cidrs []*CIDR) error {
|
2023-08-26 15:11:37 +00:00
|
|
|
var builder4, builder6 netipx.IPSetBuilder
|
2020-11-25 11:01:53 +00:00
|
|
|
|
|
|
|
for _, cidr := range cidrs {
|
2023-08-26 15:11:37 +00:00
|
|
|
ip := net.IP(cidr.GetIp())
|
|
|
|
ipPrefixString := ip.String() + "/" + strconv.Itoa(int(cidr.GetPrefix()))
|
|
|
|
ipPrefix, err := netip.ParsePrefix(ipPrefixString)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-25 11:01:53 +00:00
|
|
|
switch len(ip) {
|
2023-08-26 15:11:37 +00:00
|
|
|
case net.IPv4len:
|
|
|
|
builder4.AddPrefix(ipPrefix)
|
|
|
|
case net.IPv6len:
|
|
|
|
builder6.AddPrefix(ipPrefix)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-26 15:11:37 +00:00
|
|
|
if ip4, err := builder4.IPSet(); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
m.ip4 = ip4
|
|
|
|
}
|
2020-11-25 11:01:53 +00:00
|
|
|
|
2023-08-26 15:11:37 +00:00
|
|
|
if ip6, err := builder6.IPSet(); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
m.ip6 = ip6
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-08 05:19:25 +00:00
|
|
|
func (m *GeoIPMatcher) SetReverseMatch(isReverseMatch bool) {
|
|
|
|
m.reverseMatch = isReverseMatch
|
|
|
|
}
|
|
|
|
|
2023-08-26 15:11:37 +00:00
|
|
|
func (m *GeoIPMatcher) match4(ip net.IP) bool {
|
|
|
|
nip, ok := netipx.FromStdIP(ip)
|
|
|
|
if !ok {
|
2020-11-25 11:01:53 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-08-26 15:11:37 +00:00
|
|
|
return m.ip4.Contains(nip)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
2023-08-26 15:11:37 +00:00
|
|
|
func (m *GeoIPMatcher) match6(ip net.IP) bool {
|
|
|
|
nip, ok := netipx.FromStdIP(ip)
|
|
|
|
if !ok {
|
2020-11-25 11:01:53 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-08-26 15:11:37 +00:00
|
|
|
return m.ip6.Contains(nip)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Match returns true if the given ip is included by the GeoIP.
|
|
|
|
func (m *GeoIPMatcher) Match(ip net.IP) bool {
|
2023-08-26 15:11:37 +00:00
|
|
|
isMatched := false
|
2020-11-25 11:01:53 +00:00
|
|
|
switch len(ip) {
|
2023-08-26 15:11:37 +00:00
|
|
|
case net.IPv4len:
|
|
|
|
isMatched = m.match4(ip)
|
|
|
|
case net.IPv6len:
|
|
|
|
isMatched = m.match6(ip)
|
|
|
|
}
|
|
|
|
if m.reverseMatch {
|
|
|
|
return !isMatched
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
2023-08-26 15:11:37 +00:00
|
|
|
return isMatched
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GeoIPMatcherContainer is a container for GeoIPMatchers. It keeps unique copies of GeoIPMatcher by country code.
|
|
|
|
type GeoIPMatcherContainer struct {
|
|
|
|
matchers []*GeoIPMatcher
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds a new GeoIP set into the container.
|
|
|
|
// If the country code of GeoIP is not empty, GeoIPMatcherContainer will try to find an existing one, instead of adding a new one.
|
|
|
|
func (c *GeoIPMatcherContainer) Add(geoip *GeoIP) (*GeoIPMatcher, error) {
|
|
|
|
if len(geoip.CountryCode) > 0 {
|
|
|
|
for _, m := range c.matchers {
|
2021-04-08 05:19:25 +00:00
|
|
|
if m.countryCode == geoip.CountryCode && m.reverseMatch == geoip.ReverseMatch {
|
2020-11-25 11:01:53 +00:00
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &GeoIPMatcher{
|
2021-04-08 05:19:25 +00:00
|
|
|
countryCode: geoip.CountryCode,
|
|
|
|
reverseMatch: geoip.ReverseMatch,
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
if err := m.Init(geoip.Cidr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(geoip.CountryCode) > 0 {
|
|
|
|
c.matchers = append(c.matchers, m)
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2021-10-19 16:57:14 +00:00
|
|
|
var globalGeoIPContainer GeoIPMatcherContainer
|