From ee0b8dede44ae368815206e332da3ea434b4fcc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20P=2E=20Barazzutti?= Date: Tue, 7 Dec 2021 05:01:15 +0100 Subject: [PATCH] tls related fixes --- app/httpping.go | 2 ++ app/webclient.go | 67 ++++++++++++++++++++++-------------------------- cmd/root.go | 18 ------------- 3 files changed, 33 insertions(+), 54 deletions(-) diff --git a/app/httpping.go b/app/httpping.go index bbbd723..3caae41 100644 --- a/app/httpping.go +++ b/app/httpping.go @@ -70,4 +70,6 @@ func HTTPPing(config *Config) { if len(latencies) > 0 { fmt.Printf("%s\n", stats.PingStatsFromLatencies(latencies).String()) } + + os.Exit(0) } diff --git a/app/webclient.go b/app/webclient.go index b280ae2..6546236 100644 --- a/app/webclient.go +++ b/app/webclient.go @@ -26,13 +26,13 @@ func (webClient *WebClient) resolve(host string) (*net.IPAddr, error) { // WebClient represents an HTTP/S client designed to do performance analysis type WebClient struct { - connCounter *ConnCounter - httpClient *http.Client - reused bool - connTarget string - config *Config - url *url.URL - dialCtx func(ctx context.Context, network, addr string) (net.Conn, error) + connCounter *ConnCounter + httpClient *http.Client + reused bool + connTarget string + config *Config + url *url.URL + dialCtx, dialTLSCtx func(ctx context.Context, network, addr string) (net.Conn, error) } // NewWebClient builds a new instance of WebClient which will provides functions for Http-Ping @@ -59,6 +59,7 @@ func NewWebClient(config *Config) (*WebClient, error) { } dialer := &net.Dialer{} + dialerTLS := &tls.Dialer{} webClient.dialCtx = func(ctx context.Context, network, addr string) (net.Conn, error) { @@ -69,6 +70,21 @@ func NewWebClient(config *Config) (*WebClient, error) { return webClient.connCounter.Bind(conn), nil } + webClient.dialTLSCtx = func(ctx context.Context, network, addr string) (net.Conn, error) { + dialerTLS.Config = &tls.Config{ + InsecureSkipVerify: webClient.config.NoCheckCertificate, + ServerName: webClient.url.Hostname(), + } + + conn, err := dialerTLS.DialContext(ctx, network, webClient.connTarget) + + if err != nil { + return conn, err + } + + return webClient.connCounter.Bind(conn), nil + } + jar, _ := cookiejar.New(nil) webClient.httpClient = &http.Client{ @@ -80,35 +96,14 @@ func NewWebClient(config *Config) (*WebClient, error) { } webClient.httpClient.Transport = &http.Transport{ - Proxy: http.ProxyFromEnvironment, - DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { - dialer := &net.Dialer{} - conn, err := dialer.DialContext(ctx, network, webClient.connTarget) - if err != nil { - return conn, err - } - return webClient.connCounter.Bind(conn), nil - }, - DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) { - netDialer := &net.Dialer{} - - tlsDialer := &tls.Dialer{ - NetDialer: netDialer, - } - conn, err := tlsDialer.DialContext(ctx, network, addr) - if err != nil { - return conn, err - } - return conn, err - }, - - ForceAttemptHTTP2: true, - MaxIdleConns: 100, - DisableKeepAlives: webClient.config.DisableKeepAlive, - IdleConnTimeout: 90 * time.Second, - TLSHandshakeTimeout: 10 * time.Second, - ExpectContinueTimeout: 1 * time.Second, - TLSClientConfig: &tls.Config{InsecureSkipVerify: webClient.config.NoCheckCertificate}, + Proxy: http.ProxyFromEnvironment, + DialContext: webClient.dialCtx, + DialTLSContext: webClient.dialTLSCtx, + + ForceAttemptHTTP2: true, + MaxIdleConns: 1, + DisableKeepAlives: webClient.config.DisableKeepAlive, + IdleConnTimeout: config.Interval + config.Wait, } var cookies []*http.Cookie diff --git a/cmd/root.go b/cmd/root.go index 14d5067..4d14871 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -112,24 +112,6 @@ func prepareRootCmd() *cobra.Command { } config.Target = args[0] - //ch := app.HTTPPingE(&config) - // - //count := 0 - //failures := 0 - //for measure := range ch { - // if !measure.IsFailure { - // if config.LogLevel == 1 { - // fmt.Printf("%4d: code=%d size=%d time=%.2f ms\n", count, measure.StatusCode, measure.Bytes, float64(measure.Duration.Nanoseconds())/1e6) - // } else if config.LogLevel == 2 { - // fmt.Printf("%4d: code=%d conn-reused=%t size=%d in=%d out=%d time=%.2f ms\n", count, measure.StatusCode, measure.SocketReused, measure.Bytes, measure.InBytes, measure.OutBytes, float64(measure.Duration.Nanoseconds())/1e6) - // } - // } else { - // fmt.Printf("%4d: %s\n", count, measure.FailureCause) - // failures++ - // } - // count++ - // - //} app.HTTPPing(&config)