@marcel91200 / caddy Mirror · read-only
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 01:00 UTC

master
go 151 lines 5 KB
Raw
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package reverseproxy
16
17 import (
18 "fmt"
19 "net"
20 "net/url"
21 "strings"
22
23 "github.com/caddyserver/caddy/v2"
24 )
25
26 type parsedAddr struct {
27 network, scheme, host, port string
28 valid bool
29 }
30
31 func (p parsedAddr) dialAddr() string {
32 if !p.valid {
33 return ""
34 }
35 // for simplest possible config, we only need to include
36 // the network portion if the user specified one
37 if p.network != "" {
38 return caddy.JoinNetworkAddress(p.network, p.host, p.port)
39 }
40
41 // if the host is a placeholder, then we don't want to join with an empty port,
42 // because that would just append an extra ':' at the end of the address.
43 if p.port == "" && strings.Contains(p.host, "{") {
44 return p.host
45 }
46 return net.JoinHostPort(p.host, p.port)
47 }
48
49 func (p parsedAddr) rangedPort() bool {
50 return strings.Contains(p.port, "-")
51 }
52
53 func (p parsedAddr) replaceablePort() bool {
54 return strings.Contains(p.port, "{") && strings.Contains(p.port, "}")
55 }
56
57 func (p parsedAddr) isUnix() bool {
58 return caddy.IsUnixNetwork(p.network)
59 }
60
61 // parseUpstreamDialAddress parses configuration inputs for
62 // the dial address, including support for a scheme in front
63 // as a shortcut for the port number, and a network type,
64 // for example 'unix' to dial a unix socket.
65 func parseUpstreamDialAddress(upstreamAddr string) (parsedAddr, error) {
66 var network, scheme, host, port string
67
68 if strings.Contains(upstreamAddr, "://") {
69 // we get a parsing error if a placeholder is specified
70 // so we return a more user-friendly error message instead
71 // to explain what to do instead
72 if strings.Contains(upstreamAddr, "{") {
73 return parsedAddr{}, fmt.Errorf("due to parsing difficulties, placeholders are not allowed when an upstream address contains a scheme")
74 }
75
76 toURL, err := url.Parse(upstreamAddr)
77 if err != nil {
78 // if the error seems to be due to a port range,
79 // try to replace the port range with a dummy
80 // single port so that url.Parse() will succeed
81 if strings.Contains(err.Error(), "invalid port") && strings.Contains(err.Error(), "-") {
82 index := strings.LastIndex(upstreamAddr, ":")
83 if index == -1 {
84 return parsedAddr{}, fmt.Errorf("parsing upstream URL: %v", err)
85 }
86 portRange := upstreamAddr[index+1:]
87 if strings.Count(portRange, "-") != 1 {
88 return parsedAddr{}, fmt.Errorf("parsing upstream URL: parse \"%v\": port range invalid: %v", upstreamAddr, portRange)
89 }
90 toURL, err = url.Parse(strings.ReplaceAll(upstreamAddr, portRange, "0"))
91 if err != nil {
92 return parsedAddr{}, fmt.Errorf("parsing upstream URL: %v", err)
93 }
94 port = portRange
95 } else {
96 return parsedAddr{}, fmt.Errorf("parsing upstream URL: %v", err)
97 }
98 }
99 if port == "" {
100 port = toURL.Port()
101 }
102
103 // there is currently no way to perform a URL rewrite between choosing
104 // a backend and proxying to it, so we cannot allow extra components
105 // in backend URLs
106 if toURL.Path != "" || toURL.RawQuery != "" || toURL.Fragment != "" {
107 return parsedAddr{}, fmt.Errorf("for now, URLs for proxy upstreams only support scheme, host, and port components")
108 }
109
110 // ensure the port and scheme aren't in conflict
111 if toURL.Scheme == "http" && port == "443" {
112 return parsedAddr{}, fmt.Errorf("upstream address has conflicting scheme (http://) and port (:443, the HTTPS port)")
113 }
114 if toURL.Scheme == "https" && port == "80" {
115 return parsedAddr{}, fmt.Errorf("upstream address has conflicting scheme (https://) and port (:80, the HTTP port)")
116 }
117 if toURL.Scheme == "h2c" && port == "443" {
118 return parsedAddr{}, fmt.Errorf("upstream address has conflicting scheme (h2c://) and port (:443, the HTTPS port)")
119 }
120
121 // if port is missing, attempt to infer from scheme
122 if port == "" {
123 switch toURL.Scheme {
124 case "", "http", "h2c":
125 port = "80"
126 case "https":
127 port = "443"
128 }
129 }
130
131 scheme, host = toURL.Scheme, toURL.Hostname()
132 } else {
133 var err error
134 network, host, port, err = caddy.SplitNetworkAddress(upstreamAddr)
135 if err != nil {
136 host = upstreamAddr
137 }
138 // we can assume a port if only a hostname is specified, but use of a
139 // placeholder without a port likely means a port will be filled in
140 if port == "" && !strings.Contains(host, "{") && !caddy.IsUnixNetwork(network) && !caddy.IsFdNetwork(network) {
141 port = "80"
142 }
143 }
144
145 // special case network to support both unix and h2c at the same time
146 if network == "unix+h2c" {
147 network = "unix"
148 scheme = "h2c"
149 }
150 return parsedAddr{network, scheme, host, port, true}, nil
151 }