-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_windows.go
61 lines (51 loc) · 1.44 KB
/
utils_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"strings"
"unsafe"
"github.com/miekg/dns"
"golang.org/x/sys/windows"
)
func dnsClientConfig() (*dns.ClientConfig, error) {
l := uint32(20000)
b := make([]byte, l)
if err := windows.GetAdaptersAddresses(windows.AF_UNSPEC, windows.GAA_FLAG_INCLUDE_PREFIX, 0, (*windows.IpAdapterAddresses)(unsafe.Pointer(&b[0])), &l); err != nil {
return nil, err
}
var addresses []*windows.IpAdapterAddresses
for addr := (*windows.IpAdapterAddresses)(unsafe.Pointer(&b[0])); addr != nil; addr = addr.Next {
addresses = append(addresses, addr)
}
resolvers := map[string]bool{}
for _, addr := range addresses {
for next := addr.FirstUnicastAddress; next != nil; next = next.Next {
if addr.OperStatus != windows.IfOperStatusUp {
continue
}
if next.Address.IP() != nil {
for dnsServer := addr.FirstDnsServerAddress; dnsServer != nil; dnsServer = dnsServer.Next {
ip := dnsServer.Address.IP()
if ip.IsMulticast() || ip.IsLinkLocalMulticast() || ip.IsLinkLocalUnicast() || ip.IsUnspecified() {
continue
}
if ip.To16() != nil && strings.HasPrefix(ip.To16().String(), "fec0:") {
continue
}
resolvers[ip.String()] = true
}
break
}
}
}
var servers []string
for server := range resolvers {
servers = append(servers, server)
}
return &dns.ClientConfig{
Servers: servers,
Search: []string{},
Port: "53",
Ndots: 1,
Timeout: 5,
Attempts: 1,
}, nil
}