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"
|
|
|
|
|
|
|
|
"tailscale.com/types/logger"
|
|
|
|
)
|
|
|
|
|
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-02 09:17:50 +00:00
|
|
|
impl OSConfigurator
|
2020-07-31 20:27:09 +00:00
|
|
|
|
2021-04-02 06:42:58 +00:00
|
|
|
config OSConfig
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewManagers created a new manager from the given config.
|
2021-04-02 06:26:52 +00:00
|
|
|
func NewManager(logf logger.Logf, interfaceName string) *Manager {
|
|
|
|
logf = logger.WithPrefix(logf, "dns: ")
|
2020-07-31 20:27:09 +00:00
|
|
|
m := &Manager{
|
2021-04-02 06:26:52 +00:00
|
|
|
logf: logf,
|
|
|
|
impl: newManager(logf, interfaceName),
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m.logf("using %T", m.impl)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2021-04-02 06:42:58 +00:00
|
|
|
func (m *Manager) Set(config OSConfig) error {
|
2020-07-31 20:27:09 +00:00
|
|
|
if config.Equal(m.config) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m.logf("Set: %+v", config)
|
|
|
|
|
|
|
|
if len(config.Nameservers) == 0 {
|
2021-04-02 09:17:50 +00:00
|
|
|
err := m.impl.Set(OSConfig{})
|
2020-07-31 20:27:09 +00:00
|
|
|
// If we save the config, we will not retry next time. Only do this on success.
|
|
|
|
if err == nil {
|
|
|
|
m.config = config
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-02 09:17:50 +00:00
|
|
|
err := m.impl.Set(config)
|
2020-07-31 20:27:09 +00:00
|
|
|
// If we save the config, we will not retry next time. Only do this on success.
|
|
|
|
if err == nil {
|
|
|
|
m.config = config
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Up() error {
|
2021-04-02 09:17:50 +00:00
|
|
|
return m.impl.Set(m.config)
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Down() error {
|
2021-04-02 09:17:50 +00:00
|
|
|
return m.impl.Close()
|
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-02 06:26:52 +00:00
|
|
|
dns := NewManager(logf, interfaceName)
|
2021-04-02 05:35:26 +00:00
|
|
|
if err := dns.Down(); err != nil {
|
|
|
|
logf("dns down: %v", err)
|
|
|
|
}
|
|
|
|
}
|