diff --git a/http/http.go b/http/http.go index b7d4eec..c04f514 100644 --- a/http/http.go +++ b/http/http.go @@ -13,7 +13,6 @@ import ( "net" "net/http" "strconv" - "strings" ) const ( @@ -24,7 +23,7 @@ const ( type Server struct { Template string IPHeader string - LookupAddr func(net.IP) ([]string, error) + LookupAddr func(net.IP) (string, error) LookupPort func(net.IP, uint64) error db database.Client } @@ -72,10 +71,9 @@ func (s *Server) newResponse(r *http.Request) (Response, error) { ipDecimal := iputil.ToDecimal(ip) country, _ := s.db.Country(ip) city, _ := s.db.City(ip) - var hostnames []string + var hostname string if s.LookupAddr != nil { - h, _ := s.LookupAddr(ip) - hostnames = h + hostname, _ = s.LookupAddr(ip) } return Response{ IP: ip, @@ -83,17 +81,14 @@ func (s *Server) newResponse(r *http.Request) (Response, error) { Country: country.Name, CountryISO: country.ISO, City: city, - Hostname: strings.Join(hostnames, " "), + Hostname: hostname, }, nil } func (s *Server) newPortResponse(r *http.Request) (PortResponse, error) { lastElement := filepath.Base(r.URL.Path) port, err := strconv.ParseUint(lastElement, 10, 16) - if err != nil { - return PortResponse{Port: port}, err - } - if port < 1 || port > 65355 { + if err != nil || port < 1 || port > 65355 { return PortResponse{Port: port}, fmt.Errorf("invalid port: %d", port) } ip, err := ipFromRequest(s.IPHeader, r) diff --git a/http/http_test.go b/http/http_test.go index b4a9e05..66a3027 100644 --- a/http/http_test.go +++ b/http/http_test.go @@ -11,8 +11,8 @@ import ( "github.com/mpolden/ipd/iputil/database" ) -func lookupAddr(net.IP) ([]string, error) { return []string{"localhost"}, nil } -func lookupPort(net.IP, uint64) error { return nil } +func lookupAddr(net.IP) (string, error) { return "localhost", nil } +func lookupPort(net.IP, uint64) error { return nil } type testDb struct{} diff --git a/iputil/iputil.go b/iputil/iputil.go index 76e8500..d181845 100644 --- a/iputil/iputil.go +++ b/iputil/iputil.go @@ -8,12 +8,13 @@ import ( "time" ) -func LookupAddr(ip net.IP) ([]string, error) { +func LookupAddr(ip net.IP) (string, error) { names, err := net.LookupAddr(ip.String()) - for i, _ := range names { - names[i] = strings.TrimRight(names[i], ".") // Always return unrooted name + if err != nil || len(names) == 0 { + return "", err } - return names, err + // Always return unrooted name + return strings.TrimRight(names[0], "."), nil } func LookupPort(ip net.IP, port uint64) error {