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 (
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/NYTimes/gziphandler v1.1.1
github.com/etcd-io/bbolt v1.3.3
@ -17,7 +17,7 @@ require (
github.com/miekg/dns v1.1.19
github.com/sparrc/go-ping v0.0.0-20181106165434-ef3ab45e41b0
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/net v0.0.0-20191002035440-2ec189313ef0
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.2.4 h1:GUssokegKxKF13K67Pgl0ZGwqHjNN6X7sep5ik6ORdY=
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/go.mod h1:y+XdxBdbRG9v7pfjznlvv4Ufi2HTG8D0YMqR22OVy0Y=
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=

View file

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

View file

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

View file

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