Use different package for geoip

This commit is contained in:
Torjus Håkestad 2021-04-10 12:12:08 +02:00
parent a5b344277c
commit 3eaa7d2397
3 changed files with 12 additions and 7 deletions

3
go.mod
View File

@ -10,9 +10,10 @@ require (
github.com/go-chi/chi/v5 v5.0.2 github.com/go-chi/chi/v5 v5.0.2
github.com/google/uuid v1.2.0 github.com/google/uuid v1.2.0
github.com/jackc/pgx/v4 v4.11.0 github.com/jackc/pgx/v4 v4.11.0
github.com/oschwald/geoip2-golang v1.5.0 github.com/oschwald/maxminddb-golang v1.8.0
github.com/pelletier/go-toml v1.9.0 github.com/pelletier/go-toml v1.9.0
github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/urfave/cli/v2 v2.3.0 github.com/urfave/cli/v2 v2.3.0
go.uber.org/zap v1.13.0 go.uber.org/zap v1.13.0
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2

2
go.sum
View File

@ -260,8 +260,6 @@ github.com/openzipkin-contrib/zipkin-go-opentracing v0.4.5/go.mod h1:/wsWhb9smxS
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw= github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
github.com/openzipkin/zipkin-go v0.2.1/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4= github.com/openzipkin/zipkin-go v0.2.1/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4=
github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4= github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4=
github.com/oschwald/geoip2-golang v1.5.0 h1:igg2yQIrrcRccB1ytFXqBfOHCjXWIoMv85lVJ1ONZzw=
github.com/oschwald/geoip2-golang v1.5.0/go.mod h1:xdvYt5xQzB8ORWFqPnqMwZpCpgNagttWdoZLlJQzg7s=
github.com/oschwald/maxminddb-golang v1.8.0 h1:Uh/DSnGoxsyp/KYbY1AuP0tYEwfs0sCph9p/UMXK/Hk= github.com/oschwald/maxminddb-golang v1.8.0 h1:Uh/DSnGoxsyp/KYbY1AuP0tYEwfs0sCph9p/UMXK/Hk=
github.com/oschwald/maxminddb-golang v1.8.0/go.mod h1:RXZtst0N6+FY/3qCNmZMBApR19cdQj43/NM9VkrNAis= github.com/oschwald/maxminddb-golang v1.8.0/go.mod h1:RXZtst0N6+FY/3qCNmZMBApR19cdQj43/NM9VkrNAis=
github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM= github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM=

View File

@ -4,23 +4,29 @@ import (
_ "embed" _ "embed"
"net" "net"
"github.com/oschwald/geoip2-golang" "github.com/oschwald/maxminddb-golang"
) )
//go:embed Geoacumen-Country.mmdb //go:embed Geoacumen-Country.mmdb
var mmdb []byte var mmdb []byte
func (s *HoneypotServer) LookupCountry(ip net.IP) string { func (s *HoneypotServer) LookupCountry(ip net.IP) string {
db, err := geoip2.FromBytes(mmdb) db, err := maxminddb.FromBytes(mmdb)
if err != nil { if err != nil {
s.Logger.Warnw("Error opening geoip database", "error", err) s.Logger.Warnw("Error opening geoip database", "error", err)
return "??" return "??"
} }
country, err := db.Country(ip) var record struct {
Country struct {
ISOCode string `maxminddb:"iso_code"`
} `maxminddb:"country"`
}
err = db.Lookup(ip, &record)
if err != nil { if err != nil {
s.Logger.Warnw("Error doing geoip lookup", "error", err) s.Logger.Warnw("Error doing geoip lookup", "error", err)
return "??" return "??"
} }
return country.Country.IsoCode return record.Country.ISOCode
} }