return parsed and raw user agent

This commit is contained in:
Kevin 2019-07-13 22:50:31 +00:00
parent fb5fac92d2
commit 155c1a5afe
3 changed files with 28 additions and 18 deletions

View File

@ -31,17 +31,18 @@ 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"`
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"`
City string `json:"city,omitempty"` City string `json:"city,omitempty"`
Hostname string `json:"hostname,omitempty"` Hostname string `json:"hostname,omitempty"`
Latitude float64 `json:"latitude,omitempty"` Latitude float64 `json:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty"` Longitude float64 `json:"longitude,omitempty"`
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"`
} }
type PortResponse struct { type PortResponse struct {
@ -104,6 +105,12 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
if asn.AutonomousSystemNumber > 0 { if asn.AutonomousSystemNumber > 0 {
autonomousSystemNumber = fmt.Sprintf("AS%d", asn.AutonomousSystemNumber) autonomousSystemNumber = fmt.Sprintf("AS%d", asn.AutonomousSystemNumber)
} }
var userAgent *useragent.UserAgent
userAgentRaw := r.UserAgent()
if userAgentRaw != "" {
parsed := useragent.Parse(userAgentRaw)
userAgent = &parsed
}
return Response{ return Response{
IP: ip, IP: ip,
IPDecimal: ipDecimal, IPDecimal: ipDecimal,
@ -116,6 +123,7 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
Longitude: city.Longitude, Longitude: city.Longitude,
ASN: autonomousSystemNumber, ASN: autonomousSystemNumber,
ASNOrg: asn.AutonomousSystemOrganization, ASNOrg: asn.AutonomousSystemOrganization,
UserAgent: userAgent,
}, nil }, nil
} }

View File

@ -134,7 +134,7 @@ func TestJSONHandlers(t *testing.T) {
out string out string
status int 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/foo", `{"error":"invalid port: foo"}`, 400},
{s.URL + "/port/0", `{"error":"invalid port: 0"}`, 400}, {s.URL + "/port/0", `{"error":"invalid port: 0"}`, 400},
{s.URL + "/port/65537", `{"error":"invalid port: 65537"}`, 400}, {s.URL + "/port/65537", `{"error":"invalid port: 65537"}`, 400},

View File

@ -5,9 +5,10 @@ import (
) )
type UserAgent struct { type UserAgent struct {
Product string Product string `json:"product,omitempty"`
Version string Version string `json:"version,omitempty"`
Comment string Comment string `json:"comment,omitempty"`
RawValue string `json:"raw_value,omitempty"`
} }
func Parse(s string) UserAgent { func Parse(s string) UserAgent {
@ -31,8 +32,9 @@ func Parse(s string) UserAgent {
} }
} }
return UserAgent{ return UserAgent{
Product: parts[0], Product: parts[0],
Version: version, Version: version,
Comment: comment, Comment: comment,
RawValue: s,
} }
} }