Files
AdGuardHome/internal/dnsforward/beforerequest.go
T
Fedor Setrakov d4d35186c7 Pull request 2520: AGDNS-3360-rm-log-dnsforward
Squashed commit of the following:

commit aca896ce0eb2b2a569f9dd2d41e5f23cd349a3ba
Merge: d7feb54ff ba2fcd79a
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Tue Nov 11 14:48:53 2025 +0300

    Merge branch 'master' into AGDNS-3360-rm-log-dnsforward

commit d7feb54ff71075124c956daf688fbf29d9be2b47
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Mon Nov 10 19:06:22 2025 +0300

    dnsforward: imp log

commit cb9403cb6e04934db9ce9ba04fc1b427d6a3c88b
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Fri Nov 7 11:03:41 2025 +0300

    all: imp tests, imp docs

commit f95e541cf008bf1eb0c2c0132f85dda86225f813
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Thu Nov 6 13:07:59 2025 +0300

    all: rm accidentally added files

commit 49175747e3400244a0a893b9eb03410145433561
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Thu Nov 6 12:53:49 2025 +0300

    all: imp logs

commit 8f8f6d843da82c20e7c7d51c46fc461852b364af
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Wed Nov 5 15:34:57 2025 +0300

    dnsforward: imp log

commit 08e7fe0e01a8f688b6b63c8baa1df0ec77679841
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Wed Nov 5 15:32:33 2025 +0300

    dnsforward: fix log format

commit 4401776ff3eb85427d27234c8e50d5616dc017e6
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Wed Nov 5 15:13:21 2025 +0300

    all: imp logs

commit bf842d1d4b3d45470be34ba376b0eee7331a1a6f
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Sat Nov 1 18:55:19 2025 +0300

    all: rm golibs/log
2025-11-11 15:01:10 +03:00

128 lines
3.3 KiB
Go

package dnsforward
import (
"context"
"encoding/binary"
"fmt"
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/dnsproxy/proxy"
"github.com/AdguardTeam/golibs/errors"
"github.com/miekg/dns"
)
// type check
var _ proxy.BeforeRequestHandler = (*Server)(nil)
// HandleBefore is the handler that is called before any other processing,
// including logs. It performs access checks and puts the ClientID, if there
// is one, into the server's cache.
//
// TODO(d.kolyshev): Extract to separate package.
func (s *Server) HandleBefore(
_ *proxy.Proxy,
pctx *proxy.DNSContext,
) (err error) {
// TODO(f.setrakov): Obtain context from arguments.
ctx := context.TODO()
clientID, err := s.clientIDFromDNSContext(ctx, pctx)
if err != nil {
return &proxy.BeforeRequestError{
Err: fmt.Errorf("getting clientid: %w", err),
Response: s.NewMsgSERVFAIL(pctx.Req),
}
}
blocked, _ := s.IsBlockedClient(pctx.Addr.Addr(), clientID)
if blocked {
return s.preBlockedResponse(pctx)
}
if len(pctx.Req.Question) == 1 {
q := pctx.Req.Question[0]
qt := q.Qtype
host := aghnet.NormalizeDomain(q.Name)
if s.access.isBlockedHost(host, qt) {
// TODO(s.chzhen): Pass context.
s.logger.DebugContext(
context.TODO(),
"request is in access blocklist",
"dns_type", dns.Type(qt),
"host", host,
)
return s.preBlockedResponse(pctx)
}
}
if clientID != "" {
key := [8]byte{}
binary.BigEndian.PutUint64(key[:], pctx.RequestID)
s.clientIDCache.Set(key[:], []byte(clientID))
}
return nil
}
// clientIDFromDNSContext extracts the client's ID from the server name of the
// client's DoT or DoQ request or the path of the client's DoH. If the protocol
// is not one of these, clientID is an empty string and err is nil.
func (s *Server) clientIDFromDNSContext(
ctx context.Context,
pctx *proxy.DNSContext,
) (clientID string, err error) {
proto := pctx.Proto
if proto == proxy.ProtoHTTPS {
clientID, err = clientIDFromDNSContextHTTPS(pctx)
if err != nil {
return "", fmt.Errorf("checking url: %w", err)
} else if clientID != "" {
return clientID, nil
}
// Go on and check the domain name as well.
} else if proto != proxy.ProtoTLS && proto != proxy.ProtoQUIC {
return "", nil
}
hostSrvName := s.conf.TLSConf.ServerName
if hostSrvName == "" {
return "", nil
}
cliSrvName, err := clientServerName(ctx, s.logger, pctx, proto)
if err != nil {
return "", fmt.Errorf("getting client server-name: %w", err)
}
clientID, err = clientIDFromClientServerName(
hostSrvName,
cliSrvName,
s.conf.TLSConf.StrictSNICheck,
)
if err != nil {
return "", fmt.Errorf("clientid check: %w", err)
}
return clientID, nil
}
// errAccessBlocked is a sentinel error returned when a request is blocked by
// access settings.
var errAccessBlocked errors.Error = "blocked by access settings"
// preBlockedResponse returns a protocol-appropriate response for a request that
// was blocked by access settings.
func (s *Server) preBlockedResponse(pctx *proxy.DNSContext) (err error) {
if pctx.Proto == proxy.ProtoUDP || pctx.Proto == proxy.ProtoDNSCrypt {
// Return nil so that dnsproxy drops the connection and thus
// prevent DNS amplification attacks.
return errAccessBlocked
}
return &proxy.BeforeRequestError{
Err: errAccessBlocked,
Response: s.makeResponseREFUSED(pctx.Req),
}
}