diff options
author | 2016-11-24 16:57:20 +0100 | |
---|---|---|
committer | 2016-11-24 16:57:20 +0100 | |
commit | 4a8db8a4cebce229fc46d331b68c850e8ba9b717 (patch) | |
tree | deb9d5e831a157d564f462a1f3edcac5f21cd4cb /middleware/proxy | |
parent | c8dd0459c7f3de9b025fe58109b08271547162bb (diff) | |
download | coredns-4a8db8a4cebce229fc46d331b68c850e8ba9b717.tar.gz coredns-4a8db8a4cebce229fc46d331b68c850e8ba9b717.tar.zst coredns-4a8db8a4cebce229fc46d331b68c850e8ba9b717.zip |
middleware/proxy: config syntax cleanups (#435)
* middleware/proxy: config syntax cleanups
Allow port numbers to be used in the transfer statements and clean
up the proxy stanza parsing. Also allow, when specifying an upstream,
/etc/resolv.conf (or any other file) to be used for getting the upstream
nameserver.
Add tests and fix the documentation to make clear what is allowed.
* Fix the other upstream parse as well
Diffstat (limited to 'middleware/proxy')
-rw-r--r-- | middleware/proxy/README.md | 8 | ||||
-rw-r--r-- | middleware/proxy/upstream.go | 36 |
2 files changed, 9 insertions, 35 deletions
diff --git a/middleware/proxy/README.md b/middleware/proxy/README.md index 83ca573d1..837f166fc 100644 --- a/middleware/proxy/README.md +++ b/middleware/proxy/README.md @@ -10,7 +10,7 @@ In its most basic form, a simple reverse proxy uses this syntax: ~~~ -proxy FROM To +proxy FROM TO ~~~ * **FROM** is the base path to match for the request to be proxied @@ -68,13 +68,13 @@ proxy example.org localhost:9005 Load-balance all requests between three backends (using random policy): ~~~ -proxy . web1.local:53 web2.local:1053 web3.local +proxy . dns1.local:53 dns2.local:1053 dns3.local ~~~ Same as above, but round-robin style: ~~~ -proxy . web1.local:53 web2.local:1053 web3.local { +proxy . dns1.local:53 dns2.local:1053 dns3.local { policy round_robin } ~~~ @@ -82,7 +82,7 @@ proxy . web1.local:53 web2.local:1053 web3.local { With health checks and proxy headers to pass hostname, IP, and scheme upstream: ~~~ -proxy . web1.local:53 web2.local:53 web3.local:53 { +proxy . dns1.local:53 dns2.local:53 dns3.local:53 { policy round_robin health_check /health:8080 } diff --git a/middleware/proxy/upstream.go b/middleware/proxy/upstream.go index 02575d1a9..61ada110b 100644 --- a/middleware/proxy/upstream.go +++ b/middleware/proxy/upstream.go @@ -1,18 +1,17 @@ package proxy import ( - "fmt" "io" "io/ioutil" "net" "net/http" - "os" "strconv" "strings" "sync/atomic" "time" "github.com/miekg/coredns/middleware" + "github.com/miekg/coredns/middleware/pkg/dnsutil" "github.com/mholt/caddy/caddyfile" "github.com/miekg/dns" @@ -68,26 +67,9 @@ func NewStaticUpstreams(c *caddyfile.Dispenser) ([]Upstream, error) { } // process the host list, substituting in any nameservers in files - var toHosts []string - for _, host := range to { - h, _, err := net.SplitHostPort(host) - if err != nil { - h = host - } - if x := net.ParseIP(h); x == nil { - // it's a file, parse as resolv.conf - c, err := dns.ClientConfigFromFile(host) - if err == os.ErrNotExist { - return upstreams, fmt.Errorf("not an IP address or file: `%s'", h) - } else if err != nil { - return upstreams, err - } - for _, s := range c.Servers { - toHosts = append(toHosts, net.JoinHostPort(s, c.Port)) - } - } else { - toHosts = append(toHosts, host) - } + toHosts, err := dnsutil.ParseHostPortOrFile(to...) + if err != nil { + return upstreams, err } for c.NextBlock() { @@ -99,7 +81,7 @@ func NewStaticUpstreams(c *caddyfile.Dispenser) ([]Upstream, error) { upstream.Hosts = make([]*UpstreamHost, len(toHosts)) for i, host := range toHosts { uh := &UpstreamHost{ - Name: defaultHostPort(host), + Name: host, Conns: 0, Fails: 0, FailTimeout: upstream.FailTimeout, @@ -297,11 +279,3 @@ func (u *staticUpstream) IsAllowedPath(name string) bool { } return true } - -func defaultHostPort(s string) string { - _, _, e := net.SplitHostPort(s) - if e == nil { - return s - } - return net.JoinHostPort(s, "53") -} |