Cache DNS lookups when resolving safebrowsing or parental servers, also cache replacement hostnames as well.
This commit is contained in:
parent
e2295c1a11
commit
a5d1053520
|
@ -45,6 +45,16 @@ func init() {
|
|||
})
|
||||
}
|
||||
|
||||
type cacheEntry struct {
|
||||
answer []dns.RR
|
||||
lastUpdated time.Time
|
||||
}
|
||||
|
||||
var (
|
||||
lookupCacheTime = time.Minute * 30
|
||||
lookupCache = map[string]cacheEntry{}
|
||||
)
|
||||
|
||||
type plugSettings struct {
|
||||
SafeBrowsingBlockHost string
|
||||
ParentalBlockHost string
|
||||
|
@ -324,6 +334,8 @@ func (p *plug) replaceHostWithValAndReply(ctx context.Context, w dns.ResponseWri
|
|||
records = append(records, result)
|
||||
} else {
|
||||
// this is a domain name, need to look it up
|
||||
cacheentry := lookupCache[val]
|
||||
if time.Since(cacheentry.lastUpdated) > lookupCacheTime {
|
||||
req := new(dns.Msg)
|
||||
req.SetQuestion(dns.Fqdn(val), question.Qtype)
|
||||
req.RecursionDesired = true
|
||||
|
@ -338,6 +350,13 @@ func (p *plug) replaceHostWithValAndReply(ctx context.Context, w dns.ResponseWri
|
|||
answer.Header().Name = question.Name
|
||||
}
|
||||
records = result.Answer
|
||||
cacheentry.answer = result.Answer
|
||||
cacheentry.lastUpdated = time.Now()
|
||||
lookupCache[val] = cacheentry
|
||||
}
|
||||
} else {
|
||||
// get from cache
|
||||
records = cacheentry.answer
|
||||
}
|
||||
}
|
||||
m := new(dns.Msg)
|
||||
|
|
|
@ -16,6 +16,7 @@ import (
|
|||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
_ "github.com/benburkert/dns/init"
|
||||
"github.com/bluele/gcache"
|
||||
"golang.org/x/net/publicsuffix"
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue