Merge: #1092 * /control/querylog_config: support optional parameters

* commit '7c81efcbcba9f5c072ed03d37490cf5e7dd33f89':
  * go.mod: use bbolt from github
  * /control/querylog_config: support optional parameters
This commit is contained in:
Andrey Meshkov 2019-11-20 20:28:28 +03:00
commit ee41c18d53
5 changed files with 24 additions and 20 deletions

4
go.mod
View File

@ -4,7 +4,7 @@ go 1.12
require ( require (
github.com/AdguardTeam/dnsproxy v0.19.6 github.com/AdguardTeam/dnsproxy v0.19.6
github.com/AdguardTeam/golibs v0.2.4 github.com/AdguardTeam/golibs v0.3.0
github.com/AdguardTeam/urlfilter v0.6.1 github.com/AdguardTeam/urlfilter v0.6.1
github.com/NYTimes/gziphandler v1.1.1 github.com/NYTimes/gziphandler v1.1.1
github.com/etcd-io/bbolt v1.3.3 github.com/etcd-io/bbolt v1.3.3
@ -17,7 +17,7 @@ require (
github.com/miekg/dns v1.1.19 github.com/miekg/dns v1.1.19
github.com/sparrc/go-ping v0.0.0-20181106165434-ef3ab45e41b0 github.com/sparrc/go-ping v0.0.0-20181106165434-ef3ab45e41b0
github.com/stretchr/testify v1.4.0 github.com/stretchr/testify v1.4.0
go.etcd.io/bbolt v1.3.3 go.etcd.io/bbolt v1.3.3 // indirect
golang.org/x/crypto v0.0.0-20191001170739-f9e2070545dc golang.org/x/crypto v0.0.0-20191001170739-f9e2070545dc
golang.org/x/net v0.0.0-20191002035440-2ec189313ef0 golang.org/x/net v0.0.0-20191002035440-2ec189313ef0
golang.org/x/sys v0.0.0-20191002091554-b397fe3ad8ed golang.org/x/sys v0.0.0-20191002091554-b397fe3ad8ed

2
go.sum
View File

@ -4,6 +4,8 @@ github.com/AdguardTeam/golibs v0.1.3 h1:hmapdTtMtIk3T8eQDwTOLdqZLGDKNKk9325uC8z1
github.com/AdguardTeam/golibs v0.1.3/go.mod h1:b0XkhgIcn2TxwX6C5AQMtpIFAgjPehNgxJErWkwA3ko= github.com/AdguardTeam/golibs v0.1.3/go.mod h1:b0XkhgIcn2TxwX6C5AQMtpIFAgjPehNgxJErWkwA3ko=
github.com/AdguardTeam/golibs v0.2.4 h1:GUssokegKxKF13K67Pgl0ZGwqHjNN6X7sep5ik6ORdY= github.com/AdguardTeam/golibs v0.2.4 h1:GUssokegKxKF13K67Pgl0ZGwqHjNN6X7sep5ik6ORdY=
github.com/AdguardTeam/golibs v0.2.4/go.mod h1:R3M+mAg3nWG4X4Hsag5eef/TckHFH12ZYhK7AzJc8+U= github.com/AdguardTeam/golibs v0.2.4/go.mod h1:R3M+mAg3nWG4X4Hsag5eef/TckHFH12ZYhK7AzJc8+U=
github.com/AdguardTeam/golibs v0.3.0 h1:1zO8ulGEOdXDDM++Ap4sYfTsT/Z4tZBZtiWSA4ykcOU=
github.com/AdguardTeam/golibs v0.3.0/go.mod h1:R3M+mAg3nWG4X4Hsag5eef/TckHFH12ZYhK7AzJc8+U=
github.com/AdguardTeam/urlfilter v0.6.1 h1:JX3gNYmgD9TCWE+G0C4MOn8WHYLAoVt0agltSvfldkY= github.com/AdguardTeam/urlfilter v0.6.1 h1:JX3gNYmgD9TCWE+G0C4MOn8WHYLAoVt0agltSvfldkY=
github.com/AdguardTeam/urlfilter v0.6.1/go.mod h1:y+XdxBdbRG9v7pfjznlvv4Ufi2HTG8D0YMqR22OVy0Y= github.com/AdguardTeam/urlfilter v0.6.1/go.mod h1:y+XdxBdbRG9v7pfjznlvv4Ufi2HTG8D0YMqR22OVy0Y=
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I= github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=

View File

@ -13,7 +13,7 @@ import (
"time" "time"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
"go.etcd.io/bbolt" "github.com/etcd-io/bbolt"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )

View File

@ -26,7 +26,8 @@ const (
// queryLog is a structure that writes and reads the DNS query log // queryLog is a structure that writes and reads the DNS query log
type queryLog struct { type queryLog struct {
conf Config conf *Config
lock sync.Mutex
logFile string // path to the log file logFile string // path to the log file
bufferLock sync.RWMutex bufferLock sync.RWMutex
@ -40,7 +41,8 @@ type queryLog struct {
func newQueryLog(conf Config) *queryLog { func newQueryLog(conf Config) *queryLog {
l := queryLog{} l := queryLog{}
l.logFile = filepath.Join(conf.BaseDir, queryLogFileName) l.logFile = filepath.Join(conf.BaseDir, queryLogFileName)
l.conf = conf l.conf = &Config{}
*l.conf = conf
if !checkInterval(l.conf.Interval) { if !checkInterval(l.conf.Interval) {
l.conf.Interval = 1 l.conf.Interval = 1
} }
@ -59,12 +61,6 @@ func checkInterval(days uint32) bool {
return days == 1 || days == 7 || days == 30 || days == 90 return days == 1 || days == 7 || days == 30 || days == 90
} }
// Set new configuration at runtime
func (l *queryLog) configure(conf Config) {
l.conf.Enabled = conf.Enabled
l.conf.Interval = conf.Interval
}
func (l *queryLog) WriteDiskConfig(dc *DiskConfig) { func (l *queryLog) WriteDiskConfig(dc *DiskConfig) {
dc.Enabled = l.conf.Enabled dc.Enabled = l.conf.Enabled
dc.Interval = l.conf.Interval dc.Interval = l.conf.Interval

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/AdguardTeam/golibs/jsonutil"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
@ -129,24 +130,29 @@ func (l *queryLog) handleQueryLogInfo(w http.ResponseWriter, r *http.Request) {
// Set configuration // Set configuration
func (l *queryLog) handleQueryLogConfig(w http.ResponseWriter, r *http.Request) { func (l *queryLog) handleQueryLogConfig(w http.ResponseWriter, r *http.Request) {
d := qlogConfig{}
reqData := qlogConfig{} req, err := jsonutil.DecodeObject(&d, r.Body)
err := json.NewDecoder(r.Body).Decode(&reqData)
if err != nil { if err != nil {
httpError(r, w, http.StatusBadRequest, "json decode: %s", err) httpError(r, w, http.StatusBadRequest, "%s", err)
return return
} }
if !checkInterval(reqData.Interval) { if req.Exists("interval") && !checkInterval(d.Interval) {
httpError(r, w, http.StatusBadRequest, "Unsupported interval") httpError(r, w, http.StatusBadRequest, "Unsupported interval")
return return
} }
conf := Config{ l.lock.Lock()
Enabled: reqData.Enabled, // copy data, modify it, then activate. Other threads (readers) don't need to use this lock.
Interval: reqData.Interval, conf := *l.conf
if req.Exists("enabled") {
conf.Enabled = d.Enabled
} }
l.configure(conf) if req.Exists("interval") {
conf.Interval = d.Interval
}
l.conf = &conf
l.lock.Unlock()
l.conf.ConfigModified() l.conf.ConfigModified()
} }