2021-02-02 12:05:47 +01:00
|
|
|
package resolver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"math"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DialContextFunc is a generic function for dialing a connection.
|
|
|
|
type DialContextFunc func(context.Context, string, string) (net.Conn, error)
|
|
|
|
|
|
|
|
// DNSOverTCP is a DNS over TCP/TLS RoundTripper. Use NewDNSOverTCP
|
|
|
|
// and NewDNSOverTLS to create specific instances that use plaintext
|
|
|
|
// queries or encrypted queries over TLS.
|
|
|
|
//
|
|
|
|
// As a known bug, this implementation always creates a new connection
|
|
|
|
// for each incoming query, thus increasing the response delay.
|
|
|
|
type DNSOverTCP struct {
|
|
|
|
dial DialContextFunc
|
|
|
|
address string
|
|
|
|
network string
|
|
|
|
requiresPadding bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSOverTCP creates a new DNSOverTCP transport.
|
2021-09-09 20:49:12 +02:00
|
|
|
func NewDNSOverTCP(dial DialContextFunc, address string) *DNSOverTCP {
|
|
|
|
return &DNSOverTCP{
|
2021-02-02 12:05:47 +01:00
|
|
|
dial: dial,
|
|
|
|
address: address,
|
|
|
|
network: "tcp",
|
|
|
|
requiresPadding: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSOverTLS creates a new DNSOverTLS transport.
|
2021-09-09 20:49:12 +02:00
|
|
|
func NewDNSOverTLS(dial DialContextFunc, address string) *DNSOverTCP {
|
|
|
|
return &DNSOverTCP{
|
2021-02-02 12:05:47 +01:00
|
|
|
dial: dial,
|
|
|
|
address: address,
|
|
|
|
network: "dot",
|
|
|
|
requiresPadding: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RoundTrip implements RoundTripper.RoundTrip.
|
2021-09-09 20:49:12 +02:00
|
|
|
func (t *DNSOverTCP) RoundTrip(ctx context.Context, query []byte) ([]byte, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
if len(query) > math.MaxUint16 {
|
|
|
|
return nil, errors.New("query too long")
|
|
|
|
}
|
|
|
|
conn, err := t.dial(ctx, "tcp", t.address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
if err = conn.SetDeadline(time.Now().Add(10 * time.Second)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Write request
|
|
|
|
buf := []byte{byte(len(query) >> 8)}
|
|
|
|
buf = append(buf, byte(len(query)))
|
|
|
|
buf = append(buf, query...)
|
|
|
|
if _, err = conn.Write(buf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Read response
|
|
|
|
header := make([]byte, 2)
|
|
|
|
if _, err = io.ReadFull(conn, header); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
length := int(header[0])<<8 | int(header[1])
|
|
|
|
reply := make([]byte, length)
|
|
|
|
if _, err = io.ReadFull(conn, reply); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return reply, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequiresPadding returns true for DoT and false for TCP
|
|
|
|
// according to RFC8467.
|
2021-09-09 20:49:12 +02:00
|
|
|
func (t *DNSOverTCP) RequiresPadding() bool {
|
2021-02-02 12:05:47 +01:00
|
|
|
return t.requiresPadding
|
|
|
|
}
|
|
|
|
|
|
|
|
// Network returns the transport network (e.g., doh, dot)
|
2021-09-09 20:49:12 +02:00
|
|
|
func (t *DNSOverTCP) Network() string {
|
2021-02-02 12:05:47 +01:00
|
|
|
return t.network
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address returns the upstream server address.
|
2021-09-09 20:49:12 +02:00
|
|
|
func (t *DNSOverTCP) Address() string {
|
2021-02-02 12:05:47 +01:00
|
|
|
return t.address
|
|
|
|
}
|
|
|
|
|
2021-09-09 20:49:12 +02:00
|
|
|
// CloseIdleConnections closes idle connections.
|
|
|
|
func (t *DNSOverTCP) CloseIdleConnections() {
|
|
|
|
// nothing to do
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ RoundTripper = &DNSOverTCP{}
|