Skip to content

Commit a9768d2

Browse files
reverseproxy: Configurable forward proxy URL (#6114)
Co-authored-by: WeidiDeng <[email protected]>
1 parent 52822a4 commit a9768d2

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

modules/caddyhttp/reverseproxy/caddyfile.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,7 @@ func (h *Handler) FinalizeUnmarshalCaddyfile(helper httpcaddyfile.Helper) error
907907
// read_buffer <size>
908908
// write_buffer <size>
909909
// max_response_header <size>
910+
// forward_proxy_url <url>
910911
// dial_timeout <duration>
911912
// dial_fallback_delay <duration>
912913
// response_header_timeout <duration>
@@ -994,6 +995,12 @@ func (h *HTTPTransport) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
994995
return d.Errf("invalid proxy protocol version '%s'", proxyProtocol)
995996
}
996997

998+
case "forward_proxy_url":
999+
if !d.NextArg() {
1000+
return d.ArgErr()
1001+
}
1002+
h.ForwardProxyURL = d.Val()
1003+
9971004
case "dial_timeout":
9981005
if !d.NextArg() {
9991006
return d.ArgErr()

modules/caddyhttp/reverseproxy/httptransport.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
weakrand "math/rand"
2424
"net"
2525
"net/http"
26+
"net/url"
2627
"os"
2728
"reflect"
2829
"strings"
@@ -71,6 +72,22 @@ type HTTPTransport struct {
7172
// connecting to an upstream. Default: off.
7273
ProxyProtocol string `json:"proxy_protocol,omitempty"`
7374

75+
// URL to the server that the HTTP transport will use to proxy
76+
// requests to the upstream. See http.Transport.Proxy for
77+
// information regarding supported protocols. This value takes
78+
// precedence over `HTTP_PROXY`, etc.
79+
//
80+
// Providing a value to this parameter results in
81+
// requests flowing through the reverse_proxy in the following
82+
// way:
83+
//
84+
// User Agent ->
85+
// reverse_proxy ->
86+
// forward_proxy_url -> upstream
87+
//
88+
// Default: http.ProxyFromEnvironment
89+
ForwardProxyURL string `json:"forward_proxy_url,omitempty"`
90+
7491
// How long to wait before timing out trying to connect to
7592
// an upstream. Default: `3s`.
7693
DialTimeout caddy.Duration `json:"dial_timeout,omitempty"`
@@ -265,8 +282,21 @@ func (h *HTTPTransport) NewTransport(caddyCtx caddy.Context) (*http.Transport, e
265282
return conn, nil
266283
}
267284

285+
// negotiate any HTTP/SOCKS proxy for the HTTP transport
286+
var proxy func(*http.Request) (*url.URL, error)
287+
if h.ForwardProxyURL != "" {
288+
pUrl, err := url.Parse(h.ForwardProxyURL)
289+
if err != nil {
290+
return nil, fmt.Errorf("failed to parse transport proxy url: %v", err)
291+
}
292+
caddyCtx.Logger().Info("setting transport proxy url", zap.String("url", h.ForwardProxyURL))
293+
proxy = http.ProxyURL(pUrl)
294+
} else {
295+
proxy = http.ProxyFromEnvironment
296+
}
297+
268298
rt := &http.Transport{
269-
Proxy: http.ProxyFromEnvironment,
299+
Proxy: proxy,
270300
DialContext: dialContext,
271301
MaxConnsPerHost: h.MaxConnsPerHost,
272302
ResponseHeaderTimeout: time.Duration(h.ResponseHeaderTimeout),

0 commit comments

Comments
 (0)