2018-11-01 04:10:41 -04:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2021-09-10 04:31:25 -04:00
|
|
|
"inet.af/netaddr"
|
2018-11-01 16:30:36 -04:00
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/app/router/routercommon"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
2018-11-01 04:10:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type GeoIPMatcher struct {
|
2021-04-06 00:29:12 -04:00
|
|
|
countryCode string
|
2021-09-10 04:31:25 -04:00
|
|
|
reverseMatch bool
|
|
|
|
ip4 *netaddr.IPSet
|
|
|
|
ip6 *netaddr.IPSet
|
2018-11-01 04:10:41 -04:00
|
|
|
}
|
|
|
|
|
2021-09-07 04:13:58 -04:00
|
|
|
func (m *GeoIPMatcher) Init(cidrs []*routercommon.CIDR) error {
|
2021-09-10 04:31:25 -04:00
|
|
|
var builder4, builder6 netaddr.IPSetBuilder
|
2018-11-01 04:10:41 -04:00
|
|
|
for _, cidr := range cidrs {
|
2021-09-10 22:07:54 -04:00
|
|
|
netaddrIP, ok := netaddr.FromStdIP(net.IP(cidr.GetIp()))
|
|
|
|
if !ok {
|
|
|
|
return newError("invalid IP address ", cidr)
|
2021-09-10 04:31:25 -04:00
|
|
|
}
|
2021-09-10 22:07:54 -04:00
|
|
|
ipPrefix := netaddr.IPPrefixFrom(netaddrIP, uint8(cidr.GetPrefix()))
|
|
|
|
switch {
|
|
|
|
case netaddrIP.Is4():
|
2021-09-10 04:31:25 -04:00
|
|
|
builder4.AddPrefix(ipPrefix)
|
2021-09-10 22:07:54 -04:00
|
|
|
case netaddrIP.Is6():
|
2021-09-10 04:31:25 -04:00
|
|
|
builder6.AddPrefix(ipPrefix)
|
2018-11-01 04:10:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 04:31:25 -04:00
|
|
|
var err error
|
|
|
|
m.ip4, err = builder4.IPSet()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.ip6, err = builder6.IPSet()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-11-01 04:10:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 00:29:12 -04:00
|
|
|
func (m *GeoIPMatcher) SetReverseMatch(isReverseMatch bool) {
|
2021-09-10 04:31:25 -04:00
|
|
|
m.reverseMatch = isReverseMatch
|
2021-04-06 00:29:12 -04:00
|
|
|
}
|
|
|
|
|
2021-09-10 04:31:25 -04:00
|
|
|
func (m *GeoIPMatcher) match4(ip net.IP) bool {
|
|
|
|
nip, ok := netaddr.FromStdIP(ip)
|
|
|
|
if !ok {
|
2018-11-01 04:10:41 -04:00
|
|
|
return false
|
|
|
|
}
|
2021-09-10 04:31:25 -04:00
|
|
|
return m.ip4.Contains(nip)
|
2018-11-01 04:10:41 -04:00
|
|
|
}
|
|
|
|
|
2021-09-10 04:31:25 -04:00
|
|
|
func (m *GeoIPMatcher) match6(ip net.IP) bool {
|
|
|
|
nip, ok := netaddr.FromStdIP(ip)
|
|
|
|
if !ok {
|
2018-11-01 04:10:41 -04:00
|
|
|
return false
|
|
|
|
}
|
2021-09-10 04:31:25 -04:00
|
|
|
return m.ip6.Contains(nip)
|
2018-11-01 04:10:41 -04:00
|
|
|
}
|
|
|
|
|
2018-11-01 05:39:03 -04:00
|
|
|
// Match returns true if the given ip is included by the GeoIP.
|
2018-11-01 04:10:41 -04:00
|
|
|
func (m *GeoIPMatcher) Match(ip net.IP) bool {
|
2021-09-10 04:31:25 -04:00
|
|
|
isMatched := false
|
2018-11-01 04:10:41 -04:00
|
|
|
switch len(ip) {
|
2021-09-10 04:31:25 -04:00
|
|
|
case net.IPv4len:
|
|
|
|
isMatched = m.match4(ip)
|
|
|
|
case net.IPv6len:
|
|
|
|
isMatched = m.match6(ip)
|
|
|
|
}
|
|
|
|
if m.reverseMatch {
|
|
|
|
return !isMatched
|
2018-11-01 04:10:41 -04:00
|
|
|
}
|
2021-09-10 04:31:25 -04:00
|
|
|
return isMatched
|
2018-11-01 04:10:41 -04:00
|
|
|
}
|
|
|
|
|
2018-11-01 05:39:03 -04:00
|
|
|
// GeoIPMatcherContainer is a container for GeoIPMatchers. It keeps unique copies of GeoIPMatcher by country code.
|
|
|
|
type GeoIPMatcherContainer struct {
|
2018-11-01 04:10:41 -04:00
|
|
|
matchers []*GeoIPMatcher
|
|
|
|
}
|
|
|
|
|
2018-11-01 05:39:03 -04:00
|
|
|
// 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.
|
2021-09-07 04:13:58 -04:00
|
|
|
func (c *GeoIPMatcherContainer) Add(geoip *routercommon.GeoIP) (*GeoIPMatcher, error) {
|
2021-09-10 04:31:25 -04:00
|
|
|
if geoip.CountryCode != "" {
|
2018-11-01 04:10:41 -04:00
|
|
|
for _, m := range c.matchers {
|
2021-09-10 04:31:25 -04:00
|
|
|
if m.countryCode == geoip.CountryCode && m.reverseMatch == geoip.InverseMatch {
|
2018-11-01 04:10:41 -04:00
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &GeoIPMatcher{
|
2021-04-06 00:29:12 -04:00
|
|
|
countryCode: geoip.CountryCode,
|
2021-09-10 04:31:25 -04:00
|
|
|
reverseMatch: geoip.InverseMatch,
|
2018-11-01 04:10:41 -04:00
|
|
|
}
|
|
|
|
if err := m.Init(geoip.Cidr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-10 04:31:25 -04:00
|
|
|
if geoip.CountryCode != "" {
|
2018-11-01 04:10:41 -04:00
|
|
|
c.matchers = append(c.matchers, m)
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2021-05-19 17:28:52 -04:00
|
|
|
var globalGeoIPContainer GeoIPMatcherContainer
|