2020-07-31 20:27:09 +00:00
|
|
|
// Copyright (c) 2020 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"
|
2020-10-30 03:01:31 +00:00
|
|
|
"os/exec"
|
2020-07-31 20:27:09 +00:00
|
|
|
"strings"
|
2020-10-30 03:01:31 +00:00
|
|
|
"syscall"
|
2020-07-31 20:27:09 +00:00
|
|
|
|
|
|
|
"github.com/tailscale/wireguard-go/tun"
|
|
|
|
"golang.org/x/sys/windows/registry"
|
|
|
|
"tailscale.com/types/logger"
|
|
|
|
)
|
|
|
|
|
2020-08-19 18:16:57 +00:00
|
|
|
const (
|
|
|
|
ipv4RegBase = `SYSTEM\CurrentControlSet\Services\Tcpip\Parameters`
|
|
|
|
ipv6RegBase = `SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters`
|
|
|
|
tsRegBase = `SOFTWARE\Tailscale IPN`
|
|
|
|
)
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
type windowsManager struct {
|
|
|
|
logf logger.Logf
|
|
|
|
guid string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newManager(mconfig ManagerConfig) managerImpl {
|
|
|
|
return windowsManager{
|
|
|
|
logf: mconfig.Logf,
|
|
|
|
guid: tun.WintunGUID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 18:16:57 +00:00
|
|
|
func setRegistryString(path, name, value string) error {
|
|
|
|
key, err := registry.OpenKey(registry.LOCAL_MACHINE, path, registry.SET_VALUE)
|
2020-07-31 20:27:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("opening %s: %w", path, err)
|
|
|
|
}
|
|
|
|
defer key.Close()
|
|
|
|
|
2020-08-19 18:16:57 +00:00
|
|
|
err = key.SetStringValue(name, value)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("setting %s[%s]: %w", path, name, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m windowsManager) setNameservers(basePath string, nameservers []string) error {
|
|
|
|
path := fmt.Sprintf(`%s\Interfaces\%s`, basePath, m.guid)
|
|
|
|
value := strings.Join(nameservers, ",")
|
|
|
|
return setRegistryString(path, "NameServer", value)
|
|
|
|
}
|
2020-07-31 20:27:09 +00:00
|
|
|
|
2020-10-30 03:01:31 +00:00
|
|
|
func (m windowsManager) setDomains(basePath string, domains []string) error {
|
|
|
|
path := fmt.Sprintf(`%s\Interfaces\%s`, basePath, m.guid)
|
|
|
|
value := strings.Join(domains, ",")
|
|
|
|
return setRegistryString(path, "SearchList", value)
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m windowsManager) Up(config Config) error {
|
|
|
|
var ipsv4 []string
|
|
|
|
var ipsv6 []string
|
2020-08-19 18:16:57 +00:00
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
for _, ip := range config.Nameservers {
|
|
|
|
if ip.Is4() {
|
|
|
|
ipsv4 = append(ipsv4, ip.String())
|
|
|
|
} else {
|
|
|
|
ipsv6 = append(ipsv6, ip.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 18:16:57 +00:00
|
|
|
if err := m.setNameservers(ipv4RegBase, ipsv4); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-30 03:01:31 +00:00
|
|
|
if err := m.setDomains(ipv4RegBase, config.Domains); err != nil {
|
2020-07-31 20:27:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-08-19 18:16:57 +00:00
|
|
|
|
|
|
|
if err := m.setNameservers(ipv6RegBase, ipsv6); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-30 03:01:31 +00:00
|
|
|
if err := m.setDomains(ipv6RegBase, config.Domains); err != nil {
|
2020-08-19 18:16:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newSearchList := strings.Join(config.Domains, ",")
|
|
|
|
if err := setRegistryString(tsRegBase, "SearchList", newSearchList); err != nil {
|
2020-07-31 20:27:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-30 03:01:31 +00:00
|
|
|
// 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
|
|
|
|
// have changed, which makes the DNS settings actually take
|
|
|
|
// effect.
|
|
|
|
//
|
|
|
|
// This command can take a few seconds to run.
|
|
|
|
cmd := exec.Command("ipconfig", "/registerdns")
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return fmt.Errorf("running ipconfig /registerdns: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m windowsManager) Down() error {
|
|
|
|
return m.Up(Config{Nameservers: nil, Domains: nil})
|
|
|
|
}
|