Skip to content

Commit 77b27d9

Browse files
committed
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)
2 parents 4c29840 + b7704a0 commit 77b27d9

2 files changed

Lines changed: 38 additions & 28 deletions

File tree

dnscrypt-proxy/plugin_cache.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"github.com/miekg/dns"
1111
)
1212

13+
const StaleResponseTtl = 30 * time.Second
14+
1315
type CachedResponse struct {
1416
expiration time.Time
1517
msg dns.Msg
@@ -68,29 +70,35 @@ func (plugin *PluginCache) Reload() error {
6870

6971
func (plugin *PluginCache) Eval(pluginsState *PluginsState, msg *dns.Msg) error {
7072
cacheKey := computeCacheKey(pluginsState, msg)
73+
7174
cachedResponses.RLock()
72-
defer cachedResponses.RUnlock()
7375
if cachedResponses.cache == nil {
76+
cachedResponses.RUnlock()
7477
return nil
7578
}
7679
cachedAny, ok := cachedResponses.cache.Get(cacheKey)
7780
if !ok {
81+
cachedResponses.RUnlock()
7882
return nil
7983
}
8084
cached := cachedAny.(CachedResponse)
81-
85+
expiration := cached.expiration
8286
synth := cached.msg.Copy()
87+
cachedResponses.RUnlock()
88+
8389
synth.Id = msg.Id
8490
synth.Response = true
8591
synth.Compress = true
8692
synth.Question = msg.Question
8793

88-
if time.Now().After(cached.expiration) {
94+
if time.Now().After(expiration) {
95+
expiration2 := time.Now().Add(StaleResponseTtl)
96+
updateTTL(synth, expiration2)
8997
pluginsState.sessionData["stale"] = synth
9098
return nil
9199
}
92100

93-
updateTTL(synth, cached.expiration)
101+
updateTTL(synth, expiration)
94102

95103
pluginsState.synthResponse = synth
96104
pluginsState.action = PluginsActionSynth

dnscrypt-proxy/proxy.go

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,10 @@ func (proxy *Proxy) clientsCountDec() {
572572
}
573573
}
574574

575-
func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string, query []byte, clientAddr *net.Addr, clientPc net.Conn, start time.Time, onlyCached bool) (response []byte) {
575+
func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string, query []byte, clientAddr *net.Addr, clientPc net.Conn, start time.Time, onlyCached bool) []byte {
576+
var response []byte = nil
576577
if len(query) < MinDNSPacketSize {
577-
return
578+
return response
578579
}
579580
pluginsState := NewPluginsState(proxy, clientProto, clientAddr, serverProto, start)
580581
serverName := "-"
@@ -586,25 +587,25 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
586587
}
587588
query, _ = pluginsState.ApplyQueryPlugins(&proxy.pluginsGlobals, query, needsEDNS0Padding)
588589
if len(query) < MinDNSPacketSize || len(query) > MaxDNSPacketSize {
589-
return
590+
return response
590591
}
591592
if pluginsState.action == PluginsActionDrop {
592593
pluginsState.returnCode = PluginsReturnCodeDrop
593594
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
594-
return
595+
return response
595596
}
596597
var err error
597598
if pluginsState.synthResponse != nil {
598599
response, err = pluginsState.synthResponse.PackBuffer(response)
599600
if err != nil {
600601
pluginsState.returnCode = PluginsReturnCodeParseError
601602
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
602-
return
603+
return response
603604
}
604605
}
605606
if onlyCached {
606607
if len(response) == 0 {
607-
return
608+
return response
608609
}
609610
serverInfo = nil
610611
}
@@ -621,7 +622,7 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
621622
if err != nil {
622623
pluginsState.returnCode = PluginsReturnCodeParseError
623624
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
624-
return
625+
return response
625626
}
626627
serverInfo.noticeBegin(proxy)
627628
if serverProto == "udp" {
@@ -639,7 +640,7 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
639640
if err != nil {
640641
pluginsState.returnCode = PluginsReturnCodeParseError
641642
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
642-
return
643+
return response
643644
}
644645
response, err = proxy.exchangeWithTCPServer(serverInfo, sharedKey, encryptedQuery, clientNonce)
645646
}
@@ -660,25 +661,26 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
660661
}
661662
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
662663
serverInfo.noticeFailure(proxy)
663-
return
664+
return response
664665
}
665666
} else if serverInfo.Proto == stamps.StampProtoTypeDoH {
666667
tid := TransactionID(query)
667668
SetTransactionID(query, 0)
668669
serverInfo.noticeBegin(proxy)
669670
serverResponse, _, tls, _, err := proxy.xTransport.DoHQuery(serverInfo.useGet, serverInfo.URL, query, proxy.timeout)
670671
SetTransactionID(query, tid)
671-
if err == nil || tls == nil || !tls.HandshakeComplete {
672-
response = nil
673-
} else if stale, ok := pluginsState.sessionData["stale"]; ok {
674-
dlog.Debug("Serving stale response")
675-
response, err = (stale.(*dns.Msg)).Pack()
672+
673+
if err != nil || tls == nil || !tls.HandshakeComplete {
674+
if stale, ok := pluginsState.sessionData["stale"]; ok {
675+
dlog.Debug("Serving stale response")
676+
response, err = (stale.(*dns.Msg)).Pack()
677+
}
676678
}
677679
if err != nil {
678680
pluginsState.returnCode = PluginsReturnCodeNetworkError
679681
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
680682
serverInfo.noticeFailure(proxy)
681-
return
683+
return response
682684
}
683685
if response == nil {
684686
response = serverResponse
@@ -689,7 +691,7 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
689691
} else if serverInfo.Proto == stamps.StampProtoTypeODoHTarget {
690692
tid := TransactionID(query)
691693
if len(serverInfo.odohTargetConfigs) == 0 {
692-
return
694+
return response
693695
}
694696
target := serverInfo.odohTargetConfigs[rand.Intn(len(serverInfo.odohTargetConfigs))]
695697
odohQuery, err := target.encryptQuery(query)
@@ -736,7 +738,7 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
736738
pluginsState.returnCode = PluginsReturnCodeNetworkError
737739
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
738740
serverInfo.noticeFailure(proxy)
739-
return
741+
return response
740742
}
741743
} else {
742744
dlog.Fatal("Unsupported protocol")
@@ -745,26 +747,26 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
745747
pluginsState.returnCode = PluginsReturnCodeParseError
746748
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
747749
serverInfo.noticeFailure(proxy)
748-
return
750+
return response
749751
}
750752
response, err = pluginsState.ApplyResponsePlugins(&proxy.pluginsGlobals, response, ttl)
751753
if err != nil {
752754
pluginsState.returnCode = PluginsReturnCodeParseError
753755
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
754756
serverInfo.noticeFailure(proxy)
755-
return
757+
return response
756758
}
757759
if pluginsState.action == PluginsActionDrop {
758760
pluginsState.returnCode = PluginsReturnCodeDrop
759761
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
760-
return
762+
return response
761763
}
762764
if pluginsState.synthResponse != nil {
763765
response, err = pluginsState.synthResponse.PackBuffer(response)
764766
if err != nil {
765767
pluginsState.returnCode = PluginsReturnCodeParseError
766768
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
767-
return
769+
return response
768770
}
769771
}
770772
if rcode := Rcode(response); rcode == dns.RcodeServerFailure { // SERVFAIL
@@ -788,15 +790,15 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
788790
if serverInfo != nil {
789791
serverInfo.noticeFailure(proxy)
790792
}
791-
return
793+
return response
792794
}
793795
if clientProto == "udp" {
794796
if len(response) > pluginsState.maxUnencryptedUDPSafePayloadSize {
795797
response, err = TruncatedResponse(response)
796798
if err != nil {
797799
pluginsState.returnCode = PluginsReturnCodeParseError
798800
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
799-
return
801+
return response
800802
}
801803
}
802804
clientPc.(net.PacketConn).WriteTo(response, *clientAddr)
@@ -813,7 +815,7 @@ func (proxy *Proxy) processIncomingQuery(clientProto string, serverProto string,
813815
if serverInfo != nil {
814816
serverInfo.noticeFailure(proxy)
815817
}
816-
return
818+
return response
817819
}
818820
if clientPc != nil {
819821
clientPc.Write(response)

0 commit comments

Comments
 (0)