From c9bc2d3926c8b5a3b56a354fb3cede7efb69bef3 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Sat, 21 Mar 2026 15:00:14 +0800 Subject: [PATCH] chore: don't modify msg when getting minimalTTL --- dns/resolver.go | 13 +++---------- dns/util.go | 26 +++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/dns/resolver.go b/dns/resolver.go index 9184fbfb2..929592981 100644 --- a/dns/resolver.go +++ b/dns/resolver.go @@ -165,11 +165,10 @@ func (r *Resolver) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, e q := m.Question[0] domain := msgToDomain(m) - cacheM, expireTime, hit := r.cache.GetWithExpire(q.String()) + msg, expireTime, hit := getMsgFromCache(r.cache, q) if hit { - log.Debugln("[DNS] cache hit %s --> %s, expire at %s", domain, msgToLogString(cacheM), expireTime.Format("2006-01-02 15:04:05")) + log.Debugln("[DNS] cache hit %s --> %s, expire at %s", domain, msgToLogString(msg), expireTime.Format("2006-01-02 15:04:05")) now := time.Now() - msg = cacheM.Copy() if expireTime.Before(now) { setMsgTTL(msg, uint32(1)) // Continue fetch continueFetch = true @@ -201,14 +200,8 @@ func (r *Resolver) exchangeWithoutCache(ctx context.Context, m *D.Msg) (msg *D.M return } - msg := result - if cache { - // OPT RRs MUST NOT be cached, forwarded, or stored in or loaded from master files. - msg.Extra = lo.Filter(msg.Extra, func(rr D.RR, index int) bool { - return rr.Header().Rrtype != D.TypeOPT - }) - putMsgToCache(r.cache, q.String(), q, msg) + putMsgToCache(r.cache, q, result) } }() diff --git a/dns/util.go b/dns/util.go index b7082818c..ef57a7c24 100644 --- a/dns/util.go +++ b/dns/util.go @@ -45,7 +45,19 @@ func updateTTL(records []D.RR, ttl uint32) { } } -func putMsgToCache(c dnsCache, key string, q D.Question, msg *D.Msg) { +// getMsgFromCache returns a cached dns message if it exists, otherwise returns nil. +// the returned msg is a copy of the original msg, so it can be modified without affecting the original msg. +func getMsgFromCache(c dnsCache, q D.Question) (*D.Msg, time.Time, bool) { + msg, expireTime, hit := c.GetWithExpire(q.String()) + if msg != nil { + msg = msg.Copy() // never modify the original msg + } + return msg, expireTime, hit +} + +// putMsgToCache puts a dns message into the cache. +// the msg is copied before being stored in the cache, so it can be modified without affecting the original msg. +func putMsgToCache(c dnsCache, q D.Question, msg *D.Msg) { // skip dns cache for acme challenge if q.Qtype == D.TypeTXT && strings.HasPrefix(q.Name, "_acme-challenge.") { log.Debugln("[DNS] dns cache ignored because of acme challenge for: %s", q.Name) @@ -58,12 +70,20 @@ func putMsgToCache(c dnsCache, key string, q D.Question, msg *D.Msg) { // If it does so it MUST NOT cache it for longer than five (5) minutes [...] ttl = serverFailureCacheTTL } else { - ttl = minimalTTL(append(append(msg.Answer, msg.Ns...), msg.Extra...)) + ttl = minimalTTL(lo.Concat(msg.Answer, msg.Ns, msg.Extra)) } if ttl == 0 { return } - c.SetWithExpire(key, msg.Copy(), time.Now().Add(time.Duration(ttl)*time.Second)) + + msg = msg.Copy() // never modify the original msg + + // OPT RRs MUST NOT be cached, forwarded, or stored in or loaded from master files. + msg.Extra = lo.Filter(msg.Extra, func(rr D.RR, index int) bool { + return rr.Header().Rrtype != D.TypeOPT + }) + + c.SetWithExpire(q.String(), msg, time.Now().Add(time.Duration(ttl)*time.Second)) } func setMsgTTL(msg *D.Msg, ttl uint32) {