forked from coredns/coredns
/
type.go
48 lines (40 loc) · 1.3 KB
/
type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Package rewrite is a plugin for rewriting requests internally to something different.
package rewrite
import (
"context"
"fmt"
"strings"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
// typeRule is a type rewrite rule.
type typeRule struct {
fromType uint16
toType uint16
nextAction string
}
func newTypeRule(nextAction string, args ...string) (Rule, error) {
var from, to uint16
var ok bool
if from, ok = dns.StringToType[strings.ToUpper(args[0])]; !ok {
return nil, fmt.Errorf("invalid type %q", strings.ToUpper(args[0]))
}
if to, ok = dns.StringToType[strings.ToUpper(args[1])]; !ok {
return nil, fmt.Errorf("invalid type %q", strings.ToUpper(args[1]))
}
return &typeRule{from, to, nextAction}, nil
}
// Rewrite rewrites the current request.
func (rule *typeRule) Rewrite(ctx context.Context, state request.Request) Result {
if rule.fromType > 0 && rule.toType > 0 {
if state.QType() == rule.fromType {
state.Req.Question[0].Qtype = rule.toType
return RewriteDone
}
}
return RewriteIgnored
}
// Mode returns the processing mode.
func (rule *typeRule) Mode() string { return rule.nextAction }
// GetResponseRule return a rule to rewrite the response with. Currently not implemented.
func (rule *typeRule) GetResponseRule() ResponseRule { return ResponseRule{} }