1
1
package http
2
2
3
3
import (
4
- "crypto/tls"
5
4
"expvar"
6
5
"fmt"
7
6
"io"
@@ -12,18 +11,32 @@ import (
12
11
13
12
"github.com/elastic/beats/libbeat/logp"
14
13
"github.com/elastic/beats/libbeat/outputs"
14
+ "github.com/elastic/beats/libbeat/outputs/outil"
15
15
"github.com/elastic/beats/libbeat/outputs/transport"
16
16
)
17
17
18
18
type Client struct {
19
19
Connection
20
- params map [string ]string
20
+ tlsConfig * transport.TLSConfig
21
+ params map [string ]string
21
22
22
23
// additional configs
23
24
compressionLevel int
24
25
proxyURL * url.URL
25
26
}
26
27
28
+ type ClientSettings struct {
29
+ URL string
30
+ Proxy * url.URL
31
+ TLS * transport.TLSConfig
32
+ Username , Password string
33
+ Parameters map [string ]string
34
+ Index outil.Selector
35
+ Pipeline * outil.Selector
36
+ Timeout time.Duration
37
+ CompressionLevel int
38
+ }
39
+
27
40
type Connection struct {
28
41
URL string
29
42
Username string
@@ -48,29 +61,38 @@ var (
48
61
)
49
62
50
63
func NewClient (
51
- hostURL string , proxyURL * url.URL , tls * tls.Config ,
52
- username , password string ,
53
- params map [string ]string ,
54
- timeout time.Duration ,
55
- compression int ,
64
+ s ClientSettings ,
56
65
) (* Client , error ) {
57
66
proxy := http .ProxyFromEnvironment
58
- if proxyURL != nil {
59
- proxy = http .ProxyURL (proxyURL )
67
+ if s . Proxy != nil {
68
+ proxy = http .ProxyURL (s . Proxy )
60
69
}
61
70
62
- logp .Info ("Http url: %s" , hostURL )
71
+ logp .Info ("Http url: %s" , s . URL )
63
72
64
- dialer := transport .NetDialer (timeout )
65
- dialer = transport .StatsDialer (dialer , & transport.IOStats {
73
+ // TODO: add socks5 proxy support
74
+ var dialer , tlsDialer transport.Dialer
75
+ var err error
76
+
77
+ dialer = transport .NetDialer (s .Timeout )
78
+ tlsDialer , err = transport .TLSDialer (dialer , s .TLS , s .Timeout )
79
+ if err != nil {
80
+ return nil , err
81
+ }
82
+
83
+ iostats := & transport.IOStats {
66
84
Read : statReadBytes ,
67
85
Write : statWriteBytes ,
68
86
ReadErrors : statReadErrors ,
69
87
WriteErrors : statWriteErrors ,
70
- })
88
+ }
89
+ dialer = transport .StatsDialer (dialer , iostats )
90
+ tlsDialer = transport .StatsDialer (tlsDialer , iostats )
91
+
92
+ params := s .Parameters
71
93
72
- var err error
73
94
var encoder bodyEncoder
95
+ compression := s .CompressionLevel
74
96
if compression == 0 {
75
97
encoder = newJSONEncoder (nil )
76
98
} else {
@@ -82,22 +104,23 @@ func NewClient(
82
104
83
105
client := & Client {
84
106
Connection : Connection {
85
- URL : hostURL ,
86
- Username : username ,
87
- Password : password ,
107
+ URL : s . URL ,
108
+ Username : s . Username ,
109
+ Password : s . Password ,
88
110
http : & http.Client {
89
111
Transport : & http.Transport {
90
- Dial : dialer .Dial ,
91
- TLSClientConfig : tls ,
92
- Proxy : proxy ,
112
+ Dial : dialer .Dial ,
113
+ DialTLS : tlsDialer . Dial ,
114
+ Proxy : proxy ,
93
115
},
94
- Timeout : timeout ,
116
+ Timeout : s . Timeout ,
95
117
},
96
118
encoder : encoder ,
97
119
},
98
120
params : params ,
99
121
100
- proxyURL : proxyURL ,
122
+ compressionLevel : compression ,
123
+ proxyURL : s .Proxy ,
101
124
}
102
125
103
126
return client , nil
@@ -108,17 +131,17 @@ func (client *Client) Clone() *Client {
108
131
// client's close is for example generated for topology-map support. With params
109
132
// most likely containing the ingest node pipeline and default callback trying to
110
133
// create install a template, we don't want these to be included in the clone.
111
-
112
- transport := client .http .Transport .(* http.Transport )
113
134
c , _ := NewClient (
114
- client .URL ,
115
- client .proxyURL ,
116
- transport .TLSClientConfig ,
117
- client .Username ,
118
- client .Password ,
119
- nil , // XXX: do not pass params?
120
- client .http .Timeout ,
121
- client .compressionLevel ,
135
+ ClientSettings {
136
+ URL : client .URL ,
137
+ Proxy : client .proxyURL ,
138
+ TLS : client .tlsConfig ,
139
+ Username : client .Username ,
140
+ Password : client .Password ,
141
+ Parameters : nil , // XXX: do not pass params?
142
+ Timeout : client .http .Timeout ,
143
+ CompressionLevel : client .compressionLevel ,
144
+ },
122
145
)
123
146
return c
124
147
}
0 commit comments