2021-02-11 12:20:30 +00:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2021-06-29 12:53:28 +00:00
|
|
|
"encoding/binary"
|
2021-02-11 12:20:30 +00:00
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2021-04-07 13:36:38 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2021-02-11 12:20:30 +00:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2021-05-24 14:28:11 +00:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2021-02-11 12:20:30 +00:00
|
|
|
"github.com/lucas-clemente/quic-go"
|
|
|
|
)
|
|
|
|
|
2021-03-25 13:00:27 +00:00
|
|
|
// ValidateClientID returns an error if clientID is not a valid client ID.
|
|
|
|
func ValidateClientID(clientID string) (err error) {
|
2021-04-07 13:36:38 +00:00
|
|
|
err = aghnet.ValidateDomainNameLabel(clientID)
|
2021-03-25 13:00:27 +00:00
|
|
|
if err != nil {
|
2021-05-06 10:02:48 +00:00
|
|
|
// Replace the domain name label wrapper with our own.
|
|
|
|
return fmt.Errorf("invalid client id %q: %w", clientID, errors.Unwrap(err))
|
2021-02-11 12:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// clientIDFromClientServerName extracts and validates a client ID. hostSrvName
|
|
|
|
// is the server name of the host. cliSrvName is the server name as sent by the
|
|
|
|
// client. When strict is true, and client and host server name don't match,
|
|
|
|
// clientIDFromClientServerName will return an error.
|
|
|
|
func clientIDFromClientServerName(hostSrvName, cliSrvName string, strict bool) (clientID string, err error) {
|
|
|
|
if hostSrvName == cliSrvName {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(cliSrvName, hostSrvName) {
|
|
|
|
if !strict {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("client server name %q doesn't match host server name %q", cliSrvName, hostSrvName)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientID = cliSrvName[:len(cliSrvName)-len(hostSrvName)-1]
|
|
|
|
err = ValidateClientID(clientID)
|
|
|
|
if err != nil {
|
2021-03-25 13:00:27 +00:00
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
|
|
|
return "", err
|
2021-02-11 12:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return clientID, nil
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
// clientIDFromDNSContextHTTPS extracts the client's ID from the path of the
|
2021-02-11 12:20:30 +00:00
|
|
|
// client's DNS-over-HTTPS request.
|
2021-06-29 12:53:28 +00:00
|
|
|
func clientIDFromDNSContextHTTPS(pctx *proxy.DNSContext) (clientID string, err error) {
|
2021-02-11 12:20:30 +00:00
|
|
|
r := pctx.HTTPRequest
|
|
|
|
if r == nil {
|
2021-06-29 12:53:28 +00:00
|
|
|
return "", fmt.Errorf(
|
|
|
|
"proxy ctx http request of proto %s is nil",
|
|
|
|
pctx.Proto,
|
|
|
|
)
|
2021-02-11 12:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
origPath := r.URL.Path
|
|
|
|
parts := strings.Split(path.Clean(origPath), "/")
|
|
|
|
if parts[0] == "" {
|
|
|
|
parts = parts[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(parts) == 0 || parts[0] != "dns-query" {
|
2021-06-29 12:53:28 +00:00
|
|
|
return "", fmt.Errorf("client id check: invalid path %q", origPath)
|
2021-02-11 12:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch len(parts) {
|
|
|
|
case 1:
|
|
|
|
// Just /dns-query, no client ID.
|
2021-06-29 12:53:28 +00:00
|
|
|
return "", nil
|
2021-02-11 12:20:30 +00:00
|
|
|
case 2:
|
|
|
|
clientID = parts[1]
|
|
|
|
default:
|
2021-06-29 12:53:28 +00:00
|
|
|
return "", fmt.Errorf("client id check: invalid path %q: extra parts", origPath)
|
2021-02-11 12:20:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
err = ValidateClientID(clientID)
|
2021-02-11 12:20:30 +00:00
|
|
|
if err != nil {
|
2021-06-29 12:53:28 +00:00
|
|
|
return "", fmt.Errorf("client id check: %w", err)
|
2021-02-11 12:20:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
return clientID, nil
|
2021-02-11 12:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// tlsConn is a narrow interface for *tls.Conn to simplify testing.
|
|
|
|
type tlsConn interface {
|
|
|
|
ConnectionState() (cs tls.ConnectionState)
|
|
|
|
}
|
|
|
|
|
|
|
|
// quicSession is a narrow interface for quic.Session to simplify testing.
|
|
|
|
type quicSession interface {
|
|
|
|
ConnectionState() (cs quic.ConnectionState)
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
// 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) {
|
2021-02-11 12:20:30 +00:00
|
|
|
proto := pctx.Proto
|
|
|
|
if proto == proxy.ProtoHTTPS {
|
2021-06-29 12:53:28 +00:00
|
|
|
return clientIDFromDNSContextHTTPS(pctx)
|
2021-02-11 12:20:30 +00:00
|
|
|
} else if proto != proxy.ProtoTLS && proto != proxy.ProtoQUIC {
|
2021-06-29 12:53:28 +00:00
|
|
|
return "", nil
|
2021-02-11 12:20:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
hostSrvName := s.conf.ServerName
|
2021-02-11 12:20:30 +00:00
|
|
|
if hostSrvName == "" {
|
2021-06-29 12:53:28 +00:00
|
|
|
return "", nil
|
2021-02-11 12:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cliSrvName := ""
|
2021-06-29 12:53:28 +00:00
|
|
|
switch proto {
|
|
|
|
case proxy.ProtoTLS:
|
2021-02-11 12:20:30 +00:00
|
|
|
conn := pctx.Conn
|
|
|
|
tc, ok := conn.(tlsConn)
|
|
|
|
if !ok {
|
2021-06-29 12:53:28 +00:00
|
|
|
return "", fmt.Errorf(
|
|
|
|
"proxy ctx conn of proto %s is %T, want *tls.Conn",
|
|
|
|
proto,
|
|
|
|
conn,
|
|
|
|
)
|
2021-02-11 12:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cliSrvName = tc.ConnectionState().ServerName
|
2021-06-29 12:53:28 +00:00
|
|
|
case proxy.ProtoQUIC:
|
2021-02-11 12:20:30 +00:00
|
|
|
qs, ok := pctx.QUICSession.(quicSession)
|
|
|
|
if !ok {
|
2021-06-29 12:53:28 +00:00
|
|
|
return "", fmt.Errorf(
|
|
|
|
"proxy ctx quic session of proto %s is %T, want quic.Session",
|
|
|
|
proto,
|
|
|
|
pctx.QUICSession,
|
|
|
|
)
|
2021-02-11 12:20:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 09:49:41 +00:00
|
|
|
cliSrvName = qs.ConnectionState().TLS.ServerName
|
2021-02-11 12:20:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
clientID, err = clientIDFromClientServerName(
|
|
|
|
hostSrvName,
|
|
|
|
cliSrvName,
|
|
|
|
s.conf.StrictSNICheck,
|
|
|
|
)
|
2021-02-11 12:20:30 +00:00
|
|
|
if err != nil {
|
2021-06-29 12:53:28 +00:00
|
|
|
return "", fmt.Errorf("client id check: %w", err)
|
|
|
|
}
|
2021-02-11 12:20:30 +00:00
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
return clientID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// processClientID puts the clientID into the DNS context, if there is one.
|
|
|
|
func (s *Server) processClientID(dctx *dnsContext) (rc resultCode) {
|
|
|
|
pctx := dctx.proxyCtx
|
|
|
|
|
|
|
|
var key [8]byte
|
|
|
|
binary.BigEndian.PutUint64(key[:], pctx.RequestID)
|
|
|
|
clientIDData := s.clientIDCache.Get(key[:])
|
|
|
|
if clientIDData == nil {
|
|
|
|
return resultCodeSuccess
|
2021-02-11 12:20:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
dctx.clientID = string(clientIDData)
|
2021-02-11 12:20:30 +00:00
|
|
|
|
|
|
|
return resultCodeSuccess
|
|
|
|
}
|