- Fixed implementation in geoip resolver trie tree
This commit is contained in:
Toby Chui 2024-07-31 21:57:59 +08:00
parent 6ac16caf37
commit 54475e4b99
4 changed files with 12 additions and 50 deletions

View File

@ -19,7 +19,6 @@ type Store struct {
geodbIpv6 [][]string //Parsed geodb list for ipv6 geodbIpv6 [][]string //Parsed geodb list for ipv6
geotrie *trie geotrie *trie
geotrieIpv6 *trie geotrieIpv6 *trie
//geoipCache sync.Map
sysdb *database.Database sysdb *database.Database
option *StoreOptions option *StoreOptions
} }

View File

@ -43,7 +43,7 @@ func TestResolveCountryCodeFromIP(t *testing.T) {
// Create a new store // Create a new store
store, err := geodb.NewGeoDb(nil, &geodb.StoreOptions{ store, err := geodb.NewGeoDb(nil, &geodb.StoreOptions{
false, false,
false, true,
}) })
if err != nil { if err != nil {
t.Errorf("error creating store: %v", err) t.Errorf("error creating store: %v", err)
@ -56,6 +56,7 @@ func TestResolveCountryCodeFromIP(t *testing.T) {
{"176.113.115.113", "RU"}, {"176.113.115.113", "RU"},
{"65.21.233.213", "FI"}, {"65.21.233.213", "FI"},
{"94.23.207.193", "FR"}, {"94.23.207.193", "FR"},
{"77.131.21.232", "FR"},
} }
for _, testcase := range knownIpCountryMap { for _, testcase := range knownIpCountryMap {

View File

@ -16,13 +16,6 @@ func (s *Store) search(ip string) string {
ip = strings.Split(ip, ",")[0] ip = strings.Split(ip, ",")[0]
ip = strings.TrimSpace(ip) ip = strings.TrimSpace(ip)
} }
//See if there are cached country code for this ip
/*
ccc, ok := s.geoipCache.Load(ip)
if ok {
return ccc.(string)
}
*/
//Search in geotrie tree //Search in geotrie tree
cc := "" cc := ""

View File

@ -1,7 +1,6 @@
package geodb package geodb
import ( import (
"math"
"net" "net"
) )
@ -41,14 +40,10 @@ func (t *trie) insert(ipAddr string, cc string) {
ipBytes := ipToBytes(ipAddr) ipBytes := ipToBytes(ipAddr)
current := t.root current := t.root
for _, b := range ipBytes { for _, b := range ipBytes {
//For each byte in the ip address //For each byte in the ip address (4 / 16 bytes)
//each byte is 8 bit //each byte is 8 bit
for j := 0; j < 8; j++ { for j := 7; j >= 0; j-- {
bitwise := (b&uint8(math.Pow(float64(2), float64(j))) > 0) bit := int(b >> j & 1)
bit := 0b0000
if bitwise {
bit = 0b0001
}
if current.childrens[bit] == nil { if current.childrens[bit] == nil {
current.childrens[bit] = &trie_Node{ current.childrens[bit] = &trie_Node{
childrens: [2]*trie_Node{}, childrens: [2]*trie_Node{},
@ -58,21 +53,9 @@ func (t *trie) insert(ipAddr string, cc string) {
current = current.childrens[bit] current = current.childrens[bit]
} }
} }
/*
for i := 63; i >= 0; i-- {
bit := (ipInt64 >> uint(i)) & 1
if current.childrens[bit] == nil {
current.childrens[bit] = &trie_Node{
childrens: [2]*trie_Node{},
cc: cc,
}
}
current = current.childrens[bit]
}
*/
} }
// isReservedIP check if the given ip address is NOT a public ip address
func isReservedIP(ip string) bool { func isReservedIP(ip string) bool {
parsedIP := net.ParseIP(ip) parsedIP := net.ParseIP(ip)
if parsedIP == nil { if parsedIP == nil {
@ -86,12 +69,10 @@ func isReservedIP(ip string) bool {
if parsedIP.IsLinkLocalUnicast() || parsedIP.IsLinkLocalMulticast() { if parsedIP.IsLinkLocalUnicast() || parsedIP.IsLinkLocalMulticast() {
return true return true
} }
//Check if the IP is in the reserved private range
if parsedIP.IsPrivate() { if parsedIP.IsPrivate() {
return true return true
} }
// If the IP address is not a reserved address, return false
return false return false
} }
@ -106,27 +87,15 @@ func (t *trie) search(ipAddr string) string {
for _, b := range ipBytes { for _, b := range ipBytes {
//For each byte in the ip address //For each byte in the ip address
//each byte is 8 bit //each byte is 8 bit
for j := 0; j < 8; j++ { for j := 7; j >= 0; j-- {
bitwise := (b&uint8(math.Pow(float64(2), float64(j))) > 0) bit := int(b >> j & 1)
bit := 0b0000
if bitwise {
bit = 0b0001
}
if current.childrens[bit] == nil { if current.childrens[bit] == nil {
return current.cc return current.cc
} }
current = current.childrens[bit] current = current.childrens[bit]
} }
} }
/*
for i := 63; i >= 0; i-- {
bit := (ipInt64 >> uint(i)) & 1
if current.childrens[bit] == nil {
return current.cc
}
current = current.childrens[bit]
}
*/
if len(current.childrens) == 0 { if len(current.childrens) == 0 {
return current.cc return current.cc
} }