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
reverseproxy: add support for custom DNS resolver (#3479)
* reverse proxy: add support for custom resolver * reverse proxy: don't pollute the global resolver with bootstrap resolver setup * Improve documentation of reverseproxy.UpstreamResolver fields Co-authored-by: Matt Holt <mholt@users.noreply.github.com> * reverse proxy: clarify the name resolution conventions of upstream resolvers and bootstrap resolver * remove support for bootstraper of resolver * godoc and code-style changes Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
Mohammed Al Sahaf committed
Jul 19, 2020 at 00:00 UTC
bd9d796e6ed64c713002e3503a8b0012bd4f1460
1 file changed
+40
-1
modules/caddyhttp/reverseproxy/httptransport.go
+40
-1
@@ -21,6 +21,7 @@ import (
21
"encoding/base64"
22
"fmt"
23
"io/ioutil"
24
+ weakrand "math/rand"
25
"net"
26
"net/http"
27
"reflect"
@@ -43,6 +44,9 @@ type HTTPTransport struct {
44
// able to borrow/use at least some of these config fields; if so,
45
// maybe move them into a type called CommonTransport and embed it?
46
47
+ // Configures the DNS resolver used to resolve the IP address of upstream hostnames.
48
+ Resolver *UpstreamResolver `json:"resolver,omitempty"`
49
+
50
// Configures TLS to the upstream. Setting this to an empty struct
51
// is sufficient to enable TLS with reasonable defaults.
52
TLS *TLSConfig `json:"tls,omitempty"`
@@ -146,7 +150,30 @@ func (h *HTTPTransport) NewTransport(ctx caddy.Context) (*http.Transport, error)
150
dialer := &net.Dialer{
151
Timeout: time.Duration(h.DialTimeout),
152
FallbackDelay: time.Duration(h.FallbackDelay),
149
- // TODO: Resolver
153
+ }
154
+
155
+ if h.Resolver != nil {
156
+ for _, v := range h.Resolver.Addresses {
157
+ addr, err := caddy.ParseNetworkAddress(v)
158
+ if err != nil {
159
+ return nil, err
160
+ }
161
+ if addr.PortRangeSize() != 1 {
162
+ return nil, fmt.Errorf("resolver address must have exactly one address; cannot call %v", addr)
163
+ }
164
+ h.Resolver.netAddrs = append(h.Resolver.netAddrs, addr)
165
+ }
166
+ d := &net.Dialer{
167
+ Timeout: time.Duration(h.DialTimeout),
168
+ FallbackDelay: time.Duration(h.FallbackDelay),
169
+ }
170
+ dialer.Resolver = &net.Resolver{
171
+ PreferGo: true,
172
+ Dial: func(ctx context.Context, _, _ string) (net.Conn, error) {
173
+ addr := h.Resolver.netAddrs[weakrand.Intn(len(h.Resolver.netAddrs))]
174
+ return d.DialContext(ctx, addr.Network, addr.JoinHostPort(0))
175
+ },
176
+ }
177
}
178
179
rt := &http.Transport{
@@ -359,6 +386,18 @@ func (t TLSConfig) MakeTLSClientConfig(ctx caddy.Context) (*tls.Config, error) {
386
return cfg, nil
387
}
388
389
+// UpstreamResolver holds the set of addresses of DNS resolvers of
390
+// upstream addresses
391
+type UpstreamResolver struct {
392
+ // The addresses of DNS resolvers to use when looking up the addresses of proxy upstreams.
393
+ // It accepts [network addresses](/docs/conventions#network-addresses)
394
+ // with port range of only 1. If the host is an IP address, it will be dialed directly to resolve the upstream server.
395
+ // If the host is not an IP address, the addresses are resolved using the [name resolution convention](https://golang.org/pkg/net/#hdr-Name_Resolution) of the Go standard library.
396
+ // If the array contains more than 1 resolver address, one is chosen at random.
397
+ Addresses []string `json:"addresses,omitempty"`
398
+ netAddrs []caddy.NetworkAddress
399
+}
400
+
401
// KeepAlive holds configuration pertaining to HTTP Keep-Alive.
402
type KeepAlive struct {
403
// Whether HTTP Keep-Alive is enabled. Default: true