From 7e45c2fc2428679ac7454c5244b0130901449324 Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Fri, 8 Nov 2019 12:31:50 +0300 Subject: [PATCH] + new setting "dns.querylog_memsize" * set 1000 entries by default (not 5000) --- home/config.go | 14 +++++++++----- home/dns.go | 1 + querylog/qlog.go | 3 +-- querylog/querylog.go | 2 ++ querylog/querylog_file.go | 2 +- querylog/querylog_test.go | 1 + 6 files changed, 15 insertions(+), 8 deletions(-) diff --git a/home/config.go b/home/config.go index 8622155f..cb264787 100644 --- a/home/config.go +++ b/home/config.go @@ -106,6 +106,7 @@ type dnsConfig struct { QueryLogEnabled bool `yaml:"querylog_enabled"` // if true, query log is enabled QueryLogInterval uint32 `yaml:"querylog_interval"` // time interval for query log (in days) + QueryLogMemSize uint32 `yaml:"querylog_memsize"` // number of entries kept in memory before they are flushed to disk dnsforward.FilteringConfig `yaml:",inline"` @@ -161,11 +162,9 @@ var config = configuration{ BindPort: 3000, BindHost: "0.0.0.0", DNS: dnsConfig{ - BindHost: "0.0.0.0", - Port: 53, - StatsInterval: 1, - QueryLogEnabled: true, - QueryLogInterval: 1, + BindHost: "0.0.0.0", + Port: 53, + StatsInterval: 1, FilteringConfig: dnsforward.FilteringConfig{ ProtectionEnabled: true, // whether or not use any of dnsfilter features BlockingMode: "nxdomain", // mode how to answer filtered requests @@ -202,6 +201,10 @@ func initConfig() { config.WebSessionTTLHours = 30 * 24 + config.DNS.QueryLogEnabled = true + config.DNS.QueryLogInterval = 90 + config.DNS.QueryLogMemSize = 1000 + config.DNS.CacheSize = 4 * 1024 * 1024 config.DNS.DnsfilterConf.SafeBrowsingCacheSize = 1 * 1024 * 1024 config.DNS.DnsfilterConf.SafeSearchCacheSize = 1 * 1024 * 1024 @@ -310,6 +313,7 @@ func (c *configuration) write() error { config.queryLog.WriteDiskConfig(&dc) config.DNS.QueryLogEnabled = dc.Enabled config.DNS.QueryLogInterval = dc.Interval + config.DNS.QueryLogMemSize = dc.MemSize } if config.dnsFilter != nil { diff --git a/home/dns.go b/home/dns.go index af39ee12..0ec5a042 100644 --- a/home/dns.go +++ b/home/dns.go @@ -50,6 +50,7 @@ func initDNSServer() { Enabled: config.DNS.QueryLogEnabled, BaseDir: baseDir, Interval: config.DNS.QueryLogInterval, + MemSize: config.DNS.QueryLogMemSize, ConfigModified: onConfigModified, HTTPRegister: httpRegister, } diff --git a/querylog/qlog.go b/querylog/qlog.go index 07b8cd5c..bf585c53 100644 --- a/querylog/qlog.go +++ b/querylog/qlog.go @@ -16,7 +16,6 @@ import ( ) const ( - logBufferCap = 5000 // maximum capacity of buffer before it's flushed to disk queryLogFileName = "querylog.json" // .gz added during compression getDataLimit = 500 // GetData(): maximum log entries to return @@ -147,7 +146,7 @@ func (l *queryLog) Add(question *dns.Msg, answer *dns.Msg, result *dnsfilter.Res l.buffer = append(l.buffer, &entry) needFlush := false if !l.flushPending { - needFlush = len(l.buffer) >= logBufferCap + needFlush = len(l.buffer) >= int(l.conf.MemSize) if needFlush { l.flushPending = true } diff --git a/querylog/querylog.go b/querylog/querylog.go index 5158a211..e7e790e7 100644 --- a/querylog/querylog.go +++ b/querylog/querylog.go @@ -13,6 +13,7 @@ import ( type DiskConfig struct { Enabled bool Interval uint32 + MemSize uint32 } // QueryLog - main interface @@ -32,6 +33,7 @@ type Config struct { Enabled bool BaseDir string // directory where log file is stored Interval uint32 // interval to rotate logs (in days) + MemSize uint32 // number of entries kept in memory before they are flushed to disk // Called when the configuration is changed by HTTP request ConfigModified func() diff --git a/querylog/querylog_file.go b/querylog/querylog_file.go index cc32afb0..a2250581 100644 --- a/querylog/querylog_file.go +++ b/querylog/querylog_file.go @@ -27,7 +27,7 @@ func (l *queryLog) flushLogBuffer(fullFlush bool) error { // flush remainder to file l.bufferLock.Lock() - needFlush := len(l.buffer) >= logBufferCap + needFlush := len(l.buffer) >= int(l.conf.MemSize) if !needFlush && !fullFlush { l.bufferLock.Unlock() return nil diff --git a/querylog/querylog_test.go b/querylog/querylog_test.go index 63bcbfd8..5b2776cd 100644 --- a/querylog/querylog_test.go +++ b/querylog/querylog_test.go @@ -23,6 +23,7 @@ func TestQueryLog(t *testing.T) { conf := Config{ Enabled: true, Interval: 1, + MemSize: 100, } conf.BaseDir = prepareTestDir() defer func() { _ = os.RemoveAll(conf.BaseDir) }()