0
0
mirror of https://github.com/thegeeklab/wp-plugin-go.git synced 2024-11-14 19:40:39 +00:00
wp-plugin-go/pkg/urfave/network.go

90 lines
2.1 KiB
Go
Raw Normal View History

2019-10-11 18:37:45 +00:00
// Copyright (c) 2019, the Drone Plugins project authors.
// Please see the AUTHORS file for details. All rights reserved.
// Use of this source code is governed by an Apache 2.0 license that can be
// found in the LICENSE file.
package urfave
import (
"context"
2019-10-11 23:20:53 +00:00
"crypto/tls"
2019-10-11 18:37:45 +00:00
"net"
"net/http"
"time"
2019-10-11 22:50:57 +00:00
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
2019-10-11 18:37:45 +00:00
)
//---------------------------------------------------------------------
// Transport Flags
//---------------------------------------------------------------------
// Network contains options for connecting to the network.
type Network struct {
// Context for making network requests.
//
// If `trace` logging is requested the context will use `httptrace` to
// capture all network requests.
Context context.Context
// Client for making network requests.
Client *http.Client
/// Whether SSL verification is skipped
SkipVerify bool
2019-10-11 18:37:45 +00:00
}
2019-10-11 23:20:53 +00:00
const networkSkipVerifyFlag = "transport.skip-verify"
2019-10-11 18:37:45 +00:00
// networkFlags has the cli.Flags for the Transport.
func networkFlags() []cli.Flag {
return []cli.Flag{
&cli.BoolFlag{
Name: networkSkipVerifyFlag,
Usage: "skip ssl verify",
EnvVars: []string{"PLUGIN_SKIP_VERIFY"},
2019-10-11 18:37:45 +00:00
},
}
}
// NetworkFromContext creates a Transport from the cli.Context.
func NetworkFromContext(ctx *cli.Context) Network {
// Create the client
2019-10-11 23:20:53 +00:00
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
skipVerify := ctx.Bool(networkSkipVerifyFlag)
if skipVerify {
2019-10-11 23:20:53 +00:00
logrus.Warning("ssl verification is turned off")
transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
2019-10-11 18:37:45 +00:00
}
// Create the context
context := context.Background()
2019-10-11 22:50:57 +00:00
if ctx.String(logLevelFlag) == logrus.TraceLevel.String() {
2019-10-11 18:37:45 +00:00
context = traceHTTP(context)
}
return Network{
2019-10-11 23:20:53 +00:00
Client: &http.Client{
Transport: transport,
},
Context: context,
SkipVerify: skipVerify,
2019-10-11 18:37:45 +00:00
}
}