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
|
||||
}
|
||||
cached := cachedAny.(CachedResponse)
|
||||
if time.Now().After(cached.expiration) {
|
||||
return nil
|
||||
}
|
||||
|
||||
updateTTL(&cached.msg, cached.expiration)
|
||||
|
||||
synth := cached.msg
|
||||
synth.Id = msg.Id
|
||||
synth.Response = true
|
||||
synth.Compress = true
|
||||
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.action = PluginsActionSynth
|
||||
pluginsState.cacheHit = true
|
||||
|
|
|
@ -94,9 +94,6 @@ func (plugin *PluginWhitelistName) Eval(pluginsState *PluginsState, msg *dns.Msg
|
|||
}
|
||||
}
|
||||
if whitelist {
|
||||
if pluginsState.sessionData == nil {
|
||||
pluginsState.sessionData = make(map[string]interface{})
|
||||
}
|
||||
pluginsState.sessionData["whitelisted"] = true
|
||||
if plugin.logger != nil {
|
||||
var clientIPStr string
|
||||
|
|
|
@ -240,6 +240,7 @@ func NewPluginsState(proxy *Proxy, clientProto string, clientAddr *net.Addr, sta
|
|||
qName: "",
|
||||
requestStart: start,
|
||||
maxUnencryptedUDPSafePayloadSize: MaxDNSUDPSafePacketSize,
|
||||
sessionData: make(map[string]interface{}),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -493,6 +493,12 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
|
|||
} else {
|
||||
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 neterr, ok := err.(net.Error); ok && neterr.Timeout() {
|
||||
pluginsState.returnCode = PluginsReturnCodeServerTimeout
|
||||
|
@ -509,13 +515,21 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
|
|||
serverInfo.noticeBegin(proxy)
|
||||
resp, _, err := proxy.xTransport.DoHQuery(serverInfo.useGet, serverInfo.URL, query, proxy.timeout)
|
||||
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 {
|
||||
pluginsState.returnCode = PluginsReturnCodeNetworkError
|
||||
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
|
||||
serverInfo.noticeFailure(proxy)
|
||||
return
|
||||
}
|
||||
response, err = ioutil.ReadAll(io.LimitReader(resp.Body, int64(MaxDNSPacketSize)))
|
||||
if response == nil {
|
||||
response, err = ioutil.ReadAll(io.LimitReader(resp.Body, int64(MaxDNSPacketSize)))
|
||||
}
|
||||
if err != nil {
|
||||
pluginsState.returnCode = PluginsReturnCodeNetworkError
|
||||
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
|
||||
|
|
Loading…
Add table
Reference in a new issue