From 861c9c80f6b91f47b63516b3e145805e3fa99ba5 Mon Sep 17 00:00:00 2001 From: Jeff Gu Date: Tue, 9 May 2017 23:10:20 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Http=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81=E3=80=82=E9=9C=80=E8=A6=81=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E4=BB=A3=E7=90=86=E5=8F=AA=E9=9C=80=E8=A6=81=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8FHTTP=5FPROXY?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- qpm/core/context.go | 49 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/qpm/core/context.go b/qpm/core/context.go index 4a3f7be..4401187 100644 --- a/qpm/core/context.go +++ b/qpm/core/context.go @@ -2,12 +2,17 @@ package core import ( "fmt" - "google.golang.org/grpc" - "google.golang.org/grpc/credentials" "log" + "net" + "net/url" "os" - msg "qpm.io/common/messages" "runtime" + "time" + + "github.com/mwitkow/go-http-dialer" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials" + msg "qpm.io/common/messages" ) var ( @@ -38,17 +43,43 @@ func NewContext() *Context { address = Address } - noTls := os.Getenv("NO_TLS") == "1" + // noTls := os.Getenv("NO_TLS") == "1" - var tlsOption grpc.DialOption - if noTls { - tlsOption = grpc.WithInsecure() + // var tlsOption grpc.DialOption + // if noTls { + // tlsOption = grpc.WithInsecure() + // } else { + // tlsOption = grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")) + // } + + // conn, err := grpc.Dial(address, tlsOption, grpc.WithUserAgent(UA)) + + opts := make([]grpc.DialOption, 0) + opts = append(opts, grpc.WithUserAgent(UA)) + + noTLS := os.Getenv("NO_TLS") == "1" + if noTLS { + opts = append(opts, grpc.WithInsecure()) } else { - tlsOption = grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")) + opts = append(opts, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, ""))) } - conn, err := grpc.Dial(address, tlsOption, grpc.WithUserAgent(UA)) + httpProxy := os.Getenv("HTTP_PROXY") + if httpProxy != "" { + log.Println("env: ", httpProxy) + httpProxyURL, err := url.Parse(httpProxy) + if err != nil { + log.Fatalf("did not get http proxy: %v", err) + } else { + proxyDialer := http_dialer.New(httpProxyURL) + opts = append(opts, grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) { return proxyDialer.Dial("tcp", addr) })) + } + } + conn, err := grpc.Dial(address, opts...) + if err != nil { + log.Fatalf("did not connect: %v", err) + } if err != nil { log.Fatalf("did not connect: %v", err) } From 0a34a58b2989dc6ab6d1bbf4e57d5ef85592a1cd Mon Sep 17 00:00:00 2001 From: Jeff Gu Date: Wed, 17 May 2017 15:10:45 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0go-http-dialer=E7=9A=84?= =?UTF-8?q?=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitmodules | 4 ++++ vendor/github.com/mwitkow/go-http-dialer | 1 + 2 files changed, 5 insertions(+) create mode 160000 vendor/github.com/mwitkow/go-http-dialer diff --git a/.gitmodules b/.gitmodules index 79ed2e4..1d6dfe0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -13,3 +13,7 @@ [submodule "crypto"] path = vendor/golang.org/x/crypto url = https://go.googlesource.com/crypto +[submodule "go-http-dialer"] + path = vendor/github.com/mwitkow/go-http-dialer + url = https://github.com/mwitkow/go-http-dialer + diff --git a/vendor/github.com/mwitkow/go-http-dialer b/vendor/github.com/mwitkow/go-http-dialer new file mode 160000 index 0000000..378f744 --- /dev/null +++ b/vendor/github.com/mwitkow/go-http-dialer @@ -0,0 +1 @@ +Subproject commit 378f744fb2b81a6b96e3f40cde4f3bcab5a9cff0