-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Configurable http and https proxy addrs #22
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,7 +118,7 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request | |
host += ":80" | ||
} | ||
|
||
httpsProxy, err := httpsProxyFromEnv(r.URL) | ||
httpsProxy, err := httpsProxy(r.URL, proxy.HttpsProxyAddr) | ||
if err != nil { | ||
ctx.Warnf("Error configuring HTTPS proxy err=%q url=%q", err, r.URL.String()) | ||
} | ||
|
@@ -559,10 +559,15 @@ func (proxy *ProxyHttpServer) connectDialProxyWithContext(ctx *ProxyCtx, proxyHo | |
return c, nil | ||
} | ||
|
||
// httpsProxyFromEnv allows goproxy to respect no_proxy env vars | ||
// httpsProxy allows goproxy to respect no_proxy env vars | ||
// https://github.com/stripe/goproxy/pull/5 | ||
func httpsProxyFromEnv(reqURL *url.URL) (string, error) { | ||
func httpsProxy(reqURL *url.URL, httpProxyAddr string) (string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you change the var name to |
||
cfg := httpproxy.FromEnvironment() | ||
|
||
if httpProxyAddr != "" { | ||
cfg.HTTPSProxy = httpProxyAddr | ||
} | ||
|
||
// We only use this codepath for HTTPS CONNECT proxies so we shouldn't | ||
// return anything from HTTPProxy | ||
cfg.HTTPProxy = "" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,12 @@ import ( | |
"log" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"regexp" | ||
"sync/atomic" | ||
|
||
"golang.org/x/net/http/httpproxy" | ||
) | ||
|
||
// The basic proxy type. Implements http.Handler. | ||
|
@@ -54,6 +57,10 @@ type ProxyHttpServer struct { | |
// ConnectRespHandler allows users to mutate the response to the CONNECT request before it | ||
// is returned to the client. | ||
ConnectRespHandler func(ctx *ProxyCtx, resp *http.Response) error | ||
|
||
// HTTP and HTTPS proxy addresses | ||
HttpProxyAddr string | ||
HttpsProxyAddr string | ||
} | ||
|
||
var hasPort = regexp.MustCompile(`:\d+$`) | ||
|
@@ -179,8 +186,40 @@ func (proxy *ProxyHttpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) | |
} | ||
} | ||
|
||
type options struct { | ||
httpProxyAddr string | ||
httpsProxyAddr string | ||
} | ||
type fnOption func(*options) | ||
|
||
func (fn fnOption) apply(opts *options) { fn(opts) } | ||
|
||
type ProxyHttpServerOptions interface { | ||
apply(*options) | ||
} | ||
|
||
func WithHttpProxyAddr(httpProxyAddr string) ProxyHttpServerOptions { | ||
return fnOption(func(opts *options) { | ||
opts.httpProxyAddr = httpProxyAddr | ||
}) | ||
} | ||
|
||
func WithHttpsProxyAddr(httpsProxyAddr string) ProxyHttpServerOptions { | ||
return fnOption(func(opts *options) { | ||
opts.httpsProxyAddr = httpsProxyAddr | ||
}) | ||
} | ||
|
||
// NewProxyHttpServer creates and returns a proxy server, logging to stderr by default | ||
func NewProxyHttpServer() *ProxyHttpServer { | ||
func NewProxyHttpServer(opts ...ProxyHttpServerOptions) *ProxyHttpServer { | ||
appliedOpts := &options{ | ||
httpProxyAddr: "", | ||
httpsProxyAddr: "", | ||
} | ||
for _, opt := range opts { | ||
opt.apply(appliedOpts) | ||
} | ||
|
||
proxy := ProxyHttpServer{ | ||
Logger: log.New(os.Stderr, "", log.LstdFlags), | ||
reqHandlers: []ReqHandler{}, | ||
|
@@ -191,7 +230,26 @@ func NewProxyHttpServer() *ProxyHttpServer { | |
}), | ||
Tr: &http.Transport{TLSClientConfig: tlsClientSkipVerify, Proxy: http.ProxyFromEnvironment}, | ||
} | ||
proxy.ConnectDial = dialerFromEnv(&proxy) | ||
|
||
cfg := httpproxy.FromEnvironment() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about silently overriding environment proxy variables. WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also could we name this something to signal what it actually is instead of just |
||
if appliedOpts.httpProxyAddr != "" { | ||
proxy.HttpProxyAddr = appliedOpts.httpProxyAddr | ||
cfg.HTTPProxy = appliedOpts.httpProxyAddr | ||
} | ||
|
||
if appliedOpts.httpsProxyAddr != "" { | ||
proxy.HttpsProxyAddr = appliedOpts.httpsProxyAddr | ||
cfg.HTTPSProxy = appliedOpts.httpsProxyAddr | ||
proxy.ConnectDial = proxy.NewConnectDialToProxy(appliedOpts.httpsProxyAddr) | ||
} else { | ||
proxy.ConnectDial = dialerFromEnv(&proxy) | ||
} | ||
|
||
if appliedOpts.httpProxyAddr != "" || appliedOpts.httpsProxyAddr != "" { | ||
proxy.Tr.Proxy = func(req *http.Request) (*url.URL, error) { | ||
return cfg.ProxyFunc()(req.URL) | ||
} | ||
} | ||
|
||
return &proxy | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you extend this comment to clarify which configuration takes precedent?