awl/pkg/resolvers/QUIC.go
Sam Therapy e782c9cb23
All checks were successful
continuous-integration/drone/push Build is passing
feat: change import URL
Signed-off-by: Sam Therapy <sam@samtherapy.net>
2023-02-17 19:18:58 +01:00

101 lines
2.3 KiB
Go

// SPDX-License-Identifier: BSD-3-Clause
//go:build !gccgo
package resolvers
import (
"crypto/tls"
"fmt"
"io"
"time"
"dns.froth.zone/awl/pkg/util"
"github.com/miekg/dns"
"github.com/quic-go/quic-go"
)
// QUICResolver is for DNS-over-QUIC queries.
type QUICResolver struct {
opts *util.Options
}
var _ Resolver = (*QUICResolver)(nil)
// LookUp performs a DNS query.
func (resolver *QUICResolver) LookUp(msg *dns.Msg) (resp util.Response, err error) {
tls := &tls.Config{
//nolint:gosec // This is intentional if the user requests it
InsecureSkipVerify: resolver.opts.TLSNoVerify,
ServerName: resolver.opts.TLSHost,
MinVersion: tls.VersionTLS12,
NextProtos: []string{"doq"},
}
conf := new(quic.Config)
conf.HandshakeIdleTimeout = resolver.opts.Request.Timeout
resolver.opts.Logger.Debug("quic: making query")
connection, err := quic.DialAddr(resolver.opts.Request.Server, tls, conf)
if err != nil {
return resp, fmt.Errorf("doq: dial: %w", err)
}
resolver.opts.Logger.Debug("quic: packing query")
// Compress request to over-the-wire
buf, err := msg.Pack()
if err != nil {
return resp, fmt.Errorf("doq: pack: %w", err)
}
t := time.Now()
resolver.opts.Logger.Debug("quic: creating stream")
stream, err := connection.OpenStream()
if err != nil {
return resp, fmt.Errorf("doq: quic stream creation: %w", err)
}
resolver.opts.Logger.Debug("quic: writing to stream")
_, err = stream.Write(buf)
if err != nil {
return resp, fmt.Errorf("doq: quic stream write: %w", err)
}
resolver.opts.Logger.Debug("quic: reading stream")
fullRes, err := io.ReadAll(stream)
if err != nil {
return resp, fmt.Errorf("doq: quic stream read: %w", err)
}
resp.RTT = time.Since(t)
resolver.opts.Logger.Debug("quic: closing connection")
// Close with error: no error
err = connection.CloseWithError(0, "")
if err != nil {
return resp, fmt.Errorf("doq: quic connection close: %w", err)
}
resolver.opts.Logger.Debug("quic: closing stream")
err = stream.Close()
if err != nil {
return resp, fmt.Errorf("doq: quic stream close: %w", err)
}
resp.DNS = &dns.Msg{}
resolver.opts.Logger.Debug("quic: unpacking response")
err = resp.DNS.Unpack(fullRes)
if err != nil {
return resp, fmt.Errorf("doq: unpack: %w", err)
}
return
}