mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-28 09:58:52 +03:00
b3210cfa7e
Merge in DNS/adguard-home from 3835-imp-error-msg to master
Updates #3835.
Squashed commit of the following:
commit ba31cb67833df9f293fe13be96a35c2a823f115b
Merge: 19c7dfc9 4be69d35
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Thu Dec 16 20:07:25 2021 +0300
Merge branch 'master' into 3835-imp-error-msg
commit 19c7dfc96284a271d30d7111c86c439be3461389
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Thu Dec 16 19:42:10 2021 +0300
all: imp more
commit 5b9c6a3e357238bf44ef800a6033a7671f27d469
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Thu Dec 16 18:57:02 2021 +0300
all: introduce aghhttp
commit 29caa17200957aad2b98461573bb33d80931adcf
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Thu Dec 16 14:23:53 2021 +0300
all: imp more
commit 754c020191d7b9518cb0e789f3f5741ba38c3cf4
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Dec 15 20:53:41 2021 +0300
all: imp code, log changes
commit ec712dd562f31fcc2fbc27e7035f926c79827444
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Dec 15 18:40:54 2021 +0300
home: check ports properly
139 lines
3.6 KiB
Go
139 lines
3.6 KiB
Go
// HTTP request handlers for accessing statistics data and configuration settings
|
|
|
|
package stats
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
)
|
|
|
|
// 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
|
|
|
|
// statsResponse is a response for getting statistics.
|
|
type statsResponse struct {
|
|
TimeUnits string `json:"time_units"`
|
|
|
|
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"`
|
|
|
|
TopQueried []topAddrs `json:"top_queried_domains"`
|
|
TopClients []topAddrs `json:"top_clients"`
|
|
TopBlocked []topAddrs `json:"top_blocked_domains"`
|
|
|
|
DNSQueries []uint64 `json:"dns_queries"`
|
|
|
|
BlockedFiltering []uint64 `json:"blocked_filtering"`
|
|
ReplacedSafebrowsing []uint64 `json:"replaced_safebrowsing"`
|
|
ReplacedParental []uint64 `json:"replaced_parental"`
|
|
}
|
|
|
|
// handleStats is a handler for getting statistics.
|
|
func (s *statsCtx) handleStats(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
|
|
var resp statsResponse
|
|
if s.conf.limit == 0 {
|
|
resp = statsResponse{
|
|
TimeUnits: "days",
|
|
|
|
TopBlocked: []topAddrs{},
|
|
TopClients: []topAddrs{},
|
|
TopQueried: []topAddrs{},
|
|
|
|
BlockedFiltering: []uint64{},
|
|
DNSQueries: []uint64{},
|
|
ReplacedParental: []uint64{},
|
|
ReplacedSafebrowsing: []uint64{},
|
|
}
|
|
} else {
|
|
var ok bool
|
|
resp, ok = s.getData()
|
|
|
|
log.Debug("stats: prepared data in %v", time.Since(start))
|
|
|
|
if !ok {
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "Couldn't get statistics data")
|
|
|
|
return
|
|
}
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
err := json.NewEncoder(w).Encode(resp)
|
|
if err != nil {
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "json encode: %s", err)
|
|
|
|
return
|
|
}
|
|
}
|
|
|
|
type config struct {
|
|
IntervalDays uint32 `json:"interval"`
|
|
}
|
|
|
|
// Get configuration
|
|
func (s *statsCtx) handleStatsInfo(w http.ResponseWriter, r *http.Request) {
|
|
resp := config{}
|
|
resp.IntervalDays = s.conf.limit / 24
|
|
|
|
data, err := json.Marshal(resp)
|
|
if err != nil {
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "json encode: %s", err)
|
|
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, err = w.Write(data)
|
|
if err != nil {
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "http write: %s", err)
|
|
}
|
|
}
|
|
|
|
// Set configuration
|
|
func (s *statsCtx) handleStatsConfig(w http.ResponseWriter, r *http.Request) {
|
|
reqData := config{}
|
|
err := json.NewDecoder(r.Body).Decode(&reqData)
|
|
if err != nil {
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "json decode: %s", err)
|
|
|
|
return
|
|
}
|
|
|
|
if !checkInterval(reqData.IntervalDays) {
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "Unsupported interval")
|
|
|
|
return
|
|
}
|
|
|
|
s.setLimit(int(reqData.IntervalDays))
|
|
s.conf.ConfigModified()
|
|
}
|
|
|
|
// Reset data
|
|
func (s *statsCtx) handleStatsReset(w http.ResponseWriter, r *http.Request) {
|
|
s.clear()
|
|
}
|
|
|
|
// Register web handlers
|
|
func (s *statsCtx) initWeb() {
|
|
if s.conf.HTTPRegister == nil {
|
|
return
|
|
}
|
|
|
|
s.conf.HTTPRegister(http.MethodGet, "/control/stats", s.handleStats)
|
|
s.conf.HTTPRegister(http.MethodPost, "/control/stats_reset", s.handleStatsReset)
|
|
s.conf.HTTPRegister(http.MethodPost, "/control/stats_config", s.handleStatsConfig)
|
|
s.conf.HTTPRegister(http.MethodGet, "/control/stats_info", s.handleStatsInfo)
|
|
}
|