mirror of
https://github.com/warmbly/warmbly.git
synced 2026-08-20 16:01:19 +00:00
47 lines
870 B
Go
47 lines
870 B
Go
package geo
|
|
|
|
import (
|
|
"net/netip"
|
|
)
|
|
|
|
type Info struct {
|
|
City string
|
|
Country string
|
|
CountryCode string
|
|
Region string
|
|
PostalCode string
|
|
}
|
|
|
|
func (c *Client) Lookup(ip netip.Addr) (*Info, error) {
|
|
info := &Info{}
|
|
|
|
if ip.IsPrivate() || ip.IsLoopback() {
|
|
info.City = "Local"
|
|
info.Country = "Local Network"
|
|
return info, nil
|
|
}
|
|
|
|
if c.r != nil {
|
|
cityRecord, err := c.r.City(ip)
|
|
if err == nil && cityRecord != nil {
|
|
info.City = cityRecord.City.Names.English
|
|
if len(cityRecord.Subdivisions) > 0 {
|
|
info.Region = cityRecord.Subdivisions[0].Names.English
|
|
}
|
|
info.Country = cityRecord.Country.Names.English
|
|
info.CountryCode = cityRecord.Country.ISOCode
|
|
info.PostalCode = cityRecord.Postal.Code
|
|
}
|
|
}
|
|
|
|
if info.City == "" {
|
|
info.City = "Unknown"
|
|
}
|
|
|
|
if info.Country == "" {
|
|
info.Country = "Unknown"
|
|
}
|
|
|
|
return info, nil
|
|
}
|