-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy.go
68 lines (51 loc) · 1.3 KB
/
policy.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package policy
import (
"context"
"strings"
"time"
"github.com/coredns/coredns/request"
"github.com/coredns/coredns/plugin"
"github.com/miekg/dns"
)
// Policy is a plugin that returns a HINFO reply to ANY queries.
type Policy struct {
Next plugin.Handler
ctx context.Context
cancel context.CancelFunc
rule string // rule file either be the path of a file or a URL
period time.Duration
base64 bool
cacheDir string
engine EngineInterface
}
func (a *Policy) Filter(ctx context.Context, req *request.Request) bool {
state := request.Request{W: req.W, Req: req.Req}
switch state.QType() {
case dns.TypeA, dns.TypeAAAA:
default:
return false
}
ok := a.engine.Match(req.Req)
log.Debugf("filter %s %v", a.rule, ok)
return ok
}
func (a *Policy) ViewName() string {
return "policy/" + a.rule
}
// ServeDNS implements the plugin.Handler interface.
func (a *Policy) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
return plugin.NextOrFailure(a.Name(), a.Next, ctx, w, r)
}
// Name implements the Handler interface.
func (a *Policy) Name() string { return "policy" }
func (a *Policy) OnStartup() error {
a.engine.Run()
return nil
}
func (a *Policy) OnShutdown() error {
a.cancel()
return nil
}
func toHostName(in string) string {
return strings.TrimSuffix(in, ".")
}