From 14b464e56d04b4599214ca19c3009f191a5212c8 Mon Sep 17 00:00:00 2001
From: Ferdinand Holzer <ferdinand.holzer@gmail.com>
Date: Tue, 28 May 2019 22:48:56 +0200
Subject: [PATCH] Log whether response was served from cache

---
 dnscrypt-proxy/plugin_cache.go     | 1 +
 dnscrypt-proxy/plugin_query_log.go | 4 ++--
 dnscrypt-proxy/plugins.go          | 1 +
 3 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/dnscrypt-proxy/plugin_cache.go b/dnscrypt-proxy/plugin_cache.go
index 7dcb6b9f..267e433b 100644
--- a/dnscrypt-proxy/plugin_cache.go
+++ b/dnscrypt-proxy/plugin_cache.go
@@ -132,6 +132,7 @@ func (plugin *PluginCache) Eval(pluginsState *PluginsState, msg *dns.Msg) error
 	synth.Question = msg.Question
 	pluginsState.synthResponse = &synth
 	pluginsState.action = PluginsActionSynth
+	pluginsState.cacheHit = true
 	return nil
 }
 
diff --git a/dnscrypt-proxy/plugin_query_log.go b/dnscrypt-proxy/plugin_query_log.go
index 576e3bf1..112a175d 100644
--- a/dnscrypt-proxy/plugin_query_log.go
+++ b/dnscrypt-proxy/plugin_query_log.go
@@ -84,8 +84,8 @@ func (plugin *PluginQueryLog) Eval(pluginsState *PluginsState, msg *dns.Msg) err
 		tsStr := fmt.Sprintf("[%d-%02d-%02d %02d:%02d:%02d]", year, int(month), day, hour, minute, second)
 		line = fmt.Sprintf("%s\t%s\t%s\t%s\t%s\t%d\n", tsStr, clientIPStr, StringQuote(qName), qType, returnCode, requestDuration/time.Millisecond)
 	} else if plugin.format == "ltsv" {
-		line = fmt.Sprintf("time:%d\thost:%s\tmessage:%s\ttype:%s\treturn:%s\tduration:%d\n",
-			time.Now().Unix(), clientIPStr, StringQuote(qName), qType, returnCode, requestDuration/time.Millisecond)
+		line = fmt.Sprintf("time:%d\thost:%s\tmessage:%s\ttype:%s\treturn:%s\tcacheHit:%v\tduration:%d\n",
+			time.Now().Unix(), clientIPStr, StringQuote(qName), qType, returnCode, pluginsState.cacheHit, requestDuration/time.Millisecond)
 	} else {
 		dlog.Fatalf("Unexpected log format: [%s]", plugin.format)
 	}
diff --git a/dnscrypt-proxy/plugins.go b/dnscrypt-proxy/plugins.go
index c2c2dc20..676d1b7b 100644
--- a/dnscrypt-proxy/plugins.go
+++ b/dnscrypt-proxy/plugins.go
@@ -73,6 +73,7 @@ type PluginsState struct {
 	questionMsg            *dns.Msg
 	requestStart           time.Time
 	requestEnd             time.Time
+	cacheHit               bool
 	returnCode             PluginsReturnCode
 }