-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtls.go
More file actions
55 lines (46 loc) · 1.02 KB
/
tls.go
File metadata and controls
55 lines (46 loc) · 1.02 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
package humcommon
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"net/http"
"time"
)
var transport *http.Transport
func GetHTTPClient() *http.Client {
return &http.Client{
Timeout: 10 * time.Second,
Transport: transport,
}
}
func InitTLS() error {
if transport != nil {
return nil
}
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
}
certPath := AppConfig.TLS.Cert
keyPath := AppConfig.TLS.Key
if certPath != "" && keyPath != "" {
mainCert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
return err
}
tlsConfig.Certificates = []tls.Certificate{mainCert}
tlsConfig.BuildNameToCertificate()
} else {
logger.Warn("Unable to init client certificates: either cert or key missing")
}
if AppConfig.TLS.CA != "" {
caCert, err := ioutil.ReadFile(AppConfig.TLS.CA)
if err != nil {
return err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig.RootCAs = caCertPool
}
transport = &http.Transport{TLSClientConfig: tlsConfig}
return nil
}