mirror of
https://github.com/DNSCrypt/dnscrypt-proxy.git
synced 2025-03-04 10:24:40 +01:00
Implement serve-stale
This commit is contained in:
parent
f22461374c
commit
f34d7b60fa
4 changed files with 24 additions and 9 deletions
|
@ -78,17 +78,20 @@ func (plugin *PluginCache) Eval(pluginsState *PluginsState, msg *dns.Msg) error
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
cached := cachedAny.(CachedResponse)
|
cached := cachedAny.(CachedResponse)
|
||||||
if time.Now().After(cached.expiration) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
updateTTL(&cached.msg, cached.expiration)
|
|
||||||
|
|
||||||
synth := cached.msg
|
synth := cached.msg
|
||||||
synth.Id = msg.Id
|
synth.Id = msg.Id
|
||||||
synth.Response = true
|
synth.Response = true
|
||||||
synth.Compress = true
|
synth.Compress = true
|
||||||
synth.Question = msg.Question
|
synth.Question = msg.Question
|
||||||
|
|
||||||
|
if time.Now().After(cached.expiration) {
|
||||||
|
pluginsState.sessionData["stale"] = &synth
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
updateTTL(&cached.msg, cached.expiration)
|
||||||
|
|
||||||
pluginsState.synthResponse = &synth
|
pluginsState.synthResponse = &synth
|
||||||
pluginsState.action = PluginsActionSynth
|
pluginsState.action = PluginsActionSynth
|
||||||
pluginsState.cacheHit = true
|
pluginsState.cacheHit = true
|
||||||
|
|
|
@ -94,9 +94,6 @@ func (plugin *PluginWhitelistName) Eval(pluginsState *PluginsState, msg *dns.Msg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if whitelist {
|
if whitelist {
|
||||||
if pluginsState.sessionData == nil {
|
|
||||||
pluginsState.sessionData = make(map[string]interface{})
|
|
||||||
}
|
|
||||||
pluginsState.sessionData["whitelisted"] = true
|
pluginsState.sessionData["whitelisted"] = true
|
||||||
if plugin.logger != nil {
|
if plugin.logger != nil {
|
||||||
var clientIPStr string
|
var clientIPStr string
|
||||||
|
|
|
@ -240,6 +240,7 @@ func NewPluginsState(proxy *Proxy, clientProto string, clientAddr *net.Addr, sta
|
||||||
qName: "",
|
qName: "",
|
||||||
requestStart: start,
|
requestStart: start,
|
||||||
maxUnencryptedUDPSafePayloadSize: MaxDNSUDPSafePacketSize,
|
maxUnencryptedUDPSafePayloadSize: MaxDNSUDPSafePacketSize,
|
||||||
|
sessionData: make(map[string]interface{}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -493,6 +493,12 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
|
||||||
} else {
|
} else {
|
||||||
response, err = proxy.exchangeWithTCPServer(serverInfo, sharedKey, encryptedQuery, clientNonce)
|
response, err = proxy.exchangeWithTCPServer(serverInfo, sharedKey, encryptedQuery, clientNonce)
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
if stale, ok := pluginsState.sessionData["stale"]; ok {
|
||||||
|
dlog.Debug("Serving stale response")
|
||||||
|
response, err = (stale.(*dns.Msg)).Pack()
|
||||||
|
}
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
|
if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
|
||||||
pluginsState.returnCode = PluginsReturnCodeServerTimeout
|
pluginsState.returnCode = PluginsReturnCodeServerTimeout
|
||||||
|
@ -509,13 +515,21 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
|
||||||
serverInfo.noticeBegin(proxy)
|
serverInfo.noticeBegin(proxy)
|
||||||
resp, _, err := proxy.xTransport.DoHQuery(serverInfo.useGet, serverInfo.URL, query, proxy.timeout)
|
resp, _, err := proxy.xTransport.DoHQuery(serverInfo.useGet, serverInfo.URL, query, proxy.timeout)
|
||||||
SetTransactionID(query, tid)
|
SetTransactionID(query, tid)
|
||||||
|
if err == nil {
|
||||||
|
response = nil
|
||||||
|
} else if stale, ok := pluginsState.sessionData["stale"]; ok {
|
||||||
|
dlog.Debug("Serving stale response")
|
||||||
|
response, err = (stale.(*dns.Msg)).Pack()
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pluginsState.returnCode = PluginsReturnCodeNetworkError
|
pluginsState.returnCode = PluginsReturnCodeNetworkError
|
||||||
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
|
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
|
||||||
serverInfo.noticeFailure(proxy)
|
serverInfo.noticeFailure(proxy)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if response == nil {
|
||||||
response, err = ioutil.ReadAll(io.LimitReader(resp.Body, int64(MaxDNSPacketSize)))
|
response, err = ioutil.ReadAll(io.LimitReader(resp.Body, int64(MaxDNSPacketSize)))
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pluginsState.returnCode = PluginsReturnCodeNetworkError
|
pluginsState.returnCode = PluginsReturnCodeNetworkError
|
||||||
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
|
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
|
||||||
|
|
Loading…
Add table
Reference in a new issue