2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-07-31 20:27:09 +00:00
|
|
|
|
|
|
|
package dns
|
|
|
|
|
2022-01-11 16:45:50 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-04-02 06:26:52 +00:00
|
|
|
|
2024-06-11 03:05:15 +00:00
|
|
|
"tailscale.com/control/controlknobs"
|
2024-04-26 17:12:46 +00:00
|
|
|
"tailscale.com/health"
|
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)
|
|
|
|
}
|
|
|
|
|
2024-06-11 03:05:15 +00:00
|
|
|
// NewOSConfigurator created a new OS configurator.
|
|
|
|
//
|
|
|
|
// The health tracker may be nil; the knobs may be nil and are ignored on this platform.
|
|
|
|
func NewOSConfigurator(logf logger.Logf, health *health.Tracker, _ *controlknobs.Knobs, interfaceName string) (OSConfigurator, error) {
|
2024-04-26 17:12:46 +00:00
|
|
|
return newOSConfigurator(logf, health, interfaceName,
|
2022-01-11 16:45:50 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-04-26 17:12:46 +00:00
|
|
|
func newOSConfigurator(logf logger.Logf, health *health.Tracker, interfaceName string, env newOSConfigEnv) (ret OSConfigurator, err error) {
|
2022-01-11 16:45:50 +00:00
|
|
|
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")
|
2024-04-26 17:12:46 +00:00
|
|
|
return newDirectManager(logf, health), nil
|
2022-01-11 16:45:50 +00:00
|
|
|
}
|
|
|
|
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")
|
2024-04-26 17:12:46 +00:00
|
|
|
return newDirectManager(logf, health), nil
|
2020-07-31 20:27:09 +00: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
|
|
|
|
}
|