This commit is contained in:
Martin Polden 2018-03-19 19:54:24 +01:00
parent b01bddb63e
commit 184676ba29
3 changed files with 12 additions and 16 deletions

View File

@ -13,7 +13,6 @@ import (
"net" "net"
"net/http" "net/http"
"strconv" "strconv"
"strings"
) )
const ( const (
@ -24,7 +23,7 @@ const (
type Server struct { type Server struct {
Template string Template string
IPHeader string IPHeader string
LookupAddr func(net.IP) ([]string, error) LookupAddr func(net.IP) (string, error)
LookupPort func(net.IP, uint64) error LookupPort func(net.IP, uint64) error
db database.Client db database.Client
} }
@ -72,10 +71,9 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
ipDecimal := iputil.ToDecimal(ip) ipDecimal := iputil.ToDecimal(ip)
country, _ := s.db.Country(ip) country, _ := s.db.Country(ip)
city, _ := s.db.City(ip) city, _ := s.db.City(ip)
var hostnames []string var hostname string
if s.LookupAddr != nil { if s.LookupAddr != nil {
h, _ := s.LookupAddr(ip) hostname, _ = s.LookupAddr(ip)
hostnames = h
} }
return Response{ return Response{
IP: ip, IP: ip,
@ -83,17 +81,14 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
Country: country.Name, Country: country.Name,
CountryISO: country.ISO, CountryISO: country.ISO,
City: city, City: city,
Hostname: strings.Join(hostnames, " "), Hostname: hostname,
}, nil }, nil
} }
func (s *Server) newPortResponse(r *http.Request) (PortResponse, error) { func (s *Server) newPortResponse(r *http.Request) (PortResponse, error) {
lastElement := filepath.Base(r.URL.Path) lastElement := filepath.Base(r.URL.Path)
port, err := strconv.ParseUint(lastElement, 10, 16) port, err := strconv.ParseUint(lastElement, 10, 16)
if err != nil { if err != nil || port < 1 || port > 65355 {
return PortResponse{Port: port}, err
}
if port < 1 || port > 65355 {
return PortResponse{Port: port}, fmt.Errorf("invalid port: %d", port) return PortResponse{Port: port}, fmt.Errorf("invalid port: %d", port)
} }
ip, err := ipFromRequest(s.IPHeader, r) ip, err := ipFromRequest(s.IPHeader, r)

View File

@ -11,8 +11,8 @@ import (
"github.com/mpolden/ipd/iputil/database" "github.com/mpolden/ipd/iputil/database"
) )
func lookupAddr(net.IP) ([]string, error) { return []string{"localhost"}, nil } func lookupAddr(net.IP) (string, error) { return "localhost", nil }
func lookupPort(net.IP, uint64) error { return nil } func lookupPort(net.IP, uint64) error { return nil }
type testDb struct{} type testDb struct{}

View File

@ -8,12 +8,13 @@ import (
"time" "time"
) )
func LookupAddr(ip net.IP) ([]string, error) { func LookupAddr(ip net.IP) (string, error) {
names, err := net.LookupAddr(ip.String()) names, err := net.LookupAddr(ip.String())
for i, _ := range names { if err != nil || len(names) == 0 {
names[i] = strings.TrimRight(names[i], ".") // Always return unrooted name return "", err
} }
return names, err // Always return unrooted name
return strings.TrimRight(names[0], "."), nil
} }
func LookupPort(ip net.IP, port uint64) error { func LookupPort(ip net.IP, port uint64) error {