Merge branch 'master' of github.com:DNSCrypt/dnscrypt-proxy

* 'master' of github.com:DNSCrypt/dnscrypt-proxy:
  Make return value explicit
  Repair stale respones for DoH
  Define a constant for the TTL of stale responses
  Update plugin_cache.go (#1900)
This commit is contained in:
Frank Denis 2021-09-27 12:31:43 +02:00
commit 77b27d9293
2 changed files with 38 additions and 28 deletions

View File

@ -10,6 +10,8 @@ import (
"github.com/miekg/dns"
)
const StaleResponseTtl = 30 * time.Second
type CachedResponse struct {
expiration time.Time
msg dns.Msg
@ -68,29 +70,35 @@ func (plugin *PluginCache) Reload() error {
func (plugin *PluginCache) Eval(pluginsState *PluginsState, msg *dns.Msg) error {
cacheKey := computeCacheKey(pluginsState, msg)
cachedResponses.RLock()
defer cachedResponses.RUnlock()
if cachedResponses.cache == nil {
cachedResponses.RUnlock()
return nil
}
cachedAny, ok := cachedResponses.cache.Get(cacheKey)
if !ok {
cachedResponses.RUnlock()
return nil
}
cached := cachedAny.(CachedResponse)
expiration := cached.expiration
synth := cached.msg.Copy()
cachedResponses.RUnlock()
synth.Id = msg.Id
synth.Response = true
synth.Compress = true
synth.Question = msg.Question
if time.Now().After(cached.expiration) {
if time.Now().After(expiration) {
expiration2 := time.Now().Add(StaleResponseTtl)
updateTTL(synth, expiration2)
pluginsState.sessionData["stale"] = synth
return nil
}
updateTTL(synth, cached.expiration)
updateTTL(synth, expiration)
pluginsState.synthResponse = synth
pluginsState.action = PluginsActionSynth

View File

@ -572,9 +572,10 @@ func (proxy *Proxy) clientsCountDec() {
}
}
func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string, query []byte, clientAddr *net.Addr, clientPc net.Conn, start time.Time, onlyCached bool) (response []byte) {
func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string, query []byte, clientAddr *net.Addr, clientPc net.Conn, start time.Time, onlyCached bool) []byte {
var response []byte = nil
if len(query) < MinDNSPacketSize {
return
return response
}
pluginsState := NewPluginsState(proxy, clientProto, clientAddr, serverProto, start)
serverName := "-"
@ -586,12 +587,12 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
}
query, _ = pluginsState.ApplyQueryPlugins(&proxy.pluginsGlobals, query, needsEDNS0Padding)
if len(query) < MinDNSPacketSize || len(query) > MaxDNSPacketSize {
return
return response
}
if pluginsState.action == PluginsActionDrop {
pluginsState.returnCode = PluginsReturnCodeDrop
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
return
return response
}
var err error
if pluginsState.synthResponse != nil {
@ -599,12 +600,12 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
if err != nil {
pluginsState.returnCode = PluginsReturnCodeParseError
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
return
return response
}
}
if onlyCached {
if len(response) == 0 {
return
return response
}
serverInfo = nil
}
@ -621,7 +622,7 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
if err != nil {
pluginsState.returnCode = PluginsReturnCodeParseError
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
return
return response
}
serverInfo.noticeBegin(proxy)
if serverProto == "udp" {
@ -639,7 +640,7 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
if err != nil {
pluginsState.returnCode = PluginsReturnCodeParseError
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
return
return response
}
response, err = proxy.exchangeWithTCPServer(serverInfo, sharedKey, encryptedQuery, clientNonce)
}
@ -660,7 +661,7 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
}
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
serverInfo.noticeFailure(proxy)
return
return response
}
} else if serverInfo.Proto == stamps.StampProtoTypeDoH {
tid := TransactionID(query)
@ -668,17 +669,18 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
serverInfo.noticeBegin(proxy)
serverResponse, _, tls, _, err := proxy.xTransport.DoHQuery(serverInfo.useGet, serverInfo.URL, query, proxy.timeout)
SetTransactionID(query, tid)
if err == nil || tls == nil || !tls.HandshakeComplete {
response = nil
} else if stale, ok := pluginsState.sessionData["stale"]; ok {
dlog.Debug("Serving stale response")
response, err = (stale.(*dns.Msg)).Pack()
if err != nil || tls == nil || !tls.HandshakeComplete {
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
return response
}
if response == nil {
response = serverResponse
@ -689,7 +691,7 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
} else if serverInfo.Proto == stamps.StampProtoTypeODoHTarget {
tid := TransactionID(query)
if len(serverInfo.odohTargetConfigs) == 0 {
return
return response
}
target := serverInfo.odohTargetConfigs[rand.Intn(len(serverInfo.odohTargetConfigs))]
odohQuery, err := target.encryptQuery(query)
@ -736,7 +738,7 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
pluginsState.returnCode = PluginsReturnCodeNetworkError
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
serverInfo.noticeFailure(proxy)
return
return response
}
} else {
dlog.Fatal("Unsupported protocol")
@ -745,26 +747,26 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
pluginsState.returnCode = PluginsReturnCodeParseError
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
serverInfo.noticeFailure(proxy)
return
return response
}
response, err = pluginsState.ApplyResponsePlugins(&proxy.pluginsGlobals, response, ttl)
if err != nil {
pluginsState.returnCode = PluginsReturnCodeParseError
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
serverInfo.noticeFailure(proxy)
return
return response
}
if pluginsState.action == PluginsActionDrop {
pluginsState.returnCode = PluginsReturnCodeDrop
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
return
return response
}
if pluginsState.synthResponse != nil {
response, err = pluginsState.synthResponse.PackBuffer(response)
if err != nil {
pluginsState.returnCode = PluginsReturnCodeParseError
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
return
return response
}
}
if rcode := Rcode(response); rcode == dns.RcodeServerFailure { // SERVFAIL
@ -788,7 +790,7 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
if serverInfo != nil {
serverInfo.noticeFailure(proxy)
}
return
return response
}
if clientProto == "udp" {
if len(response) > pluginsState.maxUnencryptedUDPSafePayloadSize {
@ -796,7 +798,7 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
if err != nil {
pluginsState.returnCode = PluginsReturnCodeParseError
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
return
return response
}
}
clientPc.(net.PacketConn).WriteTo(response, *clientAddr)
@ -813,7 +815,7 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
if serverInfo != nil {
serverInfo.noticeFailure(proxy)
}
return
return response
}
if clientPc != nil {
clientPc.Write(response)