diff options
author | Niklas <Alphakilo@users.noreply.github.com> | 2019-07-05 16:01:45 +0300 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2019-07-05 16:01:45 +0300 |
commit | 7fbc2e1b9fc80146e5fa16bde1ab636ef63725fa (patch) | |
tree | 7b12d6018829f73cbe10086609ee8db13e6f70d9 /http | |
parent | 5a28ed6bf5a41aaf4d4d29cd9264fff0f13c5829 (diff) |
ASN database, part 2 (#67)
ASN lookup
Diffstat (limited to 'http')
-rw-r--r-- | http/http.go | 19 | ||||
-rw-r--r-- | http/http_test.go | 9 |
2 files changed, 26 insertions, 2 deletions
diff --git a/http/http.go b/http/http.go index ae755c0..663effd 100644 --- a/http/http.go +++ b/http/http.go @@ -40,6 +40,8 @@ type Response struct { 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"` } type PortResponse struct { @@ -93,10 +95,15 @@ func (s *Server) newResponse(r *http.Request) (Response, error) { ipDecimal := iputil.ToDecimal(ip) country, _ := s.gr.Country(ip) city, _ := s.gr.City(ip) + asn, _ := s.gr.ASN(ip) var hostname string if s.LookupAddr != nil { hostname, _ = s.LookupAddr(ip) } + var autonomousSystemNumber string + if asn.AutonomousSystemNumber > 0 { + autonomousSystemNumber = fmt.Sprintf("AS%d", asn.AutonomousSystemNumber) + } return Response{ IP: ip, IPDecimal: ipDecimal, @@ -107,6 +114,8 @@ func (s *Server) newResponse(r *http.Request) (Response, error) { Hostname: hostname, Latitude: city.Latitude, Longitude: city.Longitude, + ASN: autonomousSystemNumber, + ASNOrg: asn.AutonomousSystemOrganization, }, nil } @@ -173,6 +182,15 @@ func (s *Server) CLICoordinatesHandler(w http.ResponseWriter, r *http.Request) * return nil } +func (s *Server) CLIASNHandler(w http.ResponseWriter, r *http.Request) *appError { + response, err := s.newResponse(r) + if err != nil { + return internalServerError(err) + } + fmt.Fprintf(w, "%s\n", response.ASN) + return nil +} + func (s *Server) JSONHandler(w http.ResponseWriter, r *http.Request) *appError { response, err := s.newResponse(r) if err != nil { @@ -305,6 +323,7 @@ func (s *Server) Handler() http.Handler { r.Route("GET", "/country-iso", s.CLICountryISOHandler) r.Route("GET", "/city", s.CLICityHandler) r.Route("GET", "/coordinates", s.CLICoordinatesHandler) + r.Route("GET", "/asn", s.CLIASNHandler) } // Browser diff --git a/http/http_test.go b/http/http_test.go index d7dd35f..cca5e9b 100644 --- a/http/http_test.go +++ b/http/http_test.go @@ -24,6 +24,10 @@ func (t *testDb) City(net.IP) (geo.City, error) { return geo.City{Name: "Bornyasherk", Latitude: 63.416667, Longitude: 10.416667}, nil } +func (t *testDb) ASN(net.IP) (geo.ASN, error) { + return geo.ASN{AutonomousSystemNumber: 59795, AutonomousSystemOrganization: "Hosting4Real"}, nil +} + func (t *testDb) IsEmpty() bool { return false } func testServer() *Server { @@ -70,6 +74,7 @@ func TestCLIHandlers(t *testing.T) { {s.URL + "/coordinates", "63.416667,10.416667\n", 200, "", ""}, {s.URL + "/city", "Bornyasherk\n", 200, "", ""}, {s.URL + "/foo", "404 page not found", 404, "", ""}, + {s.URL + "/asn", "AS59795\n", 200, "", ""}, } for _, tt := range tests { @@ -91,7 +96,7 @@ func TestDisabledHandlers(t *testing.T) { server := testServer() server.LookupPort = nil server.LookupAddr = nil - server.gr, _ = geo.Open("", "") + server.gr, _ = geo.Open("", "", "") s := httptest.NewServer(server.Handler()) var tests = []struct { @@ -129,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}`, 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"}`, 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}, |