ASN database, part 2 (#67)

ASN lookup
This commit is contained in:
Niklas
2019-07-05 15:01:45 +02:00
committed by Martin Polden
parent 5a28ed6bf5
commit 7fbc2e1b9f
9 changed files with 86 additions and 11 deletions

View File

@ -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