* plugin/rewrite: streamline the ResponseRule handling. The functionality of a response rule is now completely encapsulated behind a `ResponseRule` interface. This significantly simplifies the complete processing flow, it enables more flexible response handling and it is possible to eliminate lots of state flags, ifs and switches. Based on the new flexibility the pull request also enables to support a response name rewrite for all name rewrite types. To be compatible, an explicit `answer auto` option is added to support a best effort response rewrite (name and value). Additionally now all name rewrite rules support additional name and value reponse rewrite options. Using this feature it is also possible now to rewrite a complete sub domain hierarchy to a single domain name combined with a correct rewrite (#2389). Signed-off-by: Uwe Krueger <uwe.krueger@sap.com> * revert policy Signed-off-by: Uwe Krueger <uwe.krueger@sap.com> Co-authored-by: Miek Gieben <miek@miek.nl>
44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package rewrite
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/coredns/coredns/request"
|
|
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
type classRule struct {
|
|
fromClass uint16
|
|
toClass uint16
|
|
NextAction string
|
|
}
|
|
|
|
// newClassRule creates a class matching rule
|
|
func newClassRule(nextAction string, args ...string) (Rule, error) {
|
|
var from, to uint16
|
|
var ok bool
|
|
if from, ok = dns.StringToClass[strings.ToUpper(args[0])]; !ok {
|
|
return nil, fmt.Errorf("invalid class %q", strings.ToUpper(args[0]))
|
|
}
|
|
if to, ok = dns.StringToClass[strings.ToUpper(args[1])]; !ok {
|
|
return nil, fmt.Errorf("invalid class %q", strings.ToUpper(args[1]))
|
|
}
|
|
return &classRule{from, to, nextAction}, nil
|
|
}
|
|
|
|
// Rewrite rewrites the current request.
|
|
func (rule *classRule) Rewrite(ctx context.Context, state request.Request) (ResponseRules, Result) {
|
|
if rule.fromClass > 0 && rule.toClass > 0 {
|
|
if state.Req.Question[0].Qclass == rule.fromClass {
|
|
state.Req.Question[0].Qclass = rule.toClass
|
|
return nil, RewriteDone
|
|
}
|
|
}
|
|
return nil, RewriteIgnored
|
|
}
|
|
|
|
// Mode returns the processing mode.
|
|
func (rule *classRule) Mode() string { return rule.NextAction }
|