@ -1,81 +1,49 @@
package router
package router
import (
import (
" encoding/binary "
" net/netip "
"s ort "
"s trconv "
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/net"
"go4.org/netipx"
)
)
type ipv6 struct {
a uint64
b uint64
}
type GeoIPMatcher struct {
type GeoIPMatcher struct {
countryCode string
countryCode string
reverseMatch bool
reverseMatch bool
ip4 [ ] uint32
ip4 * netipx . IPSet
prefix4 [ ] uint8
ip6 * netipx . IPSet
ip6 [ ] ipv6
prefix6 [ ] uint8
}
func normalize4 ( ip uint32 , prefix uint8 ) uint32 {
return ( ip >> ( 32 - prefix ) ) << ( 32 - prefix )
}
func normalize6 ( ip ipv6 , prefix uint8 ) ipv6 {
if prefix <= 64 {
ip . a = ( ip . a >> ( 64 - prefix ) ) << ( 64 - prefix )
ip . b = 0
} else {
ip . b = ( ip . b >> ( 128 - prefix ) ) << ( 128 - prefix )
}
return ip
}
}
func ( m * GeoIPMatcher ) Init ( cidrs [ ] * CIDR ) error {
func ( m * GeoIPMatcher ) Init ( cidrs [ ] * CIDR ) error {
ip4Count := 0
var builder4 , builder6 netipx . IPSetBuilder
ip6Count := 0
for _ , cidr := range cidrs {
for _ , cidr := range cidrs {
ip := cidr . Ip
ip := net . IP ( cidr . GetIp ( ) )
ipPrefixString := ip . String ( ) + "/" + strconv . Itoa ( int ( cidr . GetPrefix ( ) ) )
ipPrefix , err := netip . ParsePrefix ( ipPrefixString )
if err != nil {
return err
}
switch len ( ip ) {
switch len ( ip ) {
case 4 :
case net . IPv4len :
ip4Count ++
builder4 . AddPrefix ( ipPrefix )
case 16 :
case net . IPv6len :
ip6Count ++
builder6 . AddPrefix ( ipPrefix )
default :
return newError ( "unexpect ip length: " , len ( ip ) )
}
}
}
}
cidrList := CIDRList ( cidrs )
if ip4 , err := builder4 . IPSet ( ) ; err != nil {
sort . Sort ( & cidrList )
return err
} else {
m . ip4 = make ( [ ] uint32 , 0 , ip4Count )
m . ip4 = ip4
m . prefix4 = make ( [ ] uint8 , 0 , ip4Count )
}
m . ip6 = make ( [ ] ipv6 , 0 , ip6Count )
m . prefix6 = make ( [ ] uint8 , 0 , ip6Count )
for _ , cidr := range cidrList {
ip := cidr . Ip
prefix := uint8 ( cidr . Prefix )
switch len ( ip ) {
case 4 :
m . ip4 = append ( m . ip4 , normalize4 ( binary . BigEndian . Uint32 ( ip ) , prefix ) )
m . prefix4 = append ( m . prefix4 , prefix )
case 16 :
ip6 := ipv6 {
a : binary . BigEndian . Uint64 ( ip [ 0 : 8 ] ) ,
b : binary . BigEndian . Uint64 ( ip [ 8 : 16 ] ) ,
}
ip6 = normalize6 ( ip6 , prefix )
m . ip6 = append ( m . ip6 , ip6 )
if ip6 , err := builder6 . IPSet ( ) ; err != nil {
m . prefix6 = append ( m . prefix6 , prefix )
return err
}
} else {
m . ip6 = ip6
}
}
return nil
return nil
@ -85,91 +53,37 @@ func (m *GeoIPMatcher) SetReverseMatch(isReverseMatch bool) {
m . reverseMatch = isReverseMatch
m . reverseMatch = isReverseMatch
}
}
func ( m * GeoIPMatcher ) match4 ( ip uint32 ) bool {
func ( m * GeoIPMatcher ) match4 ( ip net . IP ) bool {
if len ( m . ip4 ) == 0 {
nip , ok := netipx . FromStdIP ( ip )
return false
if ! ok {
}
if ip < m . ip4 [ 0 ] {
return false
return false
}
}
size := uint32 ( len ( m . ip4 ) )
return m . ip4 . Contains ( nip )
l := uint32 ( 0 )
r := size
for l < r {
x := ( ( l + r ) >> 1 )
if ip < m . ip4 [ x ] {
r = x
continue
}
nip := normalize4 ( ip , m . prefix4 [ x ] )
if nip == m . ip4 [ x ] {
return true
}
l = x + 1
}
return l > 0 && normalize4 ( ip , m . prefix4 [ l - 1 ] ) == m . ip4 [ l - 1 ]
}
}
func less6 ( a ipv6 , b ipv6 ) bool {
func ( m * GeoIPMatcher ) match6 ( ip net . IP ) bool {
return a . a < b . a || ( a . a == b . a && a . b < b . b )
nip , ok := netipx . FromStdIP ( ip )
}
if ! ok {
func ( m * GeoIPMatcher ) match6 ( ip ipv6 ) bool {
if len ( m . ip6 ) == 0 {
return false
}
if less6 ( ip , m . ip6 [ 0 ] ) {
return false
return false
}
}
size := uint32 ( len ( m . ip6 ) )
return m . ip6 . Contains ( nip )
l := uint32 ( 0 )
r := size
for l < r {
x := ( l + r ) / 2
if less6 ( ip , m . ip6 [ x ] ) {
r = x
continue
}
if normalize6 ( ip , m . prefix6 [ x ] ) == m . ip6 [ x ] {
return true
}
l = x + 1
}
return l > 0 && normalize6 ( ip , m . prefix6 [ l - 1 ] ) == m . ip6 [ l - 1 ]
}
}
// Match returns true if the given ip is included by the GeoIP.
// Match returns true if the given ip is included by the GeoIP.
func ( m * GeoIPMatcher ) Match ( ip net . IP ) bool {
func ( m * GeoIPMatcher ) Match ( ip net . IP ) bool {
isMatched := false
switch len ( ip ) {
switch len ( ip ) {
case 4 :
case net . IPv4len :
if m . reverseMatch {
isMatched = m . match4 ( ip )
return ! m . match4 ( binary . BigEndian . Uint32 ( ip ) )
case net . IPv6len :
}
isMatched = m . match6 ( ip )
return m . match4 ( binary . BigEndian . Uint32 ( ip ) )
}
case 16 :
if m . reverseMatch {
if m . reverseMatch {
return ! isMatched
return ! m . match6 ( ipv6 {
a : binary . BigEndian . Uint64 ( ip [ 0 : 8 ] ) ,
b : binary . BigEndian . Uint64 ( ip [ 8 : 16 ] ) ,
} )
}
return m . match6 ( ipv6 {
a : binary . BigEndian . Uint64 ( ip [ 0 : 8 ] ) ,
b : binary . BigEndian . Uint64 ( ip [ 8 : 16 ] ) ,
} )
default :
return false
}
}
return isMatched
}
}
// GeoIPMatcherContainer is a container for GeoIPMatchers. It keeps unique copies of GeoIPMatcher by country code.
// GeoIPMatcherContainer is a container for GeoIPMatchers. It keeps unique copies of GeoIPMatcher by country code.