2022-12-05 14:46:51 +03:00
|
|
|
package filtering
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2023-05-12 13:04:19 +03:00
|
|
|
"golang.org/x/exp/slices"
|
2022-12-05 14:46:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO(d.kolyshev): Use [rewrite.Item] instead.
|
|
|
|
type rewriteEntryJSON struct {
|
|
|
|
Domain string `json:"domain"`
|
|
|
|
Answer string `json:"answer"`
|
|
|
|
}
|
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
// handleRewriteList is the handler for the GET /control/rewrite/list HTTP API.
|
2022-12-05 14:46:51 +03:00
|
|
|
func (d *DNSFilter) handleRewriteList(w http.ResponseWriter, r *http.Request) {
|
|
|
|
arr := []*rewriteEntryJSON{}
|
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
func() {
|
|
|
|
d.confMu.RLock()
|
|
|
|
defer d.confMu.RUnlock()
|
|
|
|
|
|
|
|
for _, ent := range d.conf.Rewrites {
|
|
|
|
jsonEnt := rewriteEntryJSON{
|
|
|
|
Domain: ent.Domain,
|
|
|
|
Answer: ent.Answer,
|
|
|
|
}
|
|
|
|
arr = append(arr, &jsonEnt)
|
2022-12-05 14:46:51 +03:00
|
|
|
}
|
2023-09-04 17:18:43 +03:00
|
|
|
}()
|
2022-12-05 14:46:51 +03:00
|
|
|
|
2023-08-29 20:03:40 +03:00
|
|
|
aghhttp.WriteJSONResponseOK(w, r, arr)
|
2022-12-05 14:46:51 +03:00
|
|
|
}
|
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
// handleRewriteAdd is the handler for the POST /control/rewrite/add HTTP API.
|
2022-12-05 14:46:51 +03:00
|
|
|
func (d *DNSFilter) handleRewriteAdd(w http.ResponseWriter, r *http.Request) {
|
|
|
|
rwJSON := rewriteEntryJSON{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&rwJSON)
|
|
|
|
if err != nil {
|
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "json.Decode: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rw := &LegacyRewrite{
|
|
|
|
Domain: rwJSON.Domain,
|
|
|
|
Answer: rwJSON.Answer,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rw.normalize()
|
|
|
|
if err != nil {
|
|
|
|
// Shouldn't happen currently, since normalize only returns a non-nil
|
|
|
|
// error when a rewrite is nil, but be change-proof.
|
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "normalizing: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
func() {
|
|
|
|
d.confMu.Lock()
|
|
|
|
defer d.confMu.Unlock()
|
|
|
|
|
|
|
|
d.conf.Rewrites = append(d.conf.Rewrites, rw)
|
|
|
|
log.Debug(
|
|
|
|
"rewrite: added element: %s -> %s [%d]",
|
|
|
|
rw.Domain,
|
|
|
|
rw.Answer,
|
|
|
|
len(d.conf.Rewrites),
|
|
|
|
)
|
|
|
|
}()
|
2022-12-05 14:46:51 +03:00
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
d.conf.ConfigModified()
|
2022-12-05 14:46:51 +03:00
|
|
|
}
|
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
// handleRewriteDelete is the handler for the POST /control/rewrite/delete HTTP
|
|
|
|
// API.
|
2022-12-05 14:46:51 +03:00
|
|
|
func (d *DNSFilter) handleRewriteDelete(w http.ResponseWriter, r *http.Request) {
|
|
|
|
jsent := rewriteEntryJSON{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&jsent)
|
|
|
|
if err != nil {
|
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "json.Decode: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
entDel := &LegacyRewrite{
|
|
|
|
Domain: jsent.Domain,
|
|
|
|
Answer: jsent.Answer,
|
|
|
|
}
|
|
|
|
arr := []*LegacyRewrite{}
|
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
func() {
|
|
|
|
d.confMu.Lock()
|
|
|
|
defer d.confMu.Unlock()
|
2022-12-05 14:46:51 +03:00
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
for _, ent := range d.conf.Rewrites {
|
|
|
|
if ent.equal(entDel) {
|
|
|
|
log.Debug("rewrite: removed element: %s -> %s", ent.Domain, ent.Answer)
|
2022-12-05 14:46:51 +03:00
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
arr = append(arr, ent)
|
|
|
|
}
|
|
|
|
d.conf.Rewrites = arr
|
|
|
|
}()
|
2022-12-05 14:46:51 +03:00
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
d.conf.ConfigModified()
|
2022-12-05 14:46:51 +03:00
|
|
|
}
|
2023-05-12 13:04:19 +03:00
|
|
|
|
|
|
|
// rewriteUpdateJSON is a struct for JSON object with rewrite rule update info.
|
|
|
|
type rewriteUpdateJSON struct {
|
|
|
|
Target rewriteEntryJSON `json:"target"`
|
|
|
|
Update rewriteEntryJSON `json:"update"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleRewriteUpdate is the handler for the PUT /control/rewrite/update HTTP
|
|
|
|
// API.
|
|
|
|
func (d *DNSFilter) handleRewriteUpdate(w http.ResponseWriter, r *http.Request) {
|
|
|
|
updateJSON := rewriteUpdateJSON{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&updateJSON)
|
|
|
|
if err != nil {
|
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "json.Decode: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rwDel := &LegacyRewrite{
|
|
|
|
Domain: updateJSON.Target.Domain,
|
|
|
|
Answer: updateJSON.Target.Answer,
|
|
|
|
}
|
|
|
|
|
|
|
|
rwAdd := &LegacyRewrite{
|
|
|
|
Domain: updateJSON.Update.Domain,
|
|
|
|
Answer: updateJSON.Update.Answer,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rwAdd.normalize()
|
|
|
|
if err != nil {
|
|
|
|
// Shouldn't happen currently, since normalize only returns a non-nil
|
|
|
|
// error when a rewrite is nil, but be change-proof.
|
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "normalizing: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
index := -1
|
|
|
|
defer func() {
|
|
|
|
if index >= 0 {
|
2023-09-04 17:18:43 +03:00
|
|
|
d.conf.ConfigModified()
|
2023-05-12 13:04:19 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
d.confMu.Lock()
|
|
|
|
defer d.confMu.Unlock()
|
2023-05-12 13:04:19 +03:00
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
index = slices.IndexFunc(d.conf.Rewrites, rwDel.equal)
|
2023-05-12 13:04:19 +03:00
|
|
|
if index == -1 {
|
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "target rule not found")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
d.conf.Rewrites = slices.Replace(d.conf.Rewrites, index, index+1, rwAdd)
|
2023-05-12 13:04:19 +03:00
|
|
|
|
|
|
|
log.Debug("rewrite: removed element: %s -> %s", rwDel.Domain, rwDel.Answer)
|
|
|
|
log.Debug("rewrite: added element: %s -> %s", rwAdd.Domain, rwAdd.Answer)
|
|
|
|
}
|