-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
resolver.go
257 lines (227 loc) · 7.02 KB
/
resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Package dns implements a working macOS DNS resolver (really just a `Dial`er) for projects that must cross-compile from Linux systems or just don't want CGO.
package dns
import (
"context"
"errors"
"fmt"
"net"
"runtime"
"sort"
"strings"
"sync"
"time"
"github.com/johnstarich/go/dns/scutil"
"github.com/johnstarich/go/dns/staggercast"
"go.uber.org/zap"
)
// Config contains options for configuring a DNS resolver.
// Currently has no effect on non-macOS runtimes.
type Config struct {
Logger *zap.Logger
InitialNameserverDelay time.Duration
NextNameserverInterval time.Duration
}
const macOSRuntimeName = "darwin"
// New returns a default DNS resolver for this runtime
func New() *net.Resolver {
return NewWithConfig(Config{})
}
// NewWithConfig returns a DNS resolver with the given config for this runtime
func NewWithConfig(config Config) *net.Resolver {
return newWithConfig(runtime.GOOS, config)
}
func newWithConfig(runtimeName string, config Config) *net.Resolver {
if runtimeName != macOSRuntimeName {
return &net.Resolver{}
}
dialer := newMacOSDialer(config)
return &net.Resolver{
PreferGo: true,
Dial: dialer.DialContext,
}
}
type macOSDialer struct {
Config
dialer *net.Dialer
nameservers []string
nameserversMu sync.RWMutex
readResolvers func(context.Context) (scutil.Config, error)
}
func newMacOSDialer(config Config) *macOSDialer {
const (
defaultInitialNameserverDelay = 150 * time.Millisecond
defaultNextNameserverInterval = 10 * time.Millisecond
maxTimeout = 30 * time.Second
)
if config.Logger == nil {
config.Logger = zap.NewNop()
}
if config.InitialNameserverDelay == 0 {
config.InitialNameserverDelay = defaultInitialNameserverDelay
}
if config.NextNameserverInterval == 0 {
config.NextNameserverInterval = defaultNextNameserverInterval
}
return &macOSDialer{
Config: config,
dialer: &net.Dialer{Timeout: maxTimeout},
readResolvers: scutil.ReadMacOSDNS,
}
}
func (m *macOSDialer) ensureNameservers() ([]string, error) {
m.nameserversMu.RLock()
nameservers := m.nameservers
m.nameserversMu.RUnlock()
if len(nameservers) != 0 {
return nameservers, nil
}
m.nameserversMu.Lock()
defer m.nameserversMu.Unlock()
if len(m.nameservers) != 0 { // check again, could change while waiting on lock
return m.nameservers, nil
}
const maxTimeout = 5 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), maxTimeout)
defer cancel()
m.Logger.Info("Reading macOS DNS config from 'scutil'...")
cfg, err := m.readResolvers(ctx)
for _, resolver := range cfg.Resolvers {
port := resolver.Port
if port == 0 {
port = 53
}
for _, nameserver := range resolver.Nameservers {
m.nameservers = append(m.nameservers, dialableNameserver(nameserver)+":"+fmt.Sprint(port))
}
}
m.Logger.Info("Finished reading macOS DNS config from 'scutil'", zap.Error(err))
return m.nameservers, err
}
func (m *macOSDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
nameservers, err := m.ensureNameservers()
if err != nil {
m.Logger.Error("Failed looking up macOS resolvers, falling back to builtin DNS dialer", zap.Error(err))
return m.dialer.DialContext(ctx, network, address)
}
conn, err := m.dialAll(ctx, nameservers)
if err != nil {
m.Logger.Error("Failed dialing macOS nameservers, falling back to builtin DNS dialer", zap.Error(err))
return m.dialer.DialContext(ctx, network, address)
}
return conn, nil
}
func (m *macOSDialer) dialAll(ctx context.Context, nameservers []string) (net.Conn, error) {
type dialResp struct {
ix int
conn net.Conn
}
conns := make(chan dialResp, len(nameservers))
var wait sync.WaitGroup
wait.Add(len(nameservers))
m.Logger.Debug("Dialing all nameservers")
for ix, nameserver := range nameservers {
go func(ix int, nameserver string) {
conn, err := m.dialer.DialContext(ctx, "udp", nameserver)
if err != nil {
m.Logger.Warn("Error dialing nameserver", zap.String("nameserver", nameserver), zap.Int("index", ix), zap.Error(err))
} else {
conns <- dialResp{ix: ix, conn: conn}
}
wait.Done()
}(ix, nameserver)
}
wait.Wait()
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
close(conns)
var allSuccessfulResps []dialResp
for resp := range conns {
allSuccessfulResps = append(allSuccessfulResps, resp)
}
if len(allSuccessfulResps) == 0 {
return nil, errors.New("error dialing all nameservers")
}
sort.Slice(allSuccessfulResps, func(a, b int) bool { // preserve original nameserver order
return allSuccessfulResps[a].ix < allSuccessfulResps[b].ix
})
var allSuccessfulConns []staggercast.PacketConn
for _, resp := range allSuccessfulResps {
allSuccessfulConns = append(allSuccessfulConns, resp.conn.(staggercast.PacketConn)) // UDP connections must also support net.PacketConn
}
staggerConn := staggercast.New(allSuccessfulConns)
staggerConn.Stagger(staggerTicker(m.InitialNameserverDelay, m.NextNameserverInterval, m.Logger))
go m.reorderNameservers(ctx, staggerConn)
return staggerConn, nil
}
func staggerTicker(initialDelay, d time.Duration, logger *zap.Logger) (<-chan struct{}, context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())
c := make(chan struct{})
go func() {
index := 1
timer := time.NewTimer(initialDelay)
select {
case <-timer.C:
logger.Debug("scatter: Enabling connection", zap.Int("index", index))
index++
c <- struct{}{}
case <-ctx.Done():
timer.Stop()
return
}
timer.Stop()
ticker := time.NewTicker(d)
for {
select {
case <-ctx.Done():
ticker.Stop()
close(c)
return
case <-ticker.C:
logger.Debug("scatter: Enabling connection", zap.Int("index", index))
index++
c <- struct{}{}
}
}
}()
return c, cancel
}
func (m *macOSDialer) reorderNameservers(ctx context.Context, conn *staggercast.Conn) {
var zero time.Time
if deadline, ok := ctx.Deadline(); !ok || deadline == zero {
m.Logger.Debug("Skipping nameserver reorder, no deadline on context")
return
}
<-ctx.Done()
stats := conn.Stats()
if stats.FastestRemoteIndex == 0 {
return
}
m.nameserversMu.Lock()
defer m.nameserversMu.Unlock()
fastestRemoteAddr := stats.FastestRemote.String()
fastIndexNS := m.nameservers[stats.FastestRemoteIndex]
if fastIndexNS != fastestRemoteAddr {
m.Logger.Debug("Fastest remote already reordered", zap.String("remote", stats.FastestRemote.String()), zap.String("nsIndex", fastIndexNS))
return
}
newNS := []string{fastIndexNS}
newNS = append(newNS, m.nameservers[:stats.FastestRemoteIndex]...)
newNS = append(newNS, m.nameservers[stats.FastestRemoteIndex+1:]...)
m.nameservers = newNS
m.Logger.Debug("Reordering fastest nameserver to the front", zap.Strings("nameservers", newNS))
}
func dialableNameserver(nameserver string) string {
ipStr := nameserver
if i := strings.IndexRune(nameserver, '%'); i != -1 {
// remove optional IPv6 zone ID suffix to parse IP
ipStr = nameserver[:i]
}
isIPv4 := net.ParseIP(ipStr).To4() != nil
if !isIPv4 {
return fmt.Sprintf("[%s]", nameserver)
}
return nameserver
}