From bd58f3c7476021e0343749849411f511ab9a93f5 Mon Sep 17 00:00:00 2001 From: Tufan Baris Yildirim Date: Wed, 18 Sep 2019 13:10:53 +0300 Subject: [PATCH 1/4] force dialer to connect the host specified in req.URL.Host not in req.Host! --- utils/utils.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/utils/utils.go b/utils/utils.go index 348cb02f..91a64bda 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -16,6 +16,7 @@ package utils import ( + "context" "crypto/tls" "errors" "fmt" @@ -24,6 +25,7 @@ import ( "io/ioutil" "math" "math/rand" + "net" "net/http" "os" "os/exec" @@ -298,6 +300,11 @@ func HttpRequest(url, method string, content interface{}, headers []string, body } var resp *http.Response + dialer := &net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + } + transport := &http.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: !verifySSL, @@ -307,6 +314,11 @@ func HttpRequest(url, method string, content interface{}, headers []string, body ResponseHeaderTimeout: timeout, TLSHandshakeTimeout: timeout, Proxy: http.ProxyFromEnvironment, + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + // redirect all connections to host specified in url + addr = req.URL.Host + return dialer.DialContext(ctx, network, addr) + }, } client := &http.Client{ Transport: transport, From 1140dfa2133b481759c957fddf2c5a5780bf0d1a Mon Sep 17 00:00:00 2001 From: Tufan Baris Yildirim Date: Wed, 18 Sep 2019 13:29:44 +0300 Subject: [PATCH 2/4] add dialer port --- utils/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/utils.go b/utils/utils.go index 91a64bda..b71d4569 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -316,7 +316,7 @@ func HttpRequest(url, method string, content interface{}, headers []string, body Proxy: http.ProxyFromEnvironment, DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { // redirect all connections to host specified in url - addr = req.URL.Host + addr = strings.Split(req.URL.Host, ":")[0] + addr[strings.LastIndex(addr, ":"):] return dialer.DialContext(ctx, network, addr) }, } From a6718f884308b17f7bf40d85dee923f2cc4a833e Mon Sep 17 00:00:00 2001 From: Tufan Baris Yildirim Date: Wed, 18 Sep 2019 13:39:10 +0300 Subject: [PATCH 3/4] use user defined timeout instead --- utils/utils.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index b71d4569..2d7fcd94 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -301,8 +301,8 @@ func HttpRequest(url, method string, content interface{}, headers []string, body var resp *http.Response dialer := &net.Dialer{ - Timeout: 30 * time.Second, - KeepAlive: 30 * time.Second, + Timeout: timeout * time.Second, + KeepAlive: timeout * time.Second, } transport := &http.Transport{ From 206a90cc089c13b4c29512a23d46d048bb5e7098 Mon Sep 17 00:00:00 2001 From: Tufan Baris Yildirim Date: Sun, 22 Sep 2019 16:36:40 +0300 Subject: [PATCH 4/4] fix timeout value --- utils/utils.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index 2d7fcd94..4fdee729 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -301,8 +301,8 @@ func HttpRequest(url, method string, content interface{}, headers []string, body var resp *http.Response dialer := &net.Dialer{ - Timeout: timeout * time.Second, - KeepAlive: timeout * time.Second, + Timeout: timeout, + KeepAlive: timeout, } transport := &http.Transport{