-
Notifications
You must be signed in to change notification settings - Fork 36
/
httpclient.go
78 lines (68 loc) · 1.9 KB
/
httpclient.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package httpclient
import (
"crypto/tls"
"crypto/x509"
"net"
"net/http"
"net/http/cookiejar"
"strings"
"time"
"golang.org/x/net/publicsuffix"
)
// SecureTLS disables InsecureSkipVerify and adds a cert pool to an HTTP client's
// TLS config
func SecureTLS(c *http.Client, rootCAs *x509.CertPool) {
if c == nil {
return
}
tp := DefaultTransport()
if c.Transport != nil {
if assertedTransport, ok := c.Transport.(*http.Transport); ok {
tp = assertedTransport
}
// otherwise, we overwrite the transport
}
tp.TLSClientConfig.InsecureSkipVerify = false
tp.TLSClientConfig.RootCAs = rootCAs
c.Transport = tp
}
// DefaultTransport sets an HTTP Transport
func DefaultTransport() *http.Transport {
return &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: true,
Dial: (&net.Dialer{
Timeout: 120 * time.Second,
KeepAlive: 120 * time.Second,
}).Dial,
TLSHandshakeTimeout: 120 * time.Second,
ResponseHeaderTimeout: 120 * time.Second,
}
}
// SecureTLSOption disables InsecureSkipVerify and adds a cert pool to an HTTP client's
// TLS config
func SecureTLSOption(rootCAs *x509.CertPool) func(*http.Client) {
return func(c *http.Client) {
SecureTLS(c, rootCAs)
}
}
// Build builds a client session with our default parameters
func Build(opts ...func(*http.Client)) *http.Client {
// we ignore the error here because cookiejar.New always returns nil
jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
client := &http.Client{
Timeout: time.Second * 120,
Transport: DefaultTransport(),
Jar: jar,
}
for _, opt := range opts {
if opt != nil {
opt(client)
}
}
return client
}
// StandardizeProcessorName makes the processor name standard across vendors
func StandardizeProcessorName(name string) string {
return strings.ToLower(strings.TrimSuffix(strings.TrimSpace(strings.Split(name, "@")[0]), " 0"))
}