aboutsummaryrefslogtreecommitdiff
path: root/plugin
diff options
context:
space:
mode:
authorGravatar Eugen Kleiner <ekleiner@infoblox.com> 2018-08-16 00:24:47 +0300
committerGravatar Yong Tang <yong.tang.github@outlook.com> 2018-08-15 14:24:47 -0700
commitb87ed01bb24e2c82b931b022dadeb4da3869c782 (patch)
treec6e2e9fa23c6f7a4ea194f86fcffc69c161f4efc /plugin
parent81d09491590c04a09fa4e9f22673ada6e7403052 (diff)
downloadcoredns-b87ed01bb24e2c82b931b022dadeb4da3869c782.tar.gz
coredns-b87ed01bb24e2c82b931b022dadeb4da3869c782.tar.zst
coredns-b87ed01bb24e2c82b931b022dadeb4da3869c782.zip
plugin/forward: Split setup to reuse it from external plugins (#2034)
Diffstat (limited to 'plugin')
-rw-r--r--plugin/forward/setup.go111
1 files changed, 62 insertions, 49 deletions
diff --git a/plugin/forward/setup.go b/plugin/forward/setup.go
index ee48fdaf6..9fe8a6c38 100644
--- a/plugin/forward/setup.go
+++ b/plugin/forward/setup.go
@@ -13,6 +13,7 @@ import (
pkgtls "github.com/coredns/coredns/plugin/pkg/tls"
"github.com/mholt/caddy"
+ "github.com/mholt/caddy/caddyfile"
)
func init() {
@@ -70,68 +71,80 @@ func (f *Forward) OnShutdown() error {
func (f *Forward) Close() { f.OnShutdown() }
func parseForward(c *caddy.Controller) (*Forward, error) {
- f := New()
-
- protocols := map[int]int{}
-
- i := 0
+ var (
+ f *Forward
+ err error
+ i int
+ )
for c.Next() {
if i > 0 {
return nil, plugin.ErrOnce
}
i++
-
- if !c.Args(&f.from) {
- return f, c.ArgErr()
+ f, err = ParseForwardStanza(&c.Dispenser)
+ if err != nil {
+ return nil, err
}
- f.from = plugin.Host(f.from).Normalize()
+ }
+ return f, nil
+}
- to := c.RemainingArgs()
- if len(to) == 0 {
- return f, c.ArgErr()
- }
+// ParseForwardStanza parses one forward stanza
+func ParseForwardStanza(c *caddyfile.Dispenser) (*Forward, error) {
+ f := New()
- // A bit fiddly, but first check if we've got protocols and if so add them back in when we create the proxies.
- protocols = make(map[int]int)
- for i := range to {
- protocols[i], to[i] = protocol(to[i])
- }
+ protocols := map[int]int{}
- // If parseHostPortOrFile expands a file with a lot of nameserver our accounting in protocols doesn't make
- // any sense anymore... For now: lets don't care.
- toHosts, err := dnsutil.ParseHostPortOrFile(to...)
- if err != nil {
- return f, err
- }
+ if !c.Args(&f.from) {
+ return f, c.ArgErr()
+ }
+ f.from = plugin.Host(f.from).Normalize()
+
+ to := c.RemainingArgs()
+ if len(to) == 0 {
+ return f, c.ArgErr()
+ }
+
+ // A bit fiddly, but first check if we've got protocols and if so add them back in when we create the proxies.
+ protocols = make(map[int]int)
+ for i := range to {
+ protocols[i], to[i] = protocol(to[i])
+ }
+
+ // If parseHostPortOrFile expands a file with a lot of nameserver our accounting in protocols doesn't make
+ // any sense anymore... For now: lets don't care.
+ toHosts, err := dnsutil.ParseHostPortOrFile(to...)
+ if err != nil {
+ return f, err
+ }
- for i, h := range toHosts {
- // Double check the port, if e.g. is 53 and the transport is TLS make it 853.
- // This can be somewhat annoying because you *can't* have TLS on port 53 then.
- switch protocols[i] {
- case TLS:
- h1, p, err := net.SplitHostPort(h)
- if err != nil {
- break
- }
-
- // This is more of a bug in dnsutil.ParseHostPortOrFile that defaults to
- // 53 because it doesn't know about the tls:// // and friends (that should be fixed). Hence
- // Fix the port number here, back to what the user intended.
- if p == "53" {
- h = net.JoinHostPort(h1, "853")
- }
+ for i, h := range toHosts {
+ // Double check the port, if e.g. is 53 and the transport is TLS make it 853.
+ // This can be somewhat annoying because you *can't* have TLS on port 53 then.
+ switch protocols[i] {
+ case TLS:
+ h1, p, err := net.SplitHostPort(h)
+ if err != nil {
+ break
}
- // We can't set tlsConfig here, because we haven't parsed it yet.
- // We set it below at the end of parseBlock, use nil now.
- p := NewProxy(h, protocols[i])
- f.proxies = append(f.proxies, p)
+ // This is more of a bug in dnsutil.ParseHostPortOrFile that defaults to
+ // 53 because it doesn't know about the tls:// // and friends (that should be fixed). Hence
+ // Fix the port number here, back to what the user intended.
+ if p == "53" {
+ h = net.JoinHostPort(h1, "853")
+ }
}
- for c.NextBlock() {
- if err := parseBlock(c, f); err != nil {
- return f, err
- }
+ // We can't set tlsConfig here, because we haven't parsed it yet.
+ // We set it below at the end of parseBlock, use nil now.
+ p := NewProxy(h, protocols[i])
+ f.proxies = append(f.proxies, p)
+ }
+
+ for c.NextBlock() {
+ if err := parseBlock(c, f); err != nil {
+ return f, err
}
}
@@ -148,7 +161,7 @@ func parseForward(c *caddy.Controller) (*Forward, error) {
return f, nil
}
-func parseBlock(c *caddy.Controller, f *Forward) error {
+func parseBlock(c *caddyfile.Dispenser, f *Forward) error {
switch c.Val() {
case "except":
ignore := c.RemainingArgs()