From f54a2dc1da5dfd578f156cf1e0f53f32516eb844 Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Wed, 17 Aug 2022 20:40:47 +0300 Subject: [PATCH] home: imp filtering handling --- internal/home/controlfiltering.go | 53 +++++++++++++++++-------------- openapi/openapi.yaml | 19 ++++++++++- 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/internal/home/controlfiltering.go b/internal/home/controlfiltering.go index 51d86146..a4c8651a 100644 --- a/internal/home/controlfiltering.go +++ b/internal/home/controlfiltering.go @@ -13,6 +13,7 @@ import ( "time" "github.com/AdguardTeam/AdGuardHome/internal/aghhttp" + "github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/log" "github.com/miekg/dns" ) @@ -57,8 +58,8 @@ func (f *Filtering) handleFilteringAddURL(w http.ResponseWriter, r *http.Request err = validateFilterURL(fj.URL) if err != nil { - msg := fmt.Sprintf("invalid url: %s", err) - http.Error(w, msg, http.StatusBadRequest) + err = fmt.Errorf("invalid url: %s", err) + aghhttp.Error(r, w, http.StatusBadRequest, "%s", err) return } @@ -178,16 +179,16 @@ func (f *Filtering) handleFilteringRemoveURL(w http.ResponseWriter, r *http.Requ } } -type filterURLJSON struct { +type filterURLReqData struct { Name string `json:"name"` URL string `json:"url"` Enabled bool `json:"enabled"` } type filterURLReq struct { - URL string `json:"url"` - Whitelist bool `json:"whitelist"` - Data filterURLJSON `json:"data"` + Data *filterURLReqData `json:"data"` + URL string `json:"url"` + Whitelist bool `json:"whitelist"` } func (f *Filtering) handleFilteringSetURL(w http.ResponseWriter, r *http.Request) { @@ -199,10 +200,17 @@ func (f *Filtering) handleFilteringSetURL(w http.ResponseWriter, r *http.Request return } + if fj.Data == nil { + err = errors.Error("data cannot be null") + aghhttp.Error(r, w, http.StatusBadRequest, "%s", err) + + return + } + err = validateFilterURL(fj.Data.URL) if err != nil { - msg := fmt.Sprintf("invalid url: %s", err) - http.Error(w, msg, http.StatusBadRequest) + err = fmt.Errorf("invalid url: %s", err) + aghhttp.Error(r, w, http.StatusBadRequest, "%s", err) return } @@ -223,11 +231,8 @@ func (f *Filtering) handleFilteringSetURL(w http.ResponseWriter, r *http.Request } onConfigModified() - restart := false - if (status & statusEnabledChanged) != 0 { - // we must add or remove filter rules - restart = true - } + + restart := (status & statusEnabledChanged) != 0 if (status&statusUpdateRequired) != 0 && fj.Data.Enabled { // download new filter and apply its rules flags := filterRefreshBlocklists @@ -242,6 +247,7 @@ func (f *Filtering) handleFilteringSetURL(w http.ResponseWriter, r *http.Request restart = true } } + if restart { enableFilters(true) } @@ -311,20 +317,20 @@ func (f *Filtering) handleFilteringRefresh(w http.ResponseWriter, r *http.Reques } type filterJSON struct { - ID int64 `json:"id"` - Enabled bool `json:"enabled"` URL string `json:"url"` Name string `json:"name"` - RulesCount uint32 `json:"rules_count"` LastUpdated string `json:"last_updated,omitempty"` + ID int64 `json:"id"` + RulesCount uint32 `json:"rules_count"` + Enabled bool `json:"enabled"` } type filteringConfig struct { - Enabled bool `json:"enabled"` - Interval uint32 `json:"interval"` // in hours Filters []filterJSON `json:"filters"` WhitelistFilters []filterJSON `json:"whitelist_filters"` UserRules []string `json:"user_rules"` + Interval uint32 `json:"interval"` // in hours + Enabled bool `json:"enabled"` } func filterToJSON(f filter) filterJSON { @@ -402,16 +408,12 @@ func (f *Filtering) handleFilteringConfig(w http.ResponseWriter, r *http.Request } type checkHostRespRule struct { - FilterListID int64 `json:"filter_list_id"` Text string `json:"text"` + FilterListID int64 `json:"filter_list_id"` } type checkHostResp struct { Reason string `json:"reason"` - // FilterID is the ID of the rule's filter list. - // - // Deprecated: Use Rules[*].FilterListID. - FilterID int64 `json:"filter_id"` // Rule is the text of the matched rule. // @@ -426,6 +428,11 @@ type checkHostResp struct { // for Rewrite: CanonName string `json:"cname"` // CNAME value IPList []net.IP `json:"ip_addrs"` // list of IP addresses + + // FilterID is the ID of the rule's filter list. + // + // Deprecated: Use Rules[*].FilterListID. + FilterID int64 `json:"filter_id"` } func (f *Filtering) handleCheckHost(w http.ResponseWriter, r *http.Request) { diff --git a/openapi/openapi.yaml b/openapi/openapi.yaml index bb3bfe09..eb7b1894 100644 --- a/openapi/openapi.yaml +++ b/openapi/openapi.yaml @@ -1454,11 +1454,28 @@ 'description': 'Filtering URL settings' 'properties': 'data': - '$ref': '#/components/schemas/Filter' + '$ref': '#/components/schemas/FilterSetUrlData' 'url': 'type': 'string' 'whitelist': 'type': 'boolean' + 'FilterSetUrlData': + 'type': 'object' + 'description': 'Filter update data' + 'required': + - 'enabled' + - 'name' + - 'url' + 'properties': + 'enabled': + 'type': 'boolean' + 'name': + 'example': 'AdGuard Simplified Domain Names filter' + 'type': 'string' + 'url': + 'type': 'string' + 'example': > + https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt 'FilterRefreshRequest': 'type': 'object' 'description': 'Refresh Filters request data'