Skip to content

Commit

Permalink
Disable Windows DNS registration
Browse files Browse the repository at this point in the history
  • Loading branch information
i40e authored and nekohasekai committed Oct 26, 2023
1 parent 1a85bd3 commit efd9884
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
22 changes: 22 additions & 0 deletions internal/winipcfg/luid.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,25 @@ func (luid LUID) SetDNS(family AddressFamily, servers []netip.Addr, domains []st
func (luid LUID) FlushDNS(family AddressFamily) error {
return luid.SetDNS(family, nil, nil)
}

func (luid LUID) DisableDNSRegistration() error {
guid, err := luid.GUID()
if err != nil {
return err
}

dnsInterfaceSettings := &DnsInterfaceSettings{
Version: DnsInterfaceSettingsVersion1,
Flags: DnsInterfaceSettingsFlagRegistrationEnabled,
RegistrationEnabled: 0,
}

// For >= Windows 10 1809
err = SetInterfaceDnsSettings(*guid, dnsInterfaceSettings)
if err == nil || !errors.Is(err, windows.ERROR_PROC_NOT_FOUND) {
return err
}

// For < Windows 10 1809
return luid.fallbackDisableDNSRegistration()
}
19 changes: 15 additions & 4 deletions internal/winipcfg/netsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ func runNetsh(cmds []string) error {
}

const (
netshCmdTemplateFlush4 = "interface ipv4 set dnsservers name=%d source=static address=none validate=no register=both"
netshCmdTemplateFlush6 = "interface ipv6 set dnsservers name=%d source=static address=none validate=no register=both"
netshCmdTemplateAdd4 = "interface ipv4 add dnsservers name=%d address=%s validate=no"
netshCmdTemplateAdd6 = "interface ipv6 add dnsservers name=%d address=%s validate=no"
netshCmdTemplateFlush4 = "interface ipv4 set dnsservers name=%d source=static address=none validate=no"
netshCmdTemplateFlush6 = "interface ipv6 set dnsservers name=%d source=static address=none validate=no"
netshCmdTemplateAdd4 = "interface ipv4 add dnsservers name=%d address=%s validate=no"
netshCmdTemplateAdd6 = "interface ipv6 add dnsservers name=%d address=%s validate=no"
netshCmdTemplateDisableRegistration = "interface ipv6 set dnsservers name=%d register=none"
)

func (luid LUID) fallbackSetDNSForFamily(family AddressFamily, dnses []netip.Addr) error {
Expand Down Expand Up @@ -106,3 +107,13 @@ func (luid LUID) fallbackSetDNSDomain(domain string) error {
key.Close()
return err
}

func (luid LUID) fallbackDisableDNSRegistration() error {
// the DNS registration setting is shared for both IPv4 and IPv6
ipif, err := luid.IPInterface(windows.AF_INET)
if err != nil {
return err
}
cmd := fmt.Sprintf(netshCmdTemplateDisableRegistration, ipif.InterfaceIndex)
return runNetsh([]string{cmd})
}
3 changes: 3 additions & 0 deletions tun_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ func (t *NativeTun) configure() error {
return E.Cause(err, "set ipv6 dns")
}
}
if len(t.options.Inet4Address) > 0 || len(t.options.Inet6Address) > 0 {
_ = luid.DisableDNSRegistration()
}
if t.options.AutoRoute {
if len(t.options.Inet4Address) > 0 {
if len(t.options.Inet4RouteAddress) > 0 {
Expand Down

0 comments on commit efd9884

Please sign in to comment.