Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mpolden/echoip.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/http
diff options
context:
space:
mode:
authorKevin <kevinschoon@gmail.com>2019-07-14 01:50:31 +0300
committerKevin <kevinschoon@gmail.com>2019-07-14 01:50:31 +0300
commit155c1a5afe846deb3d7429e2af2efa14ae8a59f9 (patch)
tree899d2c78f0941f8a0719205faaab3e0595122770 /http
parentfb5fac92d2173c2a5b07ed4ecc7b5fefe8484ed2 (diff)
return parsed and raw user agent
Diffstat (limited to 'http')
-rw-r--r--http/http.go30
-rw-r--r--http/http_test.go2
2 files changed, 20 insertions, 12 deletions
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},