48 lines
1018 B
Go
48 lines
1018 B
Go
package geoip
|
|
|
|
import (
|
|
"net"
|
|
|
|
geoip2 "github.com/oschwald/geoip2-golang"
|
|
)
|
|
|
|
var db *geoip2.Reader
|
|
|
|
// NewReader -
|
|
func NewReader() (*geoip2.Reader, error) {
|
|
var err error
|
|
db, err = geoip2.Open("/data/GeoLite2-City.mmdb")
|
|
return db, err
|
|
}
|
|
|
|
// GeoStruct -
|
|
type GeoStruct struct {
|
|
City string `json:"city"`
|
|
Country struct {
|
|
Name string `json:"name"`
|
|
Code string `json:"code"`
|
|
} `json:"country"`
|
|
Timezone string `json:"timezone"`
|
|
Location struct {
|
|
Latitude float64 `json:"latitude"`
|
|
Longitude float64 `json:"longtitude"`
|
|
} `json:"location"`
|
|
}
|
|
|
|
// QueryGEO -
|
|
func QueryGEO(input string) (geo *GeoStruct, err error) {
|
|
ip := net.ParseIP(input)
|
|
record, err := db.City(ip)
|
|
if err != nil {
|
|
return
|
|
}
|
|
geo = &GeoStruct{}
|
|
geo.City = record.City.Names["en"]
|
|
geo.Timezone = record.Location.TimeZone
|
|
geo.Location.Latitude = record.Location.Latitude
|
|
geo.Location.Longitude = record.Location.Longitude
|
|
geo.Country.Code = record.Country.IsoCode
|
|
geo.Country.Name = record.Country.Names["en"]
|
|
return
|
|
}
|