forked from icyflame/blocker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
55 lines (44 loc) · 1.33 KB
/
setup.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
package blocker
import (
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
)
const PluginName = "blocker"
const RequiredArgs = 3
func init() {
plugin.Register(PluginName, setup)
}
func setup(c *caddy.Controller) error {
var args []string
c.NextArg() // Skip the name of the plugin, which is returned as an argument
for c.NextArg() {
args = append(args, c.Val())
}
if len(args) < RequiredArgs {
// Any errors returned from this setup function should be wrapped with plugin.Error, so we
// can present a slightly nicer error message to the user.
return plugin.Error(PluginName, c.ArgErr())
}
blocklistFilePath := args[0]
blocklistUpdateFrequency := args[1]
blocklistType := args[2]
logger := clog.NewWithPlugin(PluginName)
decider, shutdownHooks, err := PrepareBlocklist(blocklistFilePath, blocklistUpdateFrequency, blocklistType, logger)
if err != nil {
return plugin.Error(PluginName, err)
}
for _, hook := range shutdownHooks {
c.OnShutdown(hook)
}
// Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return Blocker{
Next: next,
Decider: decider,
}
})
// All OK, return a nil error.
return nil
}