From f2fdfc58bc7c5a872800d01d7655701b0cc79cb7 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Tue, 8 Apr 2025 23:42:21 +0800 Subject: [PATCH] chore: ignore interfaces not in IfOperStatusUp when fetch system dns server on windows --- dns/system_windows.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/dns/system_windows.go b/dns/system_windows.go index a86d50ed..b90fbce3 100644 --- a/dns/system_windows.go +++ b/dns/system_windows.go @@ -19,6 +19,16 @@ func dnsReadConfig() (servers []string, err error) { return } for _, aa := range aas { + // Only take interfaces whose OperStatus is IfOperStatusUp(0x01) into DNS configs. + if aa.IfType != windows.IfOperStatusUp { + continue + } + + // Only take interfaces which have at least one gateway + if aa.FirstGatewayAddress == nil { + continue + } + for dns := aa.FirstDnsServerAddress; dns != nil; dns = dns.Next { sa, err := dns.Address.Sockaddr.Sockaddr() if err != nil { @@ -63,7 +73,8 @@ func adapterAddresses() ([]*windows.IpAdapterAddresses, error) { l := uint32(15000) // recommended initial size for { b = make([]byte, l) - err := windows.GetAdaptersAddresses(syscall.AF_UNSPEC, windows.GAA_FLAG_INCLUDE_PREFIX, 0, (*windows.IpAdapterAddresses)(unsafe.Pointer(&b[0])), &l) + const flags = windows.GAA_FLAG_INCLUDE_PREFIX | windows.GAA_FLAG_INCLUDE_GATEWAYS + err := windows.GetAdaptersAddresses(syscall.AF_UNSPEC, flags, 0, (*windows.IpAdapterAddresses)(unsafe.Pointer(&b[0])), &l) if err == nil { if l == 0 { return nil, nil