Files
akvorado/geoip/lookup.go
Vincent Bernat b785d22df0 geoip: use two different lookup functions
No need to return only one result.
2022-03-08 17:23:31 +01:00

32 lines
658 B
Go

package geoip
import (
"net"
"github.com/oschwald/geoip2-golang"
)
// LookupASN returns the result of a lookup for an AS number.
func (c *Component) LookupASN(ip net.IP) uint32 {
asnDB := c.db.asn.Load()
if asnDB != nil {
asn, err := asnDB.(*geoip2.Reader).ASN(ip)
if err == nil {
return uint32(asn.AutonomousSystemNumber)
}
}
return 0
}
// LookupCountry returns the result of a lookup for country.
func (c *Component) LookupCountry(ip net.IP) string {
countryDB := c.db.country.Load()
if countryDB != nil {
country, err := countryDB.(*geoip2.Reader).Country(ip)
if err == nil {
return country.Country.IsoCode
}
}
return ""
}