Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
reverse_proxy: Add support for SRV backends (#3180)
* reverse_proxy: Begin SRV lookup support (WIP) * reverse_proxy: Finish adding support for SRV-based backends (#3179)
Matt Holt committed
Mar 24, 2020 at 10:53 UTC
e02117cb8a2b0b6dbd3dbb1de4d1569ff63ca617
4 files changed
+78
-29
modules/caddyhttp/matchers.go
+1
-1
@@ -41,7 +41,7 @@ type (
41
// especially A/AAAA pointed at your server.
42
//
43
// Automatic HTTPS can be
44
- // [customized or disabled](/docs/json/apps/http/servers/automatic_https/).
44
+ // [customized or disabled](/docs/modules/http#servers/automatic_https).
45
MatchHost []string
46
47
// MatchPath matches requests by the URI's path (case-insensitive). Path
modules/caddyhttp/reverseproxy/caddyfile.go
+26
-4
@@ -177,13 +177,36 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
177
return net.JoinHostPort(host, port), nil
178
}
179
180
+ // appendUpstream creates an upstream for address and adds
181
+ // it to the list. If the address starts with "srv+" it is
182
+ // treated as a SRV-based upstream, and any port will be
183
+ // dropped.
184
+ appendUpstream := func(address string) error {
185
+ isSRV := strings.HasPrefix(address, "srv+")
186
+ if isSRV {
187
+ address = strings.TrimPrefix(address, "srv+")
188
+ }
189
+ dialAddr, err := upstreamDialAddress(address)
190
+ if err != nil {
191
+ return err
192
+ }
193
+ if isSRV {
194
+ if host, _, err := net.SplitHostPort(dialAddr); err == nil {
195
+ dialAddr = host
196
+ }
197
+ h.Upstreams = append(h.Upstreams, &Upstream{LookupSRV: dialAddr})
198
+ } else {
199
+ h.Upstreams = append(h.Upstreams, &Upstream{Dial: dialAddr})
200
+ }
201
+ return nil
202
+ }
203
+
204
for d.Next() {
205
for _, up := range d.RemainingArgs() {
182
- dialAddr, err := upstreamDialAddress(up)
206
+ err := appendUpstream(up)
207
if err != nil {
208
return err
209
}
186
- h.Upstreams = append(h.Upstreams, &Upstream{Dial: dialAddr})
210
}
211
212
for d.NextBlock(0) {
@@ -194,11 +217,10 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
217
return d.ArgErr()
218
}
219
for _, up := range args {
197
- dialAddr, err := upstreamDialAddress(up)
220
+ err := appendUpstream(up)
221
if err != nil {
222
return err
223
}
201
- h.Upstreams = append(h.Upstreams, &Upstream{Dial: dialAddr})
224
}
225
226
case "lb_policy":
modules/caddyhttp/reverseproxy/hosts.go
+50
-23
@@ -17,6 +17,8 @@ package reverseproxy
17
import (
18
"context"
19
"fmt"
20
+ "net"
21
+ "net/http"
22
"strconv"
23
"sync/atomic"
24
@@ -63,10 +65,10 @@ type UpstreamPool []*Upstream
65
type Upstream struct {
66
Host `json:"-"`
67
66
- // The [network address](/docs/json/apps/http/#servers/listen)
68
+ // The [network address](/docs/conventions#network-addresses)
69
// to dial to connect to the upstream. Must represent precisely
70
// one socket (i.e. no port ranges). A valid network address
69
- // either has a host and port, or is a unix socket address.
71
+ // either has a host and port or is a unix socket address.
72
//
73
// Placeholders may be used to make the upstream dynamic, but be
74
// aware of the health check implications of this: a single
@@ -75,6 +77,11 @@ type Upstream struct {
77
// backends is down. Also be aware of open proxy vulnerabilities.
78
Dial string `json:"dial,omitempty"`
79
80
+ // If DNS SRV records are used for service discovery with this
81
+ // upstream, specify the DNS name for which to look up SRV
82
+ // records here, instead of specifying a dial address.
83
+ LookupSRV string `json:"lookup_srv,omitempty"`
84
+
85
// The maximum number of simultaneous requests to allow to
86
// this upstream. If set, overrides the global passive health
87
// check UnhealthyRequestCount value.
@@ -118,6 +125,47 @@ func (u *Upstream) Full() bool {
125
return u.MaxRequests > 0 && u.Host.NumRequests() >= u.MaxRequests
126
}
127
128
+// fillDialInfo returns a filled DialInfo for upstream u, using the request
129
+// context. If the upstream has a SRV lookup configured, that is done and a
130
+// returned address is chosen; otherwise, the upstream's regular dial address
131
+// field is used. Note that the returned value is not a pointer.
132
+func (u *Upstream) fillDialInfo(r *http.Request) (DialInfo, error) {
133
+ repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
134
+ var addr caddy.ParsedAddress
135
+
136
+ if u.LookupSRV != "" {
137
+ // perform DNS lookup for SRV records and choose one
138
+ srvName := repl.ReplaceAll(u.LookupSRV, "")
139
+ _, records, err := net.DefaultResolver.LookupSRV(r.Context(), "", "", srvName)
140
+ if err != nil {
141
+ return DialInfo{}, err
142
+ }
143
+ addr.Network = "tcp"
144
+ addr.Host = records[0].Target
145
+ addr.StartPort, addr.EndPort = uint(records[0].Port), uint(records[0].Port)
146
+ } else {
147
+ // use provided dial address
148
+ var err error
149
+ dial := repl.ReplaceAll(u.Dial, "")
150
+ addr, err = caddy.ParseNetworkAddress(dial)
151
+ if err != nil {
152
+ return DialInfo{}, fmt.Errorf("upstream %s: invalid dial address %s: %v", u.Dial, dial, err)
153
+ }
154
+ if numPorts := addr.PortRangeSize(); numPorts != 1 {
155
+ return DialInfo{}, fmt.Errorf("upstream %s: dial address must represent precisely one socket: %s represents %d",
156
+ u.Dial, dial, numPorts)
157
+ }
158
+ }
159
+
160
+ return DialInfo{
161
+ Upstream: u,
162
+ Network: addr.Network,
163
+ Address: addr.JoinHostPort(0),
164
+ Host: addr.Host,
165
+ Port: strconv.Itoa(int(addr.StartPort)),
166
+ }, nil
167
+}
168
+
169
// upstreamHost is the basic, in-memory representation
170
// of the state of a remote host. It implements the
171
// Host interface.
@@ -204,27 +252,6 @@ func (di DialInfo) String() string {
252
return caddy.JoinNetworkAddress(di.Network, di.Host, di.Port)
253
}
254
207
-// fillDialInfo returns a filled DialInfo for the given upstream, using
208
-// the given Replacer. Note that the returned value is not a pointer.
209
-func fillDialInfo(upstream *Upstream, repl *caddy.Replacer) (DialInfo, error) {
210
- dial := repl.ReplaceAll(upstream.Dial, "")
211
- addr, err := caddy.ParseNetworkAddress(dial)
212
- if err != nil {
213
- return DialInfo{}, fmt.Errorf("upstream %s: invalid dial address %s: %v", upstream.Dial, dial, err)
214
- }
215
- if numPorts := addr.PortRangeSize(); numPorts != 1 {
216
- return DialInfo{}, fmt.Errorf("upstream %s: dial address must represent precisely one socket: %s represents %d",
217
- upstream.Dial, dial, numPorts)
218
- }
219
- return DialInfo{
220
- Upstream: upstream,
221
- Network: addr.Network,
222
- Address: addr.JoinHostPort(0),
223
- Host: addr.Host,
224
- Port: strconv.Itoa(int(addr.StartPort)),
225
- }, nil
226
-}
227
-
255
// GetDialInfo gets the upstream dialing info out of the context,
256
// and returns true if there was a valid value; false otherwise.
257
func GetDialInfo(ctx context.Context) (DialInfo, bool) {
modules/caddyhttp/reverseproxy/reverseproxy.go
+1
-1
@@ -313,7 +313,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyht
313
// the dial address may vary per-request if placeholders are
314
// used, so perform those replacements here; the resulting
315
// DialInfo struct should have valid network address syntax
316
- dialInfo, err := fillDialInfo(upstream, repl)
316
+ dialInfo, err := upstream.fillDialInfo(r)
317
if err != nil {
318
return fmt.Errorf("making dial info: %v", err)
319
}