|
| 1 | +package origins |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "net/netip" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/rs/zerolog" |
| 11 | + |
| 12 | + "github.com/cloudflare/cloudflared/ingress" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + // We need a DNS record: |
| 17 | + // 1. That will be around for as long as cloudflared is |
| 18 | + // 2. That Cloudflare controls: to allow us to make changes if needed |
| 19 | + // 3. That is an external record to a typical customer's network: enforcing that the DNS request go to the |
| 20 | + // local DNS resolver over any local /etc/host configurations setup. |
| 21 | + // 4. That cloudflared would normally query: ensuring that users with a positive security model for DNS queries |
| 22 | + // don't need to adjust anything. |
| 23 | + // |
| 24 | + // This hostname is one that used during the edge discovery process and as such satisfies the above constraints. |
| 25 | + defaultLookupHost = "region1.v2.argotunnel.com" |
| 26 | + defaultResolverPort uint16 = 53 |
| 27 | + |
| 28 | + // We want the refresh time to be short to accommodate DNS resolver changes locally, but not too frequent as to |
| 29 | + // shuffle the resolver if multiple are configured. |
| 30 | + refreshFreq = 5 * time.Minute |
| 31 | + refreshTimeout = 5 * time.Second |
| 32 | +) |
| 33 | + |
| 34 | +var ( |
| 35 | + // Virtual DNS service address |
| 36 | + VirtualDNSServiceAddr = netip.AddrPortFrom(netip.MustParseAddr("2606:4700:0cf1:2000:0000:0000:0000:0001"), 53) |
| 37 | + |
| 38 | + defaultResolverAddr = netip.AddrPortFrom(netip.MustParseAddr("127.0.0.1"), defaultResolverPort) |
| 39 | +) |
| 40 | + |
| 41 | +type netDial func(network string, address string) (net.Conn, error) |
| 42 | + |
| 43 | +// DNSResolverService will make DNS requests to the local DNS resolver via the Dial method. |
| 44 | +type DNSResolverService struct { |
| 45 | + address netip.AddrPort |
| 46 | + addressM sync.RWMutex |
| 47 | + |
| 48 | + dialer ingress.UDPOriginProxy |
| 49 | + resolver peekResolver |
| 50 | + logger *zerolog.Logger |
| 51 | +} |
| 52 | + |
| 53 | +func NewDNSResolver(logger *zerolog.Logger) *DNSResolverService { |
| 54 | + return &DNSResolverService{ |
| 55 | + address: defaultResolverAddr, |
| 56 | + dialer: ingress.DefaultUDPDialer, |
| 57 | + resolver: &resolver{dialFunc: net.Dial}, |
| 58 | + logger: logger, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func (s *DNSResolverService) DialUDP(_ netip.AddrPort) (net.Conn, error) { |
| 63 | + s.addressM.RLock() |
| 64 | + dest := s.address |
| 65 | + s.addressM.RUnlock() |
| 66 | + // The dialer ignores the provided address because the request will instead go to the local DNS resolver. |
| 67 | + return s.dialer.DialUDP(dest) |
| 68 | +} |
| 69 | + |
| 70 | +// StartRefreshLoop is a routine that is expected to run in the background to update the DNS local resolver if |
| 71 | +// adjusted while the cloudflared process is running. |
| 72 | +func (s *DNSResolverService) StartRefreshLoop(ctx context.Context) { |
| 73 | + // Call update first to load an address before handling traffic |
| 74 | + err := s.update(ctx) |
| 75 | + if err != nil { |
| 76 | + s.logger.Err(err).Msg("Failed to initialize DNS local resolver") |
| 77 | + } |
| 78 | + for { |
| 79 | + select { |
| 80 | + case <-ctx.Done(): |
| 81 | + return |
| 82 | + case <-time.Tick(refreshFreq): |
| 83 | + err := s.update(ctx) |
| 84 | + if err != nil { |
| 85 | + s.logger.Err(err).Msg("Failed to refresh DNS local resolver") |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func (s *DNSResolverService) update(ctx context.Context) error { |
| 92 | + ctx, cancel := context.WithTimeout(ctx, refreshTimeout) |
| 93 | + defer cancel() |
| 94 | + // Make a standard DNS request to a well-known DNS record that will last a long time |
| 95 | + _, err := s.resolver.lookupNetIP(ctx, defaultLookupHost) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + |
| 100 | + // Validate the address before updating internal reference |
| 101 | + _, address := s.resolver.addr() |
| 102 | + peekAddrPort, err := netip.ParseAddrPort(address) |
| 103 | + if err == nil { |
| 104 | + s.setAddress(peekAddrPort) |
| 105 | + return nil |
| 106 | + } |
| 107 | + // It's possible that the address didn't have an attached port, attempt to parse just the address and use |
| 108 | + // the default port 53 |
| 109 | + peekAddr, err := netip.ParseAddr(address) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + s.setAddress(netip.AddrPortFrom(peekAddr, defaultResolverPort)) |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +// lock and update the address used for the local DNS resolver |
| 118 | +func (s *DNSResolverService) setAddress(addr netip.AddrPort) { |
| 119 | + s.addressM.Lock() |
| 120 | + defer s.addressM.Unlock() |
| 121 | + if s.address != addr { |
| 122 | + s.logger.Debug().Msgf("Updating DNS local resolver: %s", addr) |
| 123 | + } |
| 124 | + s.address = addr |
| 125 | +} |
| 126 | + |
| 127 | +type peekResolver interface { |
| 128 | + addr() (network string, address string) |
| 129 | + lookupNetIP(ctx context.Context, host string) ([]netip.Addr, error) |
| 130 | +} |
| 131 | + |
| 132 | +// resolver is a shim that inspects the go runtime's DNS resolution process to capture the DNS resolver |
| 133 | +// address used to complete a DNS request. |
| 134 | +type resolver struct { |
| 135 | + network string |
| 136 | + address string |
| 137 | + dialFunc netDial |
| 138 | +} |
| 139 | + |
| 140 | +func (r *resolver) addr() (network string, address string) { |
| 141 | + return r.network, r.address |
| 142 | +} |
| 143 | + |
| 144 | +func (r *resolver) lookupNetIP(ctx context.Context, host string) ([]netip.Addr, error) { |
| 145 | + resolver := &net.Resolver{ |
| 146 | + PreferGo: true, |
| 147 | + // Use the peekDial to inspect the results of the DNS resolver used during the LookupIPAddr call. |
| 148 | + Dial: r.peekDial, |
| 149 | + } |
| 150 | + return resolver.LookupNetIP(ctx, "ip", host) |
| 151 | +} |
| 152 | + |
| 153 | +func (r *resolver) peekDial(ctx context.Context, network, address string) (net.Conn, error) { |
| 154 | + r.network = network |
| 155 | + r.address = address |
| 156 | + return r.dialFunc(network, address) |
| 157 | +} |
0 commit comments