apiary/honeypot/geolocate.go

36 lines
658 B
Go
Raw Normal View History

2021-04-10 05:58:01 +00:00
package honeypot
import (
_ "embed"
"net"
2021-04-10 10:12:08 +00:00
"github.com/oschwald/maxminddb-golang"
2021-04-10 05:58:01 +00:00
)
//go:embed Geoacumen-Country.mmdb
var mmdb []byte
2021-04-10 09:59:10 +00:00
func (s *HoneypotServer) LookupCountry(ip net.IP) string {
2021-04-10 10:12:08 +00:00
db, err := maxminddb.FromBytes(mmdb)
2021-04-10 05:58:01 +00:00
if err != nil {
2021-04-10 09:59:10 +00:00
s.Logger.Warnw("Error opening geoip database", "error", err)
2021-04-10 05:58:01 +00:00
return "??"
}
2021-04-10 10:12:08 +00:00
var record struct {
Country struct {
ISOCode string `maxminddb:"iso_code"`
} `maxminddb:"country"`
}
err = db.Lookup(ip, &record)
2021-04-10 05:58:01 +00:00
if err != nil {
2021-04-10 09:59:10 +00:00
s.Logger.Warnw("Error doing geoip lookup", "error", err)
2021-04-10 05:58:01 +00:00
return "??"
}
2021-09-30 21:12:27 +00:00
if record.Country.ISOCode == "None" {
return "??"
}
2021-04-10 10:12:08 +00:00
return record.Country.ISOCode
2021-04-10 05:58:01 +00:00
}