we migrating from deprecated freegeoip, but echoip is missing information that we used. The change adds more fields to JSON output: zip_code, time_zone, region_name, region_code, metro_code(US only)

This commit is contained in:
Dmitriy Shelikhov 2020-03-25 14:13:25 +07:00
parent e67f493f51
commit a9174cf97c
2 changed files with 30 additions and 0 deletions

View File

@ -34,6 +34,8 @@ type Server struct {
type Response struct {
IP net.IP `json:"ip"`
IPDecimal *big.Int `json:"ip_decimal"`
RegionName string `json:"region_name,omitempty"`
RegionCode string `json:"region_code,omitempty"`
Country string `json:"country,omitempty"`
CountryEU *bool `json:"country_eu,omitempty"`
CountryISO string `json:"country_iso,omitempty"`
@ -44,6 +46,9 @@ type Response struct {
ASN string `json:"asn,omitempty"`
ASNOrg string `json:"asn_org,omitempty"`
UserAgent *useragent.UserAgent `json:"user_agent,omitempty"`
PostalCode string `json:"zip_code,omitempty"`
Timezone string `json:"time_zone,omitempty"`
MetroCode uint `json:"metro_code,omitempty"`
}
type PortResponse struct {
@ -129,6 +134,11 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
ASN: autonomousSystemNumber,
ASNOrg: asn.AutonomousSystemOrganization,
UserAgent: userAgent,
PostalCode: city.PostalCode,
Timezone: city.Timezone,
MetroCode: city.MetroCode,
RegionName: city.RegionName,
RegionCode: city.RegionCode,
}
s.cache.Set(ip, response)
return *response, nil

View File

@ -24,6 +24,11 @@ type City struct {
Name string
Latitude float64
Longitude float64
PostalCode string
Timezone string
MetroCode uint
RegionName string
RegionCode string
}
type ASN struct {
@ -101,12 +106,27 @@ func (g *geoip) City(ip net.IP) (City, error) {
if c, exists := record.City.Names["en"]; exists {
city.Name = c
}
if c, exists := record.Subdivisions[0].Names["en"]; exists {
city.RegionName = c
}
if record.Subdivisions[0].IsoCode != "" {
city.RegionCode = record.Subdivisions[0].IsoCode
}
if !math.IsNaN(record.Location.Latitude) {
city.Latitude = record.Location.Latitude
}
if !math.IsNaN(record.Location.Longitude) {
city.Longitude = record.Location.Longitude
}
if record.Location.TimeZone != "" {
city.Timezone = record.Location.TimeZone
}
if record.Location.MetroCode > 0 && record.Country.IsoCode == "US" {
city.MetroCode = record.Location.MetroCode
}
if record.Postal.Code != "" {
city.PostalCode = record.Postal.Code
}
return city, nil
}