Skip to content

Commit 81793d5

Browse files
committed
Pull request 405: 7903-fix-ttl-override
Updates AdguardTeam/AdGuardHome#7903. Squashed commit of the following: commit 0b10ece Author: Ainar Garipov <[email protected]> Date: Tue Jul 1 14:20:02 2025 +0300 proxy: imp docs commit 6b2c460 Author: Ainar Garipov <[email protected]> Date: Tue Jul 1 14:09:07 2025 +0300 proxy: fix ttl override
1 parent 6eb8891 commit 81793d5

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

proxy/cache.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
const defaultCacheSize = 64 * 1024
2222

2323
// cache is used to cache requests and used upstreams.
24+
//
25+
// TODO(a.garipov): Add [timeutil.Clock] and make tests less flaky.
2426
type cache struct {
2527
// itemsLock protects requests cache.
2628
itemsLock *sync.RWMutex

proxy/proxy.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ func (p *Proxy) Shutdown(ctx context.Context) (err error) {
426426
return nil
427427
}
428428

429-
// closeListeners closes all acrive listeners and returns the occurred errors.
429+
// closeListeners closes all active listeners and returns the occurred errors.
430430
//
431431
// TODO(e.burkov): Remove the argument if it remains unused.
432432
func (p *Proxy) closeListeners(errs []error) (res []error) {
@@ -642,15 +642,22 @@ func (p *Proxy) replyFromUpstream(d *DNSContext) (ok bool, err error) {
642642
unwrapped, stats := collectQueryStats(p.UpstreamMode, u, wrapped, wrappedFallbacks)
643643
d.queryStatistics = stats
644644

645-
p.handleExchangeResult(d, req, resp, unwrapped)
645+
ctx := context.TODO()
646+
p.handleExchangeResult(ctx, d, req, resp, unwrapped)
646647

647648
return resp != nil, err
648649
}
649650

650651
// handleExchangeResult handles the result after the upstream exchange. It sets
651-
// the response to d and sets the upstream that have resolved the request. If
652-
// the response is nil, it generates a server failure response.
653-
func (p *Proxy) handleExchangeResult(d *DNSContext, req, resp *dns.Msg, u upstream.Upstream) {
652+
// resp and the upstream that has resolved the request in d. If resp is nil, it
653+
// generates a server failure response. req must not be nil.
654+
func (p *Proxy) handleExchangeResult(
655+
ctx context.Context,
656+
d *DNSContext,
657+
req *dns.Msg,
658+
resp *dns.Msg,
659+
u upstream.Upstream,
660+
) {
654661
if resp == nil {
655662
d.Res = p.messages.NewMsgSERVFAIL(req)
656663
d.hasEDNS0 = false
@@ -661,7 +668,7 @@ func (p *Proxy) handleExchangeResult(d *DNSContext, req, resp *dns.Msg, u upstre
661668
d.Upstream = u
662669
d.Res = resp
663670

664-
p.setMinMaxTTL(resp)
671+
p.setMinMaxTTL(ctx, resp)
665672
if len(req.Question) > 0 && len(resp.Question) == 0 {
666673
// Explicitly construct the question section since some upstreams may
667674
// respond with invalidly constructed messages which cause out-of-range

proxy/server.go

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
"net"
99
"time"
1010

11+
"github.com/AdguardTeam/golibs/container"
1112
"github.com/AdguardTeam/golibs/errors"
13+
"github.com/AdguardTeam/golibs/logutil/optslog"
1214
"github.com/AdguardTeam/golibs/logutil/slogutil"
1315
"github.com/AdguardTeam/golibs/netutil"
1416
"github.com/miekg/dns"
@@ -227,15 +229,38 @@ func (p *Proxy) respond(d *DNSContext) {
227229
}
228230
}
229231

230-
// Set TTL value of all records according to our settings
231-
func (p *Proxy) setMinMaxTTL(r *dns.Msg) {
232-
for _, rr := range r.Answer {
233-
originalTTL := rr.Header().Ttl
234-
newTTL := respectTTLOverrides(originalTTL, p.CacheMinTTL, p.CacheMaxTTL)
235-
236-
if originalTTL != newTTL {
237-
p.logger.Debug("ttl overwritten", "old", originalTTL, "new", newTTL)
238-
rr.Header().Ttl = newTTL
232+
// setMinMaxTTL sets the TTL values of all records according to the proxy
233+
// settings. r must not be nil.
234+
func (p *Proxy) setMinMaxTTL(ctx context.Context, r *dns.Msg) {
235+
rrSets := container.KeyValues[string, []dns.RR]{{
236+
Key: "answer",
237+
Value: r.Answer,
238+
}, {
239+
Key: "extra",
240+
Value: r.Extra,
241+
}, {
242+
Key: "ns",
243+
Value: r.Ns,
244+
}}
245+
246+
for _, rrSet := range rrSets {
247+
for _, rr := range rrSet.Value {
248+
original := rr.Header().Ttl
249+
overridden := respectTTLOverrides(original, p.CacheMinTTL, p.CacheMaxTTL)
250+
251+
if original == overridden {
252+
continue
253+
}
254+
255+
optslog.Trace3(
256+
ctx,
257+
p.logger,
258+
"ttl overwritten",
259+
"section", rrSet.Key,
260+
"old", original,
261+
"new", overridden,
262+
)
263+
rr.Header().Ttl = overridden
239264
}
240265
}
241266
}

0 commit comments

Comments
 (0)