2022-03-06 02:34:19 +03:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package reverseproxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
|
|
)
|
|
|
|
|
2023-08-06 00:30:02 +03:00
|
|
|
type parsedAddr struct {
|
|
|
|
network, scheme, host, port string
|
|
|
|
valid bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p parsedAddr) dialAddr() string {
|
|
|
|
if !p.valid {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
// for simplest possible config, we only need to include
|
|
|
|
// the network portion if the user specified one
|
|
|
|
if p.network != "" {
|
|
|
|
return caddy.JoinNetworkAddress(p.network, p.host, p.port)
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the host is a placeholder, then we don't want to join with an empty port,
|
|
|
|
// because that would just append an extra ':' at the end of the address.
|
|
|
|
if p.port == "" && strings.Contains(p.host, "{") {
|
|
|
|
return p.host
|
|
|
|
}
|
|
|
|
return net.JoinHostPort(p.host, p.port)
|
|
|
|
}
|
2023-08-07 22:40:31 +03:00
|
|
|
|
2023-08-06 00:30:02 +03:00
|
|
|
func (p parsedAddr) rangedPort() bool {
|
|
|
|
return strings.Contains(p.port, "-")
|
|
|
|
}
|
2023-08-07 22:40:31 +03:00
|
|
|
|
2023-08-06 00:30:02 +03:00
|
|
|
func (p parsedAddr) replaceablePort() bool {
|
|
|
|
return strings.Contains(p.port, "{") && strings.Contains(p.port, "}")
|
|
|
|
}
|
2023-08-07 22:40:31 +03:00
|
|
|
|
2023-08-06 00:30:02 +03:00
|
|
|
func (p parsedAddr) isUnix() bool {
|
|
|
|
return caddy.IsUnixNetwork(p.network)
|
|
|
|
}
|
|
|
|
|
2022-03-06 02:34:19 +03:00
|
|
|
// parseUpstreamDialAddress parses configuration inputs for
|
|
|
|
// the dial address, including support for a scheme in front
|
|
|
|
// as a shortcut for the port number, and a network type,
|
|
|
|
// for example 'unix' to dial a unix socket.
|
2023-08-06 00:30:02 +03:00
|
|
|
func parseUpstreamDialAddress(upstreamAddr string) (parsedAddr, error) {
|
2022-03-06 02:34:19 +03:00
|
|
|
var network, scheme, host, port string
|
|
|
|
|
|
|
|
if strings.Contains(upstreamAddr, "://") {
|
|
|
|
// we get a parsing error if a placeholder is specified
|
|
|
|
// so we return a more user-friendly error message instead
|
|
|
|
// to explain what to do instead
|
|
|
|
if strings.Contains(upstreamAddr, "{") {
|
2023-08-06 00:30:02 +03:00
|
|
|
return parsedAddr{}, fmt.Errorf("due to parsing difficulties, placeholders are not allowed when an upstream address contains a scheme")
|
2022-03-06 02:34:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
toURL, err := url.Parse(upstreamAddr)
|
|
|
|
if err != nil {
|
2023-05-15 21:14:50 +03:00
|
|
|
// if the error seems to be due to a port range,
|
|
|
|
// try to replace the port range with a dummy
|
|
|
|
// single port so that url.Parse() will succeed
|
|
|
|
if strings.Contains(err.Error(), "invalid port") && strings.Contains(err.Error(), "-") {
|
|
|
|
index := strings.LastIndex(upstreamAddr, ":")
|
|
|
|
if index == -1 {
|
2023-08-06 00:30:02 +03:00
|
|
|
return parsedAddr{}, fmt.Errorf("parsing upstream URL: %v", err)
|
2023-05-15 21:14:50 +03:00
|
|
|
}
|
|
|
|
portRange := upstreamAddr[index+1:]
|
|
|
|
if strings.Count(portRange, "-") != 1 {
|
2023-08-06 00:30:02 +03:00
|
|
|
return parsedAddr{}, fmt.Errorf("parsing upstream URL: parse \"%v\": port range invalid: %v", upstreamAddr, portRange)
|
2023-05-15 21:14:50 +03:00
|
|
|
}
|
|
|
|
toURL, err = url.Parse(strings.ReplaceAll(upstreamAddr, portRange, "0"))
|
|
|
|
if err != nil {
|
2023-08-06 00:30:02 +03:00
|
|
|
return parsedAddr{}, fmt.Errorf("parsing upstream URL: %v", err)
|
2023-05-15 21:14:50 +03:00
|
|
|
}
|
|
|
|
port = portRange
|
|
|
|
} else {
|
2023-08-06 00:30:02 +03:00
|
|
|
return parsedAddr{}, fmt.Errorf("parsing upstream URL: %v", err)
|
2023-05-15 21:14:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if port == "" {
|
|
|
|
port = toURL.Port()
|
2022-03-06 02:34:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// there is currently no way to perform a URL rewrite between choosing
|
|
|
|
// a backend and proxying to it, so we cannot allow extra components
|
|
|
|
// in backend URLs
|
|
|
|
if toURL.Path != "" || toURL.RawQuery != "" || toURL.Fragment != "" {
|
2023-08-06 00:30:02 +03:00
|
|
|
return parsedAddr{}, fmt.Errorf("for now, URLs for proxy upstreams only support scheme, host, and port components")
|
2022-03-06 02:34:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ensure the port and scheme aren't in conflict
|
2023-05-15 21:14:50 +03:00
|
|
|
if toURL.Scheme == "http" && port == "443" {
|
2023-08-06 00:30:02 +03:00
|
|
|
return parsedAddr{}, fmt.Errorf("upstream address has conflicting scheme (http://) and port (:443, the HTTPS port)")
|
2022-03-06 02:34:19 +03:00
|
|
|
}
|
2023-05-15 21:14:50 +03:00
|
|
|
if toURL.Scheme == "https" && port == "80" {
|
2023-08-06 00:30:02 +03:00
|
|
|
return parsedAddr{}, fmt.Errorf("upstream address has conflicting scheme (https://) and port (:80, the HTTP port)")
|
2022-03-06 02:34:19 +03:00
|
|
|
}
|
2023-05-15 21:14:50 +03:00
|
|
|
if toURL.Scheme == "h2c" && port == "443" {
|
2023-08-06 00:30:02 +03:00
|
|
|
return parsedAddr{}, fmt.Errorf("upstream address has conflicting scheme (h2c://) and port (:443, the HTTPS port)")
|
2022-03-06 02:34:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// if port is missing, attempt to infer from scheme
|
2023-05-15 21:14:50 +03:00
|
|
|
if port == "" {
|
2022-03-06 02:34:19 +03:00
|
|
|
switch toURL.Scheme {
|
|
|
|
case "", "http", "h2c":
|
2023-05-15 21:14:50 +03:00
|
|
|
port = "80"
|
2022-03-06 02:34:19 +03:00
|
|
|
case "https":
|
2023-05-15 21:14:50 +03:00
|
|
|
port = "443"
|
2022-03-06 02:34:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 21:14:50 +03:00
|
|
|
scheme, host = toURL.Scheme, toURL.Hostname()
|
2022-03-06 02:34:19 +03:00
|
|
|
} else {
|
|
|
|
var err error
|
2023-02-08 20:05:09 +03:00
|
|
|
network, host, port, err = caddy.SplitNetworkAddress(upstreamAddr)
|
2022-03-06 02:34:19 +03:00
|
|
|
if err != nil {
|
|
|
|
host = upstreamAddr
|
|
|
|
}
|
|
|
|
// we can assume a port if only a hostname is specified, but use of a
|
|
|
|
// placeholder without a port likely means a port will be filled in
|
2023-02-08 20:05:09 +03:00
|
|
|
if port == "" && !strings.Contains(host, "{") && !caddy.IsUnixNetwork(network) {
|
2022-03-06 02:34:19 +03:00
|
|
|
port = "80"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-13 00:09:18 +03:00
|
|
|
// special case network to support both unix and h2c at the same time
|
|
|
|
if network == "unix+h2c" {
|
|
|
|
network = "unix"
|
|
|
|
scheme = "h2c"
|
|
|
|
}
|
2023-08-06 00:30:02 +03:00
|
|
|
return parsedAddr{network, scheme, host, port, true}, nil
|
2022-03-06 02:34:19 +03:00
|
|
|
}
|