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 (
|
|
|
|
"time"
|
|
|
|
|
2021-04-03 02:34:53 +00:00
|
|
|
"inet.af/netaddr"
|
|
|
|
"tailscale.com/net/dns/resolver"
|
|
|
|
"tailscale.com/net/tsaddr"
|
2020-07-31 20:27:09 +00:00
|
|
|
"tailscale.com/types/logger"
|
2021-04-03 02:34:53 +00:00
|
|
|
"tailscale.com/wgengine/monitor"
|
2020-07-31 20:27:09 +00:00
|
|
|
)
|
|
|
|
|
2020-09-11 18:00:39 +00:00
|
|
|
// We use file-ignore below instead of ignore because on some platforms,
|
|
|
|
// the lint exception is necessary and on others it is not,
|
|
|
|
// and plain ignore complains if the exception is unnecessary.
|
|
|
|
|
|
|
|
//lint:file-ignore U1000 reconfigTimeout is used on some platforms but not others
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
// reconfigTimeout is the time interval within which Manager.{Up,Down} should complete.
|
|
|
|
//
|
|
|
|
// This is particularly useful because certain conditions can cause indefinite hangs
|
|
|
|
// (such as improper dbus auth followed by contextless dbus.Object.Call).
|
|
|
|
// Such operations should be wrapped in a timeout context.
|
2020-09-08 23:03:49 +00:00
|
|
|
const reconfigTimeout = time.Second
|
2020-07-31 20:27:09 +00:00
|
|
|
|
|
|
|
// Manager manages system DNS settings.
|
|
|
|
type Manager struct {
|
|
|
|
logf logger.Logf
|
|
|
|
|
2021-04-03 02:34:53 +00:00
|
|
|
resolver *resolver.Resolver
|
|
|
|
os OSConfigurator
|
2020-07-31 20:27:09 +00:00
|
|
|
|
2021-04-03 02:34:53 +00:00
|
|
|
config Config
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewManagers created a new manager from the given config.
|
2021-04-03 02:34:53 +00:00
|
|
|
func NewManager(logf logger.Logf, oscfg OSConfigurator, linkMon *monitor.Mon) *Manager {
|
2021-04-02 06:26:52 +00:00
|
|
|
logf = logger.WithPrefix(logf, "dns: ")
|
2020-07-31 20:27:09 +00:00
|
|
|
m := &Manager{
|
2021-04-03 02:34:53 +00:00
|
|
|
logf: logf,
|
|
|
|
resolver: resolver.New(logf, linkMon),
|
|
|
|
os: oscfg,
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 02:40:13 +00:00
|
|
|
m.logf("using %T", m.os)
|
2020-07-31 20:27:09 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2021-04-03 02:34:53 +00:00
|
|
|
func (m *Manager) Set(cfg Config) error {
|
|
|
|
m.logf("Set: %+v", cfg)
|
2020-07-31 20:27:09 +00:00
|
|
|
|
2021-04-03 02:34:53 +00:00
|
|
|
if len(cfg.DefaultResolvers) == 0 {
|
|
|
|
// TODO: make other settings work even if you didn't set a
|
|
|
|
// default resolver. For now, no default resolvers == no
|
|
|
|
// managed DNS config.
|
|
|
|
cfg = Config{}
|
|
|
|
}
|
2020-07-31 20:27:09 +00:00
|
|
|
|
2021-04-03 02:34:53 +00:00
|
|
|
resolverCfg := resolver.Config{
|
|
|
|
Hosts: cfg.Hosts,
|
|
|
|
LocalDomains: cfg.AuthoritativeSuffixes,
|
|
|
|
Routes: map[string][]netaddr.IPPort{},
|
|
|
|
}
|
|
|
|
osCfg := OSConfig{
|
2021-04-06 22:21:32 +00:00
|
|
|
SearchDomains: cfg.SearchDomains,
|
2021-04-03 02:34:53 +00:00
|
|
|
}
|
|
|
|
// We must proxy through quad-100 if MagicDNS hosts are in
|
|
|
|
// use, or there are any per-domain routes.
|
|
|
|
mustProxy := len(cfg.Hosts) > 0 || len(cfg.Routes) > 0
|
|
|
|
if mustProxy {
|
|
|
|
osCfg.Nameservers = []netaddr.IP{tsaddr.TailscaleServiceIP()}
|
|
|
|
resolverCfg.Routes["."] = cfg.DefaultResolvers
|
|
|
|
for suffix, resolvers := range cfg.Routes {
|
|
|
|
resolverCfg.Routes[suffix] = resolvers
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, resolver := range cfg.DefaultResolvers {
|
|
|
|
osCfg.Nameservers = append(osCfg.Nameservers, resolver.IP)
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 02:34:53 +00:00
|
|
|
if err := m.resolver.SetConfig(resolverCfg); err != nil {
|
|
|
|
return err
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
2021-04-03 03:15:54 +00:00
|
|
|
if err := m.os.SetDNS(osCfg); err != nil {
|
2021-04-03 02:34:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-31 20:27:09 +00:00
|
|
|
|
2021-04-03 02:34:53 +00:00
|
|
|
func (m *Manager) EnqueueRequest(bs []byte, from netaddr.IPPort) error {
|
|
|
|
return m.resolver.EnqueueRequest(bs, from)
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 02:34:53 +00:00
|
|
|
func (m *Manager) NextResponse() ([]byte, netaddr.IPPort, error) {
|
|
|
|
return m.resolver.NextResponse()
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Down() error {
|
2021-04-03 02:34:53 +00:00
|
|
|
if err := m.os.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.resolver.Close()
|
|
|
|
return nil
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
2021-04-02 05:35:26 +00:00
|
|
|
|
|
|
|
// Cleanup restores the system DNS configuration to its original state
|
|
|
|
// in case the Tailscale daemon terminated without closing the router.
|
|
|
|
// No other state needs to be instantiated before this runs.
|
|
|
|
func Cleanup(logf logger.Logf, interfaceName string) {
|
2021-04-03 02:24:02 +00:00
|
|
|
oscfg := NewOSConfigurator(logf, interfaceName)
|
2021-04-03 02:34:53 +00:00
|
|
|
dns := NewManager(logf, oscfg, nil)
|
2021-04-02 05:35:26 +00:00
|
|
|
if err := dns.Down(); err != nil {
|
|
|
|
logf("dns down: %v", err)
|
|
|
|
}
|
|
|
|
}
|