diff --git a/http/http.go b/http/http.go index 023b667..6df6563 100644 --- a/http/http.go +++ b/http/http.go @@ -31,17 +31,18 @@ type Server struct { } type Response struct { - IP net.IP `json:"ip"` - IPDecimal *big.Int `json:"ip_decimal"` - Country string `json:"country,omitempty"` - CountryEU *bool `json:"country_eu,omitempty"` - CountryISO string `json:"country_iso,omitempty"` - City string `json:"city,omitempty"` - Hostname string `json:"hostname,omitempty"` - Latitude float64 `json:"latitude,omitempty"` - Longitude float64 `json:"longitude,omitempty"` - ASN string `json:"asn,omitempty"` - ASNOrg string `json:"asn_org,omitempty"` + IP net.IP `json:"ip"` + IPDecimal *big.Int `json:"ip_decimal"` + Country string `json:"country,omitempty"` + CountryEU *bool `json:"country_eu,omitempty"` + CountryISO string `json:"country_iso,omitempty"` + City string `json:"city,omitempty"` + Hostname string `json:"hostname,omitempty"` + Latitude float64 `json:"latitude,omitempty"` + Longitude float64 `json:"longitude,omitempty"` + ASN string `json:"asn,omitempty"` + ASNOrg string `json:"asn_org,omitempty"` + UserAgent *useragent.UserAgent `json:"user_agent,omitempty"` } type PortResponse struct { @@ -104,6 +105,12 @@ func (s *Server) newResponse(r *http.Request) (Response, error) { if asn.AutonomousSystemNumber > 0 { autonomousSystemNumber = fmt.Sprintf("AS%d", asn.AutonomousSystemNumber) } + var userAgent *useragent.UserAgent + userAgentRaw := r.UserAgent() + if userAgentRaw != "" { + parsed := useragent.Parse(userAgentRaw) + userAgent = &parsed + } return Response{ IP: ip, IPDecimal: ipDecimal, @@ -116,6 +123,7 @@ func (s *Server) newResponse(r *http.Request) (Response, error) { Longitude: city.Longitude, ASN: autonomousSystemNumber, ASNOrg: asn.AutonomousSystemOrganization, + UserAgent: userAgent, }, nil } diff --git a/http/http_test.go b/http/http_test.go index b4207e2..c766bf5 100644 --- a/http/http_test.go +++ b/http/http_test.go @@ -134,7 +134,7 @@ func TestJSONHandlers(t *testing.T) { out string status int }{ - {s.URL, `{"ip":"127.0.0.1","ip_decimal":2130706433,"country":"Elbonia","country_eu":false,"country_iso":"EB","city":"Bornyasherk","hostname":"localhost","latitude":63.416667,"longitude":10.416667,"asn":"AS59795","asn_org":"Hosting4Real"}`, 200}, + {s.URL, `{"ip":"127.0.0.1","ip_decimal":2130706433,"country":"Elbonia","country_eu":false,"country_iso":"EB","city":"Bornyasherk","hostname":"localhost","latitude":63.416667,"longitude":10.416667,"asn":"AS59795","asn_org":"Hosting4Real","user_agent":{"product":"curl","version":"7.2.6.0","raw_value":"curl/7.2.6.0"}}`, 200}, {s.URL + "/port/foo", `{"error":"invalid port: foo"}`, 400}, {s.URL + "/port/0", `{"error":"invalid port: 0"}`, 400}, {s.URL + "/port/65537", `{"error":"invalid port: 65537"}`, 400}, diff --git a/useragent/useragent.go b/useragent/useragent.go index e0de8e7..ebac6c4 100644 --- a/useragent/useragent.go +++ b/useragent/useragent.go @@ -5,9 +5,10 @@ import ( ) type UserAgent struct { - Product string - Version string - Comment string + Product string `json:"product,omitempty"` + Version string `json:"version,omitempty"` + Comment string `json:"comment,omitempty"` + RawValue string `json:"raw_value,omitempty"` } func Parse(s string) UserAgent { @@ -31,8 +32,9 @@ func Parse(s string) UserAgent { } } return UserAgent{ - Product: parts[0], - Version: version, - Comment: comment, + Product: parts[0], + Version: version, + Comment: comment, + RawValue: s, } }