Skip to content

Commit

Permalink
addressed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuxi-stripe committed Nov 9, 2023
1 parent 7cc6ce9 commit 215a91a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
30 changes: 19 additions & 11 deletions https.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request
host += ":80"
}

httpsProxy, err := httpsProxy(r.URL, proxy.HttpsProxyAddr)
httpsProxy, err := httpsProxyAddr(r.URL, proxy.HttpsProxyAddr)
if err != nil {
ctx.Warnf("Error configuring HTTPS proxy err=%q url=%q", err, r.URL.String())
}
Expand Down Expand Up @@ -398,14 +398,20 @@ func copyAndClose(ctx *ProxyCtx, dst, src *net.TCPConn) {
src.CloseRead()
}

func dialerFromEnv(proxy *ProxyHttpServer) func(network, addr string) (net.Conn, error) {
https_proxy := os.Getenv("HTTPS_PROXY")
// dialerFromProxy gets the HttpsProxyAddr from proxy to create a dialer.
// When the HttpsProxyAddr from proxy is empty, use the HTTPS_PROXY, https_proxy from environment variables.
func dialerFromProxy(proxy *ProxyHttpServer) func(network, addr string) (net.Conn, error) {
https_proxy := proxy.HttpsProxyAddr
if https_proxy == "" {
https_proxy = os.Getenv("https_proxy")
}
if https_proxy == "" {
return nil
https_proxy = os.Getenv("HTTPS_PROXY")
if https_proxy == "" {
https_proxy = os.Getenv("https_proxy")
}
if https_proxy == "" {
return nil
}
}

return proxy.NewConnectDialToProxy(https_proxy)
}

Expand Down Expand Up @@ -559,13 +565,15 @@ func (proxy *ProxyHttpServer) connectDialProxyWithContext(ctx *ProxyCtx, proxyHo
return c, nil
}

// httpsProxy allows goproxy to respect no_proxy env vars
// httpsProxyAddr function uses the address in httpsProxy parameter.
// When the httpProxyAddr parameter is empty, uses the HTTPS_PROXY, https_proxy from environment variables.
// httpsProxyAddr function allows goproxy to respect no_proxy env vars
// https://github.com/stripe/goproxy/pull/5
func httpsProxy(reqURL *url.URL, httpProxyAddr string) (string, error) {
func httpsProxyAddr(reqURL *url.URL, httpsProxy string) (string, error) {
cfg := httpproxy.FromEnvironment()

if httpProxyAddr != "" {
cfg.HTTPSProxy = httpProxyAddr
if httpsProxy != "" {
cfg.HTTPSProxy = httpsProxy
}

// We only use this codepath for HTTPS CONNECT proxies so we shouldn't
Expand Down
4 changes: 2 additions & 2 deletions https_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var proxytests = map[string]struct {

var envKeys = []string{"no_proxy", "http_proxy", "https_proxy", "NO_PROXY", "HTTP_PROXY", "HTTPS_PROXY"}

func TestHttpsProxy(t *testing.T) {
func TestHttpsProxyAddr(t *testing.T) {
for _, k := range envKeys {
v, ok := os.LookupEnv(k)
if ok {
Expand All @@ -52,7 +52,7 @@ func TestHttpsProxy(t *testing.T) {
t.Fatalf("bad test input URL %s: %v", spec.url, err)
}

actual, err := httpsProxy(url, spec.customHttpsProxy)
actual, err := httpsProxyAddr(url, spec.customHttpsProxy)
if err != nil {
t.Fatalf("unexpected error parsing proxy from env: %#v", err)
}
Expand Down
15 changes: 8 additions & 7 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,23 +231,24 @@ func NewProxyHttpServer(opts ...ProxyHttpServerOptions) *ProxyHttpServer {
Tr: &http.Transport{TLSClientConfig: tlsClientSkipVerify, Proxy: http.ProxyFromEnvironment},
}

cfg := httpproxy.FromEnvironment()
// httpProxyCfg holds configuration for HTTP proxy settings. See FromEnvironment for details.
httpProxyCfg := httpproxy.FromEnvironment()

if appliedOpts.httpProxyAddr != "" {
proxy.HttpProxyAddr = appliedOpts.httpProxyAddr
cfg.HTTPProxy = appliedOpts.httpProxyAddr
httpProxyCfg.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)
httpProxyCfg.HTTPSProxy = appliedOpts.httpsProxyAddr
}

proxy.ConnectDial = dialerFromProxy(&proxy)

if appliedOpts.httpProxyAddr != "" || appliedOpts.httpsProxyAddr != "" {
proxy.Tr.Proxy = func(req *http.Request) (*url.URL, error) {
return cfg.ProxyFunc()(req.URL)
return httpProxyCfg.ProxyFunc()(req.URL)
}
}

Expand Down

0 comments on commit 215a91a

Please sign in to comment.