mirror of https://github.com/mpolden/echoip
Merge pull request #70 from kevinschoon/useragent
return parsed and raw user agent
This commit is contained in:
commit
95380233d8
|
@ -42,6 +42,7 @@ type Response struct {
|
|||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -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},
|
||||
|
|
|
@ -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 {
|
||||
|
@ -34,5 +35,6 @@ func Parse(s string) UserAgent {
|
|||
Product: parts[0],
|
||||
Version: version,
|
||||
Comment: comment,
|
||||
RawValue: s,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue