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

@ -23,19 +23,11 @@ import (
// Such operations should be wrapped in a timeout context. // Such operations should be wrapped in a timeout context.
const reconfigTimeout = time.Second 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. // Manager manages system DNS settings.
type Manager struct { type Manager struct {
logf logger.Logf logf logger.Logf
impl managerImpl impl OSConfigurator
config OSConfig config OSConfig
} }
@ -60,7 +52,7 @@ func (m *Manager) Set(config OSConfig) error {
m.logf("Set: %+v", config) m.logf("Set: %+v", config)
if len(config.Nameservers) == 0 { 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 we save the config, we will not retry next time. Only do this on success.
if err == nil { if err == nil {
m.config = config m.config = config
@ -68,7 +60,7 @@ func (m *Manager) Set(config OSConfig) error {
return err 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 we save the config, we will not retry next time. Only do this on success.
if err == nil { if err == nil {
m.config = config m.config = config
@ -78,11 +70,11 @@ func (m *Manager) Set(config OSConfig) error {
} }
func (m *Manager) Up() error { func (m *Manager) Up() error {
return m.impl.Up(m.config) return m.impl.Set(m.config)
} }
func (m *Manager) Down() error { func (m *Manager) Down() error {
return m.impl.Down() return m.impl.Close()
} }
// Cleanup restores the system DNS configuration to its original state // Cleanup restores the system DNS configuration to its original state

@ -8,7 +8,7 @@ package dns
import "tailscale.com/types/logger" 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. // 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, // This is currently not implemented. Editing /etc/resolv.conf does not work,
// as most applications use the system resolver, which disregards it. // as most applications use the system resolver, which disregards it.

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

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

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

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

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

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

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

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

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

Loading…
Cancel
Save