mirror of
https://github.com/tailscale/tailscale.git
synced 2025-04-18 12:32:13 +00:00
net/dns: some WSL wiring
This commit is contained in:
parent
525eb5ce41
commit
8a46ee7920
@ -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
|
// Force DNS re-registration in Active Directory. What we actually
|
||||||
// care about is that this command invokes the undocumented hidden
|
// care about is that this command invokes the undocumented hidden
|
||||||
// function that forces Windows to notice that adapter settings
|
// function that forces Windows to notice that adapter settings
|
||||||
@ -299,6 +303,19 @@ func (m windowsManager) SetDNS(cfg OSConfig) error {
|
|||||||
return nil
|
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 {
|
func (m windowsManager) SupportsSplitDNS() bool {
|
||||||
return m.nrptWorks
|
return m.nrptWorks
|
||||||
}
|
}
|
||||||
|
52
net/dns/wsl_windows.go
Normal file
52
net/dns/wsl_windows.go
Normal 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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user