2020-02-06 00:55:26 +00:00
|
|
|
package allregions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/rs/zerolog"
|
2020-02-06 00:55:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Used to discover HA origintunneld servers
|
2022-05-20 21:51:36 +00:00
|
|
|
srvService = "v2-origintunneld"
|
2020-02-06 00:55:26 +00:00
|
|
|
srvProto = "tcp"
|
|
|
|
srvName = "argotunnel.com"
|
|
|
|
|
|
|
|
// Used to fallback to DoT when we can't use the default resolver to
|
|
|
|
// discover HA origintunneld servers (GitHub issue #75).
|
|
|
|
dotServerName = "cloudflare-dns.com"
|
|
|
|
dotServerAddr = "1.1.1.1:853"
|
2020-12-28 18:10:01 +00:00
|
|
|
dotTimeout = 15 * time.Second
|
2021-08-17 18:07:40 +00:00
|
|
|
|
|
|
|
logFieldAddress = "address"
|
2020-02-06 00:55:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Redeclare network functions so they can be overridden in tests.
|
|
|
|
var (
|
|
|
|
netLookupSRV = net.LookupSRV
|
|
|
|
netLookupIP = net.LookupIP
|
|
|
|
)
|
|
|
|
|
2022-06-02 17:57:37 +00:00
|
|
|
// ConfigIPVersion is the selection of IP versions from config
|
|
|
|
type ConfigIPVersion int8
|
|
|
|
|
|
|
|
const (
|
|
|
|
Auto ConfigIPVersion = 2
|
|
|
|
IPv4Only ConfigIPVersion = 4
|
|
|
|
IPv6Only ConfigIPVersion = 6
|
|
|
|
)
|
|
|
|
|
|
|
|
// IPVersion is the IP version of an EdgeAddr
|
|
|
|
type EdgeIPVersion int8
|
|
|
|
|
|
|
|
const (
|
|
|
|
V4 EdgeIPVersion = 4
|
|
|
|
V6 EdgeIPVersion = 6
|
|
|
|
)
|
|
|
|
|
|
|
|
// String returns the enum's constant name.
|
|
|
|
func (c EdgeIPVersion) String() string {
|
|
|
|
switch c {
|
|
|
|
case V4:
|
|
|
|
return "4"
|
|
|
|
case V6:
|
|
|
|
return "6"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 13:31:22 +00:00
|
|
|
// EdgeAddr is a representation of possible ways to refer an edge location.
|
|
|
|
type EdgeAddr struct {
|
2022-06-02 17:57:37 +00:00
|
|
|
TCP *net.TCPAddr
|
|
|
|
UDP *net.UDPAddr
|
|
|
|
IPVersion EdgeIPVersion
|
2021-08-06 13:31:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 00:55:26 +00:00
|
|
|
// If the call to net.LookupSRV fails, try to fall back to DoT from Cloudflare directly.
|
|
|
|
//
|
|
|
|
// Note: Instead of DoT, we could also have used DoH. Either of these:
|
2022-11-04 23:39:42 +00:00
|
|
|
// - directly via the JSON API (https://1.1.1.1/dns-query?ct=application/dns-json&name=_origintunneld._tcp.argotunnel.com&type=srv)
|
|
|
|
// - indirectly via `tunneldns.NewUpstreamHTTPS()`
|
|
|
|
//
|
2020-02-06 00:55:26 +00:00
|
|
|
// But both of these cases miss out on a key feature from the stdlib:
|
2022-11-04 23:39:42 +00:00
|
|
|
//
|
|
|
|
// "The returned records are sorted by priority and randomized by weight within a priority."
|
|
|
|
// (https://golang.org/pkg/net/#Resolver.LookupSRV)
|
|
|
|
//
|
2020-02-06 00:55:26 +00:00
|
|
|
// Does this matter? I don't know. It may someday. Let's use DoT so we don't need to worry about it.
|
|
|
|
// See also: Go feature request for stdlib-supported DoH: https://github.com/golang/go/issues/27552
|
|
|
|
var fallbackLookupSRV = lookupSRVWithDOT
|
|
|
|
|
|
|
|
var friendlyDNSErrorLines = []string{
|
|
|
|
`Please try the following things to diagnose this issue:`,
|
|
|
|
` 1. ensure that argotunnel.com is returning "origintunneld" service records.`,
|
|
|
|
` Run your system's equivalent of: dig srv _origintunneld._tcp.argotunnel.com`,
|
|
|
|
` 2. ensure that your DNS resolver is not returning compressed SRV records.`,
|
|
|
|
` See GitHub issue https://github.com/golang/go/issues/27546`,
|
|
|
|
` For example, you could use Cloudflare's 1.1.1.1 as your resolver:`,
|
|
|
|
` https://developers.cloudflare.com/1.1.1.1/setting-up-1.1.1.1/`,
|
|
|
|
}
|
|
|
|
|
|
|
|
// EdgeDiscovery implements HA service discovery lookup.
|
2021-08-17 18:07:40 +00:00
|
|
|
func edgeDiscovery(log *zerolog.Logger, srvService string) ([][]*EdgeAddr, error) {
|
|
|
|
log.Debug().Str("domain", "_"+srvService+"._"+srvProto+"."+srvName).Msg("looking up edge SRV record")
|
|
|
|
|
2020-02-06 00:55:26 +00:00
|
|
|
_, addrs, err := netLookupSRV(srvService, srvProto, srvName)
|
|
|
|
if err != nil {
|
|
|
|
_, fallbackAddrs, fallbackErr := fallbackLookupSRV(srvService, srvProto, srvName)
|
|
|
|
if fallbackErr != nil || len(fallbackAddrs) == 0 {
|
|
|
|
// use the original DNS error `err` in messages, not `fallbackErr`
|
2020-12-28 18:10:01 +00:00
|
|
|
log.Err(err).Msg("Error looking up Cloudflare edge IPs: the DNS query failed")
|
2020-02-06 00:55:26 +00:00
|
|
|
for _, s := range friendlyDNSErrorLines {
|
2020-11-25 06:55:13 +00:00
|
|
|
log.Error().Msg(s)
|
2020-02-06 00:55:26 +00:00
|
|
|
}
|
|
|
|
return nil, errors.Wrapf(err, "Could not lookup srv records on _%v._%v.%v", srvService, srvProto, srvName)
|
|
|
|
}
|
|
|
|
// Accept the fallback results and keep going
|
|
|
|
addrs = fallbackAddrs
|
|
|
|
}
|
|
|
|
|
2021-08-06 13:31:22 +00:00
|
|
|
var resolvedAddrPerCNAME [][]*EdgeAddr
|
2020-02-06 00:55:26 +00:00
|
|
|
for _, addr := range addrs {
|
2021-08-06 13:31:22 +00:00
|
|
|
edgeAddrs, err := resolveSRV(addr)
|
2020-02-06 00:55:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-20 21:51:36 +00:00
|
|
|
for _, e := range edgeAddrs {
|
|
|
|
log.Debug().Msgf("Edge Address: %+v", *e)
|
|
|
|
}
|
2021-08-06 13:31:22 +00:00
|
|
|
resolvedAddrPerCNAME = append(resolvedAddrPerCNAME, edgeAddrs)
|
2020-02-06 00:55:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 13:31:22 +00:00
|
|
|
return resolvedAddrPerCNAME, nil
|
2020-02-06 00:55:26 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 03:16:21 +00:00
|
|
|
func lookupSRVWithDOT(srvService string, srvProto string, srvName string) (cname string, addrs []*net.SRV, err error) {
|
2020-02-06 00:55:26 +00:00
|
|
|
// Inspiration: https://github.com/artyom/dot/blob/master/dot.go
|
|
|
|
r := &net.Resolver{
|
|
|
|
PreferGo: true,
|
|
|
|
Dial: func(ctx context.Context, _ string, _ string) (net.Conn, error) {
|
|
|
|
var dialer net.Dialer
|
|
|
|
conn, err := dialer.DialContext(ctx, "tcp", dotServerAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tlsConfig := &tls.Config{ServerName: dotServerName}
|
|
|
|
return tls.Client(conn, tlsConfig), nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), dotTimeout)
|
|
|
|
defer cancel()
|
|
|
|
return r.LookupSRV(ctx, srvService, srvProto, srvName)
|
|
|
|
}
|
|
|
|
|
2021-08-06 13:31:22 +00:00
|
|
|
func resolveSRV(srv *net.SRV) ([]*EdgeAddr, error) {
|
2020-02-06 00:55:26 +00:00
|
|
|
ips, err := netLookupIP(srv.Target)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "Couldn't resolve SRV record %v", srv)
|
|
|
|
}
|
|
|
|
if len(ips) == 0 {
|
|
|
|
return nil, fmt.Errorf("SRV record %v had no IPs", srv)
|
|
|
|
}
|
2021-08-06 13:31:22 +00:00
|
|
|
addrs := make([]*EdgeAddr, len(ips))
|
2020-02-06 00:55:26 +00:00
|
|
|
for i, ip := range ips {
|
2022-06-02 17:57:37 +00:00
|
|
|
version := V6
|
|
|
|
if ip.To4() != nil {
|
|
|
|
version = V4
|
|
|
|
}
|
2021-08-06 13:31:22 +00:00
|
|
|
addrs[i] = &EdgeAddr{
|
2022-06-02 17:57:37 +00:00
|
|
|
TCP: &net.TCPAddr{IP: ip, Port: int(srv.Port)},
|
|
|
|
UDP: &net.UDPAddr{IP: ip, Port: int(srv.Port)},
|
|
|
|
IPVersion: version,
|
2021-08-06 13:31:22 +00:00
|
|
|
}
|
2020-02-06 00:55:26 +00:00
|
|
|
}
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveAddrs resolves TCP address given a list of addresses. Address can be a hostname, however, it will return at most one
|
2020-10-30 16:23:26 +00:00
|
|
|
// of the hostname's IP addresses.
|
2021-08-06 13:31:22 +00:00
|
|
|
func ResolveAddrs(addrs []string, log *zerolog.Logger) (resolved []*EdgeAddr) {
|
2020-02-06 00:55:26 +00:00
|
|
|
for _, addr := range addrs {
|
|
|
|
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
|
|
|
|
if err != nil {
|
2021-08-17 18:07:40 +00:00
|
|
|
log.Error().Str(logFieldAddress, addr).Err(err).Msg("failed to resolve to TCP address")
|
2021-08-06 13:31:22 +00:00
|
|
|
continue
|
2020-02-06 00:55:26 +00:00
|
|
|
}
|
2021-08-06 13:31:22 +00:00
|
|
|
|
|
|
|
udpAddr, err := net.ResolveUDPAddr("udp", addr)
|
|
|
|
if err != nil {
|
2021-08-17 18:07:40 +00:00
|
|
|
log.Error().Str(logFieldAddress, addr).Err(err).Msg("failed to resolve to UDP address")
|
2021-08-06 13:31:22 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-06-02 17:57:37 +00:00
|
|
|
version := V6
|
|
|
|
if udpAddr.IP.To4() != nil {
|
|
|
|
version = V4
|
|
|
|
}
|
2021-08-06 13:31:22 +00:00
|
|
|
resolved = append(resolved, &EdgeAddr{
|
2022-06-02 17:57:37 +00:00
|
|
|
TCP: tcpAddr,
|
|
|
|
UDP: udpAddr,
|
|
|
|
IPVersion: version,
|
2021-08-06 13:31:22 +00:00
|
|
|
})
|
2020-02-06 00:55:26 +00:00
|
|
|
}
|
2020-10-30 16:23:26 +00:00
|
|
|
return
|
2020-02-06 00:55:26 +00:00
|
|
|
}
|