/
handler_forward_ip.go
155 lines (142 loc) · 3.92 KB
/
handler_forward_ip.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
package dns
import (
"math/rand"
"strconv"
"strings"
"beryju.io/gravity/pkg/roles/dns/types"
"beryju.io/gravity/pkg/roles/dns/utils"
"github.com/getsentry/sentry-go"
"github.com/miekg/dns"
"go.uber.org/zap"
)
const IPForwarderType = "forward_ip"
type IPForwarderHandler struct {
c *dns.Client
resolvers []string
z *Zone
log *zap.Logger
CacheTTL int
}
func NewIPForwarderHandler(z *Zone, config map[string]string) *IPForwarderHandler {
net, ok := config["net"]
if !ok {
net = ""
}
ipf := &IPForwarderHandler{
z: z,
c: &dns.Client{
Net: net,
Timeout: types.DefaultUpstreamTimeout,
},
resolvers: strings.Split(config["to"], ";"),
}
ipf.log = z.log.With(zap.String("handler", ipf.Identifier()))
rawTtl := config["cache_ttl"]
cacheTtl, err := strconv.Atoi(rawTtl)
if err != nil && rawTtl != "" {
ipf.log.Warn("failed to parse cache_ttl, defaulting to 0", zap.Error(err), zap.Any("config", config))
cacheTtl = 0
}
ipf.CacheTTL = cacheTtl
return ipf
}
func (ipf *IPForwarderHandler) Identifier() string {
return IPForwarderType
}
func (ipf *IPForwarderHandler) cacheToEtcd(r *utils.DNSRequest, query dns.Question, ans dns.RR, idx int) {
cs := sentry.TransactionFromContext(r.Context()).StartChild("gravity.dns.handler.forward_ip.cache")
cs.SetTag("gravity.dns.handler.forward_ip.cache.query", query.String())
cs.SetTag("gravity.dns.handler.forward_ip.cache.ans", ans.String())
defer cs.Finish()
if ans == nil {
return
}
cacheTtl := ans.Header().Ttl
// never cache if set to -1
if ipf.CacheTTL == -1 {
return
}
// If CacheTTL is set to -2 we don't expire the entry at all
// this can be used to forward a specific zone and import all the records
// on the fly
if ipf.CacheTTL == -2 {
cacheTtl = 0
} else {
// Try to set cache expiry based on TTL of answer
// if no TTL set, default to CacheTTL, and if that's
// not set, then don't cache at all
if cacheTtl < 1 {
cacheTtl = uint32(ipf.CacheTTL)
if cacheTtl < 1 {
return
}
}
}
name := strings.TrimSuffix(query.Name, utils.EnsureLeadingPeriod(ipf.z.Name))
record := ipf.z.newRecord(name, dns.TypeToString[ans.Header().Rrtype])
switch v := ans.(type) {
case *dns.A:
record.Data = v.A.String()
case *dns.AAAA:
record.Data = v.AAAA.String()
case *dns.TXT:
record.Data = strings.Join(v.Txt, TXTSeparator)
case *dns.PTR:
record.Data = v.Ptr
case *dns.CNAME:
record.Data = v.Target
case *dns.MX:
record.Data = v.Mx
record.MXPreference = v.Preference
case *dns.SRV:
record.Data = v.Target
record.SRVPort = v.Port
record.SRVPriority = v.Priority
record.SRVWeight = v.Weight
}
record.uid = strconv.Itoa(idx)
err := record.put(r.Context(), int64(cacheTtl))
if err != nil {
ipf.log.Warn("failed to cache answer", zap.Error(err))
}
}
func (ipf *IPForwarderHandler) pickResolver() string {
resolver := ipf.resolvers[rand.Intn(len(ipf.resolvers))]
if !strings.Contains(resolver, ":") {
resolver += ":53"
}
return resolver
}
func (ipf *IPForwarderHandler) Handle(w *utils.FakeDNSWriter, r *utils.DNSRequest) *dns.Msg {
if len(r.Question) < 1 {
ipf.log.Error("No question")
return nil
}
question := r.Question[0]
fs := sentry.TransactionFromContext(r.Context()).StartChild("gravity.dns.handler.forward_ip.lookup")
resolver := ipf.pickResolver()
fs.SetTag("resolver", resolver)
ipf.log.Debug("sending message to resolve", zap.String("resolver", resolver))
m, rtt, err := ipf.c.ExchangeContext(r.Context(), r.Msg, resolver)
ipf.log.Debug("dns rtt", zap.Duration("rtt", rtt))
fs.Finish()
if err != nil {
ipf.log.Warn("failed to forward", zap.Error(err))
m := new(dns.Msg)
m.SetRcode(r.Msg, dns.RcodeServerFailure)
return m
}
m.RecursionAvailable = true
for idx, ans := range m.Answer {
if ans == nil {
continue
}
go ipf.cacheToEtcd(r, question, ans, idx)
}
if len(m.Answer) < 1 {
m.SetRcode(r.Msg, dns.RcodeNameError)
} else {
m.SetRcode(r.Msg, dns.RcodeSuccess)
}
return m
}