Add field keywords to rewrite middleware (#497)

* Require Field for rewrite rules

* review feedback changes

* fix ut

* fix typo, add warning message
This commit is contained in:
Bob Wasniak 2017-02-07 16:53:16 -05:00 committed by Miek Gieben
parent b8e75509cc
commit fa1c90a479
8 changed files with 124 additions and 76 deletions

View file

@ -0,0 +1,31 @@
// Package rewrite is middleware for rewriting requests internally to something different.
package rewrite
import (
"strings"
"github.com/miekg/dns"
)
// ClassRule is a class rewrite rule.
type ClassRule struct {
fromClass, toClass uint16
}
// Initializer
func (rule ClassRule) New(args ...string) Rule {
from, to := args[0], strings.Join(args[1:], " ")
return &ClassRule{dns.StringToClass[from], dns.StringToClass[to]}
}
// Rewrite rewrites the the current request.
func (rule ClassRule) Rewrite(r *dns.Msg) Result {
if rule.fromClass > 0 && rule.toClass > 0 {
if r.Question[0].Qclass == rule.fromClass {
r.Question[0].Qclass = rule.toClass
return RewriteDone
}
}
return RewriteIgnored
}