net/dns: set WSL /etc/resolv.conf

We also have to make a one-off change to /etc/wsl.conf to stop every
invocation of wsl.exe clobbering the /etc/resolv.conf. This appears to
be a safe change to make permanently, as even though the resolv.conf is
constantly clobbered, it is always the same stable internal IP that is
set as a nameserver. (I believe the resolv.conf clobbering predates the
MS stub resolver.)

Tested on WSL2, should work for WSL1 too.

Fixes #775

Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
This commit is contained in:
David Crawshaw
2021-06-25 16:36:36 -07:00
committed by David Crawshaw
parent 9b063b86c3
commit 1147c7fd4f
5 changed files with 325 additions and 15 deletions

View File

@@ -35,9 +35,10 @@ const (
)
type windowsManager struct {
logf logger.Logf
guid string
nrptWorks bool
logf logger.Logf
guid string
nrptWorks bool
wslManager *wslManager
}
func NewOSConfigurator(logf logger.Logf, interfaceName string) (OSConfigurator, error) {
@@ -57,6 +58,11 @@ func NewOSConfigurator(logf logger.Logf, interfaceName string) (OSConfigurator,
ret.delKey(nrptBase)
}
if distros := wslDistros(logf); len(distros) > 0 {
logf("WSL distributions: %v", distros)
ret.wslManager = newWSLManager(logf, distros)
}
return ret, nil
}
@@ -296,6 +302,18 @@ func (m windowsManager) SetDNS(cfg OSConfig) error {
}
}()
// On initial setup of WSL, the restart caused by --shutdown is slow,
// so we do it out-of-line.
go func() {
if m.wslManager != nil {
if err := m.wslManager.SetDNS(cfg); err != nil {
m.logf("WSL SetDNS: %v", err) // continue
} else {
m.logf("WSL SetDNS: success")
}
}
}()
return nil
}