/
utils.go
226 lines (173 loc) · 4.02 KB
/
utils.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package scan
import (
"fmt"
"net"
"time"
"github.com/42wim/dt/structs"
"github.com/ammario/ipisp"
"github.com/miekg/dns"
)
func getIP(host string, qtype uint16, servers ...string) []net.IP {
var ips []net.IP
for _, server := range servers {
rrset, _, err := queryRRset(host, qtype, server, false)
if err != nil {
continue
}
return extractIP(rrset)
}
return ips
}
func extractIP(rrset []dns.RR) []net.IP {
var ips []net.IP
for _, rr := range rrset {
switch rr := rr.(type) {
case *dns.A:
ips = append(ips, rr.A)
case *dns.AAAA:
ips = append(ips, rr.AAAA)
}
}
return ips
}
func extractRR(rrset []dns.RR, qtypes ...uint16) []dns.RR {
var out []dns.RR
m := make(map[uint16]bool)
for _, qtype := range qtypes {
m[qtype] = true
}
for _, rr := range rrset {
if _, ok := m[rr.Header().Rrtype]; ok {
out = append(out, rr)
}
}
return out
}
func QueryClass(q string, qtype uint16, server string, sec bool, class uint16) (structs.Response, error) {
c := new(dns.Client)
m := prepMsg()
m.CheckingDisabled = true
m.RecursionDesired = true
if sec {
m.CheckingDisabled = false
m.SetEdns0(4096, true)
}
var resp structs.Response
log.Debugf("Asking %s about %s (%s)", server, q, dns.TypeToString[qtype])
m.Question[0] = dns.Question{
Name: dns.Fqdn(q),
Qtype: qtype,
Qclass: class,
}
in, rtt, err := c.Exchange(m, net.JoinHostPort(server, "53"))
if err != nil {
return resp, err
}
if in.Rcode != 0 {
resp.Rtt = rtt
return resp, fmt.Errorf("failure: %s", dns.RcodeToString[in.Rcode])
}
return structs.Response{
Msg: in,
Server: server,
Rtt: rtt,
}, nil
}
func query(q string, qtype uint16, server string, sec bool) (structs.Response, error) {
return QueryClass(q, qtype, server, sec, dns.ClassINET)
}
func queryRRset(q string, qtype uint16, server string, sec bool) ([]dns.RR, time.Duration, error) {
res, err := query(q, qtype, server, sec)
if err != nil {
return []dns.RR{}, res.Rtt, err
}
rrset := extractRR(res.Msg.Answer, qtype)
if len(rrset) == 0 {
return []dns.RR{}, res.Rtt, fmt.Errorf("no rr for %#v", qtype)
}
return rrset, res.Rtt, nil
}
func (s *Scan) FindNS(domain string) ([]structs.NSData, error) {
if nsdatas, ok := s.nsdataCache[domain]; ok {
return nsdatas, nil
}
rrset, _, err := queryRRset(domain, dns.TypeNS, s.resolver, false)
if err != nil {
return []structs.NSData{}, err
}
var nsdatas []structs.NSData
for _, rr := range rrset {
var ips []net.IP
nsdata := structs.NSData{}
ns := rr.(*dns.NS).Ns
nsdata.Name = ns
ips = append(ips, getIP(ns, dns.TypeA, s.resolver)...)
ips = append(ips, getIP(ns, dns.TypeAAAA, s.resolver)...)
var nsinfos []structs.NSInfo
for _, ip := range ips {
nsinfos = append(nsinfos, structs.NSInfo{IPInfo: structs.IPInfo{IP: ip}, Name: ns})
}
nsdata.IP = ips
nsdata.Info = nsinfos
nsdatas = append(nsdatas, nsdata)
}
if len(nsdatas) == 0 {
return nsdatas, fmt.Errorf("no NS found")
}
s.nsdataCache[domain] = nsdatas
return nsdatas, nil
}
func prepMsg() *dns.Msg {
m := new(dns.Msg)
m.Id = dns.Id()
m.RecursionDesired = true
m.Question = make([]dns.Question, 1)
return m
}
func getParentDomain(domain string) string {
i, end := dns.NextLabel(domain, 0)
if !end {
return domain[i:]
}
return "."
}
func removeWild(wild []string, rrset []dns.RR) []dns.RR {
if len(wild) == 0 {
return rrset
}
newset := []dns.RR{}
for _, rr := range rrset {
match := false
switch rr := rr.(type) {
case *dns.A:
for _, ip := range wild {
if rr.A.String() == ip {
match = true
}
}
case *dns.AAAA:
for _, ip := range wild {
if rr.AAAA.String() == ip {
match = true
}
}
}
if !match {
newset = append(newset, rr)
}
}
return newset
}
func ipinfo(ip net.IP) (structs.IPInfo, error) {
client, _ := ipisp.NewDNSClient()
resp, err := client.LookupIP(net.ParseIP(ip.String()))
if err != nil {
return structs.IPInfo{}, err
}
return structs.IPInfo{
IP: ip,
Loc: resp.Country,
ASN: resp.ASN,
ISP: resp.Name.Raw,
}, nil
}