From ea55023cb3acd6e06725229ce7432057cc19c671 Mon Sep 17 00:00:00 2001 From: Denys Vitali Date: Tue, 3 May 2022 15:12:58 +0200 Subject: [PATCH] feat: add support for HTTPS_PROXY This commit adds support for the HTTPS_PROXY environment variable. At the moment the NO_PROXY environment variable is ignored, please be aware of that! This should close fullstorydev/grpcurl#166 --- go.mod | 2 ++ go.sum | 2 ++ grpcurl.go | 21 ++++++++++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 4f40d71..6df326f 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,8 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang/protobuf v1.5.2 github.com/jhump/protoreflect v1.10.3 + github.com/mwitkow/go-http-dialer v0.0.0-20161116154839-378f744fb2b8 // indirect + golang.org/x/net v0.0.0-20200822124328-c89045814202 golang.org/x/text v0.3.7 // indirect google.golang.org/grpc v1.44.0 google.golang.org/protobuf v1.27.1 diff --git a/go.sum b/go.sum index 3d14f6d..efdeef7 100644 --- a/go.sum +++ b/go.sum @@ -116,6 +116,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mwitkow/go-http-dialer v0.0.0-20161116154839-378f744fb2b8 h1:BhQQWYKJwXPtAhm12d4gQU4LKS9Yov22yOrDc2QA7ho= +github.com/mwitkow/go-http-dialer v0.0.0-20161116154839-378f744fb2b8/go.mod h1:ntWhh7pzdiiRKBMxUB5iG+Q2gmZBxGxpX1KyK6N8kX8= github.com/nishanths/predeclared v0.0.0-20200524104333-86fad755b4d3/go.mod h1:nt3d53pc1VYcphSCIaYAJtnPYnr3Zyn8fMq2wvPGPso= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/grpcurl.go b/grpcurl.go index f36fc9e..42fd096 100644 --- a/grpcurl.go +++ b/grpcurl.go @@ -14,8 +14,11 @@ import ( "encoding/base64" "errors" "fmt" + http_dialer "github.com/mwitkow/go-http-dialer" + "golang.org/x/net/proxy" "io/ioutil" "net" + "net/url" "os" "regexp" "sort" @@ -638,7 +641,23 @@ func BlockingDial(ctx context.Context, network, address string, creds credential // handshake). And that would mean that the library would send the // wrong ":scheme" metaheader to servers: it would send "http" instead // of "https" because it is unaware that TLS is actually in use. - conn, err := (&net.Dialer{}).DialContext(ctx, network, address) + + // Use HTTPS_PROXY if it's set up: + if os.Getenv("HTTPS_PROXY") != "" { + proxyUrl, err := url.Parse(os.Getenv("HTTPS_PROXY")) + if err != nil { + panic("Invalid HTTPS_PROXY environment variable") + } + + httpTunnel := http_dialer.New(proxyUrl) + conn, err := httpTunnel.Dial(network, address) + if err != nil { + writeResult(err) + } + return conn, err + } + + conn, err := proxy.Dial(ctx, network, address) if err != nil { writeResult(err) }