@@ -57,6 +57,7 @@ func (s *Server) Shutdown() {
57
57
}
58
58
59
59
func newStaticClientConfig (ips []string ) (* dns.ClientConfig , error ) {
60
+ logrus .Infof ("newStaticClientConfig creating config for the the following IPs: %v" , ips )
60
61
s := ``
61
62
for _ , ip := range ips {
62
63
s += fmt .Sprintf ("nameserver %s\n " , ip )
@@ -120,6 +121,7 @@ func (h *Handler) handleQuery(w dns.ResponseWriter, req *dns.Msg) {
120
121
handled bool
121
122
)
122
123
reply .SetReply (req )
124
+ logrus .Debugf ("handleQuery received DNS query: %v" , req )
123
125
for _ , q := range req .Question {
124
126
hdr := dns.RR_Header {
125
127
Name : q .Name ,
@@ -128,7 +130,7 @@ func (h *Handler) handleQuery(w dns.ResponseWriter, req *dns.Msg) {
128
130
Ttl : 5 ,
129
131
}
130
132
qtype := q .Qtype
131
- switch qtype {
133
+ switch q . Qtype {
132
134
case dns .TypeAAAA :
133
135
if ! h .ipv6 {
134
136
// A "correct" answer would be to set `handled = true` and return a NODATA response.
@@ -209,63 +211,71 @@ func (h *Handler) handleQuery(w dns.ResponseWriter, req *dns.Msg) {
209
211
}
210
212
case dns .TypeTXT :
211
213
txt , err := net .LookupTXT (q .Name )
212
- if err == nil && len (txt ) > 0 {
213
- for _ , s := range txt {
214
- a := & dns.TXT {
215
- Hdr : hdr ,
216
- }
217
- // Per RFC7208 3.3, when a TXT answer has multiple strings, the answer must be treated as
218
- // a single concatenated string. net.LookupTXT is pre-concatenating such answers, which
219
- // means we need to break it back up for this resolver to return a valid response.
220
- a .Txt = chunkify (s , 255 )
221
- reply .Answer = append (reply .Answer , a )
222
- handled = true
214
+ if err != nil {
215
+ logrus .Debugf ("handleQuery lookup TXT failed: %v" , err )
216
+ continue
217
+ }
218
+ for _ , s := range txt {
219
+ a := & dns.TXT {
220
+ Hdr : hdr ,
223
221
}
222
+ // Per RFC7208 3.3, when a TXT answer has multiple strings, the answer must be treated as
223
+ // a single concatenated string. net.LookupTXT is pre-concatenating such answers, which
224
+ // means we need to break it back up for this resolver to return a valid response.
225
+ a .Txt = chunkify (s , 255 )
226
+ reply .Answer = append (reply .Answer , a )
227
+ handled = true
224
228
}
225
229
case dns .TypeNS :
226
230
ns , err := net .LookupNS (q .Name )
227
- if err == nil && len ( ns ) > 0 {
228
- for _ , s := range ns {
229
- if s . Host != "" {
230
- a := & dns. NS {
231
- Hdr : hdr ,
232
- Ns : s .Host ,
233
- }
234
- reply . Answer = append ( reply . Answer , a )
235
- handled = true
231
+ if err != nil {
232
+ logrus . Debugf ( "handleQuery lookup NS failed: %v" , err )
233
+ continue
234
+ }
235
+ for _ , s := range ns {
236
+ if s .Host != "" {
237
+ a := & dns. NS {
238
+ Hdr : hdr ,
239
+ Ns : s . Host ,
236
240
}
241
+ reply .Answer = append (reply .Answer , a )
242
+ handled = true
237
243
}
238
244
}
239
245
case dns .TypeMX :
240
246
mx , err := net .LookupMX (q .Name )
241
- if err == nil && len ( mx ) > 0 {
242
- for _ , s := range mx {
243
- if s . Host != "" {
244
- a := & dns. MX {
245
- Hdr : hdr ,
246
- Mx : s .Host ,
247
- Preference : s . Pref ,
248
- }
249
- reply . Answer = append ( reply . Answer , a )
250
- handled = true
247
+ if err != nil {
248
+ logrus . Debugf ( "handleQuery lookup MX failed: %v" , err )
249
+ continue
250
+ }
251
+ for _ , s := range mx {
252
+ if s .Host != "" {
253
+ a := & dns. MX {
254
+ Hdr : hdr ,
255
+ Mx : s . Host ,
256
+ Preference : s . Pref ,
251
257
}
258
+ reply .Answer = append (reply .Answer , a )
259
+ handled = true
252
260
}
253
261
}
254
262
case dns .TypeSRV :
255
263
_ , addrs , err := net .LookupSRV ("" , "" , q .Name )
256
- if err = = nil {
257
- hdr . Rrtype = dns . TypeSRV
258
- for _ , addr := range addrs {
259
- a := & dns. SRV {
260
- Hdr : hdr ,
261
- Target : addr . Target ,
262
- Port : addr . Port ,
263
- Priority : addr . Priority ,
264
- Weight : addr .Weight ,
265
- }
266
- reply . Answer = append ( reply . Answer , a )
267
- handled = true
264
+ if err ! = nil {
265
+ logrus . Debugf ( "handleQuery lookup SRV failed: %v" , err )
266
+ continue
267
+ }
268
+ hdr . Rrtype = dns . TypeSRV
269
+ for _ , addr := range addrs {
270
+ a := & dns. SRV {
271
+ Hdr : hdr ,
272
+ Target : addr .Target ,
273
+ Port : addr . Port ,
274
+ Priority : addr . Priority ,
275
+ Weight : addr . Weight ,
268
276
}
277
+ reply .Answer = append (reply .Answer , a )
278
+ handled = true
269
279
}
270
280
}
271
281
}
@@ -276,12 +286,14 @@ func (h *Handler) handleQuery(w dns.ResponseWriter, req *dns.Msg) {
276
286
if err := w .WriteMsg (& reply ); err != nil {
277
287
logrus .Debugf ("handleQuery failed writing DNS reply: %v" , err )
278
288
}
289
+
279
290
return
280
291
}
281
292
h .handleDefault (w , req )
282
293
}
283
294
284
295
func (h * Handler ) handleDefault (w dns.ResponseWriter , req * dns.Msg ) {
296
+ logrus .Debugf ("handleDefault for %v" , req )
285
297
for _ , client := range h .clients {
286
298
for _ , srv := range h .clientConfig .Servers {
287
299
addr := fmt .Sprintf ("%s:%s" , srv , h .clientConfig .Port )
@@ -334,6 +346,7 @@ func Start(opts ServerOptions) (*Server, error) {
334
346
s := & dns.Server {Net : "udp" , Addr : addr , Handler : h }
335
347
server .udp = s
336
348
go func () {
349
+ logrus .Debugf ("Start UDP server listening on: %v" , addr )
337
350
if e := s .ListenAndServe (); e != nil {
338
351
panic (e )
339
352
}
@@ -350,6 +363,7 @@ func Start(opts ServerOptions) (*Server, error) {
350
363
s := & dns.Server {Net : "tcp" , Addr : addr , Handler : h }
351
364
server .tcp = s
352
365
go func () {
366
+ logrus .Debugf ("Start TCP server listening on: %v" , addr )
353
367
if e := s .ListenAndServe (); e != nil {
354
368
panic (e )
355
369
}
0 commit comments