-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewriteng.go
53 lines (47 loc) · 1.43 KB
/
rewriteng.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
49
50
51
52
53
package rewriteng
import (
"context"
"fmt"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
var (
log = clog.NewWithPlugin("rewriteng")
)
// RewriteNG implements the rewriteng plugin
type RewriteNG struct {
Next plugin.Handler
Rules []*nameRule
}
// Name implements the Handler interface.
func (h RewriteNG) Name() string {
return "rewriteng"
}
// ServeDNS implements the plugin.Handler interface.
func (h RewriteNG) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
wr := NewResponseRewriter(w, r)
log.Errorf("plug-in calls RewriteNG")
state := request.Request{W: w, Req: r}
for _, rule := range h.Rules {
log.Errorf("rule evaluating %s", rule.Rule)
switch result := rule.Rewrite(ctx, state); result {
case RewriteDone:
if !validName(state.Req.Question[0].Name) {
x := state.Req.Question[0].Name
log.Errorf("Invalid name after rewrite: %s", x)
state.Req.Question[0] = wr.originalQuestion
return dns.RcodeServerFailure, fmt.Errorf("invalid name after rewrite: %s", x)
}
wr.Rules = append(wr.Rules, rule)
return plugin.NextOrFailure(h.Name(), h.Next, ctx, wr, r)
default:
// fmt.Println("After: ", state.Req.Question[0].Name)
}
}
if len(wr.Rules) == 0 {
return plugin.NextOrFailure(h.Name(), h.Next, ctx, w, r)
}
return plugin.NextOrFailure(h.Name(), h.Next, ctx, wr, r)
}