mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-08-23 01:27:13 +02:00
fix(windows): improve DNS server discovery for domain-joined machines
Add DNS suffix matching for non-physical adapters when domain-joined. This allows interfaces with matching DNS suffix to be considered valid even if not in validInterfacesMap, improving DNS server discovery for remote VPN scenarios.
This commit is contained in:
+22
-2
@@ -17,6 +17,7 @@ import (
|
|||||||
"github.com/microsoft/wmi/pkg/base/query"
|
"github.com/microsoft/wmi/pkg/base/query"
|
||||||
"github.com/microsoft/wmi/pkg/constant"
|
"github.com/microsoft/wmi/pkg/constant"
|
||||||
"github.com/microsoft/wmi/pkg/hardware/network/netadapter"
|
"github.com/microsoft/wmi/pkg/hardware/network/netadapter"
|
||||||
|
"github.com/miekg/dns"
|
||||||
"golang.org/x/sys/windows"
|
"golang.org/x/sys/windows"
|
||||||
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
||||||
"tailscale.com/net/netmon"
|
"tailscale.com/net/netmon"
|
||||||
@@ -121,12 +122,14 @@ func getDNSServers(ctx context.Context) ([]string, error) {
|
|||||||
|
|
||||||
// Try to get domain controller info if domain-joined
|
// Try to get domain controller info if domain-joined
|
||||||
var dcServers []string
|
var dcServers []string
|
||||||
|
var adDomain string
|
||||||
isDomain := checkDomainJoined(ctx)
|
isDomain := checkDomainJoined(ctx)
|
||||||
if isDomain {
|
if isDomain {
|
||||||
domainName, err := system.GetActiveDirectoryDomain()
|
domainName, err := system.GetActiveDirectoryDomain()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Debug().Msgf("Failed to get local AD domain: %v", err)
|
logger.Debug().Msgf("Failed to get local AD domain: %v", err)
|
||||||
} else {
|
} else {
|
||||||
|
adDomain = domainName
|
||||||
// Load netapi32.dll
|
// Load netapi32.dll
|
||||||
netapi32 := windows.NewLazySystemDLL("netapi32.dll")
|
netapi32 := windows.NewLazySystemDLL("netapi32.dll")
|
||||||
dsDcName := netapi32.NewProc("DsGetDcNameW")
|
dsDcName := netapi32.NewProc("DsGetDcNameW")
|
||||||
@@ -214,6 +217,10 @@ func getDNSServers(ctx context.Context) ([]string, error) {
|
|||||||
|
|
||||||
validInterfacesMap := ValidInterfaces(ctx)
|
validInterfacesMap := ValidInterfaces(ctx)
|
||||||
|
|
||||||
|
if isDomain && adDomain == "" {
|
||||||
|
logger.Warn().Msg("The machine is joined domain, but domain name is empty")
|
||||||
|
}
|
||||||
|
checkDnsSuffix := isDomain && adDomain != ""
|
||||||
// Collect DNS servers
|
// Collect DNS servers
|
||||||
for _, aa := range aas {
|
for _, aa := range aas {
|
||||||
if aa.OperStatus != winipcfg.IfOperStatusUp {
|
if aa.OperStatus != winipcfg.IfOperStatusUp {
|
||||||
@@ -227,8 +234,21 @@ func getDNSServers(ctx context.Context) ([]string, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// if not in the validInterfacesMap, skip
|
_, valid := validInterfacesMap[aa.FriendlyName()]
|
||||||
if _, ok := validInterfacesMap[aa.FriendlyName()]; !ok {
|
if !valid && checkDnsSuffix {
|
||||||
|
for suffix := aa.FirstDNSSuffix; suffix != nil; suffix = suffix.Next {
|
||||||
|
// For non-physical adapters, if the DNS suffix matches the domain name,
|
||||||
|
// (or vice versa) consider it valid. This can happen on remote VPN machines.
|
||||||
|
ds := strings.TrimSpace(suffix.String())
|
||||||
|
if dns.IsSubDomain(adDomain, ds) || dns.IsSubDomain(ds, adDomain) {
|
||||||
|
logger.Debug().Msgf("Found valid interface %s with DNS suffix %s", aa.FriendlyName(), suffix.String())
|
||||||
|
valid = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if not a valid interface, skip it
|
||||||
|
if !valid {
|
||||||
logger.Debug().Msgf("Skipping %s (not in validInterfacesMap)", aa.FriendlyName())
|
logger.Debug().Msgf("Skipping %s (not in validInterfacesMap)", aa.FriendlyName())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user