From aa9503113687f655cf39be66899e6b75c4c827d2 Mon Sep 17 00:00:00 2001 From: bakito Date: Sat, 3 Apr 2021 17:52:08 +0200 Subject: [PATCH] sync stats and query log config --- pkg/client/client.go | 40 ++++++++++++++++++++++++++++++++++-- pkg/sync/http.go | 11 +++++----- pkg/sync/sync.go | 49 ++++++++++++++++++++++++++++++++++++-------- pkg/types/types.go | 20 +++++++++++++++--- 4 files changed, 102 insertions(+), 18 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 9391cb9..935543f 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -76,6 +76,11 @@ type Client interface { AddClients(client ...types.Client) error UpdateClients(client ...types.Client) error DeleteClients(client ...types.Client) error + + QueryLogConfig() (*types.QueryLogConfig, error) + SetQueryLogConfig(enabled bool, interval int, anonymizeClientIP bool) error + StatsConfig() (*types.IntervalConfig, error) + SetStatsConfig(interval int) error } type client struct { @@ -147,7 +152,7 @@ func (cl *client) ToggleSafeSearch(enable bool) error { } func (cl *client) toggleStatus(mode string) (bool, error) { - fs := &types.FeatureStatus{} + fs := &types.EnableConfig{} _, err := cl.client.R().EnableTrace().SetResult(fs).Get(fmt.Sprintf("/%s/status", mode)) return fs.Enabled, err } @@ -214,7 +219,10 @@ func (cl *client) SetCustomRules(rules types.UserRules) error { func (cl *client) ToggleFiltering(enabled bool, interval int) error { cl.log.With("enabled", enabled, "interval", interval).Info("Toggle filtering") - _, err := cl.client.R().EnableTrace().SetBody(&types.FilteringConfig{FeatureStatus: types.FeatureStatus{Enabled: enabled}, Interval: interval}).Post("/filtering/config") + _, err := cl.client.R().EnableTrace().SetBody(&types.FilteringConfig{ + EnableConfig: types.EnableConfig{Enabled: enabled}, + IntervalConfig: types.IntervalConfig{Interval: interval}, + }).Post("/filtering/config") return err } @@ -268,3 +276,31 @@ func (cl *client) DeleteClients(clients ...types.Client) error { } return nil } + +func (cl *client) QueryLogConfig() (*types.QueryLogConfig, error) { + qlc := &types.QueryLogConfig{} + _, err := cl.client.R().EnableTrace().SetResult(qlc).Get("/querylog_info") + return qlc, err +} + +func (cl *client) SetQueryLogConfig(enabled bool, interval int, anonymizeClientIP bool) error { + cl.log.With("enabled", enabled, "interval", interval, "anonymizeClientIP", anonymizeClientIP).Info("Set query log config") + _, err := cl.client.R().EnableTrace().SetBody(&types.QueryLogConfig{ + EnableConfig: types.EnableConfig{Enabled: enabled}, + IntervalConfig: types.IntervalConfig{Interval: interval}, + AnonymizeClientIP: anonymizeClientIP, + }).Post("/querylog_config") + return err +} + +func (cl *client) StatsConfig() (*types.IntervalConfig, error) { + stats := &types.IntervalConfig{} + _, err := cl.client.R().EnableTrace().SetResult(stats).Get("/stats_info") + return stats, err +} + +func (cl *client) SetStatsConfig(interval int) error { + cl.log.With("interval", interval).Info("Set stats config") + _, err := cl.client.R().EnableTrace().SetBody(&types.IntervalConfig{Interval: interval}).Post("/stats_config") + return err +} diff --git a/pkg/sync/http.go b/pkg/sync/http.go index 9a2e68a..60b1608 100644 --- a/pkg/sync/http.go +++ b/pkg/sync/http.go @@ -64,13 +64,14 @@ func (w *worker) listenAndServe() { BaseContext: func(_ net.Listener) context.Context { return ctx }, } + var mw []func(http.HandlerFunc) http.HandlerFunc if w.cfg.API.Username != "" && w.cfg.API.Password != "" { - mux.HandleFunc("/api/v1/sync", use(w.handleSync, w.basicAuth)) - mux.HandleFunc("/", use(w.handleRoot, w.basicAuth)) - } else { - mux.HandleFunc("/api/v1/sync", w.handleSync) - mux.HandleFunc("/", w.handleRoot) + mw = append(mw, w.basicAuth) } + + mux.HandleFunc("/api/v1/sync", use(w.handleSync, mw...)) + mux.HandleFunc("/", use(w.handleRoot, mw...)) + go func() { if err := httpServer.ListenAndServe(); err != http.ErrServerClosed { l.With("error", err).Fatalf("HTTP server ListenAndServe") diff --git a/pkg/sync/sync.go b/pkg/sync/sync.go index 30051cf..75d19a0 100644 --- a/pkg/sync/sync.go +++ b/pkg/sync/sync.go @@ -109,6 +109,16 @@ func (w *worker) sync() { sl.With("error", err).Error("Error getting origin clients") return } + o.queryLogConfig, err = oc.QueryLogConfig() + if err != nil { + sl.With("error", err).Error("Error getting query log config") + return + } + o.statsConfig, err = oc.StatsConfig() + if err != nil { + sl.With("error", err).Error("Error getting stats config") + return + } replicas := w.cfg.UniqueReplicas() for _, replica := range replicas { @@ -295,16 +305,39 @@ func (w *worker) syncGeneralSettings(o *origin, rs *types.Status, replica client return err } } + + qlc, err := replica.QueryLogConfig() + if err != nil { + return err + } + if !o.queryLogConfig.Equals(qlc) { + if err = replica.SetQueryLogConfig(o.queryLogConfig.Enabled, o.queryLogConfig.Interval, o.queryLogConfig.AnonymizeClientIP); err != nil { + return err + } + } + + sc, err := replica.StatsConfig() + if err != nil { + return err + } + if o.statsConfig.Interval != sc.Interval { + if err = replica.SetStatsConfig(o.statsConfig.Interval); err != nil { + return err + } + } + return nil } type origin struct { - status *types.Status - rewrites *types.RewriteEntries - services *types.Services - filters *types.FilteringStatus - clients *types.Clients - parental bool - safeSearch bool - safeBrowsing bool + status *types.Status + rewrites *types.RewriteEntries + services *types.Services + filters *types.FilteringStatus + clients *types.Clients + queryLogConfig *types.QueryLogConfig + statsConfig *types.IntervalConfig + parental bool + safeSearch bool + safeBrowsing bool } diff --git a/pkg/types/types.go b/pkg/types/types.go index a99c40b..226086a 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -125,15 +125,29 @@ func (ur UserRules) String() string { return strings.Join(ur, "\n") } -type FeatureStatus struct { +type EnableConfig struct { Enabled bool `json:"enabled"` } -type FilteringConfig struct { - FeatureStatus +type IntervalConfig struct { Interval int `json:"interval"` } +type FilteringConfig struct { + EnableConfig + IntervalConfig +} + +type QueryLogConfig struct { + EnableConfig + IntervalConfig + AnonymizeClientIP bool `json:"anonymize_client_ip"` +} + +func (qlc *QueryLogConfig) Equals(o *QueryLogConfig) bool { + return qlc.Enabled == o.Enabled && qlc.AnonymizeClientIP == o.AnonymizeClientIP && qlc.Interval == o.Interval +} + type RefreshFilter struct { Whitelist bool `json:"whitelist"` }