Skip to content

Commit

Permalink
proxy: imp cyclo, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Oct 10, 2022
1 parent e8ca674 commit f827903
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 9 additions & 2 deletions proxy/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,7 @@ func cacheTTL(m *dns.Msg) (ttl uint32) {

switch rcode := m.Rcode; rcode {
case dns.RcodeSuccess:
qType := m.Question[0].Qtype
if (qType != dns.TypeA && qType != dns.TypeAAAA) || hasIPAns(m) || isCacheableNegative(m) {
if isCacheableSuccceded(m) {
return ttl
}
case dns.RcodeNameError:
Expand Down Expand Up @@ -369,6 +368,14 @@ func hasIPAns(m *dns.Msg) (ok bool) {
return false
}

// isCacheableSuccceded returns true if m contains useful data to be cached
// treating it as a succeesful response.
func isCacheableSuccceded(m *dns.Msg) (ok bool) {
qType := m.Question[0].Qtype

return (qType != dns.TypeA && qType != dns.TypeAAAA) || hasIPAns(m) || isCacheableNegative(m)
}

// isCacheableNegative returns true if m's header has at least a single SOA RR
// and no NS records so that it can be declared authoritative.
//
Expand Down
4 changes: 4 additions & 0 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,10 @@ func (p *Proxy) Resolve(d *DNSContext) (err error) {
d.calcFlagsAndSize()

// Use cache only if it's enabled and the query doesn't use custom upstream.
// Also don't cache responses for queries with DNSSEC checking disabled,
// just like Dnsmasq does.
//
// See https://github.com/imp/dnsmasq/blob/770bce967cfc9967273d0acfb3ea018fb7b17522/src/forward.c#L1169-L1172.
cacheWorks := p.cache != nil && d.CustomUpstreamConfig == nil && !d.Req.CheckingDisabled
if cacheWorks {
if p.replyFromCache(d) {
Expand Down

0 comments on commit f827903

Please sign in to comment.