mirror of
https://github.com/coredns/coredns.git
synced 2025-10-28 00:34:24 -04:00
New plugin: loop (#1989)
* New plugin: loop Add a plugin that detects loops. It does this by sending an unique query to our selves. If we see the query more than twice we stop the process. If there isn't a loop, the plugin disables it self and becomes a noop plugin. Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
89
plugin/loop/setup.go
Normal file
89
plugin/loop/setup.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package loop
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"net"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/core/dnsserver"
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/coredns/coredns/plugin/pkg/dnsutil"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterPlugin("loop", caddy.Plugin{
|
||||
ServerType: "dns",
|
||||
Action: setup,
|
||||
})
|
||||
}
|
||||
|
||||
func setup(c *caddy.Controller) error {
|
||||
l, err := parse(c)
|
||||
if err != nil {
|
||||
return plugin.Error("loop", err)
|
||||
}
|
||||
|
||||
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
|
||||
l.Next = next
|
||||
return l
|
||||
})
|
||||
|
||||
// Send query to ourselves and see if it end up with us again.
|
||||
c.OnStartup(func() error {
|
||||
// Another Go function, otherwise we block startup and can't send the packet.
|
||||
go func() {
|
||||
deadline := time.Now().Add(30 * time.Second)
|
||||
conf := dnsserver.GetConfig(c)
|
||||
|
||||
for time.Now().Before(deadline) {
|
||||
lh := conf.ListenHosts[0]
|
||||
addr := net.JoinHostPort(lh, conf.Port)
|
||||
if _, err := l.exchange(addr); err != nil {
|
||||
time.Sleep(1 * time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
go func() {
|
||||
time.Sleep(2 * time.Second)
|
||||
l.setDisabled()
|
||||
}()
|
||||
}
|
||||
l.setDisabled()
|
||||
}()
|
||||
return nil
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parse(c *caddy.Controller) (*Loop, error) {
|
||||
i := 0
|
||||
zone := "."
|
||||
for c.Next() {
|
||||
if i > 0 {
|
||||
return nil, plugin.ErrOnce
|
||||
}
|
||||
i++
|
||||
if c.NextArg() {
|
||||
return nil, c.ArgErr()
|
||||
}
|
||||
|
||||
if len(c.ServerBlockKeys) > 0 {
|
||||
zone = plugin.Host(c.ServerBlockKeys[0]).Normalize()
|
||||
}
|
||||
}
|
||||
return New(zone), nil
|
||||
}
|
||||
|
||||
// qname returns a random name. <rand.Int()>.<rand.Int().<zone>.
|
||||
func qname(zone string) string {
|
||||
l1 := strconv.Itoa(r.Int())
|
||||
l2 := strconv.Itoa(r.Int())
|
||||
|
||||
return dnsutil.Join([]string{l1, l2, zone})
|
||||
}
|
||||
|
||||
var r = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
Reference in New Issue
Block a user