coredns-override/setup.go

59 lines
1.1 KiB
Go

package override
import (
"net"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
)
var log = clog.NewWithPlugin("overide")
func init() {
plugin.Register("override", setup)
}
func setup(c *caddy.Controller) error {
rules, err := parseRules(c)
if err != nil {
return plugin.Error("override", err)
}
log.Debug(rules)
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return Override{Next: next, Rules: rules}
})
return nil
}
func parseRules(c *caddy.Controller) ([]Rule, error) {
var rules []Rule
for c.Next() {
for c.NextBlock() {
searchIP := net.ParseIP(c.Val())
if searchIP == nil {
return nil, c.Errf("invalid search IP: %s", c.Val())
}
if !c.NextArg() {
return nil, c.ArgErr()
}
overrideIP := net.ParseIP(c.Val())
if overrideIP == nil {
return nil, c.Errf("invalid override IP: %s", c.Val())
}
rules = append(rules, Rule{Search: searchIP, Override: overrideIP})
}
}
return rules, nil
}