diff --git a/middleware/name.go b/middleware/name.go index f866a3865..ab9772b37 100644 --- a/middleware/name.go +++ b/middleware/name.go @@ -1,6 +1,10 @@ package middleware -import "strings" +import ( + "strings" + + "github.com/miekg/dns" +) // Name represents a domain name. type Name string @@ -13,3 +17,8 @@ type Name string func (n Name) Matches(other string) bool { return strings.HasSuffix(string(n), other) } + +// Normalize lowercases and makes n fully qualified. +func (n Name) Normalize() string { + return strings.ToLower(dns.Fqdn(string(n))) +} diff --git a/middleware/rewrite/rewrite.go b/middleware/rewrite/rewrite.go index 42043506e..7ec8debdb 100644 --- a/middleware/rewrite/rewrite.go +++ b/middleware/rewrite/rewrite.go @@ -69,27 +69,31 @@ func NewSimpleRule(from, to string) SimpleRule { // It's only a type if uppercase is used. if from != strings.ToUpper(from) { tpf = 0 + from = middleware.Name(from).Normalize() } if to != strings.ToUpper(to) { tpt = 0 + to = middleware.Name(to).Normalize() } - - // lowercase and fully qualify the others here? TODO(miek) return SimpleRule{From: from, To: to, fromType: tpf, toType: tpt} } // Rewrite rewrites the the current request. func (s SimpleRule) Rewrite(r *dns.Msg) Result { + // type rewrite if s.fromType > 0 && s.toType > 0 { if r.Question[0].Qtype == s.fromType { r.Question[0].Qtype = s.toType return RewriteDone } - + return RewriteIgnored } - // if the question name matches the full name, or subset rewrite that - // s.Question[0].Name + // name rewite + if s.From == r.Question[0].Name { + r.Question[0].Name = s.To + return RewriteDone + } return RewriteIgnored } diff --git a/middleware/rewrite/rewrite.md b/middleware/rewrite/rewrite.md index 198e35825..6b5fb8161 100644 --- a/middleware/rewrite/rewrite.md +++ b/middleware/rewrite/rewrite.md @@ -16,9 +16,13 @@ rewrite from to If from *and* to look like a DNS type (`A`, `MX`, etc.) the type of the message will be rewriten, i.e. to rewrite ANY queries to HINFO, use `rewrite ANY HINFO`. -If it does not look like a type a name is assumed and the qname in the message is rewritten. +If it does not look like a type a name is assumed and the qname in the message is rewritten, this +needs to be a full match of the name `rewrite miek.nl example.org`. Advanced users may open a block and make a complex rewrite rule: +TODO(miek): this has not yet been implemented. + +> Everything below this line has not been implemented, yet. ~~~ rewrite [basename] {