2017-02-07 16:53:16 -05:00
|
|
|
package rewrite
|
|
|
|
|
|
|
|
|
|
import (
|
2018-07-08 03:18:01 -04:00
|
|
|
"context"
|
2017-03-06 16:32:17 -05:00
|
|
|
"fmt"
|
2017-02-07 16:53:16 -05:00
|
|
|
"strings"
|
|
|
|
|
|
2018-07-02 15:39:50 +01:00
|
|
|
"github.com/coredns/coredns/request"
|
|
|
|
|
|
2017-02-07 16:53:16 -05:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
|
)
|
|
|
|
|
|
2017-03-06 16:32:17 -05:00
|
|
|
type classRule struct {
|
2017-12-14 13:25:36 -05:00
|
|
|
fromClass uint16
|
|
|
|
|
toClass uint16
|
|
|
|
|
NextAction string
|
2017-02-07 16:53:16 -05:00
|
|
|
}
|
|
|
|
|
|
2017-12-14 13:25:36 -05:00
|
|
|
// newClassRule creates a class matching rule
|
|
|
|
|
func newClassRule(nextAction string, args ...string) (Rule, error) {
|
2017-03-06 16:32:17 -05:00
|
|
|
var from, to uint16
|
|
|
|
|
var ok bool
|
2017-12-14 13:25:36 -05:00
|
|
|
if from, ok = dns.StringToClass[strings.ToUpper(args[0])]; !ok {
|
|
|
|
|
return nil, fmt.Errorf("invalid class %q", strings.ToUpper(args[0]))
|
2017-03-06 16:32:17 -05:00
|
|
|
}
|
2017-12-14 13:25:36 -05:00
|
|
|
if to, ok = dns.StringToClass[strings.ToUpper(args[1])]; !ok {
|
|
|
|
|
return nil, fmt.Errorf("invalid class %q", strings.ToUpper(args[1]))
|
2017-03-06 16:32:17 -05:00
|
|
|
}
|
2017-12-14 13:25:36 -05:00
|
|
|
return &classRule{from, to, nextAction}, nil
|
2017-02-07 16:53:16 -05:00
|
|
|
}
|
|
|
|
|
|
2019-02-20 19:12:21 +07:00
|
|
|
// Rewrite rewrites the current request.
|
2018-07-08 03:18:01 -04:00
|
|
|
func (rule *classRule) Rewrite(ctx context.Context, state request.Request) Result {
|
2017-02-07 16:53:16 -05:00
|
|
|
if rule.fromClass > 0 && rule.toClass > 0 {
|
2018-07-02 15:39:50 +01:00
|
|
|
if state.Req.Question[0].Qclass == rule.fromClass {
|
|
|
|
|
state.Req.Question[0].Qclass = rule.toClass
|
2017-02-07 16:53:16 -05:00
|
|
|
return RewriteDone
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return RewriteIgnored
|
|
|
|
|
}
|
2017-09-20 13:06:53 -07:00
|
|
|
|
2018-07-02 15:39:50 +01:00
|
|
|
// Mode returns the processing mode.
|
|
|
|
|
func (rule *classRule) Mode() string { return rule.NextAction }
|
2018-01-18 10:41:14 -05:00
|
|
|
|
|
|
|
|
// GetResponseRule return a rule to rewrite the response with. Currently not implemented.
|
2018-07-02 15:39:50 +01:00
|
|
|
func (rule *classRule) GetResponseRule() ResponseRule { return ResponseRule{} }
|