mirror of https://github.com/mpolden/echoip
Ensure hostname is unrooted
This commit is contained in:
parent
73fd32f37e
commit
74e6f4dd31
|
@ -74,7 +74,7 @@ func (a *API) newResponse(r *http.Request) (Response, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
hostnames, err := a.oracle.LookupAddr(ip.String())
|
hostnames, err := a.oracle.LookupAddr(ip)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
|
|
||||||
type mockOracle struct{}
|
type mockOracle struct{}
|
||||||
|
|
||||||
func (r *mockOracle) LookupAddr(string) ([]string, error) { return []string{"localhost"}, nil }
|
func (r *mockOracle) LookupAddr(net.IP) ([]string, error) { return []string{"localhost"}, nil }
|
||||||
func (r *mockOracle) LookupCountry(net.IP) (string, error) { return "Elbonia", nil }
|
func (r *mockOracle) LookupCountry(net.IP) (string, error) { return "Elbonia", nil }
|
||||||
func (r *mockOracle) LookupCity(net.IP) (string, error) { return "Bornyasherk", nil }
|
func (r *mockOracle) LookupCity(net.IP) (string, error) { return "Bornyasherk", nil }
|
||||||
func (r *mockOracle) LookupPort(net.IP, uint64) error { return nil }
|
func (r *mockOracle) LookupPort(net.IP, uint64) error { return nil }
|
||||||
|
|
|
@ -3,13 +3,14 @@ package api
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/oschwald/geoip2-golang"
|
"github.com/oschwald/geoip2-golang"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Oracle interface {
|
type Oracle interface {
|
||||||
LookupAddr(string) ([]string, error)
|
LookupAddr(net.IP) ([]string, error)
|
||||||
LookupCountry(net.IP) (string, error)
|
LookupCountry(net.IP) (string, error)
|
||||||
LookupCity(net.IP) (string, error)
|
LookupCity(net.IP) (string, error)
|
||||||
LookupPort(net.IP, uint64) error
|
LookupPort(net.IP, uint64) error
|
||||||
|
@ -20,7 +21,7 @@ type Oracle interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type DefaultOracle struct {
|
type DefaultOracle struct {
|
||||||
lookupAddr func(string) ([]string, error)
|
lookupAddr func(net.IP) ([]string, error)
|
||||||
lookupCountry func(net.IP) (string, error)
|
lookupCountry func(net.IP) (string, error)
|
||||||
lookupCity func(net.IP) (string, error)
|
lookupCity func(net.IP) (string, error)
|
||||||
lookupPort func(net.IP, uint64) error
|
lookupPort func(net.IP, uint64) error
|
||||||
|
@ -32,15 +33,15 @@ type DefaultOracle struct {
|
||||||
|
|
||||||
func NewOracle() *DefaultOracle {
|
func NewOracle() *DefaultOracle {
|
||||||
return &DefaultOracle{
|
return &DefaultOracle{
|
||||||
lookupAddr: func(string) ([]string, error) { return nil, nil },
|
lookupAddr: func(net.IP) ([]string, error) { return nil, nil },
|
||||||
lookupCountry: func(net.IP) (string, error) { return "", nil },
|
lookupCountry: func(net.IP) (string, error) { return "", nil },
|
||||||
lookupCity: func(net.IP) (string, error) { return "", nil },
|
lookupCity: func(net.IP) (string, error) { return "", nil },
|
||||||
lookupPort: func(net.IP, uint64) error { return nil },
|
lookupPort: func(net.IP, uint64) error { return nil },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DefaultOracle) LookupAddr(address string) ([]string, error) {
|
func (r *DefaultOracle) LookupAddr(ip net.IP) ([]string, error) {
|
||||||
return r.lookupAddr(address)
|
return r.lookupAddr(ip)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DefaultOracle) LookupCountry(ip net.IP) (string, error) {
|
func (r *DefaultOracle) LookupCountry(ip net.IP) (string, error) {
|
||||||
|
@ -56,7 +57,7 @@ func (r *DefaultOracle) LookupPort(ip net.IP, port uint64) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DefaultOracle) EnableLookupAddr() {
|
func (r *DefaultOracle) EnableLookupAddr() {
|
||||||
r.lookupAddr = net.LookupAddr
|
r.lookupAddr = lookupAddr
|
||||||
r.lookupAddrEnabled = true
|
r.lookupAddrEnabled = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,6 +95,14 @@ func (r *DefaultOracle) IsLookupCountryEnabled() bool { return r.lookupCountryEn
|
||||||
func (r *DefaultOracle) IsLookupCityEnabled() bool { return r.lookupCityEnabled }
|
func (r *DefaultOracle) IsLookupCityEnabled() bool { return r.lookupCityEnabled }
|
||||||
func (r *DefaultOracle) IsLookupPortEnabled() bool { return r.lookupPortEnabled }
|
func (r *DefaultOracle) IsLookupPortEnabled() bool { return r.lookupPortEnabled }
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
return names, err
|
||||||
|
}
|
||||||
|
|
||||||
func lookupPort(ip net.IP, port uint64) error {
|
func lookupPort(ip net.IP, port uint64) error {
|
||||||
address := fmt.Sprintf("[%s]:%d", ip, port)
|
address := fmt.Sprintf("[%s]:%d", ip, port)
|
||||||
conn, err := net.DialTimeout("tcp", address, 2*time.Second)
|
conn, err := net.DialTimeout("tcp", address, 2*time.Second)
|
||||||
|
|
Loading…
Reference in New Issue