mirror of https://github.com/mpolden/echoip
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:
parent
e67f493f51
commit
a9174cf97c
10
http/http.go
10
http/http.go
|
@ -34,6 +34,8 @@ type Server struct {
|
||||||
type Response struct {
|
type Response struct {
|
||||||
IP net.IP `json:"ip"`
|
IP net.IP `json:"ip"`
|
||||||
IPDecimal *big.Int `json:"ip_decimal"`
|
IPDecimal *big.Int `json:"ip_decimal"`
|
||||||
|
RegionName string `json:"region_name,omitempty"`
|
||||||
|
RegionCode string `json:"region_code,omitempty"`
|
||||||
Country string `json:"country,omitempty"`
|
Country string `json:"country,omitempty"`
|
||||||
CountryEU *bool `json:"country_eu,omitempty"`
|
CountryEU *bool `json:"country_eu,omitempty"`
|
||||||
CountryISO string `json:"country_iso,omitempty"`
|
CountryISO string `json:"country_iso,omitempty"`
|
||||||
|
@ -44,6 +46,9 @@ type Response struct {
|
||||||
ASN string `json:"asn,omitempty"`
|
ASN string `json:"asn,omitempty"`
|
||||||
ASNOrg string `json:"asn_org,omitempty"`
|
ASNOrg string `json:"asn_org,omitempty"`
|
||||||
UserAgent *useragent.UserAgent `json:"user_agent,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 {
|
type PortResponse struct {
|
||||||
|
@ -129,6 +134,11 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
|
||||||
ASN: autonomousSystemNumber,
|
ASN: autonomousSystemNumber,
|
||||||
ASNOrg: asn.AutonomousSystemOrganization,
|
ASNOrg: asn.AutonomousSystemOrganization,
|
||||||
UserAgent: userAgent,
|
UserAgent: userAgent,
|
||||||
|
PostalCode: city.PostalCode,
|
||||||
|
Timezone: city.Timezone,
|
||||||
|
MetroCode: city.MetroCode,
|
||||||
|
RegionName: city.RegionName,
|
||||||
|
RegionCode: city.RegionCode,
|
||||||
}
|
}
|
||||||
s.cache.Set(ip, response)
|
s.cache.Set(ip, response)
|
||||||
return *response, nil
|
return *response, nil
|
||||||
|
|
|
@ -24,6 +24,11 @@ type City struct {
|
||||||
Name string
|
Name string
|
||||||
Latitude float64
|
Latitude float64
|
||||||
Longitude float64
|
Longitude float64
|
||||||
|
PostalCode string
|
||||||
|
Timezone string
|
||||||
|
MetroCode uint
|
||||||
|
RegionName string
|
||||||
|
RegionCode string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ASN struct {
|
type ASN struct {
|
||||||
|
@ -101,12 +106,27 @@ func (g *geoip) City(ip net.IP) (City, error) {
|
||||||
if c, exists := record.City.Names["en"]; exists {
|
if c, exists := record.City.Names["en"]; exists {
|
||||||
city.Name = c
|
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) {
|
if !math.IsNaN(record.Location.Latitude) {
|
||||||
city.Latitude = record.Location.Latitude
|
city.Latitude = record.Location.Latitude
|
||||||
}
|
}
|
||||||
if !math.IsNaN(record.Location.Longitude) {
|
if !math.IsNaN(record.Location.Longitude) {
|
||||||
city.Longitude = 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
|
return city, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue