diff --git a/pkg/client/client.go b/pkg/client/client.go index a2b71fa..b5f67e1 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -2,7 +2,9 @@ package client import ( "crypto/tls" + "errors" "fmt" + "net/http" "net/url" "path" @@ -96,24 +98,33 @@ func (cl *client) Host() string { } func (cl *client) Status() (*types.Status, error) { status := &types.Status{} - _, err := cl.client.R().EnableTrace().SetResult(status).Get("status") + resp, err := cl.client.R().EnableTrace().SetResult(status).Get("status") + if err == nil && resp.StatusCode() != http.StatusOK { + return nil, errors.New(resp.Status()) + } return status, err } func (cl *client) RewriteList() (*types.RewriteEntries, error) { rewrites := &types.RewriteEntries{} - _, err := cl.client.R().EnableTrace().SetResult(&rewrites).Get("/rewrite/list") + resp, err := cl.client.R().EnableTrace().SetResult(&rewrites).Get("/rewrite/list") + if err == nil && resp.StatusCode() != http.StatusOK { + return nil, errors.New(resp.Status()) + } return rewrites, err } func (cl *client) AddRewriteEntries(entries ...types.RewriteEntry) error { for _, e := range entries { cl.log.With("domain", e.Domain, "answer", e.Answer).Info("Add rewrite entry") - _, err := cl.client.R().EnableTrace().SetBody(&e).Post("/rewrite/add") + resp, err := cl.client.R().EnableTrace().SetBody(&e).Post("/rewrite/add") if err != nil { return err } + if resp.StatusCode() != http.StatusOK { + return errors.New(resp.Status()) + } } return nil } @@ -121,10 +132,13 @@ func (cl *client) AddRewriteEntries(entries ...types.RewriteEntry) error { func (cl *client) DeleteRewriteEntries(entries ...types.RewriteEntry) error { for _, e := range entries { cl.log.With("domain", e.Domain, "answer", e.Answer).Info("Delete rewrite entry") - _, err := cl.client.R().EnableTrace().SetBody(&e).Post("/rewrite/delete") + resp, err := cl.client.R().EnableTrace().SetBody(&e).Post("/rewrite/delete") if err != nil { return err } + if resp.StatusCode() != http.StatusOK { + return errors.New(resp.Status()) + } } return nil } @@ -155,7 +169,10 @@ func (cl *client) ToggleSafeSearch(enable bool) error { func (cl *client) toggleStatus(mode string) (bool, error) { fs := &types.EnableConfig{} - _, err := cl.client.R().EnableTrace().SetResult(fs).Get(fmt.Sprintf("/%s/status", mode)) + resp, err := cl.client.R().EnableTrace().SetResult(fs).Get(fmt.Sprintf("/%s/status", mode)) + if err == nil && resp.StatusCode() != http.StatusOK { + return false, errors.New(resp.Status()) + } return fs.Enabled, err } @@ -167,13 +184,19 @@ func (cl *client) toggleBool(mode string, enable bool) error { } else { target = "disable" } - _, err := cl.client.R().EnableTrace().Post(fmt.Sprintf("/%s/%s", mode, target)) + resp, err := cl.client.R().EnableTrace().Post(fmt.Sprintf("/%s/%s", mode, target)) + if err == nil && resp.StatusCode() != http.StatusOK { + return errors.New(resp.Status()) + } return err } func (cl *client) Filtering() (*types.FilteringStatus, error) { f := &types.FilteringStatus{} - _, err := cl.client.R().EnableTrace().SetResult(f).Get("/filtering/status") + resp, err := cl.client.R().EnableTrace().SetResult(f).Get("/filtering/status") + if err == nil && resp.StatusCode() != http.StatusOK { + return nil, errors.New(resp.Status()) + } return f, err } @@ -181,10 +204,13 @@ func (cl *client) AddFilters(whitelist bool, filters ...types.Filter) error { for _, f := range filters { cl.log.With("url", f.URL, "whitelist", whitelist).Info("Add filter") ff := &types.Filter{Name: f.Name, URL: f.URL, Whitelist: whitelist} - _, err := cl.client.R().EnableTrace().SetBody(ff).Post("/filtering/add_url") + resp, err := cl.client.R().EnableTrace().SetBody(ff).Post("/filtering/add_url") if err != nil { return err } + if resp.StatusCode() != http.StatusOK { + return errors.New(resp.Status()) + } } return nil } @@ -193,66 +219,94 @@ func (cl *client) DeleteFilters(whitelist bool, filters ...types.Filter) error { for _, f := range filters { cl.log.With("url", f.URL, "whitelist", whitelist).Info("Delete filter") ff := &types.Filter{URL: f.URL, Whitelist: whitelist} - _, err := cl.client.R().EnableTrace().SetBody(ff).Post("/filtering/remove_url") + resp, err := cl.client.R().EnableTrace().SetBody(ff).Post("/filtering/remove_url") if err != nil { return err } + if resp.StatusCode() != http.StatusOK { + return errors.New(resp.Status()) + } } return nil } func (cl *client) RefreshFilters(whitelist bool) error { cl.log.With("whitelist", whitelist).Info("Refresh filter") - _, err := cl.client.R().EnableTrace().SetBody(&types.RefreshFilter{Whitelist: whitelist}).Post("/filtering/refresh") + resp, err := cl.client.R().EnableTrace().SetBody(&types.RefreshFilter{Whitelist: whitelist}).Post("/filtering/refresh") + if err == nil && resp.StatusCode() != http.StatusOK { + return errors.New(resp.Status()) + } return err } func (cl *client) ToggleProtection(enable bool) error { cl.log.With("enable", enable).Info("Toggle protection") - _, err := cl.client.R().EnableTrace().SetBody(&types.Protection{ProtectionEnabled: enable}).Post("/dns_config") + resp, err := cl.client.R().EnableTrace().SetBody(&types.Protection{ProtectionEnabled: enable}).Post("/dns_config") + + if err == nil && resp.StatusCode() != http.StatusOK { + return errors.New(resp.Status()) + } return err } func (cl *client) SetCustomRules(rules types.UserRules) error { cl.log.With("rules", len(rules)).Info("Set user rules") - _, err := cl.client.R().EnableTrace().SetBody(rules.String()).Post("/filtering/set_rules") + resp, err := cl.client.R().EnableTrace().SetBody(rules.String()).Post("/filtering/set_rules") + if err == nil && resp.StatusCode() != http.StatusOK { + return errors.New(resp.Status()) + } return err } 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{ + resp, err := cl.client.R().EnableTrace().SetBody(&types.FilteringConfig{ EnableConfig: types.EnableConfig{Enabled: enabled}, IntervalConfig: types.IntervalConfig{Interval: interval}, }).Post("/filtering/config") + if err == nil && resp.StatusCode() != http.StatusOK { + return errors.New(resp.Status()) + } return err } func (cl *client) Services() (*types.Services, error) { svcs := &types.Services{} - _, err := cl.client.R().EnableTrace().SetResult(svcs).Get("/blocked_services/list") + resp, err := cl.client.R().EnableTrace().SetResult(svcs).Get("/blocked_services/list") + if err == nil && resp.StatusCode() != http.StatusOK { + return nil, errors.New(resp.Status()) + } return svcs, err } func (cl *client) SetServices(services types.Services) error { cl.log.With("services", len(services)).Info("Set services") - _, err := cl.client.R().EnableTrace().SetBody(&services).Post("/blocked_services/set") + resp, err := cl.client.R().EnableTrace().SetBody(&services).Post("/blocked_services/set") + if err == nil && resp.StatusCode() != http.StatusOK { + return errors.New(resp.Status()) + } return err } func (cl *client) Clients() (*types.Clients, error) { clients := &types.Clients{} - _, err := cl.client.R().EnableTrace().SetResult(clients).Get("/clients") + resp, err := cl.client.R().EnableTrace().SetResult(clients).Get("/clients") + if err == nil && resp.StatusCode() != http.StatusOK { + return nil, errors.New(resp.Status()) + } return clients, err } func (cl *client) AddClients(clients ...types.Client) error { for _, client := range clients { cl.log.With("name", client.Name).Info("Add client") - _, err := cl.client.R().EnableTrace().SetBody(&client).Post("/clients/add") + resp, err := cl.client.R().EnableTrace().SetBody(&client).Post("/clients/add") if err != nil { return err } + if resp.StatusCode() != http.StatusOK { + return errors.New(resp.Status()) + } } return nil } @@ -260,10 +314,14 @@ func (cl *client) AddClients(clients ...types.Client) error { func (cl *client) UpdateClients(clients ...types.Client) error { for _, client := range clients { cl.log.With("name", client.Name).Info("Update client") - _, err := cl.client.R().EnableTrace().SetBody(&types.ClientUpdate{Name: client.Name, Data: client}).Post("/clients/update") + resp, err := cl.client.R().EnableTrace().SetBody(&types.ClientUpdate{Name: client.Name, Data: client}).Post("/clients/update") if err != nil { return err } + if resp.StatusCode() != http.StatusOK { + return errors.New(resp.Status()) + } + } return nil } @@ -271,38 +329,54 @@ func (cl *client) UpdateClients(clients ...types.Client) error { func (cl *client) DeleteClients(clients ...types.Client) error { for _, client := range clients { cl.log.With("name", client.Name).Info("Delete client") - _, err := cl.client.R().EnableTrace().SetBody(&client).Post("/clients/delete") + resp, err := cl.client.R().EnableTrace().SetBody(&client).Post("/clients/delete") if err != nil { return err } + if resp.StatusCode() != http.StatusOK { + return errors.New(resp.Status()) + } } return nil } func (cl *client) QueryLogConfig() (*types.QueryLogConfig, error) { qlc := &types.QueryLogConfig{} - _, err := cl.client.R().EnableTrace().SetResult(qlc).Get("/querylog_info") + resp, err := cl.client.R().EnableTrace().SetResult(qlc).Get("/querylog_info") + if err == nil && resp.StatusCode() != http.StatusOK { + return nil, errors.New(resp.Status()) + } 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{ + resp, err := cl.client.R().EnableTrace().SetBody(&types.QueryLogConfig{ EnableConfig: types.EnableConfig{Enabled: enabled}, IntervalConfig: types.IntervalConfig{Interval: interval}, AnonymizeClientIP: anonymizeClientIP, }).Post("/querylog_config") + if err == nil && resp.StatusCode() != http.StatusOK { + return errors.New(resp.Status()) + } return err } func (cl *client) StatsConfig() (*types.IntervalConfig, error) { stats := &types.IntervalConfig{} - _, err := cl.client.R().EnableTrace().SetResult(stats).Get("/stats_info") + resp, err := cl.client.R().EnableTrace().SetResult(stats).Get("/stats_info") + if err == nil && resp.StatusCode() != http.StatusOK { + return nil, errors.New(resp.Status()) + } 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") + resp, err := cl.client.R().EnableTrace().SetBody(&types.IntervalConfig{Interval: interval}).Post("/stats_config") + + if err == nil && resp.StatusCode() != http.StatusOK { + return errors.New(resp.Status()) + } return err } diff --git a/pkg/sync/sync.go b/pkg/sync/sync.go index 7c67e73..98b40b7 100644 --- a/pkg/sync/sync.go +++ b/pkg/sync/sync.go @@ -144,6 +144,7 @@ func (w *worker) syncTo(l *zap.SugaredLogger, o *origin, replica types.AdGuardIn rc, err := client.New(replica) if err != nil { l.With("error", err, "url", replica.URL).Error("Error creating replica client") + return } rl := l.With("to", rc.Host())