awl/query/QUIC.go
grumbulon ed4d74bb96
All checks were successful
continuous-integration/drone/push Build is passing
refactor of query.go (#27)
Includes a fix for #26

Co-authored-by: Sam Therapy <sam@samtherapy.net>
Reviewed-on: #27
Co-authored-by: grumbulon <grumbulon@grumbulon.xyz>
Co-committed-by: grumbulon <grumbulon@grumbulon.xyz>
2022-07-03 20:45:12 +00:00

69 lines
1.2 KiB
Go

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