diff --git a/net/dns/manager_windows.go b/net/dns/manager_windows.go index 83cc0db51..26597821f 100644 --- a/net/dns/manager_windows.go +++ b/net/dns/manager_windows.go @@ -259,6 +259,10 @@ func (m windowsManager) SetDNS(cfg OSConfig) error { } } + if len(cfg.SearchDomains) != 0 { + go w.setWSL2DNS() + } + // Force DNS re-registration in Active Directory. What we actually // care about is that this command invokes the undocumented hidden // function that forces Windows to notice that adapter settings @@ -299,6 +303,19 @@ func (m windowsManager) SetDNS(cfg OSConfig) error { return nil } +func (w windowsManager) setWSL2DNS() { + cmd := exec.Command("wsl.exe", "-l", "-q") + cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} + out, err := cmd.CombinedOutput() + if err != nil { + m.logf("wsl.exe did not list distros, skipping WSL DNS config") + return + } + for _, distroName := range strings.Split(string(out), "\n") { + distroName = strings.TrimSpace(distroName) + } +} + func (m windowsManager) SupportsSplitDNS() bool { return m.nrptWorks } diff --git a/net/dns/wsl_windows.go b/net/dns/wsl_windows.go new file mode 100644 index 000000000..7a5d54b65 --- /dev/null +++ b/net/dns/wsl_windows.go @@ -0,0 +1,52 @@ +// Copyright (c) 2021 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 ( + "fmt" + "io" + "os/exec" + "strings" + "syscall" +) + +// We access WSL2 file systems via wsl.exe instead of \\wsl$\ because +// the netpath appears to operate as the standard user, not root. + +func wslFileExists(distro string, fileName string) (bool, error) { + cmd := exec.Command("wsl.exe", "-u", "root", "-d", distro, "/bin/test", fileName) + cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} + err := cmd.Run() + if err == nil { + return true, nil + } + if ee, _ := err.(*exec.ExitError); ee != nil { + if ee.ExitCode() == 1 { + return false, nil + } + } + return false, fmt.Errorf("wslFileExists(%q): %w", fileName, err) +} + +func wslReadFile(distro, fileName string) (string, error) { + cmd := exec.Command("wsl.exe", "-u", "root", "-d", distro, "/bin/cat", fileName) + cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} + b, err := cmd.CombinedOutput() + if err != nil { + return "", fmt.Errorf("wslReadFile(%q): %w", fileName, err) + } + return strings.TrimSpace(string(b)), nil +} + +func wslWriteFile(distro, fileName string, r io.Reader) error { + cmd := exec.Command("wsl.exe", "-u", "root", "-d", distro, "tee", fileName) + cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} + cmd.Stdin = r + cmd.Stdout = null + if err := cmd.Run(); err != nil { + return "", fmt.Errorf("wslWriteFile(%q): %w", fileName, err) + } + return nil +}