mirror of
https://github.com/coredns/coredns.git
synced 2025-12-08 19:35:10 -05:00
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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user