Move and fix geoip package

This commit is contained in:
Daniel
2020-03-20 23:00:18 +01:00
parent 5b40ffbedf
commit f270ccc21f
7 changed files with 29 additions and 17 deletions

View File

@@ -41,5 +41,6 @@ func GetLocation(ip net.IP) (record *Location, err error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return record, nil return record, nil
} }

View File

@@ -3,16 +3,9 @@ package geoip
import ( import (
"net" "net"
"testing" "testing"
"github.com/safing/portmaster/updates"
) )
func TestLocationLookup(t *testing.T) { func TestLocationLookup(t *testing.T) {
err := updates.InitForTesting()
if err != nil {
t.Fatal(err)
}
ip1 := net.ParseIP("81.2.69.142") ip1 := net.ParseIP("81.2.69.142")
loc1, err := GetLocation(ip1) loc1, err := GetLocation(ip1)
if err != nil { if err != nil {
@@ -39,7 +32,21 @@ func TestLocationLookup(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
t.Logf("%v", loc1) t.Logf("%v", loc4)
ip5 := net.ParseIP("194.232.1.1")
loc5, err := GetLocation(ip5)
if err != nil {
t.Fatal(err)
}
t.Logf("%v", loc5)
ip6 := net.ParseIP("151.101.1.164")
loc6, err := GetLocation(ip6)
if err != nil {
t.Fatal(err)
}
t.Logf("%v", loc6)
dist1 := loc1.EstimateNetworkProximity(loc2) dist1 := loc1.EstimateNetworkProximity(loc2)
dist2 := loc2.EstimateNetworkProximity(loc3) dist2 := loc2.EstimateNetworkProximity(loc3)
@@ -50,5 +57,4 @@ func TestLocationLookup(t *testing.T) {
t.Logf("proximity %s <> %s: %d", ip2, ip3, dist2) t.Logf("proximity %s <> %s: %d", ip2, ip3, dist2)
t.Logf("proximity %s <> %s: %d", ip1, ip3, dist3) t.Logf("proximity %s <> %s: %d", ip1, ip3, dist3)
t.Logf("proximity %s <> %s: %d", ip1, ip4, dist4) t.Logf("proximity %s <> %s: %d", ip1, ip4, dist4)
} }

View File

@@ -2,7 +2,6 @@ package geoip
import ( import (
"context" "context"
"fmt"
"github.com/safing/portbase/modules" "github.com/safing/portbase/modules"
) )
@@ -12,15 +11,10 @@ var (
) )
func init() { func init() {
module = modules.Register("geoip", nil, start, nil, "updates") module = modules.Register("geoip", prep, nil, nil, "updates")
} }
func start() error { func prep() error {
err := prepDatabaseForUse()
if err != nil {
return fmt.Errorf("goeip: failed to load databases: %s", err)
}
return module.RegisterEventHook( return module.RegisterEventHook(
"updates", "updates",
"resource update", "resource update",

View File

@@ -0,0 +1,11 @@
package geoip
import (
"testing"
"github.com/safing/portmaster/core/pmtesting"
)
func TestMain(m *testing.M) {
pmtesting.TestMain(m)
}