2021-09-28 12:42:01 +02:00
|
|
|
package netxlite
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DNSOverUDP is a DNS over UDP RoundTripper.
|
|
|
|
type DNSOverUDP struct {
|
|
|
|
dialer Dialer
|
|
|
|
address string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSOverUDP creates a DNSOverUDP instance.
|
2021-09-09 20:49:12 +02:00
|
|
|
func NewDNSOverUDP(dialer Dialer, address string) *DNSOverUDP {
|
|
|
|
return &DNSOverUDP{dialer: dialer, address: address}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RoundTrip implements RoundTripper.RoundTrip.
|
2021-09-09 20:49:12 +02:00
|
|
|
func (t *DNSOverUDP) RoundTrip(ctx context.Context, query []byte) ([]byte, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
conn, err := t.dialer.DialContext(ctx, "udp", t.address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
// Use five seconds timeout like Bionic does. See
|
|
|
|
// https://labs.ripe.net/Members/baptiste_jonglez_1/persistent-dns-connections-for-reliability-and-performance
|
|
|
|
if err = conn.SetDeadline(time.Now().Add(5 * time.Second)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err = conn.Write(query); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
reply := make([]byte, 1<<17)
|
|
|
|
var n int
|
|
|
|
n, err = conn.Read(reply)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return reply[:n], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequiresPadding returns false for UDP according to RFC8467
|
2021-09-09 20:49:12 +02:00
|
|
|
func (t *DNSOverUDP) RequiresPadding() bool {
|
2021-02-02 12:05:47 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Network returns the transport network (e.g., doh, dot)
|
2021-09-09 20:49:12 +02:00
|
|
|
func (t *DNSOverUDP) Network() string {
|
2021-02-02 12:05:47 +01:00
|
|
|
return "udp"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address returns the upstream server address.
|
2021-09-09 20:49:12 +02:00
|
|
|
func (t *DNSOverUDP) 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 *DNSOverUDP) CloseIdleConnections() {
|
|
|
|
// nothing to do
|
|
|
|
}
|
|
|
|
|
2021-09-28 10:47:59 +02:00
|
|
|
var _ DNSTransport = &DNSOverUDP{}
|