2021-02-11 15:20:30 +03:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2021-05-24 17:28:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2021-08-09 16:03:37 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2021-02-11 15:20:30 +03:00
|
|
|
"github.com/lucas-clemente/quic-go"
|
|
|
|
)
|
|
|
|
|
2022-02-10 15:42:59 +03:00
|
|
|
// ValidateClientID returns an error if id is not a valid ClientID.
|
|
|
|
func ValidateClientID(id string) (err error) {
|
|
|
|
err = netutil.ValidateDomainNameLabel(id)
|
2021-03-25 16:00:27 +03:00
|
|
|
if err != nil {
|
2021-05-06 13:02:48 +03:00
|
|
|
// Replace the domain name label wrapper with our own.
|
2022-02-10 15:42:59 +03:00
|
|
|
return fmt.Errorf("invalid clientid %q: %w", id, errors.Unwrap(err))
|
2021-02-11 15:20:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-10 15:42:59 +03:00
|
|
|
// clientIDFromClientServerName extracts and validates a ClientID. hostSrvName
|
2021-02-11 15:20:30 +03:00
|
|
|
// 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.
|
2021-08-12 18:35:30 +03:00
|
|
|
func clientIDFromClientServerName(
|
|
|
|
hostSrvName string,
|
|
|
|
cliSrvName string,
|
|
|
|
strict bool,
|
|
|
|
) (clientID string, err error) {
|
2021-02-11 15:20:30 +03:00
|
|
|
if hostSrvName == cliSrvName {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2022-11-23 16:52:05 +03:00
|
|
|
if !netutil.IsImmediateSubdomain(cliSrvName, hostSrvName) {
|
2021-02-11 15:20:30 +03:00
|
|
|
if !strict {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2021-08-12 18:35:30 +03:00
|
|
|
return "", fmt.Errorf(
|
|
|
|
"client server name %q doesn't match host server name %q",
|
|
|
|
cliSrvName,
|
|
|
|
hostSrvName,
|
|
|
|
)
|
2021-02-11 15:20:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
clientID = cliSrvName[:len(cliSrvName)-len(hostSrvName)-1]
|
|
|
|
err = ValidateClientID(clientID)
|
|
|
|
if err != nil {
|
2021-03-25 16:00:27 +03:00
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
|
|
|
return "", err
|
2021-02-11 15:20:30 +03:00
|
|
|
}
|
|
|
|
|
2022-01-25 18:54:37 +03:00
|
|
|
return strings.ToLower(clientID), nil
|
2021-02-11 15:20:30 +03:00
|
|
|
}
|
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
// clientIDFromDNSContextHTTPS extracts the client's ID from the path of the
|
2021-02-11 15:20:30 +03:00
|
|
|
// client's DNS-over-HTTPS request.
|
2021-06-29 15:53:28 +03:00
|
|
|
func clientIDFromDNSContextHTTPS(pctx *proxy.DNSContext) (clientID string, err error) {
|
2021-02-11 15:20:30 +03:00
|
|
|
r := pctx.HTTPRequest
|
|
|
|
if r == nil {
|
2021-06-29 15:53:28 +03:00
|
|
|
return "", fmt.Errorf(
|
|
|
|
"proxy ctx http request of proto %s is nil",
|
|
|
|
pctx.Proto,
|
|
|
|
)
|
2021-02-11 15:20:30 +03: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" {
|
2022-02-10 15:42:59 +03:00
|
|
|
return "", fmt.Errorf("clientid check: invalid path %q", origPath)
|
2021-02-11 15:20:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch len(parts) {
|
|
|
|
case 1:
|
2022-02-10 15:42:59 +03:00
|
|
|
// Just /dns-query, no ClientID.
|
2021-06-29 15:53:28 +03:00
|
|
|
return "", nil
|
2021-02-11 15:20:30 +03:00
|
|
|
case 2:
|
|
|
|
clientID = parts[1]
|
|
|
|
default:
|
2022-02-10 15:42:59 +03:00
|
|
|
return "", fmt.Errorf("clientid check: invalid path %q: extra parts", origPath)
|
2021-02-11 15:20:30 +03:00
|
|
|
}
|
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
err = ValidateClientID(clientID)
|
2021-02-11 15:20:30 +03:00
|
|
|
if err != nil {
|
2022-02-10 15:42:59 +03:00
|
|
|
return "", fmt.Errorf("clientid check: %w", err)
|
2021-02-11 15:20:30 +03:00
|
|
|
}
|
|
|
|
|
2022-01-25 18:54:37 +03:00
|
|
|
return strings.ToLower(clientID), nil
|
2021-02-11 15:20:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// tlsConn is a narrow interface for *tls.Conn to simplify testing.
|
|
|
|
type tlsConn interface {
|
|
|
|
ConnectionState() (cs tls.ConnectionState)
|
|
|
|
}
|
|
|
|
|
2022-01-25 18:54:37 +03:00
|
|
|
// quicConnection is a narrow interface for quic.Connection to simplify testing.
|
|
|
|
type quicConnection interface {
|
2021-02-11 15:20:30 +03:00
|
|
|
ConnectionState() (cs quic.ConnectionState)
|
|
|
|
}
|
|
|
|
|
2021-06-29 15:53:28 +03: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 15:20:30 +03:00
|
|
|
proto := pctx.Proto
|
|
|
|
if proto == proxy.ProtoHTTPS {
|
2022-11-02 16:18:02 +03:00
|
|
|
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.
|
2021-02-11 15:20:30 +03:00
|
|
|
} else if proto != proxy.ProtoTLS && proto != proxy.ProtoQUIC {
|
2021-06-29 15:53:28 +03:00
|
|
|
return "", nil
|
2021-02-11 15:20:30 +03:00
|
|
|
}
|
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
hostSrvName := s.conf.ServerName
|
2021-02-11 15:20:30 +03:00
|
|
|
if hostSrvName == "" {
|
2021-06-29 15:53:28 +03:00
|
|
|
return "", nil
|
2021-02-11 15:20:30 +03:00
|
|
|
}
|
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
cliSrvName, err := clientServerName(pctx, proto)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2021-02-11 15:20:30 +03:00
|
|
|
}
|
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
clientID, err = clientIDFromClientServerName(
|
|
|
|
hostSrvName,
|
|
|
|
cliSrvName,
|
|
|
|
s.conf.StrictSNICheck,
|
|
|
|
)
|
2021-02-11 15:20:30 +03:00
|
|
|
if err != nil {
|
2022-02-10 15:42:59 +03:00
|
|
|
return "", fmt.Errorf("clientid check: %w", err)
|
2021-06-29 15:53:28 +03:00
|
|
|
}
|
2021-02-11 15:20:30 +03:00
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
return clientID, nil
|
|
|
|
}
|
2022-11-02 16:18:02 +03:00
|
|
|
|
|
|
|
// clientServerName returns the TLS server name based on the protocol.
|
|
|
|
func clientServerName(pctx *proxy.DNSContext, proto proxy.Proto) (srvName string, err error) {
|
|
|
|
switch proto {
|
|
|
|
case proxy.ProtoHTTPS:
|
|
|
|
// github.com/lucas-clemente/quic-go seems to not populate the TLS
|
|
|
|
// field. So, if the request comes over HTTP/3, use the Host header
|
|
|
|
// value as the server name.
|
|
|
|
//
|
|
|
|
// See https://github.com/lucas-clemente/quic-go/issues/2879.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Remove this crutch once they fix it.
|
|
|
|
r := pctx.HTTPRequest
|
|
|
|
if r.ProtoAtLeast(3, 0) {
|
|
|
|
var host string
|
|
|
|
host, err = netutil.SplitHost(r.Host)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("parsing host: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
srvName = host
|
|
|
|
} else if connState := r.TLS; connState != nil {
|
|
|
|
srvName = r.TLS.ServerName
|
|
|
|
}
|
|
|
|
case proxy.ProtoQUIC:
|
|
|
|
qConn := pctx.QUICConnection
|
|
|
|
conn, ok := qConn.(quicConnection)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("pctx conn of proto %s is %T, want quic.Connection", proto, qConn)
|
|
|
|
}
|
|
|
|
|
|
|
|
srvName = conn.ConnectionState().TLS.ServerName
|
|
|
|
case proxy.ProtoTLS:
|
|
|
|
conn := pctx.Conn
|
|
|
|
tc, ok := conn.(tlsConn)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("pctx conn of proto %s is %T, want *tls.Conn", proto, conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
srvName = tc.ConnectionState().ServerName
|
|
|
|
}
|
|
|
|
|
|
|
|
return srvName, nil
|
|
|
|
}
|