Pull request: rm-jsonutil
Merge in DNS/adguard-home from rm-jsonutil to master Squashed commit of the following: commit dec746d321adbeb41bfd0c44e71d198809c4731e Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Oct 28 18:41:39 2022 +0300 querylog: rm jsonutil
This commit is contained in:
parent
3dd7393b3f
commit
746e9df727
|
@ -1,7 +1,9 @@
|
||||||
package querylog
|
package querylog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -9,20 +11,28 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
||||||
"github.com/AdguardTeam/golibs/jsonutil"
|
|
||||||
"github.com/AdguardTeam/golibs/log"
|
"github.com/AdguardTeam/golibs/log"
|
||||||
"github.com/AdguardTeam/golibs/stringutil"
|
"github.com/AdguardTeam/golibs/stringutil"
|
||||||
"github.com/AdguardTeam/golibs/timeutil"
|
"github.com/AdguardTeam/golibs/timeutil"
|
||||||
"golang.org/x/net/idna"
|
"golang.org/x/net/idna"
|
||||||
)
|
)
|
||||||
|
|
||||||
type qlogConfig struct {
|
// configJSON is the JSON structure for the querylog configuration.
|
||||||
// Use float64 here to support fractional numbers and not mess the API
|
type configJSON struct {
|
||||||
// users by changing the units.
|
// Interval is the querylog rotation interval. Use float64 here to support
|
||||||
|
// fractional numbers and not mess the API users by changing the units.
|
||||||
Interval float64 `json:"interval"`
|
Interval float64 `json:"interval"`
|
||||||
Enabled bool `json:"enabled"`
|
|
||||||
AnonymizeClientIP bool `json:"anonymize_client_ip"`
|
// Enabled shows if the querylog is enabled. It is an [aghalg.NullBool]
|
||||||
|
// to be able to tell when it's set without using pointers.
|
||||||
|
Enabled aghalg.NullBool `json:"enabled"`
|
||||||
|
|
||||||
|
// AnonymizeClientIP shows if the clients' IP addresses must be anonymized.
|
||||||
|
// It is an [aghalg.NullBool] to be able to tell when it's set without using
|
||||||
|
// pointers.
|
||||||
|
AnonymizeClientIP aghalg.NullBool `json:"anonymize_client_ip"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register web handlers
|
// Register web handlers
|
||||||
|
@ -56,13 +66,11 @@ func (l *queryLog) handleQueryLogClear(_ http.ResponseWriter, _ *http.Request) {
|
||||||
|
|
||||||
// Get configuration
|
// Get configuration
|
||||||
func (l *queryLog) handleQueryLogInfo(w http.ResponseWriter, r *http.Request) {
|
func (l *queryLog) handleQueryLogInfo(w http.ResponseWriter, r *http.Request) {
|
||||||
resp := qlogConfig{
|
_ = aghhttp.WriteJSONResponse(w, r, configJSON{
|
||||||
Enabled: l.conf.Enabled,
|
Enabled: aghalg.BoolToNullBool(l.conf.Enabled),
|
||||||
Interval: l.conf.RotationIvl.Hours() / 24,
|
Interval: l.conf.RotationIvl.Hours() / 24,
|
||||||
AnonymizeClientIP: l.conf.AnonymizeClientIP,
|
AnonymizeClientIP: aghalg.BoolToNullBool(l.conf.AnonymizeClientIP),
|
||||||
}
|
})
|
||||||
|
|
||||||
_ = aghhttp.WriteJSONResponse(w, r, resp)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AnonymizeIP masks ip to anonymize the client if the ip is a valid one.
|
// AnonymizeIP masks ip to anonymize the client if the ip is a valid one.
|
||||||
|
@ -79,19 +87,25 @@ func AnonymizeIP(ip net.IP) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set configuration
|
// handleQueryLogConfig handles the POST /control/querylog_config queries.
|
||||||
func (l *queryLog) handleQueryLogConfig(w http.ResponseWriter, r *http.Request) {
|
func (l *queryLog) handleQueryLogConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
d := &qlogConfig{}
|
// Set NaN as initial value to be able to know if it changed later by
|
||||||
req, err := jsonutil.DecodeObject(d, r.Body)
|
// comparing it to NaN.
|
||||||
|
newConf := &configJSON{
|
||||||
|
Interval: math.NaN(),
|
||||||
|
}
|
||||||
|
|
||||||
|
err := json.NewDecoder(r.Body).Decode(newConf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
|
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ivl := time.Duration(float64(timeutil.Day) * d.Interval)
|
ivl := time.Duration(float64(timeutil.Day) * newConf.Interval)
|
||||||
if req.Exists("interval") && !checkInterval(ivl) {
|
hasIvl := !math.IsNaN(newConf.Interval)
|
||||||
aghhttp.Error(r, w, http.StatusBadRequest, "Unsupported interval")
|
if hasIvl && !checkInterval(ivl) {
|
||||||
|
aghhttp.Error(r, w, http.StatusBadRequest, "unsupported interval")
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -104,19 +118,23 @@ func (l *queryLog) handleQueryLogConfig(w http.ResponseWriter, r *http.Request)
|
||||||
// Copy data, modify it, then activate. Other threads (readers) don't need
|
// Copy data, modify it, then activate. Other threads (readers) don't need
|
||||||
// to use this lock.
|
// to use this lock.
|
||||||
conf := *l.conf
|
conf := *l.conf
|
||||||
if req.Exists("enabled") {
|
if newConf.Enabled != aghalg.NBNull {
|
||||||
conf.Enabled = d.Enabled
|
conf.Enabled = newConf.Enabled == aghalg.NBTrue
|
||||||
}
|
}
|
||||||
if req.Exists("interval") {
|
|
||||||
|
if hasIvl {
|
||||||
conf.RotationIvl = ivl
|
conf.RotationIvl = ivl
|
||||||
}
|
}
|
||||||
if req.Exists("anonymize_client_ip") {
|
|
||||||
if conf.AnonymizeClientIP = d.AnonymizeClientIP; conf.AnonymizeClientIP {
|
if newConf.AnonymizeClientIP != aghalg.NBNull {
|
||||||
|
conf.AnonymizeClientIP = newConf.AnonymizeClientIP == aghalg.NBTrue
|
||||||
|
if conf.AnonymizeClientIP {
|
||||||
l.anonymizer.Store(AnonymizeIP)
|
l.anonymizer.Store(AnonymizeIP)
|
||||||
} else {
|
} else {
|
||||||
l.anonymizer.Store(nil)
|
l.anonymizer.Store(nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
l.conf = &conf
|
l.conf = &conf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue