@@ -23,6 +23,7 @@ type HandlerOptions struct {
23
23
IPv6 bool
24
24
StaticHosts map [string ]string
25
25
UpstreamServers []string
26
+ TruncateReply bool
26
27
}
27
28
28
29
type ServerOptions struct {
@@ -33,9 +34,10 @@ type ServerOptions struct {
33
34
}
34
35
35
36
type Handler struct {
37
+ truncate bool
36
38
clientConfig * dns.ClientConfig
37
39
clients []* dns.Client
38
- IPv6 bool
40
+ ipv6 bool
39
41
cnameToHost map [string ]string
40
42
hostToIP map [string ]net.IP
41
43
}
@@ -98,7 +100,7 @@ func NewHandler(opts HandlerOptions) (dns.Handler, error) {
98
100
h := & Handler {
99
101
clientConfig : cc ,
100
102
clients : clients ,
101
- IPv6 : opts .IPv6 ,
103
+ ipv6 : opts .IPv6 ,
102
104
cnameToHost : make (map [string ]string ),
103
105
hostToIP : make (map [string ]net.IP ),
104
106
}
@@ -128,7 +130,7 @@ func (h *Handler) handleQuery(w dns.ResponseWriter, req *dns.Msg) {
128
130
qtype := q .Qtype
129
131
switch qtype {
130
132
case dns .TypeAAAA :
131
- if ! h .IPv6 {
133
+ if ! h .ipv6 {
132
134
// A "correct" answer would be to set `handled = true` and return a NODATA response.
133
135
// Unfortunately some older resolvers use a slow random source to set the transaction id.
134
136
// This creates a problem on M1 computers, which are too fast for that implementation:
@@ -268,8 +270,12 @@ func (h *Handler) handleQuery(w dns.ResponseWriter, req *dns.Msg) {
268
270
}
269
271
}
270
272
if handled {
271
- reply .Truncate (truncateSize )
272
- _ = w .WriteMsg (& reply )
273
+ if h .truncate {
274
+ reply .Truncate (truncateSize )
275
+ }
276
+ if err := w .WriteMsg (& reply ); err != nil {
277
+ logrus .Debugf ("handleQuery failed writing DNS reply: %v" , err )
278
+ }
273
279
return
274
280
}
275
281
h .handleDefault (w , req )
@@ -280,17 +286,29 @@ func (h *Handler) handleDefault(w dns.ResponseWriter, req *dns.Msg) {
280
286
for _ , srv := range h .clientConfig .Servers {
281
287
addr := fmt .Sprintf ("%s:%s" , srv , h .clientConfig .Port )
282
288
reply , _ , err := client .Exchange (req , addr )
283
- if err == nil {
289
+ if err != nil {
290
+ logrus .Debugf ("handleDefault failed to perform a synchronous query with upstream [%v]: %v" , addr , err )
291
+ continue
292
+ }
293
+ if h .truncate {
294
+ logrus .Debugf ("handleDefault truncating reply: %v" , reply )
284
295
reply .Truncate (truncateSize )
285
- _ = w .WriteMsg (reply )
286
- return
287
296
}
297
+ if err = w .WriteMsg (reply ); err != nil {
298
+ logrus .Debugf ("handleDefault failed writing DNS reply to [%v]: %v" , addr , err )
299
+ }
300
+ return
288
301
}
289
302
}
290
303
var reply dns.Msg
291
304
reply .SetReply (req )
292
- reply .Truncate (truncateSize )
293
- _ = w .WriteMsg (& reply )
305
+ if h .truncate {
306
+ logrus .Debugf ("handleDefault truncating reply: %v" , reply )
307
+ reply .Truncate (truncateSize )
308
+ }
309
+ if err := w .WriteMsg (& reply ); err != nil {
310
+ logrus .Debugf ("handleDefault failed writing DNS reply: %v" , err )
311
+ }
294
312
}
295
313
296
314
func (h * Handler ) ServeDNS (w dns.ResponseWriter , req * dns.Msg ) {
@@ -303,13 +321,16 @@ func (h *Handler) ServeDNS(w dns.ResponseWriter, req *dns.Msg) {
303
321
}
304
322
305
323
func Start (opts ServerOptions ) (* Server , error ) {
306
- h , err := NewHandler (opts .HandlerOptions )
307
- if err != nil {
308
- return nil , err
309
- }
310
324
server := & Server {}
311
325
if opts .UDPPort > 0 {
312
- addr := fmt .Sprintf ("127.0.0.1:%d" , opts .UDPPort )
326
+ udpOpts := opts
327
+ // always enable reply truncate for UDP
328
+ udpOpts .TruncateReply = true
329
+ h , err := NewHandler (udpOpts .HandlerOptions )
330
+ if err != nil {
331
+ return nil , err
332
+ }
333
+ addr := fmt .Sprintf ("%s:%d" , opts .Address , opts .UDPPort )
313
334
s := & dns.Server {Net : "udp" , Addr : addr , Handler : h }
314
335
server .udp = s
315
336
go func () {
@@ -319,7 +340,13 @@ func Start(opts ServerOptions) (*Server, error) {
319
340
}()
320
341
}
321
342
if opts .TCPPort > 0 {
322
- addr := fmt .Sprintf ("127.0.0.1:%d" , opts .TCPPort )
343
+ tcpOpts := opts
344
+ tcpOpts .TruncateReply = false
345
+ h , err := NewHandler (tcpOpts .HandlerOptions )
346
+ if err != nil {
347
+ return nil , err
348
+ }
349
+ addr := fmt .Sprintf ("%s:%d" , opts .Address , opts .TCPPort )
323
350
s := & dns.Server {Net : "tcp" , Addr : addr , Handler : h }
324
351
server .tcp = s
325
352
go func () {
0 commit comments