awl/query/QUIC.go
Sam Therapy 1b5d5a3fed
Some checks failed
continuous-integration/drone/push Build is failing
Do "a few things"
Signed-off-by: Sam Therapy <sam@samtherapy.net>
2022-07-20 23:14:15 +02:00

73 lines
1.4 KiB
Go

// SPDX-License-Identifier: BSD-3-Clause
package query
import (
"crypto/tls"
"io"
"time"
"git.froth.zone/sam/awl/cli"
"git.froth.zone/sam/awl/internal/structs"
"github.com/lucas-clemente/quic-go"
"github.com/miekg/dns"
)
type QUICResolver struct {
server string
opts cli.Options
}
func (r *QUICResolver) LookUp(msg *dns.Msg) (structs.Response, error) {
var resp structs.Response
tls := &tls.Config{
MinVersion: tls.VersionTLS12,
NextProtos: []string{"doq"},
}
r.opts.Logger.Debug("making DoQ request")
connection, err := quic.DialAddr(r.server, tls, nil)
if err != nil {
return structs.Response{}, err
}
// Compress request to over-the-wire
buf, err := msg.Pack()
if err != nil {
return structs.Response{}, err
}
t := time.Now()
stream, err := connection.OpenStream()
if err != nil {
return structs.Response{}, err
}
_, err = stream.Write(buf)
if err != nil {
return structs.Response{}, err
}
fullRes, err := io.ReadAll(stream)
if err != nil {
return structs.Response{}, err
}
resp.RTT = time.Since(t)
// Close with error: no error
err = connection.CloseWithError(0, "")
if err != nil {
return structs.Response{}, err
}
err = stream.Close()
if err != nil {
return structs.Response{}, err
}
resp.DNS = &dns.Msg{}
r.opts.Logger.Debug("unpacking DoQ response")
err = resp.DNS.Unpack(fullRes)
if err != nil {
return structs.Response{}, err
}
return resp, nil
}