mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-27 22:23:48 +03:00
reverse_proxy: Fix panic for some CLI flag values (closes #2848)
This commit is contained in:
parent
8d3c64932e
commit
8ef0a0b4f8
1 changed files with 15 additions and 9 deletions
|
@ -17,7 +17,7 @@ package reverseproxy
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -63,18 +63,21 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) {
|
||||||
from = "localhost:" + httpcaddyfile.DefaultPort
|
from = "localhost:" + httpcaddyfile.DefaultPort
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// URLs need a scheme in order to parse successfully
|
||||||
if !strings.Contains(from, "://") {
|
if !strings.Contains(from, "://") {
|
||||||
from = "http://" + from
|
from = "http://" + from
|
||||||
}
|
}
|
||||||
|
if !strings.Contains(to, "://") {
|
||||||
|
to = "http://" + to
|
||||||
|
}
|
||||||
|
|
||||||
fromURL, err := url.Parse(from)
|
fromURL, err := url.Parse(from)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fromURL.Host = from
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("parsing 'from' URL: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
toURL, err := url.Parse(to)
|
toURL, err := url.Parse(to)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
toURL.Host = to
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("parsing 'to' URL: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ht := HTTPTransport{}
|
ht := HTTPTransport{}
|
||||||
|
@ -99,16 +102,19 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) {
|
||||||
caddyconfig.JSONModuleObject(handler, "handler", "reverse_proxy", nil),
|
caddyconfig.JSONModuleObject(handler, "handler", "reverse_proxy", nil),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if fromURL.Hostname() != "" {
|
urlHost := fromURL.Hostname()
|
||||||
|
if urlHost != "" {
|
||||||
route.MatcherSetsRaw = []map[string]json.RawMessage{
|
route.MatcherSetsRaw = []map[string]json.RawMessage{
|
||||||
map[string]json.RawMessage{
|
map[string]json.RawMessage{
|
||||||
"host": caddyconfig.JSON(caddyhttp.MatchHost{fromURL.Hostname()}, nil),
|
"host": caddyconfig.JSON(caddyhttp.MatchHost{urlHost}, nil),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
listen := ":" + httpcaddyfile.DefaultPort
|
listen := ":80"
|
||||||
if certmagic.HostQualifies(fromURL.Hostname()) {
|
if urlPort := fromURL.Port(); urlPort != "" {
|
||||||
|
listen = ":" + urlPort
|
||||||
|
} else if certmagic.HostQualifies(urlHost) {
|
||||||
listen = ":443"
|
listen = ":443"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,7 +138,7 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) {
|
||||||
return caddy.ExitCodeFailedStartup, err
|
return caddy.ExitCodeFailedStartup, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Caddy 2 proxying from %s to %s", from, to)
|
fmt.Printf("Caddy 2 proxying from %s to %s\n", fromURL, toURL)
|
||||||
|
|
||||||
select {}
|
select {}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue