-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsetup.go
66 lines (53 loc) · 2.08 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
56
57
58
59
60
61
62
63
64
65
66
package blocker
import (
"fmt"
"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 = 4
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.Errf("number of arguments is less than the required count: given: %d, required: %d", len(args), RequiredArgs))
}
blocklistFilePath := args[0]
blocklistUpdateFrequency := args[1]
blocklistType := args[2]
if blocklistType != string(BlocklistType_Hosts) && blocklistType != string(BlocklistType_ABP) {
return plugin.Error(PluginName, c.Errf("blocklist type is not one of the allowed values: %s, %s", BlocklistType_Hosts, BlocklistType_ABP))
}
blocklistResponseType := args[3]
if blocklistResponseType != string(ResponseType_Empty) && blocklistResponseType != string(ResponseType_NXDOMAIN) {
return plugin.Error(PluginName, c.Errf("blocklist response type is not one of the allowed values: %s, %s", ResponseType_Empty, ResponseType_NXDOMAIN))
}
logger := clog.NewWithPlugin(PluginName)
decider, shutdownHooks, err := PrepareBlocklist(blocklistFilePath, blocklistUpdateFrequency, blocklistType, logger)
if err != nil {
return plugin.Error(PluginName, fmt.Errorf("could not prepare blocklist in memory > %w", 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,
ResponseType: blocklistResponseType,
}
})
// All OK, return a nil error.
return nil
}