Commit b87ed01b authored by Eugen Kleiner's avatar Eugen Kleiner Committed by Yong Tang

plugin/forward: Split setup to reuse it from external plugins (#2034)

parent 81d09491
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
pkgtls "github.com/coredns/coredns/plugin/pkg/tls" pkgtls "github.com/coredns/coredns/plugin/pkg/tls"
"github.com/mholt/caddy" "github.com/mholt/caddy"
"github.com/mholt/caddy/caddyfile"
) )
func init() { func init() {
...@@ -70,68 +71,80 @@ func (f *Forward) OnShutdown() error { ...@@ -70,68 +71,80 @@ func (f *Forward) OnShutdown() error {
func (f *Forward) Close() { f.OnShutdown() } func (f *Forward) Close() { f.OnShutdown() }
func parseForward(c *caddy.Controller) (*Forward, error) { func parseForward(c *caddy.Controller) (*Forward, error) {
f := New() var (
f *Forward
protocols := map[int]int{} err error
i int
i := 0 )
for c.Next() { for c.Next() {
if i > 0 { if i > 0 {
return nil, plugin.ErrOnce return nil, plugin.ErrOnce
} }
i++ i++
f, err = ParseForwardStanza(&c.Dispenser)
if !c.Args(&f.from) { if err != nil {
return f, c.ArgErr() return nil, err
} }
f.from = plugin.Host(f.from).Normalize() }
return f, nil
}
to := c.RemainingArgs() // ParseForwardStanza parses one forward stanza
if len(to) == 0 { func ParseForwardStanza(c *caddyfile.Dispenser) (*Forward, error) {
return f, c.ArgErr() 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 := map[int]int{}
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 if !c.Args(&f.from) {
// any sense anymore... For now: lets don't care. return f, c.ArgErr()
toHosts, err := dnsutil.ParseHostPortOrFile(to...) }
if err != nil { f.from = plugin.Host(f.from).Normalize()
return f, err
} 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 { for i, h := range toHosts {
// Double check the port, if e.g. is 53 and the transport is TLS make it 853. // 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. // This can be somewhat annoying because you *can't* have TLS on port 53 then.
switch protocols[i] { switch protocols[i] {
case TLS: case TLS:
h1, p, err := net.SplitHostPort(h) h1, p, err := net.SplitHostPort(h)
if err != nil { if err != nil {
break 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")
}
} }
// We can't set tlsConfig here, because we haven't parsed it yet. // This is more of a bug in dnsutil.ParseHostPortOrFile that defaults to
// We set it below at the end of parseBlock, use nil now. // 53 because it doesn't know about the tls:// // and friends (that should be fixed). Hence
p := NewProxy(h, protocols[i]) // Fix the port number here, back to what the user intended.
f.proxies = append(f.proxies, p) if p == "53" {
h = net.JoinHostPort(h1, "853")
}
} }
for c.NextBlock() { // We can't set tlsConfig here, because we haven't parsed it yet.
if err := parseBlock(c, f); err != nil { // We set it below at the end of parseBlock, use nil now.
return f, err 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) { ...@@ -148,7 +161,7 @@ func parseForward(c *caddy.Controller) (*Forward, error) {
return f, nil return f, nil
} }
func parseBlock(c *caddy.Controller, f *Forward) error { func parseBlock(c *caddyfile.Dispenser, f *Forward) error {
switch c.Val() { switch c.Val() {
case "except": case "except":
ignore := c.RemainingArgs() ignore := c.RemainingArgs()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment