diff options
Diffstat (limited to 'middleware/bind')
-rw-r--r-- | middleware/bind/README.md | 21 | ||||
-rw-r--r-- | middleware/bind/bind.go | 10 | ||||
-rw-r--r-- | middleware/bind/bind_test.go | 30 | ||||
-rw-r--r-- | middleware/bind/setup.go | 23 |
4 files changed, 84 insertions, 0 deletions
diff --git a/middleware/bind/README.md b/middleware/bind/README.md new file mode 100644 index 000000000..ad23b6153 --- /dev/null +++ b/middleware/bind/README.md @@ -0,0 +1,21 @@ +# bind + +bind overrides the host to which the server should bind. Normally, the listener binds to the +wildcard host. However, you may force the listener to bind to another IP instead. This +directive accepts only an address, not a port. + +## Syntax + +~~~ txt +bind address +~~~ + +address is the IP address to bind to. + +## Examples + +To make your socket accessible only to that machine, bind to IP 127.0.0.1 (localhost): + +~~~ txt +bind 127.0.0.1 +~~~ diff --git a/middleware/bind/bind.go b/middleware/bind/bind.go new file mode 100644 index 000000000..ac27c993b --- /dev/null +++ b/middleware/bind/bind.go @@ -0,0 +1,10 @@ +package bind + +import "github.com/mholt/caddy" + +func init() { + caddy.RegisterPlugin("bind", caddy.Plugin{ + ServerType: "dns", + Action: setupBind, + }) +} diff --git a/middleware/bind/bind_test.go b/middleware/bind/bind_test.go new file mode 100644 index 000000000..d61741a02 --- /dev/null +++ b/middleware/bind/bind_test.go @@ -0,0 +1,30 @@ +package bind + +import ( + "testing" + + "github.com/miekg/coredns/core/dnsserver" + + "github.com/mholt/caddy" +) + +func TestSetupBind(t *testing.T) { + c := caddy.NewTestController("dns", `bind 1.2.3.4`) + err := setupBind(c) + if err != nil { + t.Fatalf("Expected no errors, but got: %v", err) + } + + cfg := dnsserver.GetConfig(c) + if got, want := cfg.ListenHost, "1.2.3.4"; got != want { + t.Errorf("Expected the config's ListenHost to be %s, was %s", want, got) + } +} + +func TestBindAddress(t *testing.T) { + c := caddy.NewTestController("dns", `bind 1.2.3.bla`) + err := setupBind(c) + if err == nil { + t.Fatalf("Expected errors, but got none") + } +} diff --git a/middleware/bind/setup.go b/middleware/bind/setup.go new file mode 100644 index 000000000..c08098b5d --- /dev/null +++ b/middleware/bind/setup.go @@ -0,0 +1,23 @@ +package bind + +import ( + "fmt" + "net" + + "github.com/miekg/coredns/core/dnsserver" + + "github.com/mholt/caddy" +) + +func setupBind(c *caddy.Controller) error { + config := dnsserver.GetConfig(c) + for c.Next() { + if !c.Args(&config.ListenHost) { + return c.ArgErr() + } + } + if net.ParseIP(config.ListenHost) == nil { + return fmt.Errorf("not a valid IP address: %s", config.ListenHost) + } + return nil +} |