-
Notifications
You must be signed in to change notification settings - Fork 1
/
response.go
156 lines (144 loc) · 4.15 KB
/
response.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
package rewriteng
import (
"net"
"strconv"
"strings"
"github.com/coredns/coredns/plugin"
"github.com/miekg/dns"
)
// ResponseRewriter rewrites answers, additional and authority sections
type ResponseRewriter struct {
dns.ResponseWriter
originalQuestion dns.Question
Rules []*nameRule
recursionDesired bool
}
// NewResponseRewriter returns a pointer to a new ResponseRewriter.
func NewResponseRewriter(w dns.ResponseWriter, r *dns.Msg) *ResponseRewriter {
return &ResponseRewriter{
ResponseWriter: w,
originalQuestion: r.Question[0],
recursionDesired: r.RecursionDesired,
}
}
// WriteMsg records the status code and calls the underlying ResponseWriter's WriteMsg method.
func (r *ResponseRewriter) WriteMsg(res *dns.Msg) error {
res.Question[0] = r.originalQuestion
res.RecursionDesired = r.recursionDesired
// Answers
r.rewriteAnswers(res)
// Authority
r.rewriteAuthority(res)
// Additional
r.rewriteAdditional(res)
return r.ResponseWriter.WriteMsg(res)
}
func (r *ResponseRewriter) rewriteAnswers(res *dns.Msg) {
log.Debugf("In ResponseWriter %v", res)
for _, rr := range res.Answer {
var nameWriten bool
var name = rr.Header().Name
for _, rule := range r.Rules {
if !nameWriten {
if s := rule.Sub(name, answerRule, namePart); s != "" {
rr.Header().Name = s
nameWriten = true
}
}
// Rewrite Data
log.Debugf("calling rewriteAnswers -> rewriteDataParts for rr %v rule %v answerRule %s ", rr, rule, answerRule)
rewriteDataParts(rr, rule, answerRule)
}
}
}
func (r *ResponseRewriter) rewriteAuthority(res *dns.Msg) {
for _, rr := range res.Ns {
var nameWriten bool
var name = rr.Header().Name
for _, rule := range r.Rules {
if !nameWriten {
if s := rule.Sub(name, authorityRule, namePart); s != "" {
rr.Header().Name = s
nameWriten = true
}
}
// Rewrite Data
log.Debugf("calling rewriteAuthority -> rewriteDataParts for rr %v rule %v answerRule %s ", rr, rule, authorityRule)
rewriteDataParts(rr, rule, authorityRule)
}
}
}
func (r *ResponseRewriter) rewriteAdditional(res *dns.Msg) {
for _, rr := range res.Extra {
var nameWriten bool
var name = rr.Header().Name
for _, rule := range r.Rules {
if !nameWriten {
if s := rule.Sub(name, additionalRule, namePart); s != "" {
rr.Header().Name = s
nameWriten = true
}
}
// Rewrite Data
log.Debugf("calling rewriteAdditional-> rewriteDataParts for rr %v rule %v answerRule %s ", rr, rule, additionalRule)
rewriteDataParts(rr, rule, additionalRule)
}
}
}
func rewriteDataParts(rr dns.RR, rule *nameRule, ruleType string) {
switch t := rr.(type) {
case *dns.CNAME:
if s := rule.Sub(t.Target, ruleType, dataPart); s != "" {
t.Target = plugin.Name(s).Normalize()
}
case *dns.TXT:
tmpRecs := t.Txt[:0]
for _, txtr := range t.Txt {
if s := rule.Sub(txtr, ruleType, dataPart); s != "" {
tmpRecs = append(tmpRecs, s)
} else {
tmpRecs = append(tmpRecs, txtr)
}
}
t.Txt = tmpRecs
case *dns.A:
if s := rule.Sub(t.A.String(), ruleType, dataPart); s != "" {
if ip := net.ParseIP(s); ip != nil && strings.Contains(s, ".") {
t.A = ip
}
}
case *dns.AAAA:
if s := rule.Sub(t.AAAA.String(), ruleType, dataPart); s != "" {
if ip := net.ParseIP(s); ip != nil && strings.Contains(s, ":") {
t.AAAA = ip
}
}
case *dns.NS:
if s := rule.Sub(t.Ns, ruleType, dataPart); s != "" {
t.Ns = plugin.Name(s).Normalize()
}
case *dns.SOA:
if s := rule.Sub(t.Ns, ruleType, dataPart); s != "" {
t.Ns = plugin.Name(s).Normalize()
}
if s := rule.Sub(t.Mbox, ruleType, dataPart); s != "" {
t.Mbox = plugin.Name(s).Normalize()
}
if s := rule.Sub(strconv.FormatInt(int64(t.Minttl), 10), ruleType, dataPart); s != "" {
if i, err := strconv.ParseUint(s, 10, 32); err == nil {
t.Minttl = uint32(i)
}
}
case *dns.SRV:
if s := rule.Sub(t.Target, ruleType, dataPart); s != "" {
t.Target = plugin.Name(s).Normalize()
}
case *dns.PTR:
log.Debugf("Rewrite PTR rr \"%s\"", rr)
if s := rule.Sub(t.Ptr, ruleType, dataPart); s != "" {
log.Debugf("Replacement %s", s)
t.Ptr = plugin.Name(s).Normalize()
log.Debugf("Rewrite PTR t.ptr %s", t.Ptr)
}
}
}