From e1ec0d25a6b05b857fd518f6b34b876528113b70 Mon Sep 17 00:00:00 2001 From: Jiahao Lu Date: Sat, 29 Jul 2023 13:18:12 +0800 Subject: [PATCH] Fix: DNS cache max-age (#2850) Regarding DNS cache, it's advisable to not rely on the TTL values mentioned in the Ns and Extra sections. Otherwise, any DNS queries that do not yield any results (such as for non-existent.example.com) will be accidentally cached. The need for the ACME challenge hack has been eliminated and as such, it has been removed. --- dns/util.go | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/dns/util.go b/dns/util.go index 0600360..fea2f65 100644 --- a/dns/util.go +++ b/dns/util.go @@ -6,18 +6,19 @@ import ( "errors" "fmt" "net" - "strings" "time" "github.com/Dreamacro/clash/common/cache" "github.com/Dreamacro/clash/common/picker" - "github.com/Dreamacro/clash/log" D "github.com/miekg/dns" "github.com/samber/lo" ) func minimalTTL(records []D.RR) uint32 { + if len(records) == 0 { + return 0 + } return lo.MinBy(records, func(r1 D.RR, r2 D.RR) bool { return r1.Header().Ttl < r2.Header().Ttl }).Header().Ttl @@ -34,26 +35,11 @@ func updateTTL(records []D.RR, ttl uint32) { } func putMsgToCache(c *cache.LruCache, key string, 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) + ttl := minimalTTL(msg.Answer) + if ttl == 0 { return } - - var ttl uint32 - switch { - case len(msg.Answer) != 0: - ttl = minimalTTL(msg.Answer) - case len(msg.Ns) != 0: - ttl = minimalTTL(msg.Ns) - case len(msg.Extra) != 0: - ttl = minimalTTL(msg.Extra) - default: - log.Debugln("[DNS] response msg empty: %#v", msg) - return - } - - c.SetWithExpire(key, msg.Copy(), time.Now().Add(time.Second*time.Duration(ttl))) + c.SetWithExpire(key, msg.Copy(), time.Now().Add(time.Duration(ttl)*time.Second)) } func setMsgTTL(msg *D.Msg, ttl uint32) {