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.
|
2021-05-04 10:05:45 +02:00
|
|
|
func (rule *classRule) Rewrite(ctx context.Context, state request.Request) (ResponseRules, 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
|
2021-05-04 10:05:45 +02:00
|
|
|
return nil, RewriteDone
|
2017-02-07 16:53:16 -05:00
|
|
|
}
|
|
|
|
|
}
|
2021-05-04 10:05:45 +02:00
|
|
|
return nil, RewriteIgnored
|
2017-02-07 16:53:16 -05:00
|
|
|
}
|
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 }
|