Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase DNS timeout #1346

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/tools/dnsutils/dnsutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func ListenAndServe(ctx context.Context, handler Handler, listenOn string) {

for _, network := range networks {
var server = &dns.Server{Addr: listenOn, Net: network, Handler: dns.HandlerFunc(func(w dns.ResponseWriter, m *dns.Msg) {
var timeoutCtx, cancel = context.WithTimeout(context.Background(), time.Second)
var timeoutCtx, cancel = context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

handler.ServeDNS(timeoutCtx, w, m)
Expand Down
35 changes: 29 additions & 6 deletions pkg/tools/dnsutils/memory/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,41 @@ import (

const defaultTTL = 3600

// Since memory is supposed to be one of the targets that stores information, we have to keep track of whether something has been written to the writer.
// We must write something into the writer, this is how the dns package works. Otherwise, we get a timeout error on the client side.

type responseWriter struct {
dns.ResponseWriter
passed bool
}

func (r *responseWriter) WriteMsg(m *dns.Msg) error {
r.passed = true
return r.ResponseWriter.WriteMsg(m)
}

type memoryHandler struct {
recoreds *Map
records *Map
}

func (f *memoryHandler) ServeDNS(ctx context.Context, rw dns.ResponseWriter, msg *dns.Msg) {
if len(msg.Question) == 0 {
next.Handler(ctx).ServeDNS(ctx, rw, msg)
dns.HandleFailed(rw, msg)
return
}

rwWrapper := &responseWriter{
ResponseWriter: rw,
}

var name = dns.Name(msg.Question[0].Name).String()
var records, ok = f.recoreds.Load(name)
var records, ok = f.records.Load(name)

if !ok {
next.Handler(ctx).ServeDNS(ctx, rw, msg)
next.Handler(ctx).ServeDNS(ctx, rwWrapper, msg)
if !rwWrapper.passed {
dns.HandleFailed(rw, msg)
}
return
}

Expand All @@ -59,7 +79,10 @@ func (f *memoryHandler) ServeDNS(ctx context.Context, rw dns.ResponseWriter, msg
}

if len(resp.Answer) == 0 {
next.Handler(ctx).ServeDNS(ctx, rw, msg)
next.Handler(ctx).ServeDNS(ctx, rwWrapper, msg)
if !rwWrapper.passed {
dns.HandleFailed(rw, msg)
}
return
}

Expand All @@ -73,7 +96,7 @@ func NewDNSHandler(records *Map) dnsutils.Handler {
if records == nil {
panic("records cannot be nil")
}
return &memoryHandler{recoreds: records}
return &memoryHandler{records: records}
}
func a(domain string, ips []net.IP) []dns.RR {
answers := make([]dns.RR, len(ips))
Expand Down