doggo/pkg/resolvers/doq.go

131 lines
3.1 KiB
Go
Raw Normal View History

2021-12-11 16:45:24 +01:00
package resolvers
import (
"crypto/tls"
2022-06-13 17:54:34 +02:00
"encoding/binary"
2021-12-11 16:45:24 +01:00
"errors"
"fmt"
"io"
"os"
"time"
"github.com/miekg/dns"
2023-03-27 00:33:20 +02:00
"github.com/quic-go/quic-go"
2021-12-11 16:45:24 +01:00
"github.com/sirupsen/logrus"
)
// DOQResolver represents the config options for setting up a DOQ based resolver.
type DOQResolver struct {
tls *tls.Config
server string
resolverOptions Options
}
// NewDOQResolver accepts a nameserver address and configures a DOQ based resolver.
func NewDOQResolver(server string, resolverOpts Options) (Resolver, error) {
return &DOQResolver{
tls: &tls.Config{
2022-06-13 17:54:34 +02:00
NextProtos: []string{"doq"},
2021-12-11 16:45:24 +01:00
},
server: server,
resolverOptions: resolverOpts,
}, nil
}
// Lookup takes a dns.Question and sends them to DNS Server.
// It parses the Response from the server in a custom output format.
func (r *DOQResolver) Lookup(question dns.Question) (Response, error) {
var (
rsp Response
messages = prepareMessages(question, r.resolverOptions.Ndots, r.resolverOptions.SearchList)
)
session, err := quic.DialAddr(r.server, r.tls, nil)
if err != nil {
return rsp, err
}
defer session.CloseWithError(quic.ApplicationErrorCode(quic.NoError), "")
for _, msg := range messages {
r.resolverOptions.Logger.WithFields(logrus.Fields{
"domain": msg.Question[0].Name,
"ndots": r.resolverOptions.Ndots,
"nameserver": r.server,
}).Debug("Attempting to resolve")
2022-06-13 17:54:34 +02:00
// ref: https://www.rfc-editor.org/rfc/rfc9250.html#name-dns-message-ids
msg.Id = 0
2021-12-11 16:45:24 +01:00
// get the DNS Message in wire format.
var b []byte
b, err = msg.Pack()
if err != nil {
return rsp, err
}
now := time.Now()
var stream quic.Stream
stream, err = session.OpenStream()
if err != nil {
return rsp, err
}
2022-06-13 17:54:34 +02:00
var msgLen = uint16(len(b))
var msgLenBytes = []byte{byte(msgLen >> 8), byte(msgLen & 0xFF)}
_, err = stream.Write(msgLenBytes)
if err != nil {
return rsp, err
}
2022-06-13 18:03:36 +02:00
// Make a QUIC request to the DNS server with the DNS message as wire format bytes in the body.
2021-12-11 16:45:24 +01:00
_, err = stream.Write(b)
if err != nil {
2022-06-13 17:54:34 +02:00
return rsp, err
2021-12-11 16:45:24 +01:00
}
2022-06-13 17:54:34 +02:00
2021-12-11 16:45:24 +01:00
err = stream.SetDeadline(time.Now().Add(r.resolverOptions.Timeout))
if err != nil {
return rsp, err
}
var buf []byte
buf, err = io.ReadAll(stream)
if err != nil {
if errors.Is(err, os.ErrDeadlineExceeded) {
return rsp, fmt.Errorf("timeout")
}
return rsp, err
}
rtt := time.Since(now)
2022-06-13 18:03:36 +02:00
_ = stream.Close()
2022-06-13 17:54:34 +02:00
packetLen := binary.BigEndian.Uint16(buf[:2])
if packetLen != uint16(len(buf[2:])) {
return rsp, fmt.Errorf("packet length mismatch")
}
err = msg.Unpack(buf[2:])
2021-12-11 16:45:24 +01:00
if err != nil {
return rsp, err
}
// pack questions in output.
for _, q := range msg.Question {
ques := Question{
Name: q.Name,
Class: dns.ClassToString[q.Qclass],
Type: dns.TypeToString[q.Qtype],
}
rsp.Questions = append(rsp.Questions, ques)
}
// get the authorities and answers.
output := parseMessage(&msg, rtt, r.server)
rsp.Authorities = output.Authorities
rsp.Answers = output.Answers
if len(output.Answers) > 0 {
// stop iterating the searchlist.
break
}
}
return rsp, nil
}