From 365aea321109fe3ed1145e0954809cc88d168d43 Mon Sep 17 00:00:00 2001 From: Michele Spagnuolo Date: Wed, 4 Oct 2023 16:52:44 +0200 Subject: [PATCH] Check for a successful answer before rewriting TTL. --- proxy/server.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/proxy/server.go b/proxy/server.go index fca54d9..fe50865 100644 --- a/proxy/server.go +++ b/proxy/server.go @@ -223,11 +223,6 @@ func (s *Server) timer(ctx context.Context) { func (s *Server) forwardMessageAndCacheResponse(q *dns.Msg) (m *dns.Msg) { m = s.forwardMessageAndGetResponse(q) - // Rewrite the TTL. - for _, a := range m.Answer { - // If the TTL provided upstream is smaller than `minTTL`, rewrite it. - a.Header().Ttl = uint32(max(a.Header().Ttl, uint32(s.minTTL))) - } // Let's retry a few times if we can't resolve it at the first try. for c := 0; m == nil && c < connectionsPerUpstream; c++ { s.Log.Debugf("Retrying %q [%d/%d]...", q.Question, c+1, connectionsPerUpstream) @@ -237,6 +232,13 @@ func (s *Server) forwardMessageAndCacheResponse(q *dns.Msg) (m *dns.Msg) { s.Log.Infof("Giving up on %q after %d connection retries.", q.Question, connectionsPerUpstream) return nil } + if m.Answer != nil { + // Rewrite the TTL. + for _, a := range m.Answer { + // If the TTL provided upstream is smaller than `minTTL`, rewrite it. + a.Header().Ttl = uint32(max(a.Header().Ttl, uint32(s.minTTL))) + } + } s.cache.put(q, m) return m }