chore: don't modify msg when getting minimalTTL

This commit is contained in:
wwqgtxx committed 2026-03-21 17:16:28 +08:00
1 parent ae374f615d
commit c9bc2d3926
2 files changed
+26 -13

No files matched your search

+3 -10
View File
@@ -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)
}
}()
+23 -3
View File
@@ -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) {