Browse Source

wip: geoip2 fixes, tests

Ask Bjørn Hansen 8 years ago
parent
commit
26ba43ddad
7 changed files with 45 additions and 5 deletions
  1. 1 1
      config.go
  2. 3 0
      geodns.go
  3. 3 0
      health/status.go
  4. 1 1
      health/status_file.go
  5. 7 0
      http_test.go
  6. 29 2
      targeting/geoip2/geoip2.go
  7. 1 1
      zones/reader.go

+ 1 - 1
config.go

@@ -8,7 +8,7 @@ import (
 	"time"
 
 	"gopkg.in/fsnotify.v1"
-	"gopkg.in/gcfg.v1"
+	gcfg "gopkg.in/gcfg.v1"
 )
 
 type AppConfig struct {

+ 3 - 0
geodns.go

@@ -142,6 +142,9 @@ func main() {
 			log.Println("Errors reading zones", err)
 			os.Exit(2)
 		}
+
+		// todo: setup health stuff when configured
+
 		return
 	}
 

+ 3 - 0
health/status.go

@@ -2,6 +2,7 @@ package health
 
 import (
 	"fmt"
+	"log"
 	"strings"
 	"sync"
 )
@@ -69,6 +70,8 @@ func GetStatus(name string) StatusType {
 	status, ok := registry.m[check[0]]
 	registry.mu.RUnlock()
 
+	log.Printf("looking up health for '%s', status register: '%s', found: %t", name, check[0], ok)
+
 	if !ok {
 		return StatusUnknown
 	}

+ 1 - 1
health/status_file.go

@@ -35,7 +35,7 @@ func DirectoryReader(dir string) {
 		if err != nil {
 			log.Printf("loading health data: %s", err)
 		}
-		time.Sleep(3 * time.Second)
+		time.Sleep(1 * time.Second)
 	}
 }
 

+ 7 - 0
http_test.go

@@ -10,11 +10,18 @@ import (
 	"github.com/stretchr/testify/require"
 
 	"github.com/abh/geodns/server"
+	"github.com/abh/geodns/targeting"
+	"github.com/abh/geodns/targeting/geoip2"
 	"github.com/abh/geodns/zones"
 )
 
 func TestHTTP(t *testing.T) {
 
+	geoprovider, err := geoip2.New("/usr/local/share/GeoIP")
+	if err == nil {
+		targeting.Setup(geoprovider)
+	}
+
 	// todo: less global metrics ...
 	server.NewMetrics()
 

+ 29 - 2
targeting/geoip2/geoip2.go

@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"log"
 	"net"
+	"os"
 	"path/filepath"
 	"strings"
 	"sync"
@@ -21,6 +22,8 @@ const (
 	asnDB
 )
 
+var dbFiles map[geoType][]string
+
 type g2 struct {
 	dir string
 
@@ -30,9 +33,33 @@ type g2 struct {
 	mu      sync.RWMutex
 }
 
+func init() {
+	dbFiles = map[geoType][]string{
+		countryDB: []string{"GeoIP2-Country.mmdb", "GeoLite2-Country.mmdb"},
+		asnDB:     []string{"GeoIP2-ISP.mmdb"},
+		cityDB:    []string{"GeoIP2-City.mmdb", "GeoLite2-City.mmdb"},
+	}
+
+}
+
 func (g *g2) open(t geoType, db string) (*geoip2.Reader, error) {
-	f := filepath.Join(g.dir, db)
-	n, err := geoip2.Open(f)
+
+	fileName := filepath.Join(g.dir, db)
+
+	if len(db) == 0 {
+		found := false
+		for _, f := range dbFiles[t] {
+			fileName = filepath.Join(g.dir, f)
+			if _, err := os.Stat(f); err == nil {
+				found = true
+			}
+		}
+		if !found {
+			return nil, fmt.Errorf("could not find '%s' in '%s'", dbFiles[0], g.dir)
+		}
+	}
+
+	n, err := geoip2.Open(fileName)
 	if err != nil {
 		return nil, err
 	}

+ 1 - 1
zones/reader.go

@@ -191,7 +191,7 @@ func setupZoneData(data map[string]interface{}, zone *Zone) {
 
 			dnsType, ok := recordTypes[rType]
 			if !ok {
-				log.Printf("Unsupported record type '%s'\n", rType)
+				log.Printf("'%s' unsupported record type '%s'\n", zone.Origin, rType)
 				continue
 			}