2020-03-05 18:29:19 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-07-26 18:58:28 +00:00
|
|
|
// TODO(bradfitz): update this code to use netaddr more
|
|
|
|
|
2020-03-05 18:29:19 +00:00
|
|
|
// Package dnscache contains a minimal DNS cache that makes a bunch of
|
|
|
|
// assumptions that are only valid for us. Not recommended for general use.
|
|
|
|
package dnscache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-26 20:49:54 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
2020-03-05 18:29:19 +00:00
|
|
|
"fmt"
|
2020-11-11 20:37:53 +00:00
|
|
|
"log"
|
2020-03-05 18:29:19 +00:00
|
|
|
"net"
|
2020-03-09 18:19:29 +00:00
|
|
|
"runtime"
|
2020-03-05 18:29:19 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/sync/singleflight"
|
2021-02-26 20:49:54 +00:00
|
|
|
"inet.af/netaddr"
|
2022-01-24 18:52:57 +00:00
|
|
|
"tailscale.com/envknob"
|
2020-03-05 18:29:19 +00:00
|
|
|
)
|
|
|
|
|
2020-03-10 04:04:08 +00:00
|
|
|
var single = &Resolver{
|
|
|
|
Forward: &net.Resolver{PreferGo: preferGoResolver()},
|
|
|
|
}
|
2020-03-09 18:19:29 +00:00
|
|
|
|
2020-03-10 04:04:08 +00:00
|
|
|
func preferGoResolver() bool {
|
2020-03-09 18:19:29 +00:00
|
|
|
// There does not appear to be a local resolver running
|
|
|
|
// on iOS, and NetworkExtension is good at isolating DNS.
|
|
|
|
// So do not use the Go resolver on macOS/iOS.
|
2020-11-11 17:04:34 +00:00
|
|
|
if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
|
2020-03-10 04:04:08 +00:00
|
|
|
return false
|
2020-03-09 18:19:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 18:24:53 +00:00
|
|
|
// The local resolver is not available on Android.
|
|
|
|
if runtime.GOOS == "android" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-03-10 04:04:08 +00:00
|
|
|
// Otherwise, the Go resolver is fine and slightly preferred
|
|
|
|
// since it's lighter, not using cgo calls & threads.
|
|
|
|
return true
|
|
|
|
}
|
2020-03-05 18:29:19 +00:00
|
|
|
|
|
|
|
// Get returns a caching Resolver singleton.
|
|
|
|
func Get() *Resolver { return single }
|
|
|
|
|
|
|
|
// Resolver is a minimal DNS caching resolver.
|
|
|
|
//
|
|
|
|
// The TTL is always fixed for now. It's not intended for general use.
|
|
|
|
// Cache entries are never cleaned up so it's intended that this is
|
|
|
|
// only used with a fixed set of hostnames.
|
|
|
|
type Resolver struct {
|
|
|
|
// Forward is the resolver to use to populate the cache.
|
|
|
|
// If nil, net.DefaultResolver is used.
|
|
|
|
Forward *net.Resolver
|
|
|
|
|
2021-02-26 20:49:54 +00:00
|
|
|
// LookupIPFallback optionally provides a backup DNS mechanism
|
|
|
|
// to use if Forward returns an error or no results.
|
|
|
|
LookupIPFallback func(ctx context.Context, host string) ([]netaddr.IP, error)
|
|
|
|
|
2020-11-11 20:37:53 +00:00
|
|
|
// TTL is how long to keep entries cached
|
|
|
|
//
|
|
|
|
// If zero, a default (currently 10 minutes) is used.
|
|
|
|
TTL time.Duration
|
|
|
|
|
|
|
|
// UseLastGood controls whether a cached entry older than TTL is used
|
|
|
|
// if a refresh fails.
|
|
|
|
UseLastGood bool
|
|
|
|
|
2020-03-05 18:29:19 +00:00
|
|
|
sf singleflight.Group
|
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
ipCache map[string]ipCacheEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
type ipCacheEntry struct {
|
2021-07-26 18:58:28 +00:00
|
|
|
ip net.IP // either v4 or v6
|
|
|
|
ip6 net.IP // nil if no v4 or no v6
|
|
|
|
allIPs []net.IPAddr // 1+ v4 and/or v6
|
2020-03-05 18:29:19 +00:00
|
|
|
expires time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resolver) fwd() *net.Resolver {
|
|
|
|
if r.Forward != nil {
|
|
|
|
return r.Forward
|
|
|
|
}
|
|
|
|
return net.DefaultResolver
|
|
|
|
}
|
|
|
|
|
2020-11-11 20:37:53 +00:00
|
|
|
func (r *Resolver) ttl() time.Duration {
|
|
|
|
if r.TTL > 0 {
|
|
|
|
return r.TTL
|
|
|
|
}
|
|
|
|
return 10 * time.Minute
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:52:57 +00:00
|
|
|
var debug = envknob.Bool("TS_DEBUG_DNS_CACHE")
|
2020-11-11 20:37:53 +00:00
|
|
|
|
2021-01-07 03:50:19 +00:00
|
|
|
// LookupIP returns the host's primary IP address (either IPv4 or
|
|
|
|
// IPv6, but preferring IPv4) and optionally its IPv6 address, if
|
|
|
|
// there is both IPv4 and IPv6.
|
|
|
|
//
|
|
|
|
// If err is nil, ip will be non-nil. The v6 address may be nil even
|
|
|
|
// with a nil error.
|
2021-07-26 18:58:28 +00:00
|
|
|
func (r *Resolver) LookupIP(ctx context.Context, host string) (ip, v6 net.IP, allIPs []net.IPAddr, err error) {
|
2020-03-05 18:29:19 +00:00
|
|
|
if ip := net.ParseIP(host); ip != nil {
|
|
|
|
if ip4 := ip.To4(); ip4 != nil {
|
2021-07-26 18:58:28 +00:00
|
|
|
return ip4, nil, []net.IPAddr{{IP: ip4}}, nil
|
2020-03-05 18:29:19 +00:00
|
|
|
}
|
2020-11-11 20:37:53 +00:00
|
|
|
if debug {
|
|
|
|
log.Printf("dnscache: %q is an IP", host)
|
|
|
|
}
|
2021-07-26 18:58:28 +00:00
|
|
|
return ip, nil, []net.IPAddr{{IP: ip}}, nil
|
2020-03-05 18:29:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 18:58:28 +00:00
|
|
|
if ip, ip6, allIPs, ok := r.lookupIPCache(host); ok {
|
2020-11-11 20:37:53 +00:00
|
|
|
if debug {
|
|
|
|
log.Printf("dnscache: %q = %v (cached)", host, ip)
|
|
|
|
}
|
2021-07-26 18:58:28 +00:00
|
|
|
return ip, ip6, allIPs, nil
|
2020-03-05 18:29:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 18:58:28 +00:00
|
|
|
type ipRes struct {
|
2021-01-07 03:50:19 +00:00
|
|
|
ip, ip6 net.IP
|
2021-07-26 18:58:28 +00:00
|
|
|
allIPs []net.IPAddr
|
2021-01-07 03:50:19 +00:00
|
|
|
}
|
2020-03-05 18:29:19 +00:00
|
|
|
ch := r.sf.DoChan(host, func() (interface{}, error) {
|
2021-07-26 18:58:28 +00:00
|
|
|
ip, ip6, allIPs, err := r.lookupIP(host)
|
2020-03-05 18:29:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-26 18:58:28 +00:00
|
|
|
return ipRes{ip, ip6, allIPs}, nil
|
2020-03-05 18:29:19 +00:00
|
|
|
})
|
|
|
|
select {
|
|
|
|
case res := <-ch:
|
|
|
|
if res.Err != nil {
|
2020-11-11 20:37:53 +00:00
|
|
|
if r.UseLastGood {
|
2021-07-26 18:58:28 +00:00
|
|
|
if ip, ip6, allIPs, ok := r.lookupIPCacheExpired(host); ok {
|
2020-11-11 20:37:53 +00:00
|
|
|
if debug {
|
|
|
|
log.Printf("dnscache: %q using %v after error", host, ip)
|
|
|
|
}
|
2021-07-26 18:58:28 +00:00
|
|
|
return ip, ip6, allIPs, nil
|
2020-11-11 20:37:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if debug {
|
|
|
|
log.Printf("dnscache: error resolving %q: %v", host, res.Err)
|
|
|
|
}
|
2021-07-26 18:58:28 +00:00
|
|
|
return nil, nil, nil, res.Err
|
2020-03-05 18:29:19 +00:00
|
|
|
}
|
2021-07-26 18:58:28 +00:00
|
|
|
r := res.Val.(ipRes)
|
|
|
|
return r.ip, r.ip6, r.allIPs, nil
|
2020-03-05 18:29:19 +00:00
|
|
|
case <-ctx.Done():
|
2020-11-11 20:37:53 +00:00
|
|
|
if debug {
|
|
|
|
log.Printf("dnscache: context done while resolving %q: %v", host, ctx.Err())
|
|
|
|
}
|
2021-07-26 18:58:28 +00:00
|
|
|
return nil, nil, nil, ctx.Err()
|
2020-03-05 18:29:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 18:58:28 +00:00
|
|
|
func (r *Resolver) lookupIPCache(host string) (ip, ip6 net.IP, allIPs []net.IPAddr, ok bool) {
|
2020-03-05 18:29:19 +00:00
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
if ent, ok := r.ipCache[host]; ok && ent.expires.After(time.Now()) {
|
2021-07-26 18:58:28 +00:00
|
|
|
return ent.ip, ent.ip6, ent.allIPs, true
|
2020-03-05 18:29:19 +00:00
|
|
|
}
|
2021-07-26 18:58:28 +00:00
|
|
|
return nil, nil, nil, false
|
2020-03-05 18:29:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 18:58:28 +00:00
|
|
|
func (r *Resolver) lookupIPCacheExpired(host string) (ip, ip6 net.IP, allIPs []net.IPAddr, ok bool) {
|
2020-11-11 20:37:53 +00:00
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
if ent, ok := r.ipCache[host]; ok {
|
2021-07-26 18:58:28 +00:00
|
|
|
return ent.ip, ent.ip6, ent.allIPs, true
|
2020-11-11 20:37:53 +00:00
|
|
|
}
|
2021-07-26 18:58:28 +00:00
|
|
|
return nil, nil, nil, false
|
2020-11-11 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resolver) lookupTimeoutForHost(host string) time.Duration {
|
|
|
|
if r.UseLastGood {
|
2021-07-26 18:58:28 +00:00
|
|
|
if _, _, _, ok := r.lookupIPCacheExpired(host); ok {
|
2020-11-11 20:37:53 +00:00
|
|
|
// If we have some previous good value for this host,
|
|
|
|
// don't give this DNS lookup much time. If we're in a
|
|
|
|
// situation where the user's DNS server is unreachable
|
|
|
|
// (e.g. their corp DNS server is behind a subnet router
|
|
|
|
// that can't come up due to Tailscale needing to
|
|
|
|
// connect to itself), then we want to fail fast and let
|
|
|
|
// our caller (who set UseLastGood) fall back to using
|
|
|
|
// the last-known-good IP address.
|
|
|
|
return 3 * time.Second
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 10 * time.Second
|
|
|
|
}
|
|
|
|
|
2021-07-26 18:58:28 +00:00
|
|
|
func (r *Resolver) lookupIP(host string) (ip, ip6 net.IP, allIPs []net.IPAddr, err error) {
|
|
|
|
if ip, ip6, allIPs, ok := r.lookupIPCache(host); ok {
|
2020-11-11 20:37:53 +00:00
|
|
|
if debug {
|
|
|
|
log.Printf("dnscache: %q found in cache as %v", host, ip)
|
|
|
|
}
|
2021-07-26 18:58:28 +00:00
|
|
|
return ip, ip6, allIPs, nil
|
2020-03-05 18:29:19 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 20:37:53 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), r.lookupTimeoutForHost(host))
|
2020-03-05 18:29:19 +00:00
|
|
|
defer cancel()
|
|
|
|
ips, err := r.fwd().LookupIPAddr(ctx, host)
|
2021-02-26 20:49:54 +00:00
|
|
|
if (err != nil || len(ips) == 0) && r.LookupIPFallback != nil {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
var fips []netaddr.IP
|
|
|
|
fips, err = r.LookupIPFallback(ctx, host)
|
|
|
|
if err == nil {
|
|
|
|
ips = nil
|
|
|
|
for _, fip := range fips {
|
|
|
|
ips = append(ips, *fip.IPAddr())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-05 18:29:19 +00:00
|
|
|
if err != nil {
|
2021-07-26 18:58:28 +00:00
|
|
|
return nil, nil, nil, err
|
2020-03-05 18:29:19 +00:00
|
|
|
}
|
|
|
|
if len(ips) == 0 {
|
2021-07-26 18:58:28 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("no IPs for %q found", host)
|
2020-03-05 18:29:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 03:50:19 +00:00
|
|
|
have4 := false
|
2020-03-05 18:29:19 +00:00
|
|
|
for _, ipa := range ips {
|
|
|
|
if ip4 := ipa.IP.To4(); ip4 != nil {
|
2021-01-07 03:50:19 +00:00
|
|
|
if !have4 {
|
|
|
|
ip6 = ip
|
|
|
|
ip = ip4
|
|
|
|
have4 = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if have4 {
|
|
|
|
ip6 = ipa.IP
|
|
|
|
} else {
|
|
|
|
ip = ipa.IP
|
|
|
|
}
|
2020-03-05 18:29:19 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-26 18:58:28 +00:00
|
|
|
r.addIPCache(host, ip, ip6, ips, r.ttl())
|
|
|
|
return ip, ip6, ips, nil
|
2020-03-05 18:29:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 18:58:28 +00:00
|
|
|
func (r *Resolver) addIPCache(host string, ip, ip6 net.IP, allIPs []net.IPAddr, d time.Duration) {
|
2021-07-27 02:51:18 +00:00
|
|
|
if naIP, _ := netaddr.FromStdIP(ip); naIP.IsPrivate() {
|
2020-03-05 18:29:19 +00:00
|
|
|
// Don't cache obviously wrong entries from captive portals.
|
|
|
|
// TODO: use DoH or DoT for the forwarding resolver?
|
2020-11-11 20:37:53 +00:00
|
|
|
if debug {
|
|
|
|
log.Printf("dnscache: %q resolved to private IP %v; using but not caching", host, ip)
|
|
|
|
}
|
2021-01-07 03:50:19 +00:00
|
|
|
return
|
2020-03-05 18:29:19 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 20:37:53 +00:00
|
|
|
if debug {
|
|
|
|
log.Printf("dnscache: %q resolved to IP %v; caching", host, ip)
|
|
|
|
}
|
|
|
|
|
2020-03-05 18:29:19 +00:00
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
if r.ipCache == nil {
|
|
|
|
r.ipCache = make(map[string]ipCacheEntry)
|
|
|
|
}
|
2021-07-26 18:58:28 +00:00
|
|
|
r.ipCache[host] = ipCacheEntry{
|
|
|
|
ip: ip,
|
|
|
|
ip6: ip6,
|
|
|
|
allIPs: allIPs,
|
|
|
|
expires: time.Now().Add(d),
|
|
|
|
}
|
2020-03-05 18:29:19 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 20:37:53 +00:00
|
|
|
type DialContextFunc func(ctx context.Context, network, address string) (net.Conn, error)
|
|
|
|
|
|
|
|
// Dialer returns a wrapped DialContext func that uses the provided dnsCache.
|
|
|
|
func Dialer(fwd DialContextFunc, dnsCache *Resolver) DialContextFunc {
|
2021-02-26 20:49:54 +00:00
|
|
|
return func(ctx context.Context, network, address string) (retConn net.Conn, ret error) {
|
2020-11-11 20:37:53 +00:00
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
// Bogus. But just let the real dialer return an error rather than
|
|
|
|
// inventing a similar one.
|
|
|
|
return fwd(ctx, network, address)
|
|
|
|
}
|
2021-02-26 20:49:54 +00:00
|
|
|
defer func() {
|
|
|
|
// On any failure, assume our DNS is wrong and try our fallback, if any.
|
|
|
|
if ret == nil || dnsCache.LookupIPFallback == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ips, err := dnsCache.LookupIPFallback(ctx, host)
|
|
|
|
if err != nil {
|
|
|
|
// Return with original error
|
|
|
|
return
|
|
|
|
}
|
2021-07-26 21:36:21 +00:00
|
|
|
if c, err := raceDial(ctx, fwd, network, ips, port); err == nil {
|
|
|
|
retConn = c
|
|
|
|
ret = nil
|
|
|
|
return
|
2021-02-26 20:49:54 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-07-26 21:36:21 +00:00
|
|
|
ip, ip6, allIPs, err := dnsCache.LookupIP(ctx, host)
|
2020-11-11 20:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to resolve %q: %w", host, err)
|
|
|
|
}
|
2021-07-26 21:36:21 +00:00
|
|
|
i4s := v4addrs(allIPs)
|
|
|
|
if len(i4s) < 2 {
|
|
|
|
dst := net.JoinHostPort(ip.String(), port)
|
|
|
|
if debug {
|
|
|
|
log.Printf("dnscache: dialing %s, %s for %s", network, dst, address)
|
|
|
|
}
|
|
|
|
c, err := fwd(ctx, network, dst)
|
|
|
|
if err == nil || ctx.Err() != nil || ip6 == nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
// Fall back to trying IPv6.
|
|
|
|
dst = net.JoinHostPort(ip6.String(), port)
|
|
|
|
return fwd(ctx, network, dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Multiple IPv4 candidates, and 0+ IPv6.
|
|
|
|
ipsToTry := append(i4s, v6addrs(allIPs)...)
|
|
|
|
return raceDial(ctx, fwd, network, ipsToTry, port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fallbackDelay is how long to wait between trying subsequent
|
|
|
|
// addresses when multiple options are available.
|
|
|
|
// 300ms is the same as Go's Happy Eyeballs fallbackDelay value.
|
|
|
|
const fallbackDelay = 300 * time.Millisecond
|
|
|
|
|
|
|
|
// raceDial tries to dial port on each ip in ips, starting a new race
|
2021-07-26 23:16:08 +00:00
|
|
|
// dial every fallbackDelay apart, returning whichever completes first.
|
2021-07-26 21:36:21 +00:00
|
|
|
func raceDial(ctx context.Context, fwd DialContextFunc, network string, ips []netaddr.IP, port string) (net.Conn, error) {
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
type res struct {
|
|
|
|
c net.Conn
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
resc := make(chan res) // must be unbuffered
|
|
|
|
failBoost := make(chan struct{}) // best effort send on dial failure
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for i, ip := range ips {
|
|
|
|
if i != 0 {
|
|
|
|
timer := time.NewTimer(fallbackDelay)
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
case <-failBoost:
|
|
|
|
timer.Stop()
|
|
|
|
case <-ctx.Done():
|
|
|
|
timer.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go func(ip netaddr.IP) {
|
|
|
|
c, err := fwd(ctx, network, net.JoinHostPort(ip.String(), port))
|
|
|
|
if err != nil {
|
|
|
|
// Best effort wake-up a pending dial.
|
|
|
|
// e.g. IPv4 dials failing quickly on an IPv6-only system.
|
|
|
|
// In that case we don't want to wait 300ms per IPv4 before
|
|
|
|
// we get to the IPv6 addresses.
|
|
|
|
select {
|
|
|
|
case failBoost <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case resc <- res{c, err}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
if c != nil {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(ip)
|
2020-11-11 20:37:53 +00:00
|
|
|
}
|
2021-07-26 21:36:21 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
var firstErr error
|
|
|
|
var fails int
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case r := <-resc:
|
|
|
|
if r.c != nil {
|
|
|
|
return r.c, nil
|
|
|
|
}
|
|
|
|
fails++
|
|
|
|
if firstErr == nil {
|
|
|
|
firstErr = r.err
|
|
|
|
}
|
|
|
|
if fails == len(ips) {
|
|
|
|
return nil, firstErr
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func v4addrs(aa []net.IPAddr) (ret []netaddr.IP) {
|
|
|
|
for _, a := range aa {
|
|
|
|
if ip, ok := netaddr.FromStdIP(a.IP); ok && ip.Is4() {
|
|
|
|
ret = append(ret, ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func v6addrs(aa []net.IPAddr) (ret []netaddr.IP) {
|
|
|
|
for _, a := range aa {
|
|
|
|
if ip, ok := netaddr.FromStdIP(a.IP); ok && ip.Is6() {
|
|
|
|
ret = append(ret, ip)
|
2021-01-07 03:50:19 +00:00
|
|
|
}
|
2020-11-11 20:37:53 +00:00
|
|
|
}
|
2021-07-26 21:36:21 +00:00
|
|
|
return ret
|
2020-11-11 20:37:53 +00:00
|
|
|
}
|
2021-02-26 20:49:54 +00:00
|
|
|
|
|
|
|
var errTLSHandshakeTimeout = errors.New("timeout doing TLS handshake")
|
|
|
|
|
|
|
|
// TLSDialer is like Dialer but returns a func suitable for using with net/http.Transport.DialTLSContext.
|
|
|
|
// It returns a *tls.Conn type on success.
|
|
|
|
// On TLS cert validation failure, it can invoke a backup DNS resolution strategy.
|
|
|
|
func TLSDialer(fwd DialContextFunc, dnsCache *Resolver, tlsConfigBase *tls.Config) DialContextFunc {
|
|
|
|
tcpDialer := Dialer(fwd, dnsCache)
|
|
|
|
return func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
|
host, _, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tcpConn, err := tcpDialer(ctx, network, address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := cloneTLSConfig(tlsConfigBase)
|
|
|
|
if cfg.ServerName == "" {
|
|
|
|
cfg.ServerName = host
|
|
|
|
}
|
|
|
|
tlsConn := tls.Client(tcpConn, cfg)
|
|
|
|
|
|
|
|
handshakeCtx, handshakeTimeoutCancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
defer handshakeTimeoutCancel()
|
2022-01-13 21:01:29 +00:00
|
|
|
if err := tlsConn.HandshakeContext(handshakeCtx); err != nil {
|
2021-02-26 20:49:54 +00:00
|
|
|
tcpConn.Close()
|
|
|
|
// TODO: if err != errTLSHandshakeTimeout,
|
|
|
|
// assume it might be some captive portal or
|
|
|
|
// otherwise incorrect DNS and try the backup
|
|
|
|
// DNS mechanism.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return tlsConn, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cloneTLSConfig(cfg *tls.Config) *tls.Config {
|
|
|
|
if cfg == nil {
|
|
|
|
return &tls.Config{}
|
|
|
|
}
|
|
|
|
return cfg.Clone()
|
|
|
|
}
|