54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package dnsforward
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"fmt"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
"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 client ID, if there
|
|
// is one, into the server's cache.
|
|
//
|
|
// TODO(e.burkov): Write tests.
|
|
func (s *Server) HandleBefore(
|
|
_ *proxy.Proxy,
|
|
pctx *proxy.DNSContext,
|
|
) (err error) {
|
|
clientID, err := s.clientIDFromDNSContext(pctx)
|
|
if err != nil {
|
|
return fmt.Errorf("getting clientid: %w", err)
|
|
}
|
|
|
|
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) {
|
|
log.Debug("access: request %s %s is in access blocklist", dns.Type(qt), host)
|
|
|
|
return s.preBlockedResponse(pctx)
|
|
}
|
|
}
|
|
|
|
if clientID != "" {
|
|
key := [8]byte{}
|
|
binary.BigEndian.PutUint64(key[:], pctx.RequestID)
|
|
s.clientIDCache.Set(key[:], []byte(clientID))
|
|
}
|
|
|
|
return nil
|
|
}
|