Skip to content

Commit

Permalink
GeoIP fixes for when a provider hasn't been configured
Browse files Browse the repository at this point in the history
  • Loading branch information
abh committed Mar 30, 2017
1 parent 5048bc6 commit ecb34ab
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
1 change: 1 addition & 0 deletions dns/geodns.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[geoip]
;; Directory containing the GeoIP .dat database files
;directory=/usr/local/share/GeoIP/
directory=/usr/local/share/GeoIP/

[querylog]
;; directory to save query logs; disabled if not specified
Expand Down
49 changes: 32 additions & 17 deletions targeting/targeting.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,10 @@ func Geo() geo.Provider {
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)

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 {
asn, _, err := g.GetASN(ip)
if err != nil {
Expand Down Expand Up @@ -104,6 +88,37 @@ func (t TargetOptions) GetTargets(ip net.IP, hasClosest bool) ([]string, int, *g
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 {
targets = append(targets, "@")
}
Expand Down
11 changes: 3 additions & 8 deletions targeting/targeting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,15 @@ func TestGetTargets(t *testing.T) {
}
Setup(g)

// GeoIP().SetDirectory("../db")
// GeoIP().SetupGeoIPCity()
// GeoIP().SetupGeoIPCountry()
// GeoIP().SetupGeoIPASN()

tgt, _ := ParseTargets("@ continent country")
targets, _, _ := tgt.GetTargets(ip, false)
expect := []string{"us", "north-america", "@"}
if !reflect.DeepEqual(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
}

Expand Down Expand Up @@ -99,7 +94,7 @@ func TestGetTargets(t *testing.T) {
},
}

if g.HasASN() {
if ok, _ := g.HasASN(); ok {
tests = append(tests,
test{"@ continent regiongroup country region asn ip",
[]string{"[207.171.1.1]", "[207.171.1.0]", "as7012", "us-ca", "us-west", "us", "north-america", "@"},
Expand Down

0 comments on commit ecb34ab

Please sign in to comment.