From d058dee11d7cfcf0b711f0378d10c9e5cabc8982 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Sat, 5 Mar 2022 18:34:19 -0500 Subject: reverseproxy: Refactor dial address parsing, augment command parsing (#4616) --- modules/caddyhttp/reverseproxy/command.go | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) (limited to 'modules/caddyhttp/reverseproxy/command.go') diff --git a/modules/caddyhttp/reverseproxy/command.go b/modules/caddyhttp/reverseproxy/command.go index 9bbc681..121142c 100644 --- a/modules/caddyhttp/reverseproxy/command.go +++ b/modules/caddyhttp/reverseproxy/command.go @@ -18,7 +18,6 @@ import ( "encoding/json" "flag" "fmt" - "net" "net/http" "strconv" @@ -104,32 +103,14 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) { } // set up the upstream address; assume missing information from given parts - toAddr, err := httpcaddyfile.ParseAddress(to) + toAddr, toScheme, err := parseUpstreamDialAddress(to) if err != nil { return caddy.ExitCodeFailedStartup, fmt.Errorf("invalid upstream address %s: %v", to, err) } - if toAddr.Path != "" { - return caddy.ExitCodeFailedStartup, fmt.Errorf("paths are not allowed: %s", to) - } - if toAddr.Scheme == "" { - if toAddr.Port == httpsPort { - toAddr.Scheme = "https" - } else { - toAddr.Scheme = "http" - } - } - if toAddr.Port == "" { - if toAddr.Scheme == "http" { - toAddr.Port = httpPort - } else if toAddr.Scheme == "https" { - toAddr.Port = httpsPort - } - } // proceed to build the handler and server - ht := HTTPTransport{} - if toAddr.Scheme == "https" { + if toScheme == "https" { ht.TLS = new(TLSConfig) if insecure { ht.TLS.InsecureSkipVerify = true @@ -138,7 +119,7 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) { handler := Handler{ TransportRaw: caddyconfig.JSONModuleObject(ht, "protocol", "http", nil), - Upstreams: UpstreamPool{{Dial: net.JoinHostPort(toAddr.Host, toAddr.Port)}}, + Upstreams: UpstreamPool{{Dial: toAddr}}, } if changeHost { @@ -185,7 +166,7 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) { return caddy.ExitCodeFailedStartup, err } - fmt.Printf("Caddy proxying %s -> %s\n", fromAddr.String(), toAddr.String()) + fmt.Printf("Caddy proxying %s -> %s\n", fromAddr.String(), toAddr) select {} } -- cgit v1.2.3