-
Notifications
You must be signed in to change notification settings - Fork 5
/
conn_https.go
66 lines (61 loc) · 1.36 KB
/
conn_https.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
package net
import (
"bytes"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"
"sync"
"github.com/pbergman/logger"
)
type HttpsConnPool struct {
config *tls.Config
HttpConnPool
}
func (p HttpsConnPool) Start(workers int) {
p.clients = make([]*http.Client, workers)
for i := 0; i < workers; i++ {
p.wg.Add(1)
p.clients[i] = &http.Client{
Transport: &http.Transport{
TLSClientConfig: p.config,
},
}
go p.process(p.clients[i], &p.wg)
}
}
func NewHttpsConnPool(tries int, host *GraylogHost, ca, crt, pem string, logger *logger.Logger) (ConnPoolInterface, error) {
buf, err := ioutil.ReadFile(ca)
if err != nil {
return nil, err
}
logger.Debug(fmt.Sprintf("loaded ca root certificate: '%s'", ca))
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(buf)
pair, err := tls.LoadX509KeyPair(crt, pem)
if err != nil {
return nil, err
}
logger.Debug(fmt.Sprintf("loaded certificate: '%s'", crt))
logger.Debug(fmt.Sprintf("loaded private key: '%s'", pem))
return &HttpsConnPool{
HttpConnPool: HttpConnPool{
host: host,
logger: logger,
pool: &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
},
connQueue: connQueue{
tries: tries,
queue: make(chan *ConnQueueItem, 10),
},
},
config: &tls.Config{
RootCAs: certPool,
Certificates: []tls.Certificate{pair},
},
}, nil
}