-
Notifications
You must be signed in to change notification settings - Fork 4
/
https.go
92 lines (76 loc) · 1.89 KB
/
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
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
package egress
import (
"bytes"
"fmt"
"io"
"net"
"net/http"
"net/url"
"time"
"code.cloudfoundry.org/go-loggregator/v9/rpc/loggregator_v2"
"code.cloudfoundry.org/loggregator-tools/syslog-forwarder/internal/egress/config"
)
// NetworkTimeoutConfig stores various timeout values.
type NetworkConfig struct {
Keepalive time.Duration
DialTimeout time.Duration
WriteTimeout time.Duration
SkipCertVerify bool
}
type HTTPSWriter struct {
hostname string
url *url.URL
client *http.Client
}
func NewHTTPSWriter(
binding *URLBinding,
netConf NetworkConfig,
) WriteCloser {
client := httpClient(netConf)
return &HTTPSWriter{
url: binding.URL,
hostname: binding.Hostname,
client: client,
}
}
func (w *HTTPSWriter) Write(env *loggregator_v2.Envelope) error {
msgs := generateRFC5424Messages(env, w.hostname, env.SourceId)
for _, msg := range msgs {
b, err := msg.MarshalBinary()
if err != nil {
return err
}
resp, err := w.client.Post(w.url.String(), "text/plain", bytes.NewBuffer(b))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return fmt.Errorf("Syslog Writer: Post responded with %d status code", resp.StatusCode)
}
io.Copy(io.Discard, resp.Body) //nolint:errcheck
}
return nil
}
func (*HTTPSWriter) Close() error {
return nil
}
func httpClient(netConf NetworkConfig) *http.Client {
tlsConfig := config.NewTLSConfig()
tlsConfig.InsecureSkipVerify = netConf.SkipCertVerify
tr := &http.Transport{
DialContext: (&net.Dialer{
Timeout: netConf.DialTimeout,
KeepAlive: netConf.Keepalive,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: tlsConfig,
}
return &http.Client{
Transport: tr,
Timeout: 60 * time.Second,
}
}