diff options
author | 2017-02-07 16:53:16 -0500 | |
---|---|---|
committer | 2017-02-07 21:53:16 +0000 | |
commit | fa1c90a479cce4666c32c4aae8728fc12f09980b (patch) | |
tree | 3a7514b48da436767d914dfad24656b8876d0412 /middleware/rewrite/rewrite.go | |
parent | b8e75509cc7d3f73a8526ad515e85f76b9ccf59d (diff) | |
download | coredns-fa1c90a479cce4666c32c4aae8728fc12f09980b.tar.gz coredns-fa1c90a479cce4666c32c4aae8728fc12f09980b.tar.zst coredns-fa1c90a479cce4666c32c4aae8728fc12f09980b.zip |
Add field keywords to rewrite middleware (#497)
* Require Field for rewrite rules
* review feedback changes
* fix ut
* fix typo, add warning message
Diffstat (limited to 'middleware/rewrite/rewrite.go')
-rw-r--r-- | middleware/rewrite/rewrite.go | 65 |
1 files changed, 2 insertions, 63 deletions
diff --git a/middleware/rewrite/rewrite.go b/middleware/rewrite/rewrite.go index adbdbca15..cb5bbc288 100644 --- a/middleware/rewrite/rewrite.go +++ b/middleware/rewrite/rewrite.go @@ -2,10 +2,7 @@ package rewrite import ( - "strings" - "github.com/miekg/coredns/middleware" - "github.com/miekg/dns" "golang.org/x/net/context" ) @@ -59,64 +56,6 @@ func (rw Rewrite) Name() string { return "rewrite" } type Rule interface { // Rewrite rewrites the internal location of the current request. Rewrite(*dns.Msg) Result -} - -// SimpleRule is a simple rewrite rule. If the From and To look like a type -// the type of the request is rewritten, otherwise the name is. -// Note: TSIG signed requests will be invalid. -type SimpleRule struct { - From, To string - fromType, toType uint16 - fromClass, toClass uint16 -} - -// NewSimpleRule creates a new Simple Rule -func NewSimpleRule(from, to string) SimpleRule { - tpf := dns.StringToType[from] - tpt := dns.StringToType[to] - - // ANY is both a type and class, ANY class rewritting is way more less frequent - // so we default to ANY as a type. - clf := dns.StringToClass[from] - clt := dns.StringToClass[to] - if from == "ANY" { - clf = 0 - clt = 0 - } - - // It's only a type/class if uppercase is used. - if from != strings.ToUpper(from) { - tpf = 0 - clf = 0 - from = middleware.Name(from).Normalize() - } - if to != strings.ToUpper(to) { - tpt = 0 - clt = 0 - to = middleware.Name(to).Normalize() - } - return SimpleRule{From: from, To: to, fromType: tpf, toType: tpt, fromClass: clf, toClass: clt} -} - -// Rewrite rewrites the the current request. -func (s SimpleRule) Rewrite(r *dns.Msg) Result { - if s.fromType > 0 && s.toType > 0 { - if r.Question[0].Qtype == s.fromType { - r.Question[0].Qtype = s.toType - return RewriteDone - } - } - - if s.fromClass > 0 && s.toClass > 0 { - if r.Question[0].Qclass == s.fromClass { - r.Question[0].Qclass = s.toClass - return RewriteDone - } - } - - if s.From == r.Question[0].Name { - r.Question[0].Name = s.To - return RewriteDone - } - return RewriteIgnored + // New returns a new rule. + New(...string) Rule } |