From 03b3375f8cd0132dc6b17a935cfc6ffca3f7568a Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Sun, 3 Apr 2022 01:33:41 +0200 Subject: [PATCH] parse config --- setup.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/setup.go b/setup.go index 7025026..3ef0d3d 100644 --- a/setup.go +++ b/setup.go @@ -6,8 +6,11 @@ import ( "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) } @@ -31,10 +34,25 @@ func setup(c *caddy.Controller) error { func parseRules(c *caddy.Controller) ([]Rule, error) { var rules []Rule - rules = append(rules, Rule{ - Search: net.ParseIP("45.9.63.244"), - Override: net.ParseIP("10.0.0.1"), - }) + 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 }