discovery.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package allregions
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "fmt"
  6. "net"
  7. "time"
  8. "github.com/pkg/errors"
  9. "github.com/rs/zerolog"
  10. "github.com/cloudflare/cloudflared/management"
  11. )
  12. const (
  13. // Used to discover HA origintunneld servers
  14. srvService = "v2-origintunneld"
  15. srvProto = "tcp"
  16. srvName = "argotunnel.com"
  17. // Used to fallback to DoT when we can't use the default resolver to
  18. // discover HA origintunneld servers (GitHub issue #75).
  19. dotServerName = "cloudflare-dns.com"
  20. dotServerAddr = "1.1.1.1:853"
  21. dotTimeout = 15 * time.Second
  22. logFieldAddress = "address"
  23. )
  24. // Redeclare network functions so they can be overridden in tests.
  25. var (
  26. netLookupSRV = net.LookupSRV
  27. netLookupIP = net.LookupIP
  28. )
  29. // ConfigIPVersion is the selection of IP versions from config
  30. type ConfigIPVersion int8
  31. const (
  32. Auto ConfigIPVersion = 2
  33. IPv4Only ConfigIPVersion = 4
  34. IPv6Only ConfigIPVersion = 6
  35. )
  36. func (c ConfigIPVersion) String() string {
  37. switch c {
  38. case Auto:
  39. return "auto"
  40. case IPv4Only:
  41. return "4"
  42. case IPv6Only:
  43. return "6"
  44. default:
  45. return ""
  46. }
  47. }
  48. // IPVersion is the IP version of an EdgeAddr
  49. type EdgeIPVersion int8
  50. const (
  51. V4 EdgeIPVersion = 4
  52. V6 EdgeIPVersion = 6
  53. )
  54. // String returns the enum's constant name.
  55. func (c EdgeIPVersion) String() string {
  56. switch c {
  57. case V4:
  58. return "4"
  59. case V6:
  60. return "6"
  61. default:
  62. return ""
  63. }
  64. }
  65. // EdgeAddr is a representation of possible ways to refer an edge location.
  66. type EdgeAddr struct {
  67. TCP *net.TCPAddr
  68. UDP *net.UDPAddr
  69. IPVersion EdgeIPVersion
  70. }
  71. // If the call to net.LookupSRV fails, try to fall back to DoT from Cloudflare directly.
  72. //
  73. // Note: Instead of DoT, we could also have used DoH. Either of these:
  74. // - directly via the JSON API (https://1.1.1.1/dns-query?ct=application/dns-json&name=_origintunneld._tcp.argotunnel.com&type=srv)
  75. // - indirectly via `tunneldns.NewUpstreamHTTPS()`
  76. //
  77. // But both of these cases miss out on a key feature from the stdlib:
  78. //
  79. // "The returned records are sorted by priority and randomized by weight within a priority."
  80. // (https://golang.org/pkg/net/#Resolver.LookupSRV)
  81. //
  82. // Does this matter? I don't know. It may someday. Let's use DoT so we don't need to worry about it.
  83. // See also: Go feature request for stdlib-supported DoH: https://github.com/golang/go/issues/27552
  84. var fallbackLookupSRV = lookupSRVWithDOT
  85. var friendlyDNSErrorLines = []string{
  86. `Please try the following things to diagnose this issue:`,
  87. ` 1. ensure that argotunnel.com is returning "origintunneld" service records.`,
  88. ` Run your system's equivalent of: dig srv _origintunneld._tcp.argotunnel.com`,
  89. ` 2. ensure that your DNS resolver is not returning compressed SRV records.`,
  90. ` See GitHub issue https://github.com/golang/go/issues/27546`,
  91. ` For example, you could use Cloudflare's 1.1.1.1 as your resolver:`,
  92. ` https://developers.cloudflare.com/1.1.1.1/setting-up-1.1.1.1/`,
  93. }
  94. // EdgeDiscovery implements HA service discovery lookup.
  95. func edgeDiscovery(log *zerolog.Logger, srvService string) ([][]*EdgeAddr, error) {
  96. logger := log.With().Int(management.EventTypeKey, int(management.Cloudflared)).Logger()
  97. logger.Debug().
  98. Int(management.EventTypeKey, int(management.Cloudflared)).
  99. Str("domain", "_"+srvService+"._"+srvProto+"."+srvName).
  100. Msg("edge discovery: looking up edge SRV record")
  101. _, addrs, err := netLookupSRV(srvService, srvProto, srvName)
  102. if err != nil {
  103. _, fallbackAddrs, fallbackErr := fallbackLookupSRV(srvService, srvProto, srvName)
  104. if fallbackErr != nil || len(fallbackAddrs) == 0 {
  105. // use the original DNS error `err` in messages, not `fallbackErr`
  106. logger.Err(err).Msg("edge discovery: error looking up Cloudflare edge IPs: the DNS query failed")
  107. for _, s := range friendlyDNSErrorLines {
  108. logger.Error().Msg(s)
  109. }
  110. return nil, errors.Wrapf(err, "Could not lookup srv records on _%v._%v.%v", srvService, srvProto, srvName)
  111. }
  112. // Accept the fallback results and keep going
  113. addrs = fallbackAddrs
  114. }
  115. var resolvedAddrPerCNAME [][]*EdgeAddr
  116. for _, addr := range addrs {
  117. edgeAddrs, err := resolveSRV(addr)
  118. if err != nil {
  119. return nil, err
  120. }
  121. logAddrs := make([]string, len(edgeAddrs))
  122. for i, e := range edgeAddrs {
  123. logAddrs[i] = e.UDP.IP.String()
  124. }
  125. logger.Debug().
  126. Strs("addresses", logAddrs).
  127. Msg("edge discovery: resolved edge addresses")
  128. resolvedAddrPerCNAME = append(resolvedAddrPerCNAME, edgeAddrs)
  129. }
  130. return resolvedAddrPerCNAME, nil
  131. }
  132. func lookupSRVWithDOT(srvService string, srvProto string, srvName string) (cname string, addrs []*net.SRV, err error) {
  133. // Inspiration: https://github.com/artyom/dot/blob/master/dot.go
  134. r := &net.Resolver{
  135. PreferGo: true,
  136. Dial: func(ctx context.Context, _ string, _ string) (net.Conn, error) {
  137. var dialer net.Dialer
  138. conn, err := dialer.DialContext(ctx, "tcp", dotServerAddr)
  139. if err != nil {
  140. return nil, err
  141. }
  142. tlsConfig := &tls.Config{ServerName: dotServerName}
  143. return tls.Client(conn, tlsConfig), nil
  144. },
  145. }
  146. ctx, cancel := context.WithTimeout(context.Background(), dotTimeout)
  147. defer cancel()
  148. return r.LookupSRV(ctx, srvService, srvProto, srvName)
  149. }
  150. func resolveSRV(srv *net.SRV) ([]*EdgeAddr, error) {
  151. ips, err := netLookupIP(srv.Target)
  152. if err != nil {
  153. return nil, errors.Wrapf(err, "Couldn't resolve SRV record %v", srv)
  154. }
  155. if len(ips) == 0 {
  156. return nil, fmt.Errorf("SRV record %v had no IPs", srv)
  157. }
  158. addrs := make([]*EdgeAddr, len(ips))
  159. for i, ip := range ips {
  160. version := V6
  161. if ip.To4() != nil {
  162. version = V4
  163. }
  164. addrs[i] = &EdgeAddr{
  165. TCP: &net.TCPAddr{IP: ip, Port: int(srv.Port)},
  166. UDP: &net.UDPAddr{IP: ip, Port: int(srv.Port)},
  167. IPVersion: version,
  168. }
  169. }
  170. return addrs, nil
  171. }
  172. // ResolveAddrs resolves TCP address given a list of addresses. Address can be a hostname, however, it will return at most one
  173. // of the hostname's IP addresses.
  174. func ResolveAddrs(addrs []string, log *zerolog.Logger) (resolved []*EdgeAddr) {
  175. for _, addr := range addrs {
  176. tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
  177. if err != nil {
  178. log.Error().Int(management.EventTypeKey, int(management.Cloudflared)).
  179. Str(logFieldAddress, addr).Err(err).Msg("edge discovery: failed to resolve to TCP address")
  180. continue
  181. }
  182. udpAddr, err := net.ResolveUDPAddr("udp", addr)
  183. if err != nil {
  184. log.Error().Int(management.EventTypeKey, int(management.Cloudflared)).
  185. Str(logFieldAddress, addr).Err(err).Msg("edge discovery: failed to resolve to UDP address")
  186. continue
  187. }
  188. version := V6
  189. if udpAddr.IP.To4() != nil {
  190. version = V4
  191. }
  192. resolved = append(resolved, &EdgeAddr{
  193. TCP: tcpAddr,
  194. UDP: udpAddr,
  195. IPVersion: version,
  196. })
  197. }
  198. return
  199. }