1
0
mirror of https://github.com/DNSCrypt/dnscrypt-proxy.git synced 2025-01-13 02:22:48 +01:00

Working DNS cache

This commit is contained in:
Frank Denis 2018-01-10 18:53:09 +01:00
parent 77cdc1db78
commit 8e73bb4a2c

View File

@ -45,10 +45,13 @@ func NewPluginsState(proxy *Proxy, proto string) PluginsState {
*queryPlugins = append(*queryPlugins, Plugin(new(PluginBlockIPv6))) *queryPlugins = append(*queryPlugins, Plugin(new(PluginBlockIPv6)))
} }
*queryPlugins = append(*queryPlugins, Plugin(new(PluginGetSetPayloadSize))) *queryPlugins = append(*queryPlugins, Plugin(new(PluginGetSetPayloadSize)))
if proxy.cache {
*queryPlugins = append(*queryPlugins, Plugin(new(PluginCache)))
}
responsePlugins := &[]Plugin{} responsePlugins := &[]Plugin{}
if proxy.cache { if proxy.cache {
*responsePlugins = append(*responsePlugins, Plugin(new(PluginCache))) *responsePlugins = append(*responsePlugins, Plugin(new(PluginCacheResponse)))
} }
return PluginsState{ return PluginsState{
@ -193,21 +196,24 @@ type CachedResponses struct {
var cachedResponses CachedResponses var cachedResponses CachedResponses
type PluginCache struct { type PluginCacheResponse struct {
cachedResponses *CachedResponses cachedResponses *CachedResponses
} }
func (plugin *PluginCache) Name() string { func (plugin *PluginCacheResponse) Name() string {
return "cache" return "cache_response"
} }
func (plugin *PluginCache) Description() string { func (plugin *PluginCacheResponse) Description() string {
return "DNS cache." return "DNS cache (writer)."
} }
func (plugin *PluginCache) Eval(pluginsState *PluginsState, msg *dns.Msg) error { func (plugin *PluginCacheResponse) Eval(pluginsState *PluginsState, msg *dns.Msg) error {
plugin.cachedResponses = &cachedResponses plugin.cachedResponses = &cachedResponses
if msg.Rcode != dns.RcodeSuccess && msg.Rcode != dns.RcodeNXRrset {
return nil
}
cacheKey, err := computeCacheKey(pluginsState, msg) cacheKey, err := computeCacheKey(pluginsState, msg)
if err != nil { if err != nil {
return nil return nil
@ -235,6 +241,45 @@ func (plugin *PluginCache) Eval(pluginsState *PluginsState, msg *dns.Msg) error
return nil return nil
} }
type PluginCache struct {
cachedResponses *CachedResponses
}
func (plugin *PluginCache) Name() string {
return "cache"
}
func (plugin *PluginCache) Description() string {
return "DNS cache (reader)."
}
func (plugin *PluginCache) Eval(pluginsState *PluginsState, msg *dns.Msg) error {
plugin.cachedResponses = &cachedResponses
cacheKey, err := computeCacheKey(pluginsState, msg)
if err != nil {
return nil
}
plugin.cachedResponses.RLock()
defer plugin.cachedResponses.RUnlock()
if plugin.cachedResponses.cache == nil {
return nil
}
cached, ok := plugin.cachedResponses.cache[cacheKey]
if !ok {
return nil
}
if time.Now().After(cached.expiration) {
return nil
}
synth := cached.msg
synth.MsgHdr = msg.MsgHdr
synth.Question = msg.Question
pluginsState.synthResponse = &synth
pluginsState.action = PluginsActionSynth
return nil
}
func computeCacheKey(pluginsState *PluginsState, msg *dns.Msg) ([32]byte, error) { func computeCacheKey(pluginsState *PluginsState, msg *dns.Msg) ([32]byte, error) {
questions := msg.Question questions := msg.Question
if len(questions) != 1 { if len(questions) != 1 {
@ -253,6 +298,6 @@ func computeCacheKey(pluginsState *PluginsState, msg *dns.Msg) ([32]byte, error)
NormalizeName(&normalizedName) NormalizeName(&normalizedName)
h.Write(normalizedName) h.Write(normalizedName)
var sum [32]byte var sum [32]byte
h.Sum(sum[:]) h.Sum(sum[:0])
return sum, nil return sum, nil
} }