Skip to content

Commit

Permalink
refactor: extract Server, Timeout, and ReuseConn to common transport …
Browse files Browse the repository at this point in the history
…struct
  • Loading branch information
natesales committed Apr 5, 2024
1 parent 7f6c321 commit 499d6ca
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 57 deletions.
34 changes: 14 additions & 20 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,49 +119,48 @@ func createQuery(opts cli.Flags, rrTypes []uint16) []dns.Msg {
func newTransport(server string, transportType transport.Type, tlsConfig *tls.Config) (*transport.Transport, error) {
var ts transport.Transport

common := transport.Common{
Server: server,
ReuseConn: opts.ReuseConn,
Timeout: opts.Timeout,
}

switch transportType {
case transport.TypeHTTP:
if opts.ODoHProxy != "" {
log.Debugf("Using ODoH transport with target %s proxy %s", server, opts.ODoHProxy)
ts = &transport.ODoH{
Target: server,
Proxy: opts.ODoHProxy,
TLSConfig: tlsConfig,
ReuseConn: opts.ReuseConn,
}
} else {
log.Debugf("Using HTTP(s) transport: %s", server)
ts = &transport.HTTP{
Server: server,
Common: common,
TLSConfig: tlsConfig,
UserAgent: opts.HTTPUserAgent,
Method: opts.HTTPMethod,
Timeout: opts.Timeout,
HTTP2: opts.HTTP2,
HTTP3: opts.HTTP3,
NoPMTUd: !opts.PMTUD,
ReuseConn: opts.ReuseConn,
}
}
case transport.TypeDNSCrypt:
log.Debugf("Using DNSCrypt transport: %s", server)
if strings.HasPrefix(server, "sdns://") {
log.Traceln("Using provided DNS stamp for DNSCrypt")
ts = &transport.DNSCrypt{
Common: common,
ServerStamp: server,
TCP: opts.DNSCryptTCP,
Timeout: opts.Timeout,
UDPSize: opts.DNSCryptUDPSize,
ReuseConn: opts.ReuseConn,
}
} else {
log.Traceln("Using manual DNSCrypt configuration")
ts = &transport.DNSCrypt{
ts = &transport.DNSCrypt{Common: common,

TCP: opts.DNSCryptTCP,
Timeout: opts.Timeout,
UDPSize: opts.DNSCryptUDPSize,
ReuseConn: opts.ReuseConn,
Server: server,
PublicKey: opts.DNSCryptPublicKey,
ProviderName: opts.DNSCryptProvider,
}
Expand All @@ -173,34 +172,29 @@ func newTransport(server string, transportType transport.Type, tlsConfig *tls.Co
tlsConfig.NextProtos = opts.QUICALPNTokens

ts = &transport.QUIC{
Server: server,
Common: common,
TLSConfig: tc,
PMTUD: opts.PMTUD,
AddLengthPrefix: opts.QUICLengthPrefix,
ReuseConn: opts.ReuseConn,
}
case transport.TypeTLS:
log.Debugf("Using TLS transport: %s", server)
ts = &transport.TLS{
Server: server,
Common: common,
TLSConfig: tlsConfig,
Timeout: opts.Timeout,
ReuseConn: opts.ReuseConn,
}
case transport.TypeTCP:
log.Debugf("Using TCP transport: %s", server)
ts = &transport.Plain{
Server: server,
Common: common,
PreferTCP: true,
Timeout: opts.Timeout,
UDPBuffer: opts.UDPBuffer,
}
case transport.TypePlain:
log.Debugf("Using UDP with TCP fallback: %s", server)
ts = &transport.Plain{
Server: server,
Common: common,
PreferTCP: false,
Timeout: opts.Timeout,
UDPBuffer: opts.UDPBuffer,
}
default:
Expand Down
6 changes: 1 addition & 5 deletions transport/dnscrypt.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package transport

import (
"time"

"github.com/ameshkov/dnscrypt/v2"
"github.com/jedisct1/go-dnsstamps"
"github.com/miekg/dns"
log "github.com/sirupsen/logrus"
)

type DNSCrypt struct {
Common
ServerStamp string
TCP bool // default false (UDP)
Timeout time.Duration
UDPSize int
ReuseConn bool

// ServerStamp takes precedence if set
Server string
PublicKey string
ProviderName string

Expand Down
2 changes: 1 addition & 1 deletion transport/dnscrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import "time"

func dnscryptTransport() *DNSCrypt {
d := &DNSCrypt{
Common: Common{Timeout: 1 * time.Second},
ServerStamp: "sdns://AQMAAAAAAAAAETk0LjE0MC4xNC4xNDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20",
Timeout: 1 * time.Second,
}
return d
}
5 changes: 1 addition & 4 deletions transport/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"time"

"github.com/miekg/dns"
"github.com/quic-go/quic-go"
Expand All @@ -18,14 +17,12 @@ import (

// HTTP makes a DNS query over HTTP(s)
type HTTP struct {
Server string
Common
TLSConfig *tls.Config
UserAgent string
Method string
Timeout time.Duration
HTTP2, HTTP3 bool
NoPMTUd bool
ReuseConn bool

conn *http.Client
}
Expand Down
6 changes: 4 additions & 2 deletions transport/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (

func httpTransport() *HTTP {
return &HTTP{
Server: "https://cloudflare-dns.com/dns-query",
Common: Common{
Server: "https://cloudflare-dns.com/dns-query",
Timeout: 2 * time.Second,
},
TLSConfig: &tls.Config{},
UserAgent: "",
Method: http.MethodGet,
Timeout: 2 * time.Second,
HTTP3: false,
NoPMTUd: false,
}
Expand Down
7 changes: 3 additions & 4 deletions transport/odoh.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ func buildURL(s, defaultPath string) *url.URL {

// ODoH makes a DNS query over ODoH
type ODoH struct {
Target string
Common // Server is the target
Proxy string
TLSConfig *tls.Config
ReuseConn bool

conn *http.Client
}
Expand All @@ -73,7 +72,7 @@ func (o *ODoH) Exchange(m *dns.Msg) (*dns.Msg, error) {
// Query ODoH configs on target
req, err := http.NewRequest(
http.MethodGet,
buildURL(strings.TrimSuffix(o.Target, "/dns-query"), "/.well-known/odohconfigs").String(),
buildURL(strings.TrimSuffix(o.Server, "/dns-query"), "/.well-known/odohconfigs").String(),
nil,
)
if err != nil {
Expand Down Expand Up @@ -118,7 +117,7 @@ func (o *ODoH) Exchange(m *dns.Msg) (*dns.Msg, error) {
return nil, fmt.Errorf("encrypt query: %s", err)
}

t := buildURL(o.Target, "/dns-query")
t := buildURL(o.Server, "/dns-query")
p := buildURL(o.Proxy, "/proxy")
qry := p.Query()
if qry.Get("targethost") == "" {
Expand Down
4 changes: 2 additions & 2 deletions transport/odoh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func odohTransport() *ODoH {
return &ODoH{
Target: "odoh.cloudflare-dns.com",
Common: Common{Server: "odoh.cloudflare-dns.com"},
Proxy: "odoh.crypto.sx",
}
}
Expand All @@ -31,7 +31,7 @@ func TestODoHBuildURL(t *testing.T) {

func TestTransportODoHInvalidTarget(t *testing.T) {
tp := odohTransport()
tp.Target = "example.com"
tp.Server = "example.com"
_, err := tp.Exchange(validQuery())
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Invalid serialized ObliviousDoHConfig")
Expand Down
5 changes: 1 addition & 4 deletions transport/plain.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package transport

import (
"time"

"github.com/miekg/dns"
log "github.com/sirupsen/logrus"
)

// Plain makes a DNS query over TCP or UDP (with TCP fallback)
type Plain struct {
Server string
Common
PreferTCP bool
Timeout time.Duration
UDPBuffer uint16
}

Expand Down
6 changes: 4 additions & 2 deletions transport/plain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (

func plainTransport() *Plain {
return &Plain{
Server: "9.9.9.9:53",
Common: Common{
Server: "9.9.9.9:53",
Timeout: 5 * time.Second,
},
PreferTCP: false,
Timeout: 5 * time.Second,
UDPBuffer: 1232,
}
}
Expand Down
4 changes: 2 additions & 2 deletions transport/quic.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ const (

// QUIC makes a DNS query over QUIC
type QUIC struct {
Server string
Common
TLSConfig *tls.Config
PMTUD bool
AddLengthPrefix bool
ReuseConn bool

conn *quic.Connection
}
Expand All @@ -51,6 +50,7 @@ func (q *QUIC) setServerName() {

func (q *QUIC) Exchange(msg *dns.Msg) (*dns.Msg, error) {
if q.conn == nil || !q.ReuseConn {
log.Debugf("Connecting to %s", q.Server)
q.setServerName()
if len(q.TLSConfig.NextProtos) == 0 {
log.Debug("No ALPN tokens specified, using default: \"doq\"")
Expand Down
2 changes: 1 addition & 1 deletion transport/quic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "crypto/tls"

func quicTransport() *QUIC {
return &QUIC{
Server: "dns.adguard.com:8853",
Common: Common{Server: "dns.adguard.com:8853"},
PMTUD: true,
AddLengthPrefix: true,
TLSConfig: &tls.Config{NextProtos: []string{"doq"}},
Expand Down
8 changes: 2 additions & 6 deletions transport/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ import (
"crypto/tls"
"fmt"
"net"
"time"

"github.com/miekg/dns"
)

// TLS makes a DNS query over TLS
type TLS struct {
Server string
Common
TLSConfig *tls.Config
Timeout time.Duration
ReuseConn bool

conn *tls.Conn
conn *tls.Conn
}

func (t *TLS) Exchange(msg *dns.Msg) (*dns.Msg, error) {
Expand Down
8 changes: 5 additions & 3 deletions transport/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (

func tlsTransport() *TLS {
return &TLS{
Server: "dns.quad9.net:853",
Timeout: 1 * time.Second,
ReuseConn: false,
Common: Common{
Server: "dns.quad9.net:853",
Timeout: 1 * time.Second,
ReuseConn: false,
},
}
}
12 changes: 11 additions & 1 deletion transport/transport.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package transport

import "github.com/miekg/dns"
import (
"time"

"github.com/miekg/dns"
)

type Transport interface {
Exchange(*dns.Msg) (*dns.Msg, error)
Close() error
}

type Common struct {
Server string
ReuseConn bool
Timeout time.Duration
}

type Type string

const (
Expand Down

0 comments on commit 499d6ca

Please sign in to comment.