home: imp filtering handling

This commit is contained in:
Ainar Garipov 2022-08-17 20:40:47 +03:00
parent 63f6844318
commit f54a2dc1da
2 changed files with 48 additions and 24 deletions

View File

@ -13,6 +13,7 @@ import (
"time" "time"
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp" "github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
@ -57,8 +58,8 @@ func (f *Filtering) handleFilteringAddURL(w http.ResponseWriter, r *http.Request
err = validateFilterURL(fj.URL) err = validateFilterURL(fj.URL)
if err != nil { if err != nil {
msg := fmt.Sprintf("invalid url: %s", err) err = fmt.Errorf("invalid url: %s", err)
http.Error(w, msg, http.StatusBadRequest) aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
return 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"` Name string `json:"name"`
URL string `json:"url"` URL string `json:"url"`
Enabled bool `json:"enabled"` Enabled bool `json:"enabled"`
} }
type filterURLReq struct { type filterURLReq struct {
URL string `json:"url"` Data *filterURLReqData `json:"data"`
Whitelist bool `json:"whitelist"` URL string `json:"url"`
Data filterURLJSON `json:"data"` Whitelist bool `json:"whitelist"`
} }
func (f *Filtering) handleFilteringSetURL(w http.ResponseWriter, r *http.Request) { 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 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) err = validateFilterURL(fj.Data.URL)
if err != nil { if err != nil {
msg := fmt.Sprintf("invalid url: %s", err) err = fmt.Errorf("invalid url: %s", err)
http.Error(w, msg, http.StatusBadRequest) aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
return return
} }
@ -223,11 +231,8 @@ func (f *Filtering) handleFilteringSetURL(w http.ResponseWriter, r *http.Request
} }
onConfigModified() onConfigModified()
restart := false
if (status & statusEnabledChanged) != 0 { restart := (status & statusEnabledChanged) != 0
// we must add or remove filter rules
restart = true
}
if (status&statusUpdateRequired) != 0 && fj.Data.Enabled { if (status&statusUpdateRequired) != 0 && fj.Data.Enabled {
// download new filter and apply its rules // download new filter and apply its rules
flags := filterRefreshBlocklists flags := filterRefreshBlocklists
@ -242,6 +247,7 @@ func (f *Filtering) handleFilteringSetURL(w http.ResponseWriter, r *http.Request
restart = true restart = true
} }
} }
if restart { if restart {
enableFilters(true) enableFilters(true)
} }
@ -311,20 +317,20 @@ func (f *Filtering) handleFilteringRefresh(w http.ResponseWriter, r *http.Reques
} }
type filterJSON struct { type filterJSON struct {
ID int64 `json:"id"`
Enabled bool `json:"enabled"`
URL string `json:"url"` URL string `json:"url"`
Name string `json:"name"` Name string `json:"name"`
RulesCount uint32 `json:"rules_count"`
LastUpdated string `json:"last_updated,omitempty"` LastUpdated string `json:"last_updated,omitempty"`
ID int64 `json:"id"`
RulesCount uint32 `json:"rules_count"`
Enabled bool `json:"enabled"`
} }
type filteringConfig struct { type filteringConfig struct {
Enabled bool `json:"enabled"`
Interval uint32 `json:"interval"` // in hours
Filters []filterJSON `json:"filters"` Filters []filterJSON `json:"filters"`
WhitelistFilters []filterJSON `json:"whitelist_filters"` WhitelistFilters []filterJSON `json:"whitelist_filters"`
UserRules []string `json:"user_rules"` UserRules []string `json:"user_rules"`
Interval uint32 `json:"interval"` // in hours
Enabled bool `json:"enabled"`
} }
func filterToJSON(f filter) filterJSON { func filterToJSON(f filter) filterJSON {
@ -402,16 +408,12 @@ func (f *Filtering) handleFilteringConfig(w http.ResponseWriter, r *http.Request
} }
type checkHostRespRule struct { type checkHostRespRule struct {
FilterListID int64 `json:"filter_list_id"`
Text string `json:"text"` Text string `json:"text"`
FilterListID int64 `json:"filter_list_id"`
} }
type checkHostResp struct { type checkHostResp struct {
Reason string `json:"reason"` 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. // Rule is the text of the matched rule.
// //
@ -426,6 +428,11 @@ type checkHostResp struct {
// for Rewrite: // for Rewrite:
CanonName string `json:"cname"` // CNAME value CanonName string `json:"cname"` // CNAME value
IPList []net.IP `json:"ip_addrs"` // list of IP addresses 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) { func (f *Filtering) handleCheckHost(w http.ResponseWriter, r *http.Request) {

View File

@ -1454,11 +1454,28 @@
'description': 'Filtering URL settings' 'description': 'Filtering URL settings'
'properties': 'properties':
'data': 'data':
'$ref': '#/components/schemas/Filter' '$ref': '#/components/schemas/FilterSetUrlData'
'url': 'url':
'type': 'string' 'type': 'string'
'whitelist': 'whitelist':
'type': 'boolean' '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': 'FilterRefreshRequest':
'type': 'object' 'type': 'object'
'description': 'Refresh Filters request data' 'description': 'Refresh Filters request data'