net/dns: replace managerImpl with OSConfigurator in code.

Signed-off-by: David Anderson <danderson@tailscale.com>
pull/1652/head
David Anderson 4 years ago
parent 077d4dc8c7
commit befd8e4e68

@ -110,12 +110,11 @@ func isResolvedRunning() bool {
// or as cleanup if the program terminates unexpectedly.
type directManager struct{}
func newDirectManager() managerImpl {
func newDirectManager() directManager {
return directManager{}
}
// Up implements managerImpl.
func (m directManager) Up(config OSConfig) error {
func (m directManager) Set(config OSConfig) error {
// Write the tsConf file.
buf := new(bytes.Buffer)
writeResolvConf(buf, config.Nameservers, config.Domains)
@ -160,8 +159,11 @@ func (m directManager) Up(config OSConfig) error {
return nil
}
// Down implements managerImpl.
func (m directManager) Down() error {
func (m directManager) RoutingMode() RoutingMode {
return RoutingModeNone
}
func (m directManager) Close() error {
if _, err := os.Stat(backupConf); err != nil {
// If the backup file does not exist, then Up never ran successfully.
if os.IsNotExist(err) {

@ -23,19 +23,11 @@ import (
// Such operations should be wrapped in a timeout context.
const reconfigTimeout = time.Second
type managerImpl interface {
// Up updates system DNS settings to match the given configuration.
Up(OSConfig) error
// Down undoes the effects of Up.
// It is idempotent and performs no action if Up has never been called.
Down() error
}
// Manager manages system DNS settings.
type Manager struct {
logf logger.Logf
impl managerImpl
impl OSConfigurator
config OSConfig
}
@ -60,7 +52,7 @@ func (m *Manager) Set(config OSConfig) error {
m.logf("Set: %+v", config)
if len(config.Nameservers) == 0 {
err := m.impl.Down()
err := m.impl.Set(OSConfig{})
// If we save the config, we will not retry next time. Only do this on success.
if err == nil {
m.config = config
@ -68,7 +60,7 @@ func (m *Manager) Set(config OSConfig) error {
return err
}
err := m.impl.Up(config)
err := m.impl.Set(config)
// If we save the config, we will not retry next time. Only do this on success.
if err == nil {
m.config = config
@ -78,11 +70,11 @@ func (m *Manager) Set(config OSConfig) error {
}
func (m *Manager) Up() error {
return m.impl.Up(m.config)
return m.impl.Set(m.config)
}
func (m *Manager) Down() error {
return m.impl.Down()
return m.impl.Close()
}
// Cleanup restores the system DNS configuration to its original state

@ -8,7 +8,7 @@ package dns
import "tailscale.com/types/logger"
func newManager(logger.Logf, string) managerImpl {
func newManager(logger.Logf, string) OSConfigurator {
// TODO(dmytro): on darwin, we should use a macOS-specific method such as scutil.
// This is currently not implemented. Editing /etc/resolv.conf does not work,
// as most applications use the system resolver, which disregards it.

@ -6,7 +6,7 @@ package dns
import "tailscale.com/types/logger"
func newManager(logf logger.Logf, _ string) managerImpl {
func newManager(logf logger.Logf, _ string) OSConfigurator {
switch {
case isResolvconfActive():
return newResolvconfManager(logf)

@ -6,7 +6,7 @@ package dns
import "tailscale.com/types/logger"
func newManager(logf logger.Logf, interfaceName string) managerImpl {
func newManager(logf logger.Logf, interfaceName string) OSConfigurator {
switch {
case isResolvedActive():
return newResolvedManager()

@ -6,6 +6,6 @@ package dns
import "tailscale.com/types/logger"
func newManager(logger.Logf, string) managerImpl {
func newManager(logger.Logf, string) OSConfigurator {
return newDirectManager()
}

@ -25,7 +25,7 @@ type windowsManager struct {
guid string
}
func newManager(logf logger.Logf, interfaceName string) managerImpl {
func newManager(logf logger.Logf, interfaceName string) OSConfigurator {
return windowsManager{
logf: logf,
guid: interfaceName,
@ -64,7 +64,7 @@ func (m windowsManager) setDomains(basePath string, domains []string) error {
return setRegistryString(path, "SearchList", value)
}
func (m windowsManager) Up(config OSConfig) error {
func (m windowsManager) Set(config OSConfig) error {
var ipsv4 []string
var ipsv6 []string
@ -113,6 +113,10 @@ func (m windowsManager) Up(config OSConfig) error {
return nil
}
func (m windowsManager) Down() error {
return m.Up(OSConfig{Nameservers: nil, Domains: nil})
func (m windowsManager) RoutingMode() RoutingMode {
return RoutingModeNone
}
func (m windowsManager) Close() error {
return m.Set(OSConfig{})
}

@ -53,7 +53,7 @@ type nmManager struct {
interfaceName string
}
func newNMManager(interfaceName string) managerImpl {
func newNMManager(interfaceName string) nmManager {
return nmManager{
interfaceName: interfaceName,
}
@ -61,8 +61,7 @@ func newNMManager(interfaceName string) managerImpl {
type nmConnectionSettings map[string]map[string]dbus.Variant
// Up implements managerImpl.
func (m nmManager) Up(config OSConfig) error {
func (m nmManager) Set(config OSConfig) error {
ctx, cancel := context.WithTimeout(context.Background(), reconfigTimeout)
defer cancel()
@ -199,7 +198,8 @@ func (m nmManager) Up(config OSConfig) error {
return nil
}
// Down implements managerImpl.
func (m nmManager) Down() error {
return m.Up(OSConfig{Nameservers: nil, Domains: nil})
func (m nmManager) RoutingMode() RoutingMode { return RoutingModeNone }
func (m nmManager) Close() error {
return m.Set(OSConfig{})
}

@ -8,12 +8,10 @@ package dns
type noopManager struct{}
// Up implements managerImpl.
func (m noopManager) Up(OSConfig) error { return nil }
func (m noopManager) Set(OSConfig) error { return nil }
func (m noopManager) RoutingMode() RoutingMode { return RoutingModeNone }
func (m noopManager) Close() error { return nil }
// Down implements managerImpl.
func (m noopManager) Down() error { return nil }
func newNoopManager() managerImpl {
func newNoopManager() noopManager {
return noopManager{}
}

@ -27,7 +27,7 @@ type OSConfigurator interface {
// If cfg nil or the zero value, all Tailscale-related DNS
// configuration is removed.
// Set must not be called after Close.
Set(cfg *OSConfig) error
Set(cfg OSConfig) error
// DNSRoutingMode reports the DNS routing capabilities of this OS
// configurator.
RoutingMode() RoutingMode

@ -101,7 +101,7 @@ type resolvconfManager struct {
impl resolvconfImpl
}
func newResolvconfManager(logf logger.Logf) managerImpl {
func newResolvconfManager(logf logger.Logf) resolvconfManager {
impl := getResolvconfImpl()
logf("resolvconf implementation is %s", impl)
@ -115,8 +115,7 @@ func newResolvconfManager(logf logger.Logf) managerImpl {
// when running resolvconfLegacy, hopefully placing our config first.
const resolvconfConfigName = "tun-tailscale.inet"
// Up implements managerImpl.
func (m resolvconfManager) Up(config OSConfig) error {
func (m resolvconfManager) Set(config OSConfig) error {
stdin := new(bytes.Buffer)
writeResolvConf(stdin, config.Nameservers, config.Domains) // dns_direct.go
@ -139,8 +138,11 @@ func (m resolvconfManager) Up(config OSConfig) error {
return nil
}
// Down implements managerImpl.
func (m resolvconfManager) Down() error {
func (m resolvconfManager) RoutingMode() RoutingMode {
return RoutingModeNone
}
func (m resolvconfManager) Close() error {
var cmd *exec.Cmd
switch m.impl {
case resolvconfOpenresolv:

@ -77,12 +77,12 @@ func isResolvedActive() bool {
// resolvedManager uses the systemd-resolved DBus API.
type resolvedManager struct{}
func newResolvedManager() managerImpl {
func newResolvedManager() resolvedManager {
return resolvedManager{}
}
// Up implements managerImpl.
func (m resolvedManager) Up(config OSConfig) error {
func (m resolvedManager) Set(config OSConfig) error {
ctx, cancel := context.WithTimeout(context.Background(), reconfigTimeout)
defer cancel()
@ -151,8 +151,11 @@ func (m resolvedManager) Up(config OSConfig) error {
return nil
}
// Down implements managerImpl.
func (m resolvedManager) Down() error {
func (m resolvedManager) RoutingMode() RoutingMode {
return RoutingModeNone
}
func (m resolvedManager) Close() error {
ctx, cancel := context.WithTimeout(context.Background(), reconfigTimeout)
defer cancel()

Loading…
Cancel
Save