-
Notifications
You must be signed in to change notification settings - Fork 1
/
dig.go
377 lines (347 loc) · 7.51 KB
/
dig.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package dns
import (
"errors"
"fmt"
"net"
"strings"
"time"
"github.com/miekg/dns"
)
const (
dnsTimeout time.Duration = 3 * time.Second
)
var roots = []string{"a.root-servers.net", "b.root-servers.net", "d.root-servers.net", "c.root-servers.net", "e.root-servers.net", "f.root-servers.net", "g.root-servers.net", "h.root-servers.net", "i.root-servers.net", "j.root-servers.net", "k.root-servers.net", "l.root-servers.net", "m.root-servers.net"}
type Dig struct {
LocalAddr string
RemoteAddr string
EDNSSubnet net.IP
DialTimeout time.Duration
WriteTimeout time.Duration
ReadTimeout time.Duration
Protocol string
Retry int
}
func (d *Dig) protocol() string {
if d.Protocol != "" {
return d.Protocol
}
return "udp"
}
func (d *Dig) dialTimeout() time.Duration {
if d.DialTimeout != 0 {
return d.DialTimeout
}
return dnsTimeout
}
func (d *Dig) readTimeout() time.Duration {
if d.ReadTimeout != 0 {
return d.ReadTimeout
}
return dnsTimeout
}
func (d *Dig) writeTimeout() time.Duration {
if d.WriteTimeout != 0 {
return d.WriteTimeout
}
return dnsTimeout
}
func (d *Dig) retry() int {
if d.Retry > 0 {
return d.Retry
}
return 1
}
func (d *Dig) remoteAddr() (string, error) {
_, _, err := net.SplitHostPort(d.RemoteAddr)
if err != nil {
return d.RemoteAddr, errors.New("forget SetDNS ? " + err.Error())
}
return d.RemoteAddr, nil
}
func (d *Dig) conn() (net.Conn, error) {
remoteaddr, err := d.remoteAddr()
if err != nil {
return nil, err
}
if d.LocalAddr == "" {
return net.DialTimeout(d.protocol(), remoteaddr, d.dialTimeout())
}
return dial(d.protocol(), d.LocalAddr, remoteaddr, d.dialTimeout())
}
func dial(network string, local string, remote string, timeout time.Duration) (net.Conn, error) {
network = strings.ToLower(network)
dialer := new(net.Dialer)
dialer.Timeout = timeout
local = local + ":0" //端口0,系统会自动分配本机端口
switch network {
case "udp":
addr, err := net.ResolveUDPAddr(network, local)
if err != nil {
return nil, err
}
dialer.LocalAddr = addr
case "tcp":
addr, err := net.ResolveTCPAddr(network, local)
if err != nil {
return nil, err
}
dialer.LocalAddr = addr
}
return dialer.Dial(network, remote)
}
func NewMsg(Type uint16, domain string) *dns.Msg {
return newMsg(Type, domain)
}
func newMsg(Type uint16, domain string) *dns.Msg {
domain = dns.Fqdn(domain)
msg := new(dns.Msg)
msg.Id = dns.Id()
msg.RecursionDesired = true
msg.Question = make([]dns.Question, 1)
msg.Question[0] = dns.Question{
Name: domain,
Qtype: Type,
Qclass: dns.ClassINET,
}
return msg
}
func (d *Dig) Exchange(m *dns.Msg) (*dns.Msg, error) {
var msg *dns.Msg
var err error
for i := 0; i < d.retry(); i++ {
msg, err = d.exchange(m)
if err == nil {
return msg, err
}
}
return msg, err
}
func (d *Dig) exchange(m *dns.Msg) (*dns.Msg, error) {
var err error
c := new(dns.Conn)
c.Conn, err = d.conn()
if err != nil {
return nil, err
}
defer c.Close()
c.SetWriteDeadline(time.Now().Add(d.writeTimeout()))
c.SetReadDeadline(time.Now().Add(d.readTimeout()))
d.edns0clientsubnet(m)
err = c.WriteMsg(m)
if err != nil {
return nil, err
}
res, err := c.ReadMsg()
if err != nil {
return nil, err
}
return res, nil
}
func (d *Dig) SetTimeOut(t time.Duration) {
d.ReadTimeout = t
d.WriteTimeout = t
d.DialTimeout = t
}
func (d *Dig) UseDafaultDNS() {
d.SetDNS(DNS_SEVER_DEFAULT)
}
func (d *Dig) SetDNS(host string) error {
var ip string
port := "53"
switch strings.Count(host, ":") {
case 0: //ipv4 or domain
ip = host
case 1: //ipv4 or domain
var err error
ip, port, err = net.SplitHostPort(host)
if err != nil {
return err
}
default: //ipv6
if net.ParseIP(host).To16() != nil {
ip = host
} else {
ip = host[:strings.LastIndex(host, ":")]
port = host[strings.LastIndex(host, ":")+1:]
}
}
ips, err := net.LookupIP(ip)
if err != nil {
return err
}
for _, addr := range ips {
d.RemoteAddr = fmt.Sprintf("[%s]:%v", addr, port)
return nil
}
return errors.New("no such host")
}
func (d *Dig) SetEDNS0ClientSubnet(clientip string) error {
ip := net.ParseIP(clientip)
if ip.To4() == nil {
return errors.New("not a ipv4")
}
d.EDNSSubnet = ip
return nil
}
func (d *Dig) A(domain string) ([]*dns.A, error) {
m := newMsg(dns.TypeA, domain)
res, err := d.Exchange(m)
if err != nil {
return nil, err
}
var As []*dns.A
for _, v := range res.Answer {
if a, ok := v.(*dns.A); ok {
As = append(As, a)
}
}
return As, nil
}
func (d *Dig) NS(domain string) ([]*dns.NS, error) {
m := newMsg(dns.TypeNS, domain)
res, err := d.Exchange(m)
if err != nil {
return nil, err
}
var Ns []*dns.NS
for _, v := range res.Answer {
if ns, ok := v.(*dns.NS); ok {
Ns = append(Ns, ns)
}
}
return Ns, nil
}
func (d *Dig) CNAME(domain string) ([]*dns.CNAME, error) {
m := newMsg(dns.TypeCNAME, domain)
res, err := d.Exchange(m)
if err != nil {
return nil, err
}
var C []*dns.CNAME
for _, v := range res.Answer {
if c, ok := v.(*dns.CNAME); ok {
C = append(C, c)
}
}
return C, nil
}
func (d *Dig) PTR(domain string) ([]*dns.PTR, error) {
m := newMsg(dns.TypePTR, domain)
res, err := d.Exchange(m)
if err != nil {
return nil, err
}
var P []*dns.PTR
for _, v := range res.Answer {
if p, ok := v.(*dns.PTR); ok {
P = append(P, p)
}
}
return P, nil
}
func (d *Dig) TXT(domain string) ([]*dns.TXT, error) {
m := newMsg(dns.TypeTXT, domain)
res, err := d.Exchange(m)
if err != nil {
return nil, err
}
var T []*dns.TXT
for _, v := range res.Answer {
if t, ok := v.(*dns.TXT); ok {
T = append(T, t)
}
}
return T, nil
}
func (d *Dig) AAAA(domain string) ([]*dns.AAAA, error) {
m := newMsg(dns.TypeAAAA, domain)
res, err := d.Exchange(m)
if err != nil {
return nil, err
}
var aaaa []*dns.AAAA
for _, v := range res.Answer {
if a, ok := v.(*dns.AAAA); ok {
aaaa = append(aaaa, a)
}
}
return aaaa, nil
}
func (d *Dig) MX(domain string) ([]*dns.MX, error) {
msg := newMsg(dns.TypeMX, domain)
res, err := d.Exchange(msg)
if err != nil {
return nil, err
}
var M []*dns.MX
for _, v := range res.Answer {
if m, ok := v.(*dns.MX); ok {
M = append(M, m)
}
}
return M, nil
}
func (d *Dig) SRV(domain string) ([]*dns.SRV, error) {
msg := newMsg(dns.TypeSRV, domain)
res, err := d.Exchange(msg)
if err != nil {
return nil, err
}
var S []*dns.SRV
for _, v := range res.Answer {
if s, ok := v.(*dns.SRV); ok {
S = append(S, s)
}
}
return S, nil
}
func (d *Dig) CAA(domain string) ([]*dns.CAA, error) {
msg := newMsg(dns.TypeCAA, domain)
res, err := d.Exchange(msg)
if err != nil {
return nil, err
}
var C []*dns.CAA
for _, v := range res.Answer {
if c, ok := v.(*dns.CAA); ok {
C = append(C, c)
}
}
return C, nil
}
func (d *Dig) ANY(domain string) ([]dns.RR, error) {
m := newMsg(dns.TypeANY, domain)
res, err := d.Exchange(m)
if err != nil {
return nil, err
}
return res.Answer, nil
}
func (d *Dig) GetRR(Type uint16, domain string) ([]dns.RR, error) {
m := newMsg(Type, domain)
res, err := d.Exchange(m)
if err != nil {
return nil, err
}
return res.Answer, nil
}
func (d *Dig) GetMsg(Type uint16, domain string) (*dns.Msg, error) {
m := newMsg(Type, domain)
return d.Exchange(m)
}
func (d *Dig) edns0clientsubnet(m *dns.Msg) {
if d.EDNSSubnet == nil {
return
}
e := &dns.EDNS0_SUBNET{
Code: dns.EDNS0SUBNET,
Family: 1, //ipv4
SourceNetmask: 32, //ipv4
Address: d.EDNSSubnet,
}
o := new(dns.OPT)
o.Hdr.Name = "."
o.Hdr.Rrtype = dns.TypeOPT
o.Option = append(o.Option, e)
m.Extra = append(m.Extra, o)
}