2019-09-25 15:36:09 +03:00
|
|
|
// HTTP request handlers for accessing statistics data and configuration settings
|
|
|
|
|
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2019-10-07 12:03:56 +03:00
|
|
|
"time"
|
2019-09-25 15:36:09 +03:00
|
|
|
|
2023-03-23 13:46:57 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
2021-12-16 20:54:59 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
2023-03-23 13:46:57 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2019-09-25 15:36:09 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2023-03-23 13:46:57 +03:00
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
|
|
|
"golang.org/x/exp/slices"
|
2019-09-25 15:36:09 +03:00
|
|
|
)
|
|
|
|
|
2021-06-17 19:44:46 +03:00
|
|
|
// topAddrs is an alias for the types of the TopFoo fields of statsResponse.
|
|
|
|
// The key is either a client's address or a requested address.
|
|
|
|
type topAddrs = map[string]uint64
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
// StatsResp is a response to the GET /control/stats.
|
|
|
|
type StatsResp struct {
|
2021-01-21 19:55:41 +03:00
|
|
|
TimeUnits string `json:"time_units"`
|
|
|
|
|
2021-06-17 19:44:46 +03:00
|
|
|
TopQueried []topAddrs `json:"top_queried_domains"`
|
|
|
|
TopClients []topAddrs `json:"top_clients"`
|
|
|
|
TopBlocked []topAddrs `json:"top_blocked_domains"`
|
2021-01-21 19:55:41 +03:00
|
|
|
|
|
|
|
DNSQueries []uint64 `json:"dns_queries"`
|
|
|
|
|
|
|
|
BlockedFiltering []uint64 `json:"blocked_filtering"`
|
|
|
|
ReplacedSafebrowsing []uint64 `json:"replaced_safebrowsing"`
|
|
|
|
ReplacedParental []uint64 `json:"replaced_parental"`
|
2022-08-17 14:09:13 +03:00
|
|
|
|
|
|
|
NumDNSQueries uint64 `json:"num_dns_queries"`
|
|
|
|
NumBlockedFiltering uint64 `json:"num_blocked_filtering"`
|
|
|
|
NumReplacedSafebrowsing uint64 `json:"num_replaced_safebrowsing"`
|
|
|
|
NumReplacedSafesearch uint64 `json:"num_replaced_safesearch"`
|
|
|
|
NumReplacedParental uint64 `json:"num_replaced_parental"`
|
|
|
|
|
|
|
|
AvgProcessingTime float64 `json:"avg_processing_time"`
|
2021-01-21 19:55:41 +03:00
|
|
|
}
|
|
|
|
|
2023-04-05 15:50:14 +03:00
|
|
|
// handleStats is the handler for the GET /control/stats HTTP API.
|
2022-08-04 19:05:28 +03:00
|
|
|
func (s *StatsCtx) handleStats(w http.ResponseWriter, r *http.Request) {
|
2022-08-17 14:09:13 +03:00
|
|
|
start := time.Now()
|
2023-04-05 15:50:14 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
resp StatsResp
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
func() {
|
|
|
|
s.confMu.RLock()
|
|
|
|
defer s.confMu.RUnlock()
|
|
|
|
|
|
|
|
resp, ok = s.getData(uint32(s.limit.Hours()))
|
|
|
|
}()
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
log.Debug("stats: prepared data in %v", time.Since(start))
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
// Don't bring the message to the lower case since it's a part of UI
|
|
|
|
// text for the moment.
|
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "Couldn't get statistics data")
|
|
|
|
|
|
|
|
return
|
2019-09-25 15:36:09 +03:00
|
|
|
}
|
|
|
|
|
2022-10-04 14:35:10 +03:00
|
|
|
_ = aghhttp.WriteJSONResponse(w, r, resp)
|
2019-09-25 15:36:09 +03:00
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
// configResp is the response to the GET /control/stats_info.
|
|
|
|
type configResp struct {
|
2019-09-25 15:36:09 +03:00
|
|
|
IntervalDays uint32 `json:"interval"`
|
|
|
|
}
|
|
|
|
|
2023-03-23 13:46:57 +03:00
|
|
|
// getConfigResp is the response to the GET /control/stats_info.
|
|
|
|
type getConfigResp struct {
|
|
|
|
// Ignored is the list of host names, which should not be counted.
|
|
|
|
Ignored []string `json:"ignored"`
|
|
|
|
|
|
|
|
// Interval is the statistics rotation interval in milliseconds.
|
|
|
|
Interval float64 `json:"interval"`
|
|
|
|
|
|
|
|
// Enabled shows if statistics are enabled. It is an aghalg.NullBool to be
|
|
|
|
// able to tell when it's set without using pointers.
|
|
|
|
Enabled aghalg.NullBool `json:"enabled"`
|
|
|
|
}
|
|
|
|
|
2023-04-05 15:50:14 +03:00
|
|
|
// handleStatsInfo is the handler for the GET /control/stats_info HTTP API.
|
2023-03-23 13:46:57 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Remove it when migration to the new API is over.
|
2022-08-04 19:05:28 +03:00
|
|
|
func (s *StatsCtx) handleStatsInfo(w http.ResponseWriter, r *http.Request) {
|
2023-04-05 15:50:14 +03:00
|
|
|
var (
|
|
|
|
enabled bool
|
|
|
|
limit time.Duration
|
|
|
|
)
|
|
|
|
func() {
|
|
|
|
s.confMu.RLock()
|
|
|
|
defer s.confMu.RUnlock()
|
|
|
|
|
|
|
|
enabled, limit = s.enabled, s.limit
|
|
|
|
}()
|
|
|
|
|
|
|
|
days := uint32(limit / timeutil.Day)
|
2023-03-23 13:46:57 +03:00
|
|
|
ok := checkInterval(days)
|
2023-04-05 15:50:14 +03:00
|
|
|
if !ok || (enabled && days == 0) {
|
2023-03-23 13:46:57 +03:00
|
|
|
// NOTE: If interval is custom we set it to 90 days for compatibility
|
|
|
|
// with old API.
|
|
|
|
days = 90
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := configResp{IntervalDays: days}
|
2023-04-05 15:50:14 +03:00
|
|
|
if !enabled {
|
2023-02-13 18:15:33 +03:00
|
|
|
resp.IntervalDays = 0
|
|
|
|
}
|
2023-04-05 15:50:14 +03:00
|
|
|
|
2022-10-04 14:35:10 +03:00
|
|
|
_ = aghhttp.WriteJSONResponse(w, r, resp)
|
2019-09-25 15:36:09 +03:00
|
|
|
}
|
|
|
|
|
2023-04-05 15:50:14 +03:00
|
|
|
// handleGetStatsConfig is the handler for the GET /control/stats/config HTTP
|
|
|
|
// API.
|
2023-03-23 13:46:57 +03:00
|
|
|
func (s *StatsCtx) handleGetStatsConfig(w http.ResponseWriter, r *http.Request) {
|
2023-04-05 15:50:14 +03:00
|
|
|
var resp *getConfigResp
|
|
|
|
func() {
|
|
|
|
s.confMu.RLock()
|
|
|
|
defer s.confMu.RUnlock()
|
2023-03-23 13:46:57 +03:00
|
|
|
|
2023-04-05 15:50:14 +03:00
|
|
|
resp = &getConfigResp{
|
|
|
|
Ignored: s.ignored.Values(),
|
|
|
|
Interval: float64(s.limit.Milliseconds()),
|
|
|
|
Enabled: aghalg.BoolToNullBool(s.enabled),
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
slices.Sort(resp.Ignored)
|
2023-03-23 13:46:57 +03:00
|
|
|
|
|
|
|
_ = aghhttp.WriteJSONResponse(w, r, resp)
|
|
|
|
}
|
|
|
|
|
2023-04-05 15:50:14 +03:00
|
|
|
// handleStatsConfig is the handler for the POST /control/stats_config HTTP API.
|
2023-03-23 13:46:57 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Remove it when migration to the new API is over.
|
2022-08-04 19:05:28 +03:00
|
|
|
func (s *StatsCtx) handleStatsConfig(w http.ResponseWriter, r *http.Request) {
|
2022-08-17 14:09:13 +03:00
|
|
|
reqData := configResp{}
|
2019-09-25 15:36:09 +03:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&reqData)
|
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "json decode: %s", err)
|
|
|
|
|
2019-09-25 15:36:09 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !checkInterval(reqData.IntervalDays) {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "Unsupported interval")
|
|
|
|
|
2019-09-25 15:36:09 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-05 15:50:14 +03:00
|
|
|
limit := time.Duration(reqData.IntervalDays) * timeutil.Day
|
|
|
|
|
2023-03-23 13:46:57 +03:00
|
|
|
defer s.configModified()
|
|
|
|
|
2023-04-05 15:50:14 +03:00
|
|
|
s.confMu.Lock()
|
|
|
|
defer s.confMu.Unlock()
|
2023-03-23 13:46:57 +03:00
|
|
|
|
|
|
|
s.setLimit(limit)
|
|
|
|
}
|
|
|
|
|
2023-04-05 15:50:14 +03:00
|
|
|
// handlePutStatsConfig is the handler for the PUT /control/stats/config/update
|
|
|
|
// HTTP API.
|
2023-03-23 13:46:57 +03:00
|
|
|
func (s *StatsCtx) handlePutStatsConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
reqData := getConfigResp{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&reqData)
|
|
|
|
if err != nil {
|
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "json decode: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
set, err := aghnet.NewDomainNameSet(reqData.Ignored)
|
|
|
|
if err != nil {
|
|
|
|
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "ignored: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ivl := time.Duration(reqData.Interval) * time.Millisecond
|
|
|
|
err = validateIvl(ivl)
|
|
|
|
if err != nil {
|
|
|
|
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "unsupported interval: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if reqData.Enabled == aghalg.NBNull {
|
|
|
|
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "enabled is null")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer s.configModified()
|
|
|
|
|
2023-04-05 15:50:14 +03:00
|
|
|
s.confMu.Lock()
|
|
|
|
defer s.confMu.Unlock()
|
2023-03-23 13:46:57 +03:00
|
|
|
|
|
|
|
s.ignored = set
|
|
|
|
s.limit = ivl
|
|
|
|
s.enabled = reqData.Enabled == aghalg.NBTrue
|
2019-09-25 15:36:09 +03:00
|
|
|
}
|
|
|
|
|
2023-04-05 15:50:14 +03:00
|
|
|
// handleStatsReset is the handler for the POST /control/stats_reset HTTP API.
|
2022-08-04 19:05:28 +03:00
|
|
|
func (s *StatsCtx) handleStatsReset(w http.ResponseWriter, r *http.Request) {
|
2022-08-17 14:09:13 +03:00
|
|
|
err := s.clear()
|
|
|
|
if err != nil {
|
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "stats: %s", err)
|
|
|
|
}
|
2019-09-25 15:36:09 +03:00
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
// initWeb registers the handlers for web endpoints of statistics module.
|
2022-08-04 19:05:28 +03:00
|
|
|
func (s *StatsCtx) initWeb() {
|
|
|
|
if s.httpRegister == nil {
|
2019-09-25 15:36:09 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
s.httpRegister(http.MethodGet, "/control/stats", s.handleStats)
|
|
|
|
s.httpRegister(http.MethodPost, "/control/stats_reset", s.handleStatsReset)
|
2023-03-23 13:46:57 +03:00
|
|
|
s.httpRegister(http.MethodGet, "/control/stats/config", s.handleGetStatsConfig)
|
|
|
|
s.httpRegister(http.MethodPut, "/control/stats/config/update", s.handlePutStatsConfig)
|
2023-04-05 15:50:14 +03:00
|
|
|
|
|
|
|
// Deprecated handlers.
|
|
|
|
s.httpRegister(http.MethodGet, "/control/stats_info", s.handleStatsInfo)
|
|
|
|
s.httpRegister(http.MethodPost, "/control/stats_config", s.handleStatsConfig)
|
2019-09-25 15:36:09 +03:00
|
|
|
}
|