2022-03-24 14:43:41 +00:00
|
|
|
package split
|
2022-03-24 14:26:15 +00:00
|
|
|
|
|
|
|
import (
|
2022-03-24 17:33:18 +00:00
|
|
|
"net"
|
|
|
|
|
2022-03-24 14:26:15 +00:00
|
|
|
"github.com/coredns/caddy"
|
|
|
|
"github.com/coredns/coredns/core/dnsserver"
|
|
|
|
"github.com/coredns/coredns/plugin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// init registers this plugin.
|
2022-03-24 14:43:41 +00:00
|
|
|
func init() { plugin.Register("split", setup) }
|
2022-03-24 14:26:15 +00:00
|
|
|
|
|
|
|
// setup is the function that gets called when the config parser see the token "example". Setup is responsible
|
|
|
|
// for parsing any extra options the example plugin may have. The first token this function sees is "example".
|
|
|
|
func setup(c *caddy.Controller) error {
|
2022-03-24 17:33:18 +00:00
|
|
|
s := Split{}
|
|
|
|
for c.Next() {
|
|
|
|
r := rule{}
|
|
|
|
args := c.RemainingArgs()
|
|
|
|
r.zones = plugin.OriginsFromArgsOrServerBlock(args, c.ServerBlockKeys)
|
|
|
|
if c.NextBlock() {
|
|
|
|
n := network{}
|
|
|
|
_, ipnet, err := net.ParseCIDR(c.Val())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.record = ipnet
|
|
|
|
for c.NextBlock() {
|
|
|
|
for c.NextLine() {
|
|
|
|
a := c.Val()
|
|
|
|
_ = a
|
|
|
|
argsLoop:
|
|
|
|
for _, v := range c.RemainingArgs() {
|
|
|
|
_, ipnet, err := net.ParseCIDR(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, vv := range n.allowed {
|
|
|
|
if vv.Contains(ipnet.IP) {
|
|
|
|
continue argsLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n.allowed = append(n.allowed, ipnet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.networks = append(r.networks, n)
|
|
|
|
}
|
|
|
|
s.Rule = append(s.Rule, r)
|
|
|
|
}
|
2022-03-24 14:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
|
|
|
|
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
|
2022-03-24 17:33:18 +00:00
|
|
|
s.Next = next
|
|
|
|
return s
|
2022-03-24 14:26:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// All OK, return a nil error.
|
|
|
|
return nil
|
|
|
|
}
|