-
Notifications
You must be signed in to change notification settings - Fork 69
/
resolver.go
197 lines (168 loc) · 4.9 KB
/
resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"bytes"
"fmt"
"github.com/cottand/leng/internal/metric"
"github.com/prometheus/client_golang/prometheus"
"io"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/miekg/dns"
)
// ResolvError type
type ResolvError struct {
qname, net string
nameservers []string
}
type upstreamAnswer struct {
answer *dns.Msg
nameserver string
}
// Error formats a ResolvError
func (e ResolvError) Error() string {
errmsg := fmt.Sprintf("%s resolv failed on %s (%s)", e.qname, strings.Join(e.nameservers, "; "), e.net)
return errmsg
}
// Resolver type
type Resolver struct {
config *dns.ClientConfig
}
// Lookup will ask each nameserver in top-to-bottom fashion, starting a new request
// in every second, and return as early as possible (have an answer).
// It returns an error if no request has succeeded.
func (r *Resolver) Lookup(net string, req *dns.Msg, timeout int, interval int, nameServers []string, DoH string) (message *dns.Msg, err error) {
logger.Debugf("Lookup %s, timeout: %d, interval: %d, nameservers: %v, Using DoH: %v", net, timeout, interval, nameServers, DoH != "")
question := req.Question[0]
metricUpstreamResolveCounter, _ := metric.RequestUpstreamResolveCounter.CurryWith(prometheus.Labels{
"q_type": dns.Type(question.Qtype).String(),
"q_name": question.Name,
})
//Is DoH enabled
if DoH != "" {
//First try and use DOH. Privacy First
ans, err := r.DoHLookup(DoH, timeout, req)
if err == nil {
// No error so result is ok
metricUpstreamResolveCounter.With(
prometheus.Labels{
"rcode": dns.RcodeToString[ans.Rcode],
"upstream": DoH,
}).Inc()
return ans, nil
}
//For some reason the DoH lookup failed so fall back to nameservers
logger.Debugf("DoH Failed due to '%s' falling back to nameservers", err)
}
clientNet := net
if net == "http" {
clientNet = "tcp"
}
c := &dns.Client{
Net: clientNet,
ReadTimeout: time.Duration(timeout) * time.Second,
WriteTimeout: time.Duration(timeout) * time.Second,
}
qname := req.Question[0].Name
res := make(chan upstreamAnswer, 1)
var wg sync.WaitGroup
L := func(nameserver string) {
defer wg.Done()
r, _, err := c.Exchange(req, nameserver)
if err != nil {
logger.Errorf("%s socket error on %s", qname, nameserver)
logger.Errorf("error:%s", err.Error())
return
}
if r != nil && r.Rcode != dns.RcodeSuccess {
logger.Warningf("%s failed to get an valid answer on %s", qname, nameserver)
if r.Rcode == dns.RcodeServerFailure {
return
}
} else {
logger.Debugf("%s resolv on %s (%s)\n", UnFqdn(qname), nameserver, net)
}
select {
case res <- upstreamAnswer{answer: r, nameserver: nameserver}:
default:
}
}
ticker := time.NewTicker(time.Duration(interval) * time.Millisecond)
defer ticker.Stop()
// Start lookup on each nameserver top-down, in every second
for _, nameServer := range nameServers {
wg.Add(1)
go L(nameServer)
// but exit early, if we have an answer
select {
case r := <-res:
return r.answer, nil
case <-ticker.C:
continue
}
}
// wait for all the namservers to finish
wg.Wait()
select {
case r := <-res:
metricUpstreamResolveCounter.With(prometheus.Labels{
"rcode": dns.RcodeToString[r.answer.Rcode],
"upstream": r.nameserver,
}).Inc()
return r.answer, nil
default:
return nil, ResolvError{qname, net, nameServers}
}
}
// DoHLookup performs a DNS lookup over https
func (r *Resolver) DoHLookup(url string, timeout int, req *dns.Msg) (msg *dns.Msg, err error) {
qname := req.Question[0].Name
defer func() {
metric.RequestUpstreamDohRequest.With(prometheus.Labels{
"success": strconv.FormatBool(err != nil),
})
}()
//Turn message into wire format
data, err := req.Pack()
if err != nil {
logger.Errorf("Failed to pack DNS message to wire format; %s", err)
return nil, ResolvError{qname, "HTTPS", []string{url}}
}
//Make the request to the server
client := http.Client{
Timeout: time.Duration(timeout) * time.Second,
}
reader := bytes.NewReader(data)
resp, err := client.Post(url, "application/dns-message", reader)
if err != nil {
logger.Errorf("Request to DoH server failed; %s", err)
return nil, ResolvError{qname, "HTTPS", []string{url}}
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(resp.Body)
//Check the request went ok
if resp.StatusCode != http.StatusOK {
return nil, ResolvError{qname, "HTTPS", []string{url}}
}
if resp.Header.Get("Content-Type") != "application/dns-message" {
return nil, ResolvError{qname, "HTTPS", []string{url}}
}
//Unpack the message from the HTTPS response
respPacket, err := io.ReadAll(resp.Body)
if err != nil {
return nil, ResolvError{qname, "HTTPS", []string{url}}
}
res := dns.Msg{}
err = res.Unpack(respPacket)
if err != nil {
logger.Errorf("Failed to unpack message from response; %s", err)
return nil, ResolvError{qname, "HTTPS", []string{url}}
}
//Finally return
return &res, nil
}