Browse Source

GeoIP fixes for when a provider hasn't been configured

Ask Bjørn Hansen 8 years ago
parent
commit
ecb34ab423
3 changed files with 36 additions and 25 deletions
  1. 1 0
      dns/geodns.conf.sample
  2. 32 17
      targeting/targeting.go
  3. 3 8
      targeting/targeting_test.go

+ 1 - 0
dns/geodns.conf.sample

@@ -6,6 +6,7 @@
 [geoip]
 [geoip]
 ;; Directory containing the GeoIP .dat database files
 ;; Directory containing the GeoIP .dat database files
 ;directory=/usr/local/share/GeoIP/
 ;directory=/usr/local/share/GeoIP/
+directory=/usr/local/share/GeoIP/
 
 
 [querylog]
 [querylog]
 ;; directory to save query logs; disabled if not specified
 ;; directory to save query logs; disabled if not specified

+ 32 - 17
targeting/targeting.go

@@ -38,26 +38,10 @@ func Geo() geo.Provider {
 	return g
 	return g
 }
 }
 
 
-func (t TargetOptions) GetTargets(ip net.IP, hasClosest bool) ([]string, int, *geo.Location) {
+func (t TargetOptions) getGeoTargets(ip net.IP, hasClosest bool) ([]string, int, *geo.Location) {
 
 
 	targets := make([]string, 0)
 	targets := make([]string, 0)
 
 
-	if t&TargetIP > 0 {
-		ipStr := ip.String()
-		targets = append(targets, "["+ipStr+"]")
-		ip4 := ip.To4()
-		if ip4 != nil {
-			if ip4[3] != 0 {
-				ip4[3] = 0
-				targets = append(targets, "["+ip4.String()+"]")
-			}
-		} else {
-			// v6 address, also target the /48 address
-			ip48 := ip.Mask(cidr48Mask)
-			targets = append(targets, "["+ip48.String()+"]")
-		}
-	}
-
 	if t&TargetASN > 0 {
 	if t&TargetASN > 0 {
 		asn, _, err := g.GetASN(ip)
 		asn, _, err := g.GetASN(ip)
 		if err != nil {
 		if err != nil {
@@ -104,6 +88,37 @@ func (t TargetOptions) GetTargets(ip net.IP, hasClosest bool) ([]string, int, *g
 		targets = append(targets, continent)
 		targets = append(targets, continent)
 	}
 	}
 
 
+	return targets, netmask, location
+}
+
+func (t TargetOptions) GetTargets(ip net.IP, hasClosest bool) ([]string, int, *geo.Location) {
+
+	targets := make([]string, 0)
+	var location *geo.Location
+	var netmask int
+
+	if t&TargetIP > 0 {
+		ipStr := ip.String()
+		targets = append(targets, "["+ipStr+"]")
+		ip4 := ip.To4()
+		if ip4 != nil {
+			if ip4[3] != 0 {
+				ip4[3] = 0
+				targets = append(targets, "["+ip4.String()+"]")
+			}
+		} else {
+			// v6 address, also target the /48 address
+			ip48 := ip.Mask(cidr48Mask)
+			targets = append(targets, "["+ip48.String()+"]")
+		}
+	}
+
+	if g != nil {
+		var geotargets []string
+		geotargets, netmask, location = t.getGeoTargets(ip, hasClosest)
+		targets = append(targets, geotargets...)
+	}
+
 	if t&TargetGlobal > 0 {
 	if t&TargetGlobal > 0 {
 		targets = append(targets, "@")
 		targets = append(targets, "@")
 	}
 	}

+ 3 - 8
targeting/targeting_test.go

@@ -58,11 +58,6 @@ func TestGetTargets(t *testing.T) {
 	}
 	}
 	Setup(g)
 	Setup(g)
 
 
-	// GeoIP().SetDirectory("../db")
-	// GeoIP().SetupGeoIPCity()
-	// GeoIP().SetupGeoIPCountry()
-	// GeoIP().SetupGeoIPASN()
-
 	tgt, _ := ParseTargets("@ continent country")
 	tgt, _ := ParseTargets("@ continent country")
 	targets, _, _ := tgt.GetTargets(ip, false)
 	targets, _, _ := tgt.GetTargets(ip, false)
 	expect := []string{"us", "north-america", "@"}
 	expect := []string{"us", "north-america", "@"}
@@ -70,8 +65,8 @@ func TestGetTargets(t *testing.T) {
 		t.Fatalf("Unexpected parse results of targets, got '%s', expected '%s'", targets, expect)
 		t.Fatalf("Unexpected parse results of targets, got '%s', expected '%s'", targets, expect)
 	}
 	}
 
 
-	if !g.HasLocation() {
-		t.Log("City GeoIP database requred for these tests")
+	if ok, err := g.HasLocation(); !ok {
+		t.Logf("City GeoIP database required for these tests: %s", err)
 		return
 		return
 	}
 	}
 
 
@@ -99,7 +94,7 @@ func TestGetTargets(t *testing.T) {
 		},
 		},
 	}
 	}
 
 
-	if g.HasASN() {
+	if ok, _ := g.HasASN(); ok {
 		tests = append(tests,
 		tests = append(tests,
 			test{"@ continent regiongroup country region asn ip",
 			test{"@ continent regiongroup country region asn ip",
 				[]string{"[207.171.1.1]", "[207.171.1.0]", "as7012", "us-ca", "us-west", "us", "north-america", "@"},
 				[]string{"[207.171.1.1]", "[207.171.1.0]", "as7012", "us-ca", "us-west", "us", "north-america", "@"},