34 lines
759 B
Go
34 lines
759 B
Go
package modules
|
|
|
|
import (
|
|
"github.com/IncSW/geoip2"
|
|
"net"
|
|
)
|
|
|
|
type IpInfo struct {
|
|
Ip string `json:"ip"`
|
|
CountryIsoCode string `json:"country_iso_code"`
|
|
CountryName string `json:"country_name"`
|
|
City string `json:"city"`
|
|
TimeZone string `json:"time_zone"`
|
|
}
|
|
|
|
func MmdbQueryIP(ip string) IpInfo {
|
|
reader, err := geoip2.NewCityReaderFromFile("./db/GeoLite2-City.mmdb")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
record, err := reader.Lookup(net.ParseIP(ip))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
GetIpInfo := IpInfo{
|
|
Ip: ip,
|
|
CountryIsoCode: record.Country.ISOCode,
|
|
CountryName: record.Country.Names["en"],
|
|
City: record.City.Names["en"],
|
|
TimeZone: record.Location.TimeZone,
|
|
}
|
|
return GetIpInfo
|
|
}
|