-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcf_http.go
More file actions
105 lines (87 loc) · 2.61 KB
/
Copy pathcf_http.go
File metadata and controls
105 lines (87 loc) · 2.61 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package cfhttp
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"sync/atomic"
"time"
"code.cloudfoundry.org/cfhttp/unix_transport"
)
var SUPPORTED_CIPHER_SUITES = []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
}
var config Config
type Config struct {
Timeout time.Duration
}
func Initialize(timeout time.Duration) {
atomic.StoreInt64((*int64)(&config.Timeout), int64(timeout))
}
func NewClient() *http.Client {
return newClient(5*time.Second, 0*time.Second, 90*time.Second, time.Duration(atomic.LoadInt64((*int64)(&config.Timeout))))
}
func NewUnixClient(socketPath string) *http.Client {
return &http.Client{
Transport: unix_transport.NewWithDial(socketPath,
(&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 0 * time.Second,
}).Dial),
Timeout: time.Duration(atomic.LoadInt64((*int64)(&config.Timeout))),
}
}
func NewCustomTimeoutClient(customTimeout time.Duration) *http.Client {
return newClient(5*time.Second, 0*time.Second, 90*time.Second, customTimeout)
}
func NewStreamingClient() *http.Client {
return newClient(5*time.Second, 30*time.Second, 90*time.Second, 0*time.Second)
}
func newClient(dialTimeout, keepAliveTimeout, idleConnTimeout, timeout time.Duration) *http.Client {
return &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: dialTimeout,
KeepAlive: keepAliveTimeout,
}).DialContext,
IdleConnTimeout: idleConnTimeout,
},
Timeout: timeout,
}
}
func NewTLSConfig(certFile, keyFile, caCertFile string) (*tls.Config, error) {
caCertPool := x509.NewCertPool()
if caCertFile != "" {
certBytes, err := ioutil.ReadFile(caCertFile)
if err != nil {
return nil, fmt.Errorf("failed read ca cert file: %s", err.Error())
}
if ok := caCertPool.AppendCertsFromPEM(certBytes); !ok {
return nil, errors.New("Unable to load caCert")
}
}
return NewTLSConfigWithCertPool(certFile, keyFile, caCertPool)
}
func NewTLSConfigWithCertPool(certFile, keyFile string, caCertPool *x509.CertPool) (*tls.Config, error) {
tlsCert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, fmt.Errorf("failed to load keypair: %s", err.Error())
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{tlsCert},
InsecureSkipVerify: false,
ClientAuth: tls.RequireAndVerifyClientCert,
CipherSuites: SUPPORTED_CIPHER_SUITES,
MinVersion: tls.VersionTLS12,
}
if caCertPool == nil {
return nil, fmt.Errorf("CaCertPool is nil")
}
tlsConfig.RootCAs = caCertPool
tlsConfig.ClientCAs = caCertPool
return tlsConfig, nil
}