2018-05-01 23:45:06 +00:00
|
|
|
package tunneldns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-01-10 20:55:44 +00:00
|
|
|
"context"
|
2018-05-01 23:45:06 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-04-04 20:49:36 +00:00
|
|
|
"net"
|
2018-05-01 23:45:06 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
"github.com/pkg/errors"
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/rs/zerolog"
|
2018-05-01 23:45:06 +00:00
|
|
|
"golang.org/x/net/http2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultTimeout = 5 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
// UpstreamHTTPS is the upstream implementation for DNS over HTTPS service
|
|
|
|
type UpstreamHTTPS struct {
|
2020-04-04 20:49:36 +00:00
|
|
|
client *http.Client
|
|
|
|
endpoint *url.URL
|
|
|
|
bootstraps []string
|
2020-11-25 06:55:13 +00:00
|
|
|
log *zerolog.Logger
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 20:49:36 +00:00
|
|
|
// NewUpstreamHTTPS creates a new DNS over HTTPS upstream from endpoint
|
2020-11-25 06:55:13 +00:00
|
|
|
func NewUpstreamHTTPS(endpoint string, bootstraps []string, log *zerolog.Logger) (Upstream, error) {
|
2018-05-01 23:45:06 +00:00
|
|
|
u, err := url.Parse(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-25 06:55:13 +00:00
|
|
|
return &UpstreamHTTPS{client: configureClient(u.Hostname()), endpoint: u, bootstraps: bootstraps, log: log}, nil
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exchange provides an implementation for the Upstream interface
|
|
|
|
func (u *UpstreamHTTPS) Exchange(ctx context.Context, query *dns.Msg) (*dns.Msg, error) {
|
|
|
|
queryBuf, err := query.Pack()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to pack DNS query")
|
|
|
|
}
|
|
|
|
|
2020-04-04 20:49:36 +00:00
|
|
|
if len(query.Question) > 0 && query.Question[0].Name == fmt.Sprintf("%s.", u.endpoint.Hostname()) {
|
|
|
|
for _, bootstrap := range u.bootstraps {
|
|
|
|
endpoint, client, err := configureBootstrap(bootstrap)
|
|
|
|
if err != nil {
|
2020-12-28 18:10:01 +00:00
|
|
|
u.log.Err(err).Msgf("failed to configure boostrap upstream %s", bootstrap)
|
2020-04-04 20:49:36 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-11-25 06:55:13 +00:00
|
|
|
msg, err := exchange(queryBuf, query.Id, endpoint, client, u.log)
|
2020-04-04 20:49:36 +00:00
|
|
|
if err != nil {
|
2020-12-28 18:10:01 +00:00
|
|
|
u.log.Err(err).Msgf("failed to connect to a boostrap upstream %s", bootstrap)
|
2020-04-04 20:49:36 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("failed to reach any bootstrap upstream: %v", u.bootstraps)
|
|
|
|
}
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
return exchange(queryBuf, query.Id, u.endpoint, u.client, u.log)
|
2020-04-04 20:49:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
func exchange(msg []byte, queryID uint16, endpoint *url.URL, client *http.Client, log *zerolog.Logger) (*dns.Msg, error) {
|
2018-05-01 23:45:06 +00:00
|
|
|
// No content negotiation for now, use DNS wire format
|
2020-04-04 20:49:36 +00:00
|
|
|
buf, backendErr := exchangeWireformat(msg, endpoint, client)
|
2018-05-01 23:45:06 +00:00
|
|
|
if backendErr == nil {
|
|
|
|
response := &dns.Msg{}
|
|
|
|
if err := response.Unpack(buf); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to unpack DNS response from body")
|
|
|
|
}
|
|
|
|
|
2020-04-04 20:49:36 +00:00
|
|
|
response.Id = queryID
|
2018-05-01 23:45:06 +00:00
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2020-12-28 18:10:01 +00:00
|
|
|
log.Err(backendErr).Msgf("failed to connect to an HTTPS backend %q", endpoint)
|
2018-05-01 23:45:06 +00:00
|
|
|
return nil, backendErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform message exchange with the default UDP wireformat defined in current draft
|
|
|
|
// https://datatracker.ietf.org/doc/draft-ietf-doh-dns-over-https
|
2020-04-04 20:49:36 +00:00
|
|
|
func exchangeWireformat(msg []byte, endpoint *url.URL, client *http.Client) ([]byte, error) {
|
|
|
|
req, err := http.NewRequest("POST", endpoint.String(), bytes.NewBuffer(msg))
|
2018-05-01 23:45:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create an HTTPS request")
|
|
|
|
}
|
|
|
|
|
2019-06-13 07:24:56 +00:00
|
|
|
req.Header.Add("Content-Type", "application/dns-message")
|
2020-04-04 20:49:36 +00:00
|
|
|
req.Host = endpoint.Host
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2020-04-04 20:49:36 +00:00
|
|
|
resp, err := client.Do(req)
|
2018-05-01 23:45:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to perform an HTTPS request")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check response status code
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("returned status code %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read wireformat response from the body
|
|
|
|
buf, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to read the response body")
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
2020-04-04 20:49:36 +00:00
|
|
|
|
|
|
|
func configureBootstrap(bootstrap string) (*url.URL, *http.Client, error) {
|
|
|
|
b, err := url.Parse(bootstrap)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if ip := net.ParseIP(b.Hostname()); ip == nil {
|
|
|
|
return nil, nil, fmt.Errorf("bootstrap address of %s must be an IP address", b.Hostname())
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, configureClient(b.Hostname()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// configureClient will configure a HTTPS client for upstream DoH requests
|
|
|
|
func configureClient(hostname string) *http.Client {
|
|
|
|
// Update TLS and HTTP client configuration
|
2020-11-25 06:55:13 +00:00
|
|
|
tlsConfig := &tls.Config{ServerName: hostname}
|
2020-04-04 20:49:36 +00:00
|
|
|
transport := &http.Transport{
|
2020-11-25 06:55:13 +00:00
|
|
|
TLSClientConfig: tlsConfig,
|
2020-04-04 20:49:36 +00:00
|
|
|
DisableCompression: true,
|
|
|
|
MaxIdleConns: 1,
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
}
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = http2.ConfigureTransport(transport)
|
2020-04-04 20:49:36 +00:00
|
|
|
|
|
|
|
return &http.Client{
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
}
|