+ /control/blocked_services/* API

This commit is contained in:
Simon Zolin 2019-07-23 12:16:36 +03:00
parent e81a9c7d56
commit dc2d8cf075
2 changed files with 50 additions and 0 deletions

View File

@ -1,6 +1,9 @@
package home
import (
"encoding/json"
"net/http"
"github.com/AdguardTeam/AdGuardHome/dnsfilter"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/urlfilter"
@ -68,3 +71,49 @@ func ApplyBlockedServices(setts *dnsfilter.RequestFilteringSettings, list []stri
setts.ServicesRules = append(setts.ServicesRules, s)
}
}
func handleBlockedServicesList(w http.ResponseWriter, r *http.Request) {
log.Tracef("%s %v", r.Method, r.URL)
config.RLock()
list := config.DNS.BlockedServices
config.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)
return
}
}
func handleBlockedServicesSet(w http.ResponseWriter, r *http.Request) {
log.Tracef("%s %v", r.Method, r.URL)
list := []string{}
err := json.NewDecoder(r.Body).Decode(&list)
if err != nil {
httpError(w, http.StatusBadRequest, "json.Decode: %s", err)
return
}
config.Lock()
config.DNS.BlockedServices = list
config.Unlock()
log.Debug("Updated blocked services list: %d", len(list))
err = writeAllConfigsAndReloadDNS()
if err != nil {
httpError(w, http.StatusBadRequest, "%s", err)
return
}
returnOK(w)
}
// RegisterBlockedServicesHandlers - register HTTP handlers
func RegisterBlockedServicesHandlers() {
http.HandleFunc("/control/blocked_services/list", postInstall(optionalAuth(ensureGET(handleBlockedServicesList))))
http.HandleFunc("/control/blocked_services/set", postInstall(optionalAuth(ensurePOST(handleBlockedServicesSet))))
}

View File

@ -1022,6 +1022,7 @@ func registerControlHandlers() {
RegisterTLSHandlers()
RegisterClientsHandlers()
registerRewritesHandlers()
RegisterBlockedServicesHandlers()
http.HandleFunc("/dns-query", postInstall(handleDOH))
}