Skip to content

Commit 0860c7a

Browse files
committed
net/dnscache: use Go DNS resolver on Windows
Go 1.19 introduced Resolver.PreferGo which allows the Go resolver to be used on Windows. See golang/go#33097. Fixes tailscale#5161 Signed-off-by: Thomas Way <[email protected]>
1 parent 6f521c1 commit 0860c7a

File tree

2 files changed

+77
-10
lines changed

2 files changed

+77
-10
lines changed

net/dnscache/dnscache.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ import (
3131
var zaddr netip.Addr
3232

3333
var single = &Resolver{
34-
Forward: &net.Resolver{PreferGo: preferGoResolver()},
34+
Forward: &net.Resolver{PreferGo: preferGoResolver(runtime.GOOS)},
3535
}
3636

37-
func preferGoResolver() bool {
37+
func preferGoResolver(goos string) bool {
3838
// There does not appear to be a local resolver running
3939
// on iOS, and NetworkExtension is good at isolating DNS.
4040
// So do not use the Go resolver on macOS/iOS.
41-
if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
41+
if goos == "darwin" || goos == "ios" {
4242
return false
4343
}
4444

4545
// The local resolver is not available on Android.
46-
if runtime.GOOS == "android" {
46+
if goos == "android" {
4747
return false
4848
}
4949

@@ -140,12 +140,7 @@ func (r *Resolver) dlogf(format string, args ...any) {
140140
// cloudHostResolver returns a Resolver for the current cloud hosting environment.
141141
// It currently only supports Google Cloud.
142142
func (r *Resolver) cloudHostResolver() (v *net.Resolver, ok bool) {
143-
switch runtime.GOOS {
144-
case "android", "ios", "darwin":
145-
return nil, false
146-
case "windows":
147-
// TODO(bradfitz): remove this restriction once we're using Go 1.19
148-
// which supports net.Resolver.PreferGo on Windows.
143+
if !preferGoResolver(runtime.GOOS) {
149144
return nil, false
150145
}
151146
ip := cloudenv.Get().ResolverIP()

net/dnscache/dnscache_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,78 @@ func TestDialer(t *testing.T) {
3737
c.Close()
3838
}
3939

40+
func TestPreferGoResolver(t *testing.T) {
41+
// List of all known GOOS values as of Go 1.20.
42+
//
43+
// https://github.com/golang/go/blob/go1.20.3/src/go/build/syslist.go#L14-L33
44+
testCases := []struct {
45+
goos string
46+
preferred bool
47+
}{{
48+
goos: "aix",
49+
preferred: true,
50+
}, {
51+
goos: "android",
52+
preferred: false,
53+
}, {
54+
goos: "darwin",
55+
preferred: false,
56+
}, {
57+
goos: "dragonfly",
58+
preferred: true,
59+
}, {
60+
goos: "freebsd",
61+
preferred: true,
62+
}, {
63+
goos: "hurd",
64+
preferred: true,
65+
}, {
66+
goos: "illumos",
67+
preferred: true,
68+
}, {
69+
goos: "ios",
70+
preferred: false,
71+
}, {
72+
goos: "js",
73+
preferred: true,
74+
}, {
75+
goos: "linux",
76+
preferred: true,
77+
}, {
78+
goos: "nacl",
79+
preferred: true,
80+
}, {
81+
goos: "netbsd",
82+
preferred: true,
83+
}, {
84+
goos: "openbsd",
85+
preferred: true,
86+
}, {
87+
goos: "plan9",
88+
preferred: true,
89+
}, {
90+
goos: "solaris",
91+
preferred: true,
92+
}, {
93+
goos: "wasip1",
94+
preferred: true,
95+
}, {
96+
goos: "windows",
97+
preferred: true,
98+
}, {
99+
goos: "zos",
100+
preferred: true,
101+
}}
102+
for _, tc := range testCases {
103+
t.Run(tc.goos, func(t *testing.T) {
104+
preferred := preferGoResolver(tc.goos)
105+
if preferred != tc.preferred {
106+
t.Fatalf("%s: got %t, want %t", tc.goos, preferred, tc.preferred)
107+
}
108+
})
109+
}
110+
}
111+
40112
func TestDialCall_DNSWasTrustworthy(t *testing.T) {
41113
type step struct {
42114
ip netip.Addr // IP we pretended to dial

0 commit comments

Comments
 (0)