all: sync with master; upd chlog
This commit is contained in:
@@ -50,10 +50,10 @@ func initBlockedServices() {
|
||||
// BlockedServices is the configuration of blocked services.
|
||||
type BlockedServices struct {
|
||||
// Schedule is blocked services schedule for every day of the week.
|
||||
Schedule *schedule.Weekly `yaml:"schedule"`
|
||||
Schedule *schedule.Weekly `json:"schedule" yaml:"schedule"`
|
||||
|
||||
// IDs is the names of blocked services.
|
||||
IDs []string `yaml:"ids"`
|
||||
IDs []string `json:"ids" yaml:"ids"`
|
||||
}
|
||||
|
||||
// Clone returns a deep copy of blocked services.
|
||||
@@ -83,12 +83,12 @@ func (s *BlockedServices) Validate() (err error) {
|
||||
|
||||
// ApplyBlockedServices - set blocked services settings for this DNS request
|
||||
func (d *DNSFilter) ApplyBlockedServices(setts *Settings) {
|
||||
d.confLock.RLock()
|
||||
defer d.confLock.RUnlock()
|
||||
d.confMu.RLock()
|
||||
defer d.confMu.RUnlock()
|
||||
|
||||
setts.ServicesRules = []ServiceEntry{}
|
||||
|
||||
bsvc := d.BlockedServices
|
||||
bsvc := d.conf.BlockedServices
|
||||
|
||||
// TODO(s.chzhen): Use startTime from [dnsforward.dnsContext].
|
||||
if !bsvc.Schedule.Contains(time.Now()) {
|
||||
@@ -114,25 +114,37 @@ func (d *DNSFilter) ApplyBlockedServicesList(setts *Settings, list []string) {
|
||||
}
|
||||
|
||||
func (d *DNSFilter) handleBlockedServicesIDs(w http.ResponseWriter, r *http.Request) {
|
||||
_ = aghhttp.WriteJSONResponse(w, r, serviceIDs)
|
||||
aghhttp.WriteJSONResponseOK(w, r, serviceIDs)
|
||||
}
|
||||
|
||||
func (d *DNSFilter) handleBlockedServicesAll(w http.ResponseWriter, r *http.Request) {
|
||||
_ = aghhttp.WriteJSONResponse(w, r, struct {
|
||||
aghhttp.WriteJSONResponseOK(w, r, struct {
|
||||
BlockedServices []blockedService `json:"blocked_services"`
|
||||
}{
|
||||
BlockedServices: blockedServices,
|
||||
})
|
||||
}
|
||||
|
||||
// handleBlockedServicesList is the handler for the GET
|
||||
// /control/blocked_services/list HTTP API.
|
||||
//
|
||||
// Deprecated: Use handleBlockedServicesGet.
|
||||
func (d *DNSFilter) handleBlockedServicesList(w http.ResponseWriter, r *http.Request) {
|
||||
d.confLock.RLock()
|
||||
list := d.Config.BlockedServices.IDs
|
||||
d.confLock.RUnlock()
|
||||
var list []string
|
||||
func() {
|
||||
d.confMu.Lock()
|
||||
defer d.confMu.Unlock()
|
||||
|
||||
_ = aghhttp.WriteJSONResponse(w, r, list)
|
||||
list = d.conf.BlockedServices.IDs
|
||||
}()
|
||||
|
||||
aghhttp.WriteJSONResponseOK(w, r, list)
|
||||
}
|
||||
|
||||
// handleBlockedServicesSet is the handler for the POST
|
||||
// /control/blocked_services/set HTTP API.
|
||||
//
|
||||
// Deprecated: Use handleBlockedServicesUpdate.
|
||||
func (d *DNSFilter) handleBlockedServicesSet(w http.ResponseWriter, r *http.Request) {
|
||||
list := []string{}
|
||||
err := json.NewDecoder(r.Body).Decode(&list)
|
||||
@@ -142,11 +154,61 @@ func (d *DNSFilter) handleBlockedServicesSet(w http.ResponseWriter, r *http.Requ
|
||||
return
|
||||
}
|
||||
|
||||
d.confLock.Lock()
|
||||
d.Config.BlockedServices.IDs = list
|
||||
d.confLock.Unlock()
|
||||
func() {
|
||||
d.confMu.Lock()
|
||||
defer d.confMu.Unlock()
|
||||
|
||||
log.Debug("Updated blocked services list: %d", len(list))
|
||||
d.conf.BlockedServices.IDs = list
|
||||
log.Debug("Updated blocked services list: %d", len(list))
|
||||
}()
|
||||
|
||||
d.Config.ConfigModified()
|
||||
d.conf.ConfigModified()
|
||||
}
|
||||
|
||||
// handleBlockedServicesGet is the handler for the GET
|
||||
// /control/blocked_services/get HTTP API.
|
||||
func (d *DNSFilter) handleBlockedServicesGet(w http.ResponseWriter, r *http.Request) {
|
||||
var bsvc *BlockedServices
|
||||
func() {
|
||||
d.confMu.RLock()
|
||||
defer d.confMu.RUnlock()
|
||||
|
||||
bsvc = d.conf.BlockedServices.Clone()
|
||||
}()
|
||||
|
||||
aghhttp.WriteJSONResponseOK(w, r, bsvc)
|
||||
}
|
||||
|
||||
// handleBlockedServicesUpdate is the handler for the PUT
|
||||
// /control/blocked_services/update HTTP API.
|
||||
func (d *DNSFilter) handleBlockedServicesUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
bsvc := &BlockedServices{}
|
||||
err := json.NewDecoder(r.Body).Decode(bsvc)
|
||||
if err != nil {
|
||||
aghhttp.Error(r, w, http.StatusBadRequest, "json.Decode: %s", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err = bsvc.Validate()
|
||||
if err != nil {
|
||||
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "validating: %s", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if bsvc.Schedule == nil {
|
||||
bsvc.Schedule = schedule.EmptyWeekly()
|
||||
}
|
||||
|
||||
func() {
|
||||
d.confMu.Lock()
|
||||
defer d.confMu.Unlock()
|
||||
|
||||
d.conf.BlockedServices = bsvc
|
||||
}()
|
||||
|
||||
log.Debug("updated blocked services schedule: %d", len(bsvc.IDs))
|
||||
|
||||
d.conf.ConfigModified()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user