mirror of https://github.com/Dreamacro/clash.git
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.
This commit is contained in:
parent
c1d027d6d1
commit
e1ec0d25a6
26
dns/util.go
26
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) {
|
||||
|
|
Loading…
Reference in New Issue