2020-07-31 20:27:09 +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.
|
|
|
|
|
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
2021-07-20 05:24:43 +00:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2021-04-07 07:54:54 +00:00
|
|
|
"sort"
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
"inet.af/netaddr"
|
2021-07-20 05:24:43 +00:00
|
|
|
"tailscale.com/net/dns/resolver"
|
2021-04-09 22:24:47 +00:00
|
|
|
"tailscale.com/util/dnsname"
|
2020-07-31 20:27:09 +00:00
|
|
|
)
|
|
|
|
|
2021-04-02 07:34:03 +00:00
|
|
|
// Config is a DNS configuration.
|
|
|
|
type Config struct {
|
|
|
|
// DefaultResolvers are the DNS resolvers to use for DNS names
|
|
|
|
// which aren't covered by more specific per-domain routes below.
|
|
|
|
// If empty, the OS's default resolvers (the ones that predate
|
|
|
|
// Tailscale altering the configuration) are used.
|
|
|
|
DefaultResolvers []netaddr.IPPort
|
|
|
|
// Routes maps a DNS suffix to the resolvers that should be used
|
|
|
|
// for queries that fall within that suffix.
|
|
|
|
// If a query doesn't match any entry in Routes, the
|
|
|
|
// DefaultResolvers are used.
|
2021-05-17 22:50:34 +00:00
|
|
|
// A Routes entry with no resolvers means the route should be
|
|
|
|
// authoritatively answered using the contents of Hosts.
|
2021-04-09 22:24:47 +00:00
|
|
|
Routes map[dnsname.FQDN][]netaddr.IPPort
|
2021-04-02 07:34:03 +00:00
|
|
|
// SearchDomains are DNS suffixes to try when expanding
|
|
|
|
// single-label queries.
|
2021-04-09 22:24:47 +00:00
|
|
|
SearchDomains []dnsname.FQDN
|
2021-04-02 07:34:03 +00:00
|
|
|
// Hosts maps DNS FQDNs to their IPs, which can be a mix of IPv4
|
|
|
|
// and IPv6.
|
2021-05-17 22:18:25 +00:00
|
|
|
// Queries matching entries in Hosts are resolved locally by
|
|
|
|
// 100.100.100.100 without leaving the machine.
|
|
|
|
// Adding an entry to Hosts merely creates the record. If you want
|
|
|
|
// it to resolve, you also need to add appropriate routes to
|
|
|
|
// Routes.
|
2021-04-09 22:24:47 +00:00
|
|
|
Hosts map[dnsname.FQDN][]netaddr.IP
|
2021-04-02 07:34:03 +00:00
|
|
|
}
|
2021-04-07 05:00:59 +00:00
|
|
|
|
2021-07-20 05:24:43 +00:00
|
|
|
// WriteToBufioWriter write a debug version of c for logs to w, omitting
|
|
|
|
// spammy stuff like *.arpa entries and replacing it with a total count.
|
|
|
|
func (c *Config) WriteToBufioWriter(w *bufio.Writer) {
|
|
|
|
w.WriteString("{DefaultResolvers:")
|
|
|
|
resolver.WriteIPPorts(w, c.DefaultResolvers)
|
|
|
|
|
|
|
|
w.WriteString(" Routes:")
|
|
|
|
resolver.WriteRoutes(w, c.Routes)
|
|
|
|
|
|
|
|
fmt.Fprintf(w, " SearchDomains:%v", c.SearchDomains)
|
|
|
|
fmt.Fprintf(w, " Hosts:%v", len(c.Hosts))
|
|
|
|
w.WriteString("}")
|
|
|
|
}
|
|
|
|
|
2021-04-07 05:00:59 +00:00
|
|
|
// needsAnyResolvers reports whether c requires a resolver to be set
|
|
|
|
// at the OS level.
|
|
|
|
func (c Config) needsOSResolver() bool {
|
2021-05-17 22:18:25 +00:00
|
|
|
return c.hasDefaultResolvers() || c.hasRoutes()
|
2021-04-07 05:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) hasRoutes() bool {
|
|
|
|
return len(c.Routes) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// hasDefaultResolversOnly reports whether the only resolvers in c are
|
|
|
|
// DefaultResolvers.
|
|
|
|
func (c Config) hasDefaultResolversOnly() bool {
|
2021-05-17 22:18:25 +00:00
|
|
|
return c.hasDefaultResolvers() && !c.hasRoutes()
|
2021-04-07 05:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) hasDefaultResolvers() bool {
|
|
|
|
return len(c.DefaultResolvers) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// singleResolverSet returns the resolvers used by c.Routes if all
|
|
|
|
// routes use the same resolvers, or nil if multiple sets of resolvers
|
|
|
|
// are specified.
|
|
|
|
func (c Config) singleResolverSet() []netaddr.IPPort {
|
2021-05-17 22:50:34 +00:00
|
|
|
var (
|
|
|
|
prev []netaddr.IPPort
|
|
|
|
prevInitialized bool
|
|
|
|
)
|
2021-04-07 05:00:59 +00:00
|
|
|
for _, resolvers := range c.Routes {
|
2021-05-17 22:50:34 +00:00
|
|
|
if !prevInitialized {
|
|
|
|
prev = resolvers
|
|
|
|
prevInitialized = true
|
2021-04-07 05:00:59 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-05-17 22:50:34 +00:00
|
|
|
if !sameIPPorts(prev, resolvers) {
|
2021-04-07 05:00:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2021-05-17 22:50:34 +00:00
|
|
|
return prev
|
2021-04-07 05:00:59 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 22:18:25 +00:00
|
|
|
// matchDomains returns the list of match suffixes needed by Routes.
|
2021-04-09 22:24:47 +00:00
|
|
|
func (c Config) matchDomains() []dnsname.FQDN {
|
2021-05-17 22:18:25 +00:00
|
|
|
ret := make([]dnsname.FQDN, 0, len(c.Routes))
|
2021-04-07 05:00:59 +00:00
|
|
|
for suffix := range c.Routes {
|
2021-04-09 22:24:47 +00:00
|
|
|
ret = append(ret, suffix)
|
2021-04-07 05:00:59 +00:00
|
|
|
}
|
2021-04-09 22:24:47 +00:00
|
|
|
sort.Slice(ret, func(i, j int) bool {
|
|
|
|
return ret[i].WithTrailingDot() < ret[j].WithTrailingDot()
|
|
|
|
})
|
2021-04-07 05:00:59 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func sameIPPorts(a, b []netaddr.IPPort) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range a {
|
|
|
|
if a[i] != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|