forked from cloudfoundry/bosh-utils
/
default_http_clients.go
55 lines (44 loc) · 1.24 KB
/
default_http_clients.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package httpclient
import (
"crypto/tls"
"crypto/x509"
"net"
"net/http"
"time"
proxy "github.com/cloudfoundry/socks5-proxy"
)
var (
DefaultClient = CreateDefaultClientInsecureSkipVerify()
defaultDialer = SOCKS5DialFuncFromEnvironment((&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial, proxy.NewSocks5Proxy(proxy.NewHostKeyGetter("")))
)
type Client interface {
Do(*http.Request) (*http.Response, error)
}
func CreateDefaultClient(certPool *x509.CertPool) *http.Client {
insecureSkipVerify := false
return factory{}.New(insecureSkipVerify, certPool)
}
func CreateDefaultClientInsecureSkipVerify() *http.Client {
insecureSkipVerify := true
return factory{}.New(insecureSkipVerify, nil)
}
type factory struct{}
func (f factory) New(insecureSkipVerify bool, certPool *x509.CertPool) *http.Client {
client := &http.Client{
Transport: &http.Transport{
TLSNextProto: map[string]func(authority string, c *tls.Conn) http.RoundTripper{},
TLSClientConfig: &tls.Config{
RootCAs: certPool,
InsecureSkipVerify: insecureSkipVerify,
},
Proxy: http.ProxyFromEnvironment,
Dial: defaultDialer,
TLSHandshakeTimeout: 30 * time.Second,
DisableKeepAlives: true,
},
}
return client
}