From 2a6a1852f12ff7b808678422b1691195c9eb139e Mon Sep 17 00:00:00 2001 From: Amit Date: Fri, 9 Feb 2018 17:59:04 +0200 Subject: [PATCH] Cache Plugin: return non-fixed TTL for cached entries --- dnscrypt-proxy/dnsutils.go | 12 ++++++++---- dnscrypt-proxy/plugin_cache.go | 5 +++++ dnscrypt-proxy/plugins.go | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/dnscrypt-proxy/dnsutils.go b/dnscrypt-proxy/dnsutils.go index a0d07c24..887d62f2 100644 --- a/dnscrypt-proxy/dnsutils.go +++ b/dnscrypt-proxy/dnsutils.go @@ -83,20 +83,24 @@ func getMinTTL(msg *dns.Msg, minTTL uint32, maxTTL uint32, negCacheMinTTL uint32 return time.Duration(ttl) * time.Second } -func setMaxTTL(msg *dns.Msg, ttl uint32) { +func setMaxTTL(msg *dns.Msg, ttl uint32, force bool) { for _, rr := range msg.Answer { - if ttl < rr.Header().Ttl { + if (ttl < rr.Header().Ttl) || force { rr.Header().Ttl = ttl } } for _, rr := range msg.Ns { - if ttl < rr.Header().Ttl { + if (ttl < rr.Header().Ttl) || force { rr.Header().Ttl = ttl } } for _, rr := range msg.Extra { - if ttl < rr.Header().Ttl { + if (ttl < rr.Header().Ttl) || force { rr.Header().Ttl = ttl } } } + +func updateTTL(msg *dns.Msg, expiration time.Time) { + setMaxTTL(msg, uint32(time.Until(expiration) / time.Second), true) +} diff --git a/dnscrypt-proxy/plugin_cache.go b/dnscrypt-proxy/plugin_cache.go index 7ede10f7..f6d09337 100644 --- a/dnscrypt-proxy/plugin_cache.go +++ b/dnscrypt-proxy/plugin_cache.go @@ -70,6 +70,8 @@ func (plugin *PluginCacheResponse) Eval(pluginsState *PluginsState, msg *dns.Msg } } plugin.cachedResponses.cache.Add(cacheKey, cachedResponse) + + updateTTL(msg, cachedResponse.expiration) return nil } @@ -117,6 +119,9 @@ func (plugin *PluginCache) Eval(pluginsState *PluginsState, msg *dns.Msg) error if time.Now().After(cached.expiration) { return nil } + + updateTTL(&cached.msg, cached.expiration) + synth := cached.msg synth.Id = msg.Id synth.Response = true diff --git a/dnscrypt-proxy/plugins.go b/dnscrypt-proxy/plugins.go index 9774ace0..42cc62b9 100644 --- a/dnscrypt-proxy/plugins.go +++ b/dnscrypt-proxy/plugins.go @@ -178,7 +178,7 @@ func (pluginsState *PluginsState) ApplyResponsePlugins(pluginsGlobals *PluginsGl } pluginsGlobals.RUnlock() if ttl != nil { - setMaxTTL(&msg, *ttl) + setMaxTTL(&msg, *ttl, false) } packet2, err := msg.PackBuffer(packet) if err != nil {