net/dns: some WSL wiring

This commit is contained in:
David Crawshaw 2021-06-13 16:29:17 -07:00
parent 525eb5ce41
commit 8a46ee7920
2 changed files with 69 additions and 0 deletions

View File

@ -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
}

52
net/dns/wsl_windows.go Normal file
View File

@ -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
}