浏览代码

geoip2: add active-checks to queries

Add checks for the DB to be active before making a query. This was only
enabled on ASN, now enabled for City and Country queries.
Tyler Davis 4 年之前
父节点
当前提交
a510b96fd1
共有 1 个文件被更改,包括 8 次插入0 次删除
  1. 8 0
      targeting/geoip2/geoip2.go

+ 8 - 0
targeting/geoip2/geoip2.go

@@ -188,6 +188,10 @@ func (g *GeoIP2) GetCountry(ip net.IP) (country, continent string, netmask int)
 	g.country.l.RLock()
 	g.country.l.RLock()
 	defer g.country.l.RUnlock()
 	defer g.country.l.RUnlock()
 
 
+	if !g.country.active {
+		return "", "", 0
+	}
+
 	c, err := g.country.db.Country(ip)
 	c, err := g.country.db.Country(ip)
 	if err != nil {
 	if err != nil {
 		log.Printf("Could not lookup country for '%s': %s", ip.String(), err)
 		log.Printf("Could not lookup country for '%s': %s", ip.String(), err)
@@ -215,6 +219,10 @@ func (g *GeoIP2) GetLocation(ip net.IP) (l *geo.Location, err error) {
 	g.city.l.RLock()
 	g.city.l.RLock()
 	defer g.city.l.RUnlock()
 	defer g.city.l.RUnlock()
 
 
+	if !g.city.active {
+		return nil, fmt.Errorf("city db not active")
+	}
+
 	c, err := g.city.db.City(ip)
 	c, err := g.city.db.City(ip)
 	if err != nil {
 	if err != nil {
 		log.Printf("Could not lookup CountryRegion for '%s': %s", ip.String(), err)
 		log.Printf("Could not lookup CountryRegion for '%s': %s", ip.String(), err)