Skip to content

Commit

Permalink
tls related fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rbarazzutti committed Dec 7, 2021
1 parent 7ce546f commit ee0b8de
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 54 deletions.
2 changes: 2 additions & 0 deletions app/httpping.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ func HTTPPing(config *Config) {
if len(latencies) > 0 {
fmt.Printf("%s\n", stats.PingStatsFromLatencies(latencies).String())
}

os.Exit(0)
}
67 changes: 31 additions & 36 deletions app/webclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {

Expand All @@ -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{
Expand All @@ -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
Expand Down
18 changes: 0 additions & 18 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit ee0b8de

Please sign in to comment.