2021-09-28 12:42:01 +02:00
|
|
|
package netxlite
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-05-15 19:25:27 +02:00
|
|
|
//
|
|
|
|
// DNS-over-HTTPS transport
|
|
|
|
//
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-05-25 17:03:58 +02:00
|
|
|
"io"
|
2021-02-02 12:05:47 +01:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2022-05-14 17:38:31 +02:00
|
|
|
// DNSOverHTTPSTransport is a DNS-over-HTTPS DNSTransport.
|
|
|
|
type DNSOverHTTPSTransport struct {
|
2021-09-29 20:21:25 +02:00
|
|
|
// Client is the MANDATORY http client to use.
|
2022-01-03 13:53:23 +01:00
|
|
|
Client model.HTTPClient
|
2021-09-29 20:21:25 +02:00
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
// Decoder is the MANDATORY DNSDecoder.
|
|
|
|
Decoder model.DNSDecoder
|
|
|
|
|
2021-09-29 20:21:25 +02:00
|
|
|
// URL is the MANDATORY URL of the DNS-over-HTTPS server.
|
|
|
|
URL string
|
|
|
|
|
|
|
|
// HostOverride is OPTIONAL and allows to override the
|
|
|
|
// Host header sent in every request.
|
2021-02-02 12:05:47 +01:00
|
|
|
HostOverride string
|
|
|
|
}
|
|
|
|
|
2022-06-01 11:10:08 +02:00
|
|
|
// NewUnwrappedDNSOverHTTPSTransport creates a new DNSOverHTTPSTransport
|
|
|
|
// instance that has not been wrapped yet.
|
2021-09-29 20:21:25 +02:00
|
|
|
//
|
|
|
|
// Arguments:
|
|
|
|
//
|
2022-05-25 17:03:58 +02:00
|
|
|
// - client is a model.HTTPClient type;
|
2021-09-29 20:21:25 +02:00
|
|
|
//
|
2022-05-25 17:03:58 +02:00
|
|
|
// - URL is the DoH resolver URL (e.g., https://dns.google/dns-query).
|
2022-06-01 11:10:08 +02:00
|
|
|
func NewUnwrappedDNSOverHTTPSTransport(client model.HTTPClient, URL string) *DNSOverHTTPSTransport {
|
|
|
|
return NewUnwrappedDNSOverHTTPSTransportWithHostOverride(client, URL, "")
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2022-06-09 00:30:18 +02:00
|
|
|
// NewDNSOverHTTPSTransport is like NewUnwrappedDNSOverHTTPSTransport but
|
|
|
|
// returns an already wrapped DNSTransport.
|
|
|
|
func NewDNSOverHTTPSTransport(client model.HTTPClient, URL string) model.DNSTransport {
|
|
|
|
return WrapDNSTransport(NewUnwrappedDNSOverHTTPSTransport(client, URL))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSOverHTTPSTransportWithHTTPTransport is like NewDNSOverHTTPSTransport
|
|
|
|
// but takes in input an HTTPTransport rather than an HTTPClient.
|
|
|
|
func NewDNSOverHTTPSTransportWithHTTPTransport(txp model.HTTPTransport, URL string) model.DNSTransport {
|
|
|
|
return WrapDNSTransport(NewUnwrappedDNSOverHTTPSTransport(NewHTTPClient(txp), URL))
|
|
|
|
}
|
|
|
|
|
2022-06-01 11:10:08 +02:00
|
|
|
// NewUnwrappedDNSOverHTTPSTransportWithHostOverride creates a new DNSOverHTTPSTransport
|
|
|
|
// with the given Host header override. This instance has not been wrapped yet.
|
|
|
|
func NewUnwrappedDNSOverHTTPSTransportWithHostOverride(
|
2022-05-14 17:38:31 +02:00
|
|
|
client model.HTTPClient, URL, hostOverride string) *DNSOverHTTPSTransport {
|
2022-05-25 17:03:58 +02:00
|
|
|
return &DNSOverHTTPSTransport{
|
|
|
|
Client: client,
|
|
|
|
Decoder: &DNSDecoderMiekg{},
|
|
|
|
URL: URL,
|
|
|
|
HostOverride: hostOverride,
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2021-09-29 20:21:25 +02:00
|
|
|
// RoundTrip sends a query and receives a reply.
|
2022-05-25 17:03:58 +02:00
|
|
|
func (t *DNSOverHTTPSTransport) RoundTrip(
|
|
|
|
ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
|
|
|
rawQuery, err := query.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, 45*time.Second)
|
|
|
|
defer cancel()
|
2022-05-25 17:03:58 +02:00
|
|
|
req, err := http.NewRequest("POST", t.URL, bytes.NewReader(rawQuery))
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Host = t.HostOverride
|
2022-05-25 09:54:50 +02:00
|
|
|
req.Header.Set("user-agent", model.HTTPHeaderUserAgent)
|
2021-02-02 12:05:47 +01:00
|
|
|
req.Header.Set("content-type", "application/dns-message")
|
2022-05-25 17:03:58 +02:00
|
|
|
resp, err := t.Client.Do(req.WithContext(ctx))
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
// TODO(bassosimone): we should map the status code to a
|
|
|
|
// proper Error in the DNS context.
|
|
|
|
return nil, errors.New("doh: server returned error")
|
|
|
|
}
|
|
|
|
if resp.Header.Get("content-type") != "application/dns-message" {
|
|
|
|
return nil, errors.New("doh: invalid content-type")
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
const maxresponsesize = 1 << 20
|
|
|
|
limitReader := io.LimitReader(resp.Body, maxresponsesize)
|
|
|
|
rawResponse, err := ReadAllContext(ctx, limitReader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return t.Decoder.DecodeResponse(rawResponse, query)
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2021-09-29 20:21:25 +02:00
|
|
|
// RequiresPadding returns true for DoH according to RFC8467.
|
2022-05-14 17:38:31 +02:00
|
|
|
func (t *DNSOverHTTPSTransport) RequiresPadding() bool {
|
2021-02-02 12:05:47 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:21:25 +02:00
|
|
|
// Network returns the transport network, i.e., "doh".
|
2022-05-14 17:38:31 +02:00
|
|
|
func (t *DNSOverHTTPSTransport) Network() string {
|
2021-02-02 12:05:47 +01:00
|
|
|
return "doh"
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:21:25 +02:00
|
|
|
// Address returns the URL we're using for the DoH server.
|
2022-05-14 17:38:31 +02:00
|
|
|
func (t *DNSOverHTTPSTransport) Address() string {
|
2021-02-02 12:05:47 +01:00
|
|
|
return t.URL
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:21:25 +02:00
|
|
|
// CloseIdleConnections closes idle connections, if any.
|
2022-05-14 17:38:31 +02:00
|
|
|
func (t *DNSOverHTTPSTransport) CloseIdleConnections() {
|
2021-09-09 20:49:12 +02:00
|
|
|
t.Client.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
2022-05-14 17:38:31 +02:00
|
|
|
var _ model.DNSTransport = &DNSOverHTTPSTransport{}
|