Skip to content

Commit

Permalink
Fix: revert the dev commit to fix ttl bug
Browse files Browse the repository at this point in the history
Signed-off-by: Fxzx micah <48860358+fxzxmicah@users.noreply.github.com>
  • Loading branch information
fxzxmicah committed Jan 18, 2024
1 parent b023f50 commit 26bce1d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 29 deletions.
3 changes: 1 addition & 2 deletions dns/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ func (r *Resolver) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, e
setMsgTTL(msg, uint32(1)) // Continue fetch
go r.exchangeWithoutCache(ctx, m)
} else {
// updating TTL by subtracting common delta time from each DNS record
updateMsgTTL(msg, uint32(time.Until(expireTime).Seconds()))
setMsgTTL(msg, uint32(time.Until(expireTime).Seconds()))
}
return
}
Expand Down
29 changes: 3 additions & 26 deletions dns/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,8 @@ import (
"github.com/Dreamacro/clash/log"

D "github.com/miekg/dns"
"github.com/samber/lo"
)

func minimalTTL(records []D.RR) uint32 {
return lo.MinBy(records, func(r1 D.RR, r2 D.RR) bool {
return r1.Header().Ttl < r2.Header().Ttl
}).Header().Ttl
}

func updateTTL(records []D.RR, ttl uint32) {
if len(records) == 0 {
return
}
delta := minimalTTL(records) - ttl
for i := range records {
records[i].Header().Ttl = lo.Clamp(records[i].Header().Ttl-delta, 1, records[i].Header().Ttl)
}
}

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.") {
Expand All @@ -43,11 +26,11 @@ func putMsgToCache(c *cache.LruCache, key string, q D.Question, msg *D.Msg) {
var ttl uint32
switch {
case len(msg.Answer) != 0:
ttl = minimalTTL(msg.Answer)
ttl = msg.Answer[0].Header().Ttl
case len(msg.Ns) != 0:
ttl = minimalTTL(msg.Ns)
ttl = msg.Ns[0].Header().Ttl
case len(msg.Extra) != 0:
ttl = minimalTTL(msg.Extra)
ttl = msg.Extra[0].Header().Ttl
default:
log.Debugln("[DNS] response msg empty: %#v", msg)
return
Expand All @@ -70,12 +53,6 @@ func setMsgTTL(msg *D.Msg, ttl uint32) {
}
}

func updateMsgTTL(msg *D.Msg, ttl uint32) {
updateTTL(msg.Answer, ttl)
updateTTL(msg.Ns, ttl)
updateTTL(msg.Extra, ttl)
}

func isIPRequest(q D.Question) bool {
return q.Qclass == D.ClassINET && (q.Qtype == D.TypeA || q.Qtype == D.TypeAAAA)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/fxzxmicah/clash-core

go 1.20

require github.com/Dreamacro/clash v1.11.8
require github.com/Dreamacro/clash v1.15.0

replace github.com/Dreamacro/clash => ./

Expand Down

0 comments on commit 26bce1d

Please sign in to comment.