-
Notifications
You must be signed in to change notification settings - Fork 42
/
libdns_utils.go
301 lines (265 loc) · 6.72 KB
/
libdns_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
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
package dnsproxy
import (
"context"
"net"
"net/http"
"sync/atomic"
"time"
"github.com/ARwMq9b6/dnsproxy/dns_over_https/google"
"github.com/miekg/dns"
"github.com/pkg/errors"
"golang.org/x/net/proxy"
)
// --- impl dns.Msg
func MsgNewReplyFromReq(req *dns.Msg, answer ...dns.RR) *dns.Msg {
resp := new(dns.Msg)
resp.Id = req.Id
resp.Response = true
resp.Opcode = req.Opcode
resp.Rcode = dns.RcodeSuccess
if len(req.Question) > 0 {
resp.Question = make([]dns.Question, 1)
resp.Question[0] = req.Question[0]
}
resp.RecursionAvailable = true
resp.Answer = answer
return resp
}
// Perform query into Google DNS over HTTPS server
func MsgExchangeOverGoogleDOH(req *dns.Msg, rt http.RoundTripper) (resp *dns.Msg, err error) {
qtype := req.Question[0].Qtype
name := req.Question[0].Name
var ecs net.IP
opt := req.IsEdns0()
if opt != nil {
for _, s := range opt.Option {
if _ecs, ok := s.(*dns.EDNS0_SUBNET); ok {
ecs = _ecs.Address
}
}
}
dohresp, err := google.Query(rt, qtype, name, ecs.String())
if err != nil {
return nil, err
}
// Parse the google Questions to DNS RRs
questions := []dns.Question{}
for i, c := range dohresp.Question {
questions = append(questions, dns.Question{
Name: c.Name,
Qtype: uint16(c.Type),
Qclass: req.Question[i].Qclass,
})
}
// Parse google RRs to DNS RRs
answers := []dns.RR{}
for _, a := range dohresp.Answer {
answers = append(answers, RRNewFromGoogleDohRR(a))
}
// Parse google RRs to DNS RRs
authorities := []dns.RR{}
for _, ns := range dohresp.Authority {
authorities = append(authorities, RRNewFromGoogleDohRR(ns))
}
// Parse google RRs to DNS RRs
extras := []dns.RR{}
for _, a := range dohresp.Additional {
extras = append(extras, RRNewFromGoogleDohRR(a))
}
resp = &dns.Msg{
MsgHdr: dns.MsgHdr{
Id: req.Id,
Response: (dohresp.Status == 0),
Opcode: dns.OpcodeQuery,
Authoritative: false,
Truncated: dohresp.TC,
RecursionDesired: dohresp.RD,
RecursionAvailable: dohresp.RA,
//Zero: false,
AuthenticatedData: dohresp.AD,
CheckingDisabled: dohresp.CD,
Rcode: int(dohresp.Status),
},
Compress: req.Compress,
Question: questions,
Answer: answers,
Ns: authorities,
Extra: extras,
}
if ecs != nil {
MsgSetECSWithAddr(resp, ecs)
}
return resp, nil
}
// set edns-client-subnet ip
func MsgSetECSWithAddr(m *dns.Msg, addr net.IP) {
if addr == nil {
return
}
option := m.IsEdns0()
if option == nil {
option = new(dns.OPT)
option.Hdr.Name = "."
option.Hdr.Rrtype = dns.TypeOPT
m.Extra = append(m.Extra, option)
}
var ecs *dns.EDNS0_SUBNET
for _, s := range option.Option {
if _ecs, ok := s.(*dns.EDNS0_SUBNET); ok {
ecs = _ecs
break
}
}
if ecs == nil {
ecs = new(dns.EDNS0_SUBNET)
option.Option = append(option.Option, ecs)
}
ecs.Code = dns.EDNS0SUBNET
ecs.Address = addr
if addr.To4() != nil {
ecs.Family = 1 // 1 for IPv4 source address, 2 for IPv6
ecs.SourceNetmask = 32 // 32 for IPV4, 128 for IPv6
} else {
ecs.Family = 2 // 1 for IPv4 source address, 2 for IPv6
ecs.SourceNetmask = 128 // 32 for IPV4, 128 for IPv6
}
ecs.SourceScope = 0
}
// extract answer from dns msg
// FIXME: deal with name alias
func MsgExtractAnswer(msg *dns.Msg) (dns.RR, net.IP) {
if msg == nil {
return nil, nil
}
for _, ans := range msg.Answer {
switch v := ans.(type) {
case *dns.A:
if v != nil && len(v.A) != 0 {
return v, v.A
}
case *dns.AAAA:
if v != nil && len(v.AAAA) != 0 {
return v, v.AAAA
}
}
}
return nil, nil
}
// --- impl dns.RR
// Initialize a new RRGeneric from a google dns over https RR
func RRNewFromGoogleDohRR(grr google.DNSRR) dns.RR {
var rr dns.RR
// Build an RR header
rrhdr := dns.RR_Header{
Name: grr.Name,
Rrtype: uint16(grr.Type),
Class: dns.ClassINET,
Ttl: uint32(grr.TTL),
Rdlength: uint16(len(grr.Data)),
}
constructor, ok := dns.TypeToRR[uint16(grr.Type)]
if ok {
// Construct a new RR
rr = constructor()
*(rr.Header()) = rrhdr
switch v := rr.(type) {
case *dns.A:
v.A = net.ParseIP(grr.Data)
case *dns.AAAA:
v.AAAA = net.ParseIP(grr.Data)
}
} else {
rr = dns.RR(&dns.RFC3597{
Hdr: rrhdr,
Rdata: grr.Data,
})
}
return rr
}
// client for dns query
type dnsTransport struct {
nameserver string // DNS server
net string // ["tcp" | "udp" | "https"]
proxy proxy.Dialer // proxy for dns query, set to nil if don't need proxy
}
// --- impl *dnsTransport
func NewDnsTransport(nameserver, net string, _proxy proxy.Dialer) *dnsTransport {
return &dnsTransport{nameserver: nameserver, net: net, proxy: _proxy}
}
func (dt *dnsTransport) legallySpawnQuery(domain string, qtype uint16, ecsAddr ...net.IP) (*dns.Msg, error) {
req := &dns.Msg{}
req.SetQuestion(dns.Fqdn(domain), qtype)
if ecsAddr != nil {
MsgSetECSWithAddr(req, ecsAddr[0])
}
return dt.legallySpawnExchange(req)
}
func (dt *dnsTransport) legallySpawnExchange(req *dns.Msg) (*dns.Msg, error) {
const spawnNum int8 = 3
resp := make(chan *dns.Msg, spawnNum)
lastErr := make(chan error)
var failedTimes int32
for range [spawnNum]struct{}{} {
go func() {
if r, err := dt.Exchange(req); err == nil {
resp <- r
} else {
if atomic.LoadInt32(&failedTimes) == int32(spawnNum-1) {
resp <- nil
lastErr <- err
} else {
atomic.AddInt32(&failedTimes, 1)
}
}
}()
}
if r := <-resp; r != nil {
return r, nil
} else {
return nil, <-lastErr
}
}
func (dt *dnsTransport) Exchange(req *dns.Msg) (r *dns.Msg, err error) {
if dt.net == "https" {
var dialc func(ctx context.Context, network, addr string) (net.Conn, error)
if dt.proxy != nil {
dialc = func(ctx context.Context, network, addr string) (net.Conn, error) {
return dt.proxy.Dial(network, addr)
}
}
rt := &http.Transport{
DisableKeepAlives: true,
DialContext: dialc,
}
return MsgExchangeOverGoogleDOH(req, rt)
}
// --- partially copied from (*dns.Client).exchange
const dnsTimeout time.Duration = 2 * time.Second
var conn net.Conn
if p := dt.proxy; p != nil {
conn, err = p.Dial(dt.net, dt.nameserver)
} else {
conn, err = net.DialTimeout(dt.net, dt.nameserver, dnsTimeout)
}
if err != nil {
return nil, errors.WithStack(err)
}
defer conn.Close()
co := new(dns.Conn)
co.Conn = conn
opt := req.IsEdns0()
// If EDNS0 is used use that for size.
if opt != nil && opt.UDPSize() >= dns.MinMsgSize {
co.UDPSize = opt.UDPSize()
}
co.SetWriteDeadline(time.Now().Add(dnsTimeout))
if err = co.WriteMsg(req); err != nil {
return nil, errors.WithStack(err)
}
co.SetReadDeadline(time.Now().Add(dnsTimeout))
r, err = co.ReadMsg()
if err == nil && r.Id != req.Id {
err = dns.ErrId
}
return r, errors.WithStack(err)
}