net/{dnscache,interfaces}: use netaddr.IP.IsPrivate, delete copied code

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/2527/head
Brad Fitzpatrick 3 years ago committed by Brad Fitzpatrick
parent dde8e28f00
commit aaf2df7ab1

@ -46,7 +46,7 @@ require (
golang.zx2c4.com/wireguard v0.0.0-20210624150102-15b24b6179e0 golang.zx2c4.com/wireguard v0.0.0-20210624150102-15b24b6179e0
golang.zx2c4.com/wireguard/windows v0.3.16 golang.zx2c4.com/wireguard/windows v0.3.16
honnef.co/go/tools v0.1.4 honnef.co/go/tools v0.1.4
inet.af/netaddr v0.0.0-20210602152128-50f8686885e3 inet.af/netaddr v0.0.0-20210721214506-ce7a8ad02cc1
inet.af/netstack v0.0.0-20210622165351-29b14ebc044e inet.af/netstack v0.0.0-20210622165351-29b14ebc044e
inet.af/peercred v0.0.0-20210318190834-4259e17bb763 inet.af/peercred v0.0.0-20210318190834-4259e17bb763
inet.af/wf v0.0.0-20210516214145-a5343001b756 inet.af/wf v0.0.0-20210516214145-a5343001b756

@ -966,6 +966,8 @@ honnef.co/go/tools v0.1.4/go.mod h1:NgwopIslSNH47DimFoV78dnkksY2EFtX0ajyb3K/las=
inet.af/netaddr v0.0.0-20210515010201-ad03edc7c841/go.mod h1:z0nx+Dh+7N7CC8V5ayHtHGpZpxLQZZxkIaaz6HN65Ls= inet.af/netaddr v0.0.0-20210515010201-ad03edc7c841/go.mod h1:z0nx+Dh+7N7CC8V5ayHtHGpZpxLQZZxkIaaz6HN65Ls=
inet.af/netaddr v0.0.0-20210602152128-50f8686885e3 h1:RlarOdsmOUCCvy7Xm1JchJIGuQsuKwD/Lo1bjYmfuQI= inet.af/netaddr v0.0.0-20210602152128-50f8686885e3 h1:RlarOdsmOUCCvy7Xm1JchJIGuQsuKwD/Lo1bjYmfuQI=
inet.af/netaddr v0.0.0-20210602152128-50f8686885e3/go.mod h1:z0nx+Dh+7N7CC8V5ayHtHGpZpxLQZZxkIaaz6HN65Ls= inet.af/netaddr v0.0.0-20210602152128-50f8686885e3/go.mod h1:z0nx+Dh+7N7CC8V5ayHtHGpZpxLQZZxkIaaz6HN65Ls=
inet.af/netaddr v0.0.0-20210721214506-ce7a8ad02cc1 h1:mxmfTV6kjXTlFqqFETnG9FQZzNFc6AKunZVAgQ3b7WA=
inet.af/netaddr v0.0.0-20210721214506-ce7a8ad02cc1/go.mod h1:z0nx+Dh+7N7CC8V5ayHtHGpZpxLQZZxkIaaz6HN65Ls=
inet.af/netstack v0.0.0-20210622165351-29b14ebc044e h1:z11NK94NQcI3DA+a3pUC/2dRYTph1kPX6B0FnCaMDzk= inet.af/netstack v0.0.0-20210622165351-29b14ebc044e h1:z11NK94NQcI3DA+a3pUC/2dRYTph1kPX6B0FnCaMDzk=
inet.af/netstack v0.0.0-20210622165351-29b14ebc044e/go.mod h1:fG3G1dekmK8oDX3iVzt8c0zICLMLSN8SjdxbXVt0WjU= inet.af/netstack v0.0.0-20210622165351-29b14ebc044e/go.mod h1:fG3G1dekmK8oDX3iVzt8c0zICLMLSN8SjdxbXVt0WjU=
inet.af/peercred v0.0.0-20210318190834-4259e17bb763 h1:gPSJmmVzmdy4kHhlCMx912GdiUz3k/RzJGg0ADqy1dg= inet.af/peercred v0.0.0-20210318190834-4259e17bb763 h1:gPSJmmVzmdy4kHhlCMx912GdiUz3k/RzJGg0ADqy1dg=

@ -249,7 +249,7 @@ func (r *Resolver) lookupIP(host string) (ip, ip6 net.IP, allIPs []net.IPAddr, e
} }
func (r *Resolver) addIPCache(host string, ip, ip6 net.IP, allIPs []net.IPAddr, d time.Duration) { func (r *Resolver) addIPCache(host string, ip, ip6 net.IP, allIPs []net.IPAddr, d time.Duration) {
if isPrivateIP(ip) { if naIP, _ := netaddr.FromStdIP(ip); naIP.IsPrivate() {
// Don't cache obviously wrong entries from captive portals. // Don't cache obviously wrong entries from captive portals.
// TODO: use DoH or DoT for the forwarding resolver? // TODO: use DoH or DoT for the forwarding resolver?
if debug { if debug {
@ -275,24 +275,6 @@ func (r *Resolver) addIPCache(host string, ip, ip6 net.IP, allIPs []net.IPAddr,
} }
} }
func mustCIDR(s string) *net.IPNet {
_, ipNet, err := net.ParseCIDR(s)
if err != nil {
panic(err)
}
return ipNet
}
func isPrivateIP(ip net.IP) bool {
return private1.Contains(ip) || private2.Contains(ip) || private3.Contains(ip)
}
var (
private1 = mustCIDR("10.0.0.0/8")
private2 = mustCIDR("172.16.0.0/12")
private3 = mustCIDR("192.168.0.0/16")
)
type DialContextFunc func(ctx context.Context, network, address string) (net.Conn, error) type DialContextFunc func(ctx context.Context, network, address string) (net.Conn, error)
// Dialer returns a wrapped DialContext func that uses the provided dnsCache. // Dialer returns a wrapped DialContext func that uses the provided dnsCache.

@ -14,24 +14,6 @@ import (
var dialTest = flag.String("dial-test", "", "if non-empty, addr:port to test dial") var dialTest = flag.String("dial-test", "", "if non-empty, addr:port to test dial")
func TestIsPrivateIP(t *testing.T) {
tests := []struct {
ip string
want bool
}{
{"10.1.2.3", true},
{"172.16.1.100", true},
{"192.168.1.1", true},
{"1.2.3.4", false},
}
for _, test := range tests {
if got := isPrivateIP(net.ParseIP(test.ip)); got != test.want {
t.Errorf("isPrivateIP(%q)=%v, want %v", test.ip, got, test.want)
}
}
}
func TestDialer(t *testing.T) { func TestDialer(t *testing.T) {
if *dialTest == "" { if *dialTest == "" {
t.Skip("skipping; --dial-test is blank") t.Skip("skipping; --dial-test is blank")

@ -479,7 +479,7 @@ func HTTPOfListener(ln net.Listener) string {
var privateIP string var privateIP string
ForeachInterfaceAddress(func(i Interface, pfx netaddr.IPPrefix) { ForeachInterfaceAddress(func(i Interface, pfx netaddr.IPPrefix) {
ip := pfx.IP() ip := pfx.IP()
if isPrivateIP(ip) { if ip.IsPrivate() {
if privateIP == "" { if privateIP == "" {
privateIP = ip.String() privateIP = ip.String()
} }
@ -519,21 +519,15 @@ func LikelyHomeRouterIP() (gateway, myIP netaddr.IP, ok bool) {
if !i.IsUp() || ip.IsZero() || !myIP.IsZero() { if !i.IsUp() || ip.IsZero() || !myIP.IsZero() {
return return
} }
for _, prefix := range privatev4s { if gateway.IsPrivate() && ip.IsPrivate() {
if prefix.Contains(gateway) && prefix.Contains(ip) { myIP = ip
myIP = ip ok = true
ok = true return
return
}
} }
}) })
return gateway, myIP, !myIP.IsZero() return gateway, myIP, !myIP.IsZero()
} }
func isPrivateIP(ip netaddr.IP) bool {
return private1.Contains(ip) || private2.Contains(ip) || private3.Contains(ip)
}
// isUsableV4 reports whether ip is a usable IPv4 address which could // isUsableV4 reports whether ip is a usable IPv4 address which could
// conceivably be used to get Internet connectivity. Globally routable and // conceivably be used to get Internet connectivity. Globally routable and
// private IPv4 addresses are always Usable, and link local 169.254.x.x // private IPv4 addresses are always Usable, and link local 169.254.x.x
@ -557,20 +551,8 @@ func isUsableV6(ip netaddr.IP) bool {
(tsaddr.IsULA(ip) && !tsaddr.TailscaleULARange().Contains(ip)) (tsaddr.IsULA(ip) && !tsaddr.TailscaleULARange().Contains(ip))
} }
func mustCIDR(s string) netaddr.IPPrefix {
prefix, err := netaddr.ParseIPPrefix(s)
if err != nil {
panic(err)
}
return prefix
}
var ( var (
private1 = mustCIDR("10.0.0.0/8") v6Global1 = netaddr.MustParseIPPrefix("2000::/3")
private2 = mustCIDR("172.16.0.0/12")
private3 = mustCIDR("192.168.0.0/16")
privatev4s = []netaddr.IPPrefix{private1, private2, private3}
v6Global1 = mustCIDR("2000::/3")
) )
// anyInterestingIP reports whether pfxs contains any IP that matches // anyInterestingIP reports whether pfxs contains any IP that matches

@ -73,7 +73,7 @@ func likelyHomeRouterIPDarwinExec() (ret netaddr.IP, ok bool) {
return nil return nil
} }
ip, err := netaddr.ParseIP(string(mem.Append(nil, ipm))) ip, err := netaddr.ParseIP(string(mem.Append(nil, ipm)))
if err == nil && isPrivateIP(ip) { if err == nil && ip.IsPrivate() {
ret = ip ret = ip
// We've found what we're looking for. // We've found what we're looking for.
return errStopReadingNetstatTable return errStopReadingNetstatTable

@ -72,7 +72,7 @@ func likelyHomeRouterIPLinux() (ret netaddr.IP, ok bool) {
return nil // ignore error, skip line and keep going return nil // ignore error, skip line and keep going
} }
ip := netaddr.IPv4(byte(ipu32), byte(ipu32>>8), byte(ipu32>>16), byte(ipu32>>24)) ip := netaddr.IPv4(byte(ipu32), byte(ipu32>>8), byte(ipu32>>16), byte(ipu32>>24))
if isPrivateIP(ip) { if ip.IsPrivate() {
ret = ip ret = ip
} }
return nil return nil

@ -93,7 +93,7 @@ func likelyHomeRouterIPWindows() (ret netaddr.IP, ok bool) {
} }
} }
if !ret.IsZero() && !isPrivateIP(ret) { if !ret.IsZero() && !ret.IsPrivate() {
// Default route has a non-private gateway // Default route has a non-private gateway
return netaddr.IP{}, false return netaddr.IP{}, false
} }

Loading…
Cancel
Save