2020-07-31 21:27:09 +01: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
|
|
|
|
|
2022-01-11 16:45:50 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-04-02 07:26:52 +01:00
|
|
|
|
2022-01-11 16:45:50 +00:00
|
|
|
"tailscale.com/types/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
type kv struct {
|
|
|
|
k, v string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (kv kv) String() string {
|
|
|
|
return fmt.Sprintf("%s=%s", kv.k, kv.v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOSConfigurator(logf logger.Logf, interfaceName string) (OSConfigurator, error) {
|
|
|
|
return newOSConfigurator(logf, interfaceName,
|
|
|
|
newOSConfigEnv{
|
|
|
|
rcIsResolvd: rcIsResolvd,
|
|
|
|
fs: directFS{},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// newOSConfigEnv are the funcs newOSConfigurator needs, pulled out for testing.
|
|
|
|
type newOSConfigEnv struct {
|
|
|
|
fs directFS
|
|
|
|
rcIsResolvd func(resolvConfContents []byte) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newOSConfigurator(logf logger.Logf, interfaceName string, env newOSConfigEnv) (ret OSConfigurator, err error) {
|
|
|
|
var debug []kv
|
|
|
|
dbg := func(k, v string) {
|
|
|
|
debug = append(debug, kv{k, v})
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if ret != nil {
|
|
|
|
dbg("ret", fmt.Sprintf("%T", ret))
|
|
|
|
}
|
|
|
|
logf("dns: %v", debug)
|
|
|
|
}()
|
|
|
|
|
|
|
|
bs, err := env.fs.ReadFile(resolvConf)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
dbg("rc", "missing")
|
|
|
|
return newDirectManager(logf), nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading /etc/resolv.conf: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if env.rcIsResolvd(bs) {
|
|
|
|
dbg("resolvd", "yes")
|
|
|
|
return newResolvdManager(logf, interfaceName)
|
|
|
|
}
|
|
|
|
|
|
|
|
dbg("resolvd", "missing")
|
2021-10-07 01:49:32 +01:00
|
|
|
return newDirectManager(logf), nil
|
2020-07-31 21:27:09 +01:00
|
|
|
}
|
2022-01-11 16:45:50 +00:00
|
|
|
|
|
|
|
func rcIsResolvd(resolvConfContents []byte) bool {
|
|
|
|
// If we have the string "# resolvd:" in resolv.conf resolvd(8) is
|
|
|
|
// managing things.
|
|
|
|
if bytes.Contains(resolvConfContents, []byte("# resolvd:")) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|