Skip to content

Commit

Permalink
Fix: DNS cache max-age (#2850)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lujjjh authored Jul 29, 2023
1 parent c1d027d commit e1ec0d2
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions dns/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
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)
ttl := minimalTTL(msg.Answer)
if ttl == 0 {
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) {
Expand Down

0 comments on commit e1ec0d2

Please sign in to comment.