diff --git a/go.mod b/go.mod index 6655a0437..5959c5cca 100644 --- a/go.mod +++ b/go.mod @@ -46,7 +46,7 @@ require ( golang.zx2c4.com/wireguard v0.0.0-20210624150102-15b24b6179e0 golang.zx2c4.com/wireguard/windows v0.3.16 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/peercred v0.0.0-20210318190834-4259e17bb763 inet.af/wf v0.0.0-20210516214145-a5343001b756 diff --git a/go.sum b/go.sum index f1ca9668d..8c4c60bcf 100644 --- a/go.sum +++ b/go.sum @@ -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-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-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/go.mod h1:fG3G1dekmK8oDX3iVzt8c0zICLMLSN8SjdxbXVt0WjU= inet.af/peercred v0.0.0-20210318190834-4259e17bb763 h1:gPSJmmVzmdy4kHhlCMx912GdiUz3k/RzJGg0ADqy1dg= diff --git a/net/dnscache/dnscache.go b/net/dnscache/dnscache.go index 6b010d4a7..df17bc510 100644 --- a/net/dnscache/dnscache.go +++ b/net/dnscache/dnscache.go @@ -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) { - if isPrivateIP(ip) { + if naIP, _ := netaddr.FromStdIP(ip); naIP.IsPrivate() { // Don't cache obviously wrong entries from captive portals. // TODO: use DoH or DoT for the forwarding resolver? 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) // Dialer returns a wrapped DialContext func that uses the provided dnsCache. diff --git a/net/dnscache/dnscache_test.go b/net/dnscache/dnscache_test.go index 10d986da7..096049ccf 100644 --- a/net/dnscache/dnscache_test.go +++ b/net/dnscache/dnscache_test.go @@ -14,24 +14,6 @@ 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) { if *dialTest == "" { t.Skip("skipping; --dial-test is blank") diff --git a/net/interfaces/interfaces.go b/net/interfaces/interfaces.go index 405c531f7..ce385f57c 100644 --- a/net/interfaces/interfaces.go +++ b/net/interfaces/interfaces.go @@ -479,7 +479,7 @@ func HTTPOfListener(ln net.Listener) string { var privateIP string ForeachInterfaceAddress(func(i Interface, pfx netaddr.IPPrefix) { ip := pfx.IP() - if isPrivateIP(ip) { + if ip.IsPrivate() { if privateIP == "" { privateIP = ip.String() } @@ -519,21 +519,15 @@ func LikelyHomeRouterIP() (gateway, myIP netaddr.IP, ok bool) { if !i.IsUp() || ip.IsZero() || !myIP.IsZero() { return } - for _, prefix := range privatev4s { - if prefix.Contains(gateway) && prefix.Contains(ip) { - myIP = ip - ok = true - return - } + if gateway.IsPrivate() && ip.IsPrivate() { + myIP = ip + ok = true + return } }) 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 // conceivably be used to get Internet connectivity. Globally routable and // 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)) } -func mustCIDR(s string) netaddr.IPPrefix { - prefix, err := netaddr.ParseIPPrefix(s) - if err != nil { - panic(err) - } - return prefix -} - var ( - private1 = mustCIDR("10.0.0.0/8") - private2 = mustCIDR("172.16.0.0/12") - private3 = mustCIDR("192.168.0.0/16") - privatev4s = []netaddr.IPPrefix{private1, private2, private3} - v6Global1 = mustCIDR("2000::/3") + v6Global1 = netaddr.MustParseIPPrefix("2000::/3") ) // anyInterestingIP reports whether pfxs contains any IP that matches diff --git a/net/interfaces/interfaces_darwin_test.go b/net/interfaces/interfaces_darwin_test.go index c82c5f47c..3a3903c00 100644 --- a/net/interfaces/interfaces_darwin_test.go +++ b/net/interfaces/interfaces_darwin_test.go @@ -73,7 +73,7 @@ func likelyHomeRouterIPDarwinExec() (ret netaddr.IP, ok bool) { return nil } ip, err := netaddr.ParseIP(string(mem.Append(nil, ipm))) - if err == nil && isPrivateIP(ip) { + if err == nil && ip.IsPrivate() { ret = ip // We've found what we're looking for. return errStopReadingNetstatTable diff --git a/net/interfaces/interfaces_linux.go b/net/interfaces/interfaces_linux.go index dd6d05100..50dee351f 100644 --- a/net/interfaces/interfaces_linux.go +++ b/net/interfaces/interfaces_linux.go @@ -72,7 +72,7 @@ func likelyHomeRouterIPLinux() (ret netaddr.IP, ok bool) { return nil // ignore error, skip line and keep going } ip := netaddr.IPv4(byte(ipu32), byte(ipu32>>8), byte(ipu32>>16), byte(ipu32>>24)) - if isPrivateIP(ip) { + if ip.IsPrivate() { ret = ip } return nil diff --git a/net/interfaces/interfaces_windows.go b/net/interfaces/interfaces_windows.go index 68eae6848..61927b716 100644 --- a/net/interfaces/interfaces_windows.go +++ b/net/interfaces/interfaces_windows.go @@ -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 return netaddr.IP{}, false }