Skip to content

Commit

Permalink
Refactor geoip infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
abh committed Mar 29, 2017
1 parent 9c35b3c commit 5048bc6
Show file tree
Hide file tree
Showing 10 changed files with 339 additions and 224 deletions.
12 changes: 12 additions & 0 deletions geodns.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
"github.com/abh/geodns/monitor"
"github.com/abh/geodns/querylog"
"github.com/abh/geodns/server"
"github.com/abh/geodns/targeting"
"github.com/abh/geodns/targeting/geoip2"
"github.com/abh/geodns/zones"
"github.com/pborman/uuid"
)
Expand Down Expand Up @@ -200,6 +202,16 @@ func main() {
log.Println("StatHat integration has been removed in favor of more generic metrics")
}

if len(Config.GeoIPDirectory()) > 0 {
geoProvider, err := geoip2.New(Config.GeoIPDirectory())
if err != nil {
log.Printf("Configuring geo provider: %s", err)
}
if geoProvider != nil {
targeting.Setup(geoProvider)
}
}

mon := monitor.NewMonitor(serverInfo)
go mon.Run()

Expand Down
43 changes: 43 additions & 0 deletions targeting/geo/geo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package geo

import (
"math"
"net"

"github.com/golang/geo/s2"
)

type Provider interface {
HasCountry() (bool, error)
GetCountry(ip net.IP) (country, continent string, netmask int)
HasASN() (bool, error)
GetASN(net.IP) (asn string, netmask int, err error)
HasLocation() (bool, error)
GetLocation(ip net.IP) (location *Location, err error)
}

const MAX_DISTANCE = 360

type Location struct {
Country string
Continent string
RegionGroup string
Region string
Latitude float64
Longitude float64
Netmask int
}

func (l *Location) MaxDistance() float64 {
return MAX_DISTANCE
}

func (l *Location) Distance(to *Location) float64 {
if to == nil {
return MAX_DISTANCE
}
ll1 := s2.LatLngFromDegrees(l.Latitude, l.Longitude)
ll2 := s2.LatLngFromDegrees(to.Latitude, to.Longitude)
angle := ll1.Distance(ll2)
return math.Abs(angle.Degrees())
}
168 changes: 0 additions & 168 deletions targeting/geoip.go

This file was deleted.

Loading

0 comments on commit 5048bc6

Please sign in to comment.