1
0
Эх сурвалжийг харах

auto update script executed

Toby Chui 1 жил өмнө
parent
commit
14da8a51da
2 өөрчлөгдсөн 41 нэмэгдсэн , 11 устгасан
  1. 4 10
      mod/geodb/geoloader.go
  2. 37 1
      mod/geodb/trie.go

+ 4 - 10
mod/geodb/geoloader.go

@@ -21,16 +21,10 @@ func (s *Store) search(ip string) string {
 		return ccc.(string)
 	}
 
-	for _, entry := range s.geodb {
-		startIp := entry[0]
-		endIp := entry[1]
-		cc := entry[2]
-
-		if isIPInRange(ip, startIp, endIp) {
-			//Store results in cache
-			s.geoipCache.Store(ip, cc)
-			return cc
-		}
+	//Search in geotrie tree
+	cc := s.geotrie.search(ip)
+	if cc != "" {
+		s.geoipCache.Store(ip, cc)
 	}
 
 	//Not found

+ 37 - 1
mod/geodb/trie.go

@@ -80,8 +80,39 @@ func (t *trie) insert(ipAddr string, cc string) {
 	current.ends = true
 }
 
+func isReservedIP(ip string) bool {
+	parsedIP := net.ParseIP(ip)
+	if parsedIP == nil {
+		return false
+	}
+	// Check if the IP address is a loopback address
+	if parsedIP.IsLoopback() {
+		return true
+	}
+	// Check if the IP address is in the link-local address range
+	if parsedIP.IsLinkLocalUnicast() || parsedIP.IsLinkLocalMulticast() {
+		return true
+	}
+	// Check if the IP address is in the private address ranges
+	privateRanges := []*net.IPNet{
+		{IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(8, 32)},
+		{IP: net.ParseIP("172.16.0.0"), Mask: net.CIDRMask(12, 32)},
+		{IP: net.ParseIP("192.168.0.0"), Mask: net.CIDRMask(16, 32)},
+	}
+	for _, r := range privateRanges {
+		if r.Contains(parsedIP) {
+			return true
+		}
+	}
+	// If the IP address is not a reserved address, return false
+	return false
+}
+
 // Initializing the search for word in node
 func (t *trie) search(ipAddr string) string {
+	if isReservedIP(ipAddr) {
+		return ""
+	}
 	word := ipToBitString(ipAddr)
 	current := t.root
 	for _, wr := range word {
@@ -91,5 +122,10 @@ func (t *trie) search(ipAddr string) string {
 		}
 		current = current.childrens[index]
 	}
-	return current.cc
+	if current.ends {
+		return current.cc
+	}
+
+	//Not found
+	return ""
 }