Improve debug information in DNS responses
This commit is contained in:
@@ -1,36 +1,55 @@
|
||||
package nameserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/safing/portbase/log"
|
||||
"github.com/safing/portmaster/nameserver/nsutil"
|
||||
"github.com/safing/portmaster/network"
|
||||
)
|
||||
|
||||
// sendResponse sends a response to query using w. If reasonCtx is not
|
||||
// nil and implements either the Responder or RRProvider interface then
|
||||
// those functions are used to craft a DNS response. If reasonCtx is nil
|
||||
// or does not implement the Responder interface and verdict is not set
|
||||
// to failed a ZeroIP response will be sent. If verdict is set to failed
|
||||
// then a ServFail will be sent instead.
|
||||
func sendResponse(w dns.ResponseWriter, query *dns.Msg, verdict network.Verdict, reason string, reasonCtx interface{}) {
|
||||
responder, ok := reasonCtx.(nsutil.Responder)
|
||||
if !ok {
|
||||
if verdict == network.VerdictFailed {
|
||||
responder = nsutil.ServeFail()
|
||||
} else {
|
||||
responder = nsutil.ZeroIP()
|
||||
}
|
||||
// sendResponse sends a response to query using w. The response message is
|
||||
// created by responder. If addExtraRRs is not nil and implements the
|
||||
// RRProvider interface then it will be also used to add more RRs in the
|
||||
// extra section.
|
||||
func sendResponse(
|
||||
ctx context.Context,
|
||||
w dns.ResponseWriter,
|
||||
request *dns.Msg,
|
||||
responder nsutil.Responder,
|
||||
rrProviders ...nsutil.RRProvider,
|
||||
) error {
|
||||
// Have the Responder craft a DNS reply.
|
||||
reply := responder.ReplyWithDNS(ctx, request)
|
||||
if reply == nil {
|
||||
// Dropping query.
|
||||
return nil
|
||||
}
|
||||
|
||||
reply := responder.ReplyWithDNS(query, reason, reasonCtx)
|
||||
|
||||
if extra, ok := reasonCtx.(nsutil.RRProvider); ok {
|
||||
rrs := extra.GetExtraRR(query, reason, reasonCtx)
|
||||
// Add extra RRs through a custom RRProvider.
|
||||
for _, rrProvider := range rrProviders {
|
||||
rrs := rrProvider.GetExtraRRs(ctx, request)
|
||||
reply.Extra = append(reply.Extra, rrs...)
|
||||
}
|
||||
|
||||
// Write reply.
|
||||
if err := writeDNSResponse(w, reply); err != nil {
|
||||
log.Errorf("nameserver: failed to send response: %s", err)
|
||||
return fmt.Errorf("nameserver: failed to send response: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeDNSResponse(w dns.ResponseWriter, m *dns.Msg) (err error) {
|
||||
defer func() {
|
||||
// recover from panic
|
||||
if panicErr := recover(); panicErr != nil {
|
||||
err = fmt.Errorf("panic: %s", panicErr)
|
||||
log.Warningf("nameserver: panic caused by this msg: %#v", m)
|
||||
}
|
||||
}()
|
||||
|
||||
err = w.WriteMsg(m)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user