-
Notifications
You must be signed in to change notification settings - Fork 237
/
bootstrap.go
215 lines (180 loc) · 6.37 KB
/
bootstrap.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
package upstream
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/url"
"strings"
"sync"
"time"
"github.com/AdguardTeam/golibs/log"
"github.com/joomcode/errorx"
)
type bootstrapper struct {
address string // in form of "tls://one.one.one.one:853"
resolvers []*Resolver // list of Resolvers to use to resolve hostname, if necessary
timeout time.Duration // resolution duration (shared with the upstream) (0 == infinite timeout)
dialContext dialHandler // specifies the dial function for creating unencrypted TCP connections.
resolvedConfig *tls.Config
sync.RWMutex
}
// Resolver is wrapper for resolver and it's address
type Resolver struct {
resolver *net.Resolver // net.Resolver
resolverAddress string // Resolver's address
}
// NewResolver creates an instance of Resolver structure with defined net.Resolver and it's address
// resolverAddress is address of net.Resolver
// The host in the address parameter of Dial func will always be a literal IP address (from documentation)
func NewResolver(resolverAddress string, timeout time.Duration) *Resolver {
r := &Resolver{}
// set default net.Resolver as a resolver if resolverAddress is empty
if resolverAddress == "" {
r.resolver = &net.Resolver{}
return r
}
r.resolverAddress = resolverAddress
r.resolver = &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{Timeout: timeout}
return d.DialContext(ctx, network, resolverAddress)
},
}
return r
}
// LookupIPAddr returns result of LookupIPAddr method of Resolver's net.Resolver
func (r *Resolver) LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, error) {
return r.resolver.LookupIPAddr(ctx, host)
}
func toBoot(address string, bootstrapAddr []string, timeout time.Duration) bootstrapper {
resolvers := []*Resolver{}
if bootstrapAddr != nil && len(bootstrapAddr) != 0 {
for idx, adr := range bootstrapAddr {
_, _, err := net.SplitHostPort(adr)
if err != nil {
// Add the default port for bootstrap DNS address if no port is defined
adr = net.JoinHostPort(adr, "53")
bootstrapAddr[idx] = adr
}
}
// Create list of resolvers for parallel lookup
for _, boot := range bootstrapAddr {
r := NewResolver(boot, timeout)
resolvers = append(resolvers, r)
}
} else {
// nil resolver if the default one
resolvers = append(resolvers, NewResolver("", timeout))
}
return bootstrapper{
address: address,
resolvers: resolvers,
timeout: timeout,
}
}
// dialHandler specifies the dial function for creating unencrypted TCP connections.
type dialHandler func(ctx context.Context, network, addr string) (net.Conn, error)
// will get usable IP address from Address field, and caches the result
func (n *bootstrapper) get() (*tls.Config, dialHandler, error) {
n.RLock()
if n.dialContext != nil && n.resolvedConfig != nil { // fast path
tlsconfig, dialContext := n.resolvedConfig, n.dialContext
n.RUnlock()
return tlsconfig, dialContext, nil
}
//
// Slow path: resolve the IP address of the n.address's host
//
// get a host without port
host, port, err := n.getAddressHostPort()
if err != nil {
addr := n.address
n.RUnlock()
return nil, nil, fmt.Errorf("bootstrapper requires port in address %s", addr)
}
// if n.address's host is an IP, just use it right away
ip := net.ParseIP(host)
if ip != nil {
n.RUnlock()
// Upgrade lock to protect n.resolved
resolverAddress := net.JoinHostPort(host, port)
n.Lock()
dialContext := createDialContext([]string{resolverAddress}, n.timeout)
n.dialContext = dialContext
config := &tls.Config{ServerName: host}
n.resolvedConfig = config
n.Unlock()
return config, n.dialContext, nil
}
// Don't lock anymore (we can launch multiple lookup requests at a time)
// Otherwise, it might mess with the timeout specified for the Upstream
// See here: https://github.com/AdguardTeam/dnsproxy/issues/15
n.RUnlock()
//
// if it's a hostname
//
ctx, cancel := context.WithTimeout(context.TODO(), n.timeout)
defer cancel() // important to avoid a resource leak
addrs, err := LookupParallel(ctx, n.resolvers, host)
if err != nil {
return nil, nil, errorx.Decorate(err, "failed to lookup %s", host)
}
resolved := []string{}
for _, addr := range addrs {
if addr.IP.To4() == nil && addr.IP.To16() == nil {
continue
}
resolved = append(resolved, net.JoinHostPort(addr.String(), port))
}
if len(resolved) == 0 {
// couldn't find any suitable IP address
return nil, nil, fmt.Errorf("couldn't find any suitable IP address for host %s", host)
}
n.Lock()
defer n.Unlock()
dialContext := createDialContext(resolved, n.timeout)
n.dialContext = dialContext
n.resolvedConfig = &tls.Config{ServerName: host}
return n.resolvedConfig, n.dialContext, nil
}
// createDialContext returns dialContext function that tries to establish connection with all given addresses one by one
func createDialContext(addresses []string, timeout time.Duration) (dialContext dialHandler) {
dialer := &net.Dialer{
Timeout: timeout,
DualStack: true,
}
dialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
errs := []error{}
// Return first connection without error
// Note that we're using bootstrapped resolverAddress instead of what's passed to the function
for _, resolverAddress := range addresses {
log.Tracef("Dialing to %s", resolverAddress)
start := time.Now()
con, err := dialer.DialContext(ctx, network, resolverAddress)
elapsed := time.Since(start) / time.Millisecond
if err == nil {
log.Tracef("dialer successfully initialize connection to %s in %d milliseconds", resolverAddress, elapsed)
return con, err
}
errs = append(errs, err)
log.Tracef("dialer failed to initialize connection to %s, in %d milliseconds, cause: %s", resolverAddress, elapsed, err)
}
return nil, errorx.DecorateMany("all dialers failed to initialize connection: ", errs...)
}
return
}
func (n *bootstrapper) getAddressHostPort() (string, string, error) {
justHostPort := n.address
if strings.Contains(n.address, "://") {
parsedURL, err := url.Parse(n.address)
if err != nil {
return "", "", errorx.Decorate(err, "failed to parse %s", n.address)
}
justHostPort = parsedURL.Host
}
// convert host to IP if necessary, we know that it's scheme://hostname:port/
// get a host without port
return net.SplitHostPort(justHostPort)
}