Pull request 2353: AGDNS-2688-check-host
Merge in DNS/adguard-home from AGDNS-2688-check-host to master Squashed commit of the following: commit bd9ed498b0e36fa044e6921fa946062ac40fe616 Merge: 8dffd94a3c41af2763Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Mar 14 13:42:34 2025 +0300 Merge branch 'master' into AGDNS-2688-check-host commit 8dffd94a3bc700cf014cbb16aee9c6339bdc7ffa Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 12 17:12:56 2025 +0300 filtering: imp code commit d9a01c8fa60c70e3fd19c40c1a58aec00ae64a6a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Mar 11 20:33:18 2025 +0300 all: imp code commit f1aca5f2eb71a1d8bb155a309c618e7a80f8fde5 Author: Ildar Kamalov <ik@adguard.com> Date: Tue Mar 11 16:05:32 2025 +0300 ADG-9783 update check form commit a8ebb0401dbaa08fdd04171b1ac66b87d0228c7b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Mar 10 16:41:55 2025 +0300 dnsforward: imp docs commit 36f5db9075cc525c13905e0318dfbc4089355523 Merge: 9a746ee9a66fba942cAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Mar 10 16:09:22 2025 +0300 Merge branch 'master' into AGDNS-2688-check-host commit 9a746ee9a05895676a60980eb4bd1381fe8d8e4b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Mar 10 16:06:48 2025 +0300 all: imp docs commit 0a25e1e8f3536053e30049497bb42a58c6a153d6 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Mar 6 21:48:44 2025 +0300 all: imp code commit ec618bc484190dde52a0dc57d144bade8dfc22e2 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Mar 6 17:38:35 2025 +0300 all: imp code commit 979c5cfd4c34e2aac46ea11b7fcba8d2929966b8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 5 21:22:54 2025 +0300 all: add tests commit ce0d6117ad7f341edcc018a68acedaa0b718bef1 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Mar 4 15:13:06 2025 +0300 all: check host
This commit is contained in:
committed by
Eugene Burkov
parent
c41af2763f
commit
1a3853d52a
@@ -9,6 +9,8 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -420,15 +422,53 @@ type checkHostResp struct {
|
||||
FilterID rulelist.URLFilterID `json:"filter_id"`
|
||||
}
|
||||
|
||||
// handleCheckHost is the handler for the GET /control/filtering/check_host HTTP
|
||||
// API.
|
||||
func (d *DNSFilter) handleCheckHost(w http.ResponseWriter, r *http.Request) {
|
||||
host := r.URL.Query().Get("name")
|
||||
query := r.URL.Query()
|
||||
host := query.Get("name")
|
||||
if host == "" {
|
||||
aghhttp.Error(
|
||||
r,
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
`query parameter "name" is required`,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
cli := query.Get("client")
|
||||
qTypeStr := query.Get("qtype")
|
||||
qType, err := stringToDNSType(qTypeStr)
|
||||
if err != nil {
|
||||
aghhttp.Error(
|
||||
r,
|
||||
w,
|
||||
http.StatusUnprocessableEntity,
|
||||
"bad qtype query parameter: %q",
|
||||
qTypeStr,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
setts := d.Settings()
|
||||
setts.FilteringEnabled = true
|
||||
setts.ProtectionEnabled = true
|
||||
|
||||
d.ApplyBlockedServices(setts)
|
||||
result, err := d.CheckHost(host, dns.TypeA, setts)
|
||||
addr, err := netip.ParseAddr(cli)
|
||||
if err == nil {
|
||||
setts.ClientIP = addr
|
||||
d.ApplyAdditionalFiltering(addr, "", setts)
|
||||
} else if cli != "" {
|
||||
// TODO(s.chzhen): Set [Settings.ClientName] once urlfilter supports
|
||||
// multiple client names. This will handle the case when a rule exists
|
||||
// but the persistent client does not.
|
||||
d.ApplyAdditionalFiltering(netip.Addr{}, cli, setts)
|
||||
}
|
||||
|
||||
result, err := d.CheckHost(host, qType, setts)
|
||||
if err != nil {
|
||||
aghhttp.Error(
|
||||
r,
|
||||
@@ -466,6 +506,33 @@ func (d *DNSFilter) handleCheckHost(w http.ResponseWriter, r *http.Request) {
|
||||
aghhttp.WriteJSONResponseOK(w, r, resp)
|
||||
}
|
||||
|
||||
// stringToDNSType is a helper function that converts a string to DNS type. If
|
||||
// the string is empty, it returns the default value [dns.TypeA].
|
||||
func stringToDNSType(str string) (qtype uint16, err error) {
|
||||
if str == "" {
|
||||
return dns.TypeA, nil
|
||||
}
|
||||
|
||||
qtype, ok := dns.StringToType[str]
|
||||
if ok {
|
||||
return qtype, nil
|
||||
}
|
||||
|
||||
// typePref is a prefix for DNS types from experimental RFCs.
|
||||
const typePref = "TYPE"
|
||||
|
||||
if !strings.HasPrefix(str, typePref) {
|
||||
return 0, errors.ErrBadEnumValue
|
||||
}
|
||||
|
||||
val, err := strconv.ParseUint(str[len(typePref):], 10, 16)
|
||||
if err != nil {
|
||||
return 0, errors.ErrBadEnumValue
|
||||
}
|
||||
|
||||
return uint16(val), nil
|
||||
}
|
||||
|
||||
// setProtectedBool sets the value of a boolean pointer under a lock. l must
|
||||
// protect the value under ptr.
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user