MCPcopy
hub / github.com/monasticacademy/httptap / handleDNSQuery

Function handleDNSQuery

dns.go:253–328  ·  view source on GitHub ↗

handleDNSQuery answers DNS queries according to: net.DefaultResolver if the DNS request is A or AAAA cloudflare DNS for other DNS requests It always returns the special IP 169.254.77.65 for the special name host.httptap.local. Traffic sent to this address is routed to the loopback interface on t

(ctx context.Context, req *dns.Msg)

Source from the content-addressed store, hash-verified

251// Traffic sent to this address is routed to the loopback interface on the host (different
252// from the loopback device seen by the subprocess)
253func handleDNSQuery(ctx context.Context, req *dns.Msg) ([]dns.RR, error) {
254 const upstreamDNS = "1.1.1.1:53" // TODO: get from resolv.conf and nsswitch.conf
255
256 if len(req.Question) == 0 {
257 return nil, nil // this means no answer, no error, which is fine
258 }
259
260 question := req.Question[0]
261 questionType := dnsTypeCode(question.Qtype)
262
263 verbosef("got dns request for %v (%v)", question.Name, questionType)
264
265 // handle the request ourselves
266 switch question.Qtype {
267 case dns.TypeA:
268 var ips []net.IP
269 if ip, ok := specialAddresses[question.Name]; ok {
270 ips = append(ips, ip)
271 } else {
272 var err error
273 ips, err = net.DefaultResolver.LookupIP(ctx, "ip4", question.Name)
274 if err != nil {
275 return nil, fmt.Errorf("for an A record the default resolver said: %w", err)
276 }
277 }
278
279 verbosef("resolved %v to %v with default resolver", question.Name, ips)
280
281 var rrs []dns.RR
282 for _, ip := range ips {
283 rr, err := dns.NewRR(fmt.Sprintf("%s A %s", question.Name, ip))
284 if err != nil {
285 return nil, fmt.Errorf("error constructing rr: %w", err)
286 }
287 rrs = append(rrs, rr)
288 }
289 return rrs, nil
290
291 case dns.TypeAAAA:
292 ips, err := net.DefaultResolver.LookupIP(ctx, "ip6", question.Name)
293 if err != nil {
294 return nil, fmt.Errorf("for an AAAA record the default resolver said (AAAA record): %w", err)
295 }
296
297 verbosef("resolved %v to %v with default resolver", question.Name, ips)
298
299 var rrs []dns.RR
300 for _, ip := range ips {
301 rr, err := dns.NewRR(fmt.Sprintf("%s AAAA %s", question.Name, ip))
302 if err != nil {
303 return nil, fmt.Errorf("error constructing rr: %w", err)
304 }
305 rrs = append(rrs, rr)
306 }
307 return rrs, nil
308 }
309
310 verbosef("proxying %s request to upstream DNS server...", questionType)

Callers 1

handleDNSFunction · 0.85

Calls 2

dnsTypeCodeFunction · 0.85
verbosefFunction · 0.70

Tested by

no test coverage detected