diff options
author | Francis Lavoie <lavofr@gmail.com> | 2023-05-15 14:14:50 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-15 12:14:50 -0600 |
commit | 75b690d248c7681dd974f6179c98a363af417a25 (patch) | |
tree | f0df0da211635df094d6b4b9aa6e3f58a7e577a0 /modules/caddyhttp/reverseproxy/command.go | |
parent | 52d7335c2b1b8424e8971a9b03f51a5f36583535 (diff) |
reverseproxy: Expand port ranges to multiple upstreams in CLI + Caddyfile (#5494)
* reverseproxy: Expand port ranges to multiple upstreams in CLI + Caddyfile
* Add clarifying comment
Diffstat (limited to 'modules/caddyhttp/reverseproxy/command.go')
-rw-r--r-- | modules/caddyhttp/reverseproxy/command.go | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/modules/caddyhttp/reverseproxy/command.go b/modules/caddyhttp/reverseproxy/command.go index bd3efcd..48fabd5 100644 --- a/modules/caddyhttp/reverseproxy/command.go +++ b/modules/caddyhttp/reverseproxy/command.go @@ -153,9 +153,24 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) { upstreamPool := UpstreamPool{} for _, toAddr := range toAddresses { - upstreamPool = append(upstreamPool, &Upstream{ - Dial: toAddr, - }) + parsedAddr, err := caddy.ParseNetworkAddress(toAddr) + if err != nil { + return caddy.ExitCodeFailedStartup, fmt.Errorf("invalid upstream address %s: %v", toAddr, err) + } + + if parsedAddr.StartPort == 0 && parsedAddr.EndPort == 0 { + // unix networks don't have ports + upstreamPool = append(upstreamPool, &Upstream{ + Dial: toAddr, + }) + } else { + // expand a port range into multiple upstreams + for i := parsedAddr.StartPort; i <= parsedAddr.EndPort; i++ { + upstreamPool = append(upstreamPool, &Upstream{ + Dial: caddy.JoinNetworkAddress("", parsedAddr.Host, fmt.Sprint(i)), + }) + } + } } handler := Handler{ |