-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull request 2193: AGDNS-1982 Upd proxy
Closes #6854.Updates #6875. Squashed commit of the following: commit b98adbc Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Apr 10 19:21:44 2024 +0300 dnsforward: upd proxy, imp code, docs commit 4de1eb2 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Apr 10 16:09:58 2024 +0300 WIP commit afa9d61 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 9 19:24:09 2024 +0300 all: log changes commit c834067 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 9 19:06:10 2024 +0300 dnsforward: move code commit 08bb7d4 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 9 18:09:46 2024 +0300 dnsforward: imp code commit b27547e Merge: b7efca7 6f36ebc Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 9 17:33:19 2024 +0300 Merge branch 'master' into AGDNS-1982-upd-proxy commit b7efca7 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 9 17:27:14 2024 +0300 all: upd proxy finally commit 3e16fa8 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Apr 5 18:20:13 2024 +0300 dnsforward: upd proxy commit f3cdfc8 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Apr 4 20:37:32 2024 +0300 all: upd proxy, golibs commit a79298d Merge: 9feeba5 fd25dca Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Apr 4 20:34:01 2024 +0300 Merge branch 'master' into AGDNS-1982-upd-proxy commit 9feeba5 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Apr 4 20:25:57 2024 +0300 all: imp code, docs commit 6c68d46 Merge: d8108e6 ee619b2 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Apr 4 18:46:11 2024 +0300 Merge branch 'master' into AGDNS-1982-upd-proxy commit d8108e6 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Apr 3 19:25:27 2024 +0300 all: imp code commit 2046156 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Apr 3 17:10:33 2024 +0300 all: remove private rdns logic
- Loading branch information
1 parent
6f36ebc
commit ff7c715
Showing
19 changed files
with
761 additions
and
1,361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package dnsforward | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
|
||
"github.com/AdguardTeam/AdGuardHome/internal/aghnet" | ||
"github.com/AdguardTeam/dnsproxy/proxy" | ||
"github.com/AdguardTeam/golibs/errors" | ||
"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 | ||
} | ||
|
||
// 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(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.ServerName | ||
if hostSrvName == "" { | ||
return "", nil | ||
} | ||
|
||
cliSrvName, err := clientServerName(pctx, proto) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
clientID, err = clientIDFromClientServerName( | ||
hostSrvName, | ||
cliSrvName, | ||
s.conf.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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.