plugin/rewrite: handle continue in response rewrite (#1740)

* handle continue in response rewrite

* add test
This commit is contained in:
Chris O'Haver
2018-04-27 02:05:44 -04:00
committed by Miek Gieben
parent 3236464223
commit 47b2b10209
2 changed files with 33 additions and 19 deletions

View File

@@ -11,19 +11,14 @@ import (
"github.com/miekg/dns" "github.com/miekg/dns"
) )
func TestResponseReverter(t *testing.T) { var tests = []struct {
rules := []Rule{}
r, _ := newNameRule("stop", "regex", `(core)\.(dns)\.(rocks)`, "{2}.{1}.{3}", "answer", "name", `(dns)\.(core)\.(rocks)`, "{2}.{1}.{3}")
rules = append(rules, r)
tests := []struct {
from string from string
fromType uint16 fromType uint16
answer []dns.RR answer []dns.RR
to string to string
toType uint16 toType uint16
noRevert bool noRevert bool
}{ }{
{"core.dns.rocks", dns.TypeA, []dns.RR{test.A("dns.core.rocks. 5 IN A 10.0.0.1")}, "core.dns.rocks", dns.TypeA, false}, {"core.dns.rocks", dns.TypeA, []dns.RR{test.A("dns.core.rocks. 5 IN A 10.0.0.1")}, "core.dns.rocks", dns.TypeA, false},
{"core.dns.rocks", dns.TypeSRV, []dns.RR{test.SRV("dns.core.rocks. 5 IN SRV 0 100 100 srv1.dns.core.rocks.")}, "core.dns.rocks", dns.TypeSRV, false}, {"core.dns.rocks", dns.TypeSRV, []dns.RR{test.SRV("dns.core.rocks. 5 IN SRV 0 100 100 srv1.dns.core.rocks.")}, "core.dns.rocks", dns.TypeSRV, false},
{"core.dns.rocks", dns.TypeA, []dns.RR{test.A("core.dns.rocks. 5 IN A 10.0.0.1")}, "dns.core.rocks.", dns.TypeA, true}, {"core.dns.rocks", dns.TypeA, []dns.RR{test.A("core.dns.rocks. 5 IN A 10.0.0.1")}, "dns.core.rocks.", dns.TypeA, true},
@@ -33,8 +28,24 @@ func TestResponseReverter(t *testing.T) {
test.A("dns.core.rocks. 5 IN A 10.0.0.1"), test.A("dns.core.rocks. 5 IN A 10.0.0.1"),
test.A("dns.core.rocks. 5 IN A 10.0.0.2"), test.A("dns.core.rocks. 5 IN A 10.0.0.2"),
}, "core.dns.rocks", dns.TypeA, false}, }, "core.dns.rocks", dns.TypeA, false},
} }
func TestResponseReverter(t *testing.T) {
rules := []Rule{}
r, _ := newNameRule("stop", "regex", `(core)\.(dns)\.(rocks)`, "{2}.{1}.{3}", "answer", "name", `(dns)\.(core)\.(rocks)`, "{2}.{1}.{3}")
rules = append(rules, r)
doReverterTests(rules, t)
rules = []Rule{}
r, _ = newNameRule("continue", "regex", `(core)\.(dns)\.(rocks)`, "{2}.{1}.{3}", "answer", "name", `(dns)\.(core)\.(rocks)`, "{2}.{1}.{3}")
rules = append(rules, r)
doReverterTests(rules, t)
}
func doReverterTests(rules []Rule, t *testing.T) {
ctx := context.TODO() ctx := context.TODO()
for i, tc := range tests { for i, tc := range tests {
m := new(dns.Msg) m := new(dns.Msg)

View File

@@ -64,7 +64,10 @@ func (rw Rewrite) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
// } // }
} }
} }
if rw.noRevert || len(wr.ResponseRules) == 0 {
return plugin.NextOrFailure(rw.Name(), rw.Next, ctx, w, r) return plugin.NextOrFailure(rw.Name(), rw.Next, ctx, w, r)
}
return plugin.NextOrFailure(rw.Name(), rw.Next, ctx, wr, r)
} }
// Name implements the Handler interface. // Name implements the Handler interface.