-
Notifications
You must be signed in to change notification settings - Fork 2
/
resolver.go
199 lines (159 loc) · 3.6 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
198
199
package doh
import (
"context"
"math"
"net"
"strings"
"sync"
"time"
"github.com/miekg/dns"
madns "github.com/multiformats/go-multiaddr-dns"
)
type Resolver struct {
mx sync.Mutex
url string
// RR cache
ipCache map[string]ipAddrEntry
txtCache map[string]txtEntry
maxCacheTTL time.Duration
}
type ipAddrEntry struct {
ips []net.IPAddr
expire time.Time
}
type txtEntry struct {
txt []string
expire time.Time
}
type Option func(*Resolver) error
// Specifies the maximum time entries are valid in the cache
// A maxCacheTTL of zero is equivalent to `WithCacheDisabled`
func WithMaxCacheTTL(maxCacheTTL time.Duration) Option {
return func(tr *Resolver) error {
tr.maxCacheTTL = maxCacheTTL
return nil
}
}
func WithCacheDisabled() Option {
return func(tr *Resolver) error {
tr.maxCacheTTL = 0
return nil
}
}
func NewResolver(url string, opts ...Option) (*Resolver, error) {
if !strings.HasPrefix(url, "https:") {
url = "https://" + url
}
r := &Resolver{
url: url,
ipCache: make(map[string]ipAddrEntry),
txtCache: make(map[string]txtEntry),
maxCacheTTL: time.Duration(math.MaxUint32) * time.Second,
}
for _, o := range opts {
if err := o(r); err != nil {
return nil, err
}
}
return r, nil
}
var _ madns.BasicResolver = (*Resolver)(nil)
func (r *Resolver) LookupIPAddr(ctx context.Context, domain string) (result []net.IPAddr, err error) {
result, ok := r.getCachedIPAddr(domain)
if ok {
return result, nil
}
type response struct {
ips []net.IPAddr
ttl uint32
err error
}
resch := make(chan response, 2)
go func() {
ip4, ttl, err := doRequestA(ctx, r.url, domain)
resch <- response{ip4, ttl, err}
}()
go func() {
ip6, ttl, err := doRequestAAAA(ctx, r.url, domain)
resch <- response{ip6, ttl, err}
}()
var ttl uint32
for i := 0; i < 2; i++ {
r := <-resch
if r.err != nil {
return nil, r.err
}
result = append(result, r.ips...)
if ttl == 0 || r.ttl < ttl {
ttl = r.ttl
}
}
cacheTTL := minTTL(time.Duration(ttl)*time.Second, r.maxCacheTTL)
r.cacheIPAddr(domain, result, cacheTTL)
return result, nil
}
func (r *Resolver) LookupTXT(ctx context.Context, domain string) ([]string, error) {
result, ok := r.getCachedTXT(domain)
if ok {
return result, nil
}
result, ttl, err := doRequestTXT(ctx, r.url, domain)
if err != nil {
return nil, err
}
cacheTTL := minTTL(time.Duration(ttl)*time.Second, r.maxCacheTTL)
r.cacheTXT(domain, result, cacheTTL)
return result, nil
}
func (r *Resolver) getCachedIPAddr(domain string) ([]net.IPAddr, bool) {
r.mx.Lock()
defer r.mx.Unlock()
fqdn := dns.Fqdn(domain)
entry, ok := r.ipCache[fqdn]
if !ok {
return nil, false
}
if time.Now().After(entry.expire) {
delete(r.ipCache, fqdn)
return nil, false
}
return entry.ips, true
}
func (r *Resolver) cacheIPAddr(domain string, ips []net.IPAddr, ttl time.Duration) {
if ttl == 0 {
return
}
r.mx.Lock()
defer r.mx.Unlock()
fqdn := dns.Fqdn(domain)
r.ipCache[fqdn] = ipAddrEntry{ips, time.Now().Add(ttl)}
}
func (r *Resolver) getCachedTXT(domain string) ([]string, bool) {
r.mx.Lock()
defer r.mx.Unlock()
fqdn := dns.Fqdn(domain)
entry, ok := r.txtCache[fqdn]
if !ok {
return nil, false
}
if time.Now().After(entry.expire) {
delete(r.txtCache, fqdn)
return nil, false
}
return entry.txt, true
}
func (r *Resolver) cacheTXT(domain string, txt []string, ttl time.Duration) {
if ttl == 0 {
return
}
r.mx.Lock()
defer r.mx.Unlock()
fqdn := dns.Fqdn(domain)
r.txtCache[fqdn] = txtEntry{txt, time.Now().Add(ttl)}
}
func minTTL(a, b time.Duration) time.Duration {
if a < b {
return a
}
return b
}