* refactor: move blocked-services functions to dnsfilter

This commit is contained in:
Simon Zolin 2020-02-18 20:17:35 +03:00
parent e8129f15c7
commit 0789e4b20d
8 changed files with 51 additions and 51 deletions

View file

@ -1,10 +1,9 @@
package home
package dnsfilter
import (
"encoding/json"
"net/http"
"github.com/AdguardTeam/AdGuardHome/dnsfilter"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/urlfilter/rules"
)
@ -119,7 +118,7 @@ var serviceRulesArray = []svc{
}
// convert array to map
func initServices() {
func initBlockedServices() {
serviceRules = make(map[string][]*rules.NetworkRule)
for _, s := range serviceRulesArray {
netRules := []*rules.NetworkRule{}
@ -135,15 +134,20 @@ func initServices() {
}
}
// Return TRUE if a blocked service name is known
func blockedSvcKnown(s string) bool {
// BlockedSvcKnown - return TRUE if a blocked service name is known
func BlockedSvcKnown(s string) bool {
_, ok := serviceRules[s]
return ok
}
// ApplyBlockedServices - set blocked services settings for this DNS request
func ApplyBlockedServices(setts *dnsfilter.RequestFilteringSettings, list []string) {
setts.ServicesRules = []dnsfilter.ServiceEntry{}
func (d *Dnsfilter) ApplyBlockedServices(setts *RequestFilteringSettings, list []string, global bool) {
setts.ServicesRules = []ServiceEntry{}
if global {
d.confLock.RLock()
defer d.confLock.RUnlock()
list = d.Config.BlockedServices
}
for _, name := range list {
rules, ok := serviceRules[name]
@ -152,51 +156,45 @@ func ApplyBlockedServices(setts *dnsfilter.RequestFilteringSettings, list []stri
continue
}
s := dnsfilter.ServiceEntry{}
s := ServiceEntry{}
s.Name = name
s.Rules = rules
setts.ServicesRules = append(setts.ServicesRules, s)
}
}
func handleBlockedServicesList(w http.ResponseWriter, r *http.Request) {
config.RLock()
list := config.DNS.BlockedServices
config.RUnlock()
func (d *Dnsfilter) handleBlockedServicesList(w http.ResponseWriter, r *http.Request) {
d.confLock.RLock()
list := d.Config.BlockedServices
d.confLock.RUnlock()
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(list)
if err != nil {
httpError(w, http.StatusInternalServerError, "json.Encode: %s", err)
httpError(r, w, http.StatusInternalServerError, "json.Encode: %s", err)
return
}
}
func handleBlockedServicesSet(w http.ResponseWriter, r *http.Request) {
func (d *Dnsfilter) handleBlockedServicesSet(w http.ResponseWriter, r *http.Request) {
list := []string{}
err := json.NewDecoder(r.Body).Decode(&list)
if err != nil {
httpError(w, http.StatusBadRequest, "json.Decode: %s", err)
httpError(r, w, http.StatusBadRequest, "json.Decode: %s", err)
return
}
config.Lock()
config.DNS.BlockedServices = list
config.Unlock()
d.confLock.Lock()
d.Config.BlockedServices = list
d.confLock.Unlock()
log.Debug("Updated blocked services list: %d", len(list))
err = writeAllConfigsAndReloadDNS()
if err != nil {
httpError(w, http.StatusBadRequest, "%s", err)
return
}
httpOK(r, w)
d.ConfigModified()
}
// RegisterBlockedServicesHandlers - register HTTP handlers
func RegisterBlockedServicesHandlers() {
httpRegister(http.MethodGet, "/control/blocked_services/list", handleBlockedServicesList)
httpRegister(http.MethodPost, "/control/blocked_services/set", handleBlockedServicesSet)
// registerBlockedServicesHandlers - register HTTP handlers
func (d *Dnsfilter) registerBlockedServicesHandlers() {
d.Config.HTTPRegister("GET", "/control/blocked_services/list", d.handleBlockedServicesList)
d.Config.HTTPRegister("POST", "/control/blocked_services/set", d.handleBlockedServicesSet)
}

View file

@ -49,6 +49,10 @@ type Config struct {
Rewrites []RewriteEntry `yaml:"rewrites"`
// Names of services to block (globally).
// Per-client settings can override this configuration.
BlockedServices []string `yaml:"blocked_services"`
// Called when the configuration is changed by HTTP request
ConfigModified func() `yaml:"-"`
@ -175,6 +179,7 @@ func (d *Dnsfilter) WriteDiskConfig(c *Config) {
d.confLock.Lock()
*c = d.Config
c.Rewrites = rewriteArrayDup(d.Config.Rewrites)
// BlockedServices
d.confLock.Unlock()
}
@ -633,6 +638,18 @@ func New(c *Config, blockFilters []Filter) *Dnsfilter {
d.prepareRewrites()
}
bsvcs := []string{}
for _, s := range d.BlockedServices {
if !BlockedSvcKnown(s) {
log.Debug("skipping unknown blocked-service '%s'", s)
continue
}
bsvcs = append(bsvcs, s)
}
d.BlockedServices = bsvcs
initBlockedServices()
if blockFilters != nil {
err := d.initFiltering(nil, blockFilters)
if err != nil {
@ -655,6 +672,7 @@ func (d *Dnsfilter) Start() {
if d.Config.HTTPRegister != nil { // for tests
d.registerSecurityHandlers()
d.registerRewritesHandlers()
d.registerBlockedServicesHandlers()
}
}

View file

@ -14,6 +14,7 @@ import (
"time"
"github.com/AdguardTeam/AdGuardHome/dhcpd"
"github.com/AdguardTeam/AdGuardHome/dnsfilter"
"github.com/AdguardTeam/AdGuardHome/dnsforward"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/log"
@ -161,7 +162,7 @@ func (clients *clientsContainer) addFromConfig(objects []clientObject) {
}
for _, s := range cy.BlockedServices {
if !blockedSvcKnown(s) {
if !dnsfilter.BlockedSvcKnown(s) {
log.Debug("Clients: skipping unknown blocked-service '%s'", s)
continue
}

View file

@ -95,10 +95,6 @@ type dnsConfig struct {
FilteringEnabled bool `yaml:"filtering_enabled"` // whether or not use filter lists
FiltersUpdateIntervalHours uint32 `yaml:"filters_update_interval"` // time period to update filters (in hours)
DnsfilterConf dnsfilter.Config `yaml:",inline"`
// Names of services to block (globally).
// Per-client settings can override this configuration.
BlockedServices []string `yaml:"blocked_services"`
}
type tlsConfigSettings struct {
@ -234,16 +230,6 @@ func parseConfig() error {
return err
}
bsvcs := []string{}
for _, s := range config.DNS.BlockedServices {
if !blockedSvcKnown(s) {
log.Debug("skipping unknown blocked-service '%s'", s)
continue
}
bsvcs = append(bsvcs, s)
}
config.DNS.BlockedServices = bsvcs
if !checkFiltersUpdateIntervalHours(config.DNS.FiltersUpdateIntervalHours) {
config.DNS.FiltersUpdateIntervalHours = 24
}

View file

@ -9,9 +9,8 @@ import (
"strconv"
"strings"
"github.com/AdguardTeam/AdGuardHome/util"
"github.com/AdguardTeam/AdGuardHome/dnsforward"
"github.com/AdguardTeam/AdGuardHome/util"
"github.com/AdguardTeam/golibs/log"
"github.com/NYTimes/gziphandler"
)
@ -174,7 +173,6 @@ func registerControlHandlers() {
httpRegister("GET", "/control/profile", handleGetProfile)
RegisterTLSHandlers()
RegisterBlockedServicesHandlers()
RegisterAuthHandlers()
http.HandleFunc("/dns-query", postInstall(handleDOH))

View file

@ -349,7 +349,7 @@ func (f *Filtering) handleCheckHost(w http.ResponseWriter, r *http.Request) {
setts := Context.dnsFilter.GetConfig()
setts.FilteringEnabled = true
ApplyBlockedServices(&setts, config.DNS.BlockedServices)
Context.dnsFilter.ApplyBlockedServices(&setts, nil, true)
result, err := Context.dnsFilter.CheckHost(host, dns.TypeA, &setts)
if err != nil {
httpError(w, http.StatusInternalServerError, "couldn't apply filtering: %s: %s", host, err)

View file

@ -175,7 +175,7 @@ func getUpstreamsByClient(clientAddr string) []upstream.Upstream {
// If a client has his own settings, apply them
func applyAdditionalFiltering(clientAddr string, setts *dnsfilter.RequestFilteringSettings) {
ApplyBlockedServices(setts, config.DNS.BlockedServices)
Context.dnsFilter.ApplyBlockedServices(setts, nil, true)
if len(clientAddr) == 0 {
return
@ -189,7 +189,7 @@ func applyAdditionalFiltering(clientAddr string, setts *dnsfilter.RequestFilteri
log.Debug("Using settings for client with IP %s", clientAddr)
if c.UseOwnBlockedServices {
ApplyBlockedServices(setts, c.BlockedServices)
Context.dnsFilter.ApplyBlockedServices(setts, c.BlockedServices, false)
}
setts.ClientTags = c.Tags

View file

@ -172,7 +172,6 @@ func run(args options) {
}
initConfig()
initServices()
Context.tlsRoots = util.LoadSystemRootCAs()
Context.transport = &http.Transport{