...
1
2
3
4
19
20 package dns
21
22 import (
23 "fmt"
24 "strings"
25 "syscall"
26 "unsafe"
27
28 "golang.org/x/sys/windows"
29 "golang.org/x/sys/windows/registry"
30 )
31
32 const (
33 etcHostsFile = "C:/Windows/System32/drivers/etc/hosts"
34 netRegistry = `System\CurrentControlSet\Services\TCPIP\Parameters`
35 netIfacesRegistry = `System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces`
36 maxHostnameLen = 128
37 maxDomainNameLen = 128
38 maxScopeIDLen = 256
39 )
40
41
42 type FixedInfo struct {
43 HostName [maxHostnameLen + 4]byte
44 DomainName [maxDomainNameLen + 4]byte
45 CurrentDNSServer *syscall.IpAddrString
46 DNSServerList syscall.IpAddrString
47 NodeType uint32
48 ScopeID [maxScopeIDLen + 4]byte
49 EnableRouting uint32
50 EnableProxy uint32
51 EnableDNS uint32
52 }
53
54 var (
55
56
57 iphlpapidll = windows.MustLoadDLL("iphlpapi.dll")
58 procGetNetworkParams = iphlpapidll.MustFindProc("GetNetworkParams")
59 )
60
61 func elemInList(elem string, list []string) bool {
62 for _, e := range list {
63 if e == elem {
64 return true
65 }
66 }
67 return false
68 }
69
70 func getRegistryValue(reg, key string) string {
71 regKey, err := registry.OpenKey(registry.LOCAL_MACHINE, reg, registry.QUERY_VALUE)
72 if err != nil {
73 return ""
74 }
75 defer regKey.Close()
76
77 regValue, _, err := regKey.GetStringValue(key)
78 if err != nil {
79 return ""
80 }
81 return regValue
82 }
83
84
85 func GetDNSSuffixList() []string {
86
87 allSuffixes := []string{}
88 suffixes := getRegistryValue(netRegistry, "SearchList")
89 if suffixes != "" {
90 allSuffixes = strings.Split(suffixes, ",")
91 }
92
93
94 regKey, err := registry.OpenKey(registry.LOCAL_MACHINE, netIfacesRegistry, registry.ENUMERATE_SUB_KEYS)
95 if err != nil {
96 panic(err)
97 }
98 defer regKey.Close()
99
100 ifaces, err := regKey.ReadSubKeyNames(0)
101 if err != nil {
102 panic(err)
103 }
104 for _, iface := range ifaces {
105 suffixes := getRegistryValue(fmt.Sprintf("%s\\%s", netIfacesRegistry, iface), "SearchList")
106 if suffixes == "" {
107 continue
108 }
109 for _, suffix := range strings.Split(suffixes, ",") {
110 if !elemInList(suffix, allSuffixes) {
111 allSuffixes = append(allSuffixes, suffix)
112 }
113 }
114 }
115
116 return allSuffixes
117 }
118
119 func getNetworkParams() *FixedInfo {
120
121
122 var size int
123 buffer := make([]byte, 1)
124 procGetNetworkParams.Call(
125 uintptr(unsafe.Pointer(&buffer[0])),
126 uintptr(unsafe.Pointer(&size)),
127 )
128
129 buffer = make([]byte, size)
130 procGetNetworkParams.Call(
131 uintptr(unsafe.Pointer(&buffer[0])),
132 uintptr(unsafe.Pointer(&size)),
133 )
134
135 info := (*FixedInfo)(unsafe.Pointer(&buffer[0]))
136 return info
137 }
138
139 func getDNSServerList() []string {
140 dnsServerList := []string{}
141 fixedInfo := getNetworkParams()
142 list := &(fixedInfo.DNSServerList)
143
144 for list != nil {
145 dnsServer := strings.TrimRight(string(list.IpAddress.String[:]), "\x00")
146 dnsServerList = append(dnsServerList, dnsServer)
147 list = list.Next
148 }
149 return dnsServerList
150 }
151
View as plain text