-
Notifications
You must be signed in to change notification settings - Fork 1
/
dnsclient.go
193 lines (184 loc) · 4.46 KB
/
dnsclient.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
package main
import (
"fmt"
"math/rand"
"strings"
"time"
"github.com/miekg/dns"
"github.com/usher2/u2ckbot/internal/logger"
)
const (
ATTEMPTS = 1
TIMEOUT = 5
)
func appendIfMissing(slice []string, s string) []string {
for _, ele := range slice {
if ele == s {
return slice
}
}
return append(slice, s)
}
func getIP4(domain string) (res []string) {
if r, _, err := GetRR(domain, []string{"127.0.0.11:53"}, dns.TypeA); err == nil {
switch r.Rcode {
case dns.RcodeSuccess:
if len(r.Answer) > 0 {
if len(r.Answer) > 99 {
logger.Warning.Printf("Internal error, for %s, Answer too big: %d\n", domain, len(r.Answer))
}
cnt := 0
for _, rr := range r.Answer {
switch rr.Header().Rrtype {
case dns.TypeA:
if cnt > 8 {
continue
}
res = appendIfMissing(res, rr.(*dns.A).A.String())
cnt++
case dns.TypeCNAME:
res1 := getIP4(strings.TrimSuffix(rr.(*dns.CNAME).Target, "."))
for _, ele := range res1 {
res = appendIfMissing(res, ele)
}
case dns.TypeRRSIG:
logger.Debug.Printf("Warning: RRSIG (%s): %#v", domain, r.MsgHdr)
default:
logger.Debug.Printf("Warning: unknown answer (%s): %s\n", domain, rr.String())
}
}
}
}
} else {
logger.Error.Printf("Type A. Internal error (%s): %s\n", domain, err.Error())
}
return
}
func getIP6(domain string) (res []string) {
if r, _, err := GetRR(domain, []string{"127.0.0.11:53"}, dns.TypeAAAA); err == nil {
switch r.Rcode {
case dns.RcodeSuccess:
if len(r.Answer) > 0 {
if len(r.Answer) > 99 {
logger.Warning.Printf("Internal error, for %s, Answer too big: %d\n", domain, len(r.Answer))
}
cnt := 0
for _, rr := range r.Answer {
switch rr.Header().Rrtype {
case dns.TypeAAAA:
if cnt > 8 {
continue
}
res = appendIfMissing(res, rr.(*dns.AAAA).AAAA.String())
cnt++
case dns.TypeCNAME:
res1 := getIP6(strings.TrimSuffix(rr.(*dns.CNAME).Target, "."))
for _, ele := range res1 {
res = appendIfMissing(res, ele)
}
case dns.TypeRRSIG:
logger.Debug.Printf("Warning: RRSIG (%s): %#v", domain, r.MsgHdr)
default:
logger.Debug.Printf("Warning: unknown answer (%s): %s\n", domain, rr.String())
}
}
}
}
} else {
logger.Error.Printf("Type AAAA. Internal error (%s): %s\n", domain, err.Error())
}
return
}
func GetRR(domain string, nameservers []string, qtype uint16) (r *dns.Msg, rtt time.Duration, err error) {
if len(nameservers) == 0 {
err = fmt.Errorf("%s", "No nameservers!")
return
}
for a := 0; a < ATTEMPTS; a++ {
if a > 1 {
time.Sleep(250 * time.Millisecond)
}
l := rand.Perm(len(nameservers))
for _, i := range l {
nameserver := nameservers[i]
m := &dns.Msg{
MsgHdr: dns.MsgHdr{
// Authoritative: true,
AuthenticatedData: true,
CheckingDisabled: true,
RecursionDesired: true,
Opcode: dns.OpcodeQuery,
Rcode: dns.RcodeSuccess,
},
Question: make([]dns.Question, 1),
}
o := &dns.OPT{
Hdr: dns.RR_Header{
Name: ".",
Rrtype: dns.TypeOPT,
},
}
o.SetDo()
o.SetUDPSize(dns.DefaultMsgSize)
m.Extra = append(m.Extra, o)
qt := qtype
qc := uint16(dns.ClassINET)
m.Question[0] = dns.Question{Name: dns.Fqdn(domain), Qtype: qt, Qclass: qc}
m.Id = dns.Id()
r, rtt, err = lookup(m, nameserver, true)
if err == nil {
break
}
}
if err == nil {
break
}
}
return r, rtt, err
}
func lookup(m *dns.Msg, nameserver string, fallback bool) (r *dns.Msg, rtt time.Duration, err error) {
c := new(dns.Client)
c.Timeout = time.Second * TIMEOUT
if fallback {
c.Net = "udp"
} else {
c.Net = "tcp"
}
r, rtt, err = c.Exchange(m, nameserver)
/*
switch err {
case nil:
//do nothing
case dns.ErrTruncated:
if fallback {
// First EDNS, then TCP
c.Net = "tcp"
r, rtt, err = lookup(m, nameserver, false)
}
default:
//do nothing
}
*/
if r != nil {
if r.Truncated {
if fallback {
// First EDNS, then TCP
c.Net = "tcp"
r, rtt, err = lookup(m, nameserver, false)
}
}
}
if err == nil {
if r.Id != m.Id {
err = fmt.Errorf("%s", "Id mismatch")
}
// fmt.Printf("%v", r)
// fmt.Printf("\n;; query time: %.3d µs, server: %s(%s), size: %d bytes\n", rtt/1e3, nameserver, c.Net, r.Len())
}
if err != nil && r != nil {
if m.Response || m.Opcode == dns.OpcodeQuery {
err = nil
}
}
return r, rtt, err
}