Skip to content

Commit fafb92d

Browse files
committed
xdns: only bust cache (0 ttls) for a, aaaa records
1 parent 3d2c8dc commit fafb92d

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

intra/xdns/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func Net2ProxyID(network string) (proto string, pids []string) {
140140
func BustAndroidCacheIfNeeded(ans *dns.Msg) bool {
141141
if BustDnsproxydResNetCache {
142142
// TODO: skip negative records (SOA, NXDOMAIN, etc)
143-
return WithTtl(ans, ZeroTTL)
143+
return WithTtl(ans, ZeroTTL, dns.TypeA, dns.TypeAAAA)
144144
}
145145
return false
146146
}

intra/xdns/dnsutil.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func EmptyResponseFromMessage(srcMsg *dns.Msg) *dns.Msg {
9595
return nil
9696
}
9797
dstMsg := dns.Msg{
98-
MsgHdr: srcMsg.MsgHdr,
98+
MsgHdr: srcMsg.MsgHdr, // copy id, flags, etc
9999
Compress: true,
100100
}
101101
dstMsg.Question = srcMsg.Question
@@ -174,12 +174,25 @@ func Rcode(msg *dns.Msg) int {
174174
return dns.RcodeFormatError
175175
}
176176

177-
func WithTtl(msg *dns.Msg, secs uint32) (ok bool) {
177+
func WithTtl(msg *dns.Msg, secs uint32, typ ...uint16) (ok bool) {
178178
if !HasAnyAnswer(msg) {
179179
return ok
180180
}
181181
for _, a := range msg.Answer {
182-
if a.Header().Ttl > 0 {
182+
if a == nil {
183+
continue
184+
}
185+
if a.Header().Ttl <= 0 || a.Header().Ttl == secs {
186+
continue
187+
}
188+
resetTtl := len(typ) <= 0
189+
for _, t := range typ {
190+
if a.Header().Rrtype == t {
191+
resetTtl = true
192+
break
193+
}
194+
}
195+
if resetTtl {
183196
a.Header().Ttl = secs
184197
ok = true
185198
}
@@ -620,7 +633,7 @@ func AQuadAForQuery(q *dns.Msg, ips ...netip.Addr) (a *dns.Msg, err error) {
620633
if q == nil {
621634
return nil, errNoPacket
622635
}
623-
a = EmptyResponseFromMessage(q) // may be nil
636+
a = EmptyResponseFromMessage(q) // may return nil
624637
if a == nil {
625638
return nil, errNoPacket
626639
}

0 commit comments

Comments
 (0)