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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/*
* This file is part of Go Responsiveness.
*
* Go Responsiveness is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
* Go Responsiveness is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with Foobar. If not, see <https://www.gnu.org/licenses/>.
*/
package lgc
import (
"context"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptrace"
"sync/atomic"
"time"
"github.com/network-quality/goresponsiveness/utilities"
"golang.org/x/net/http2"
)
var GenerateConnectionId func() uint64 = func() func() uint64 {
var nextConnectionId uint64 = 0
return func() uint64 {
return atomic.AddUint64(&nextConnectionId, 1)
}
}()
type LoadGeneratingConnection interface {
Start(context.Context, bool) bool
Transferred() uint64
Client() *http.Client
IsValid() bool
ClientId() uint64
}
type LoadGeneratingConnectionStats struct {
dnsStart httptrace.DNSStartInfo
dnsDone httptrace.DNSDoneInfo
connInfo httptrace.GotConnInfo
httpInfo httptrace.WroteRequestInfo
tlsConnInfo tls.ConnectionState
dnsStartTime time.Time
dnsCompleteTime time.Time
tlsStartTime time.Time
tlsCompleteTime time.Time
connectStartTime time.Time
connectCompleteTime time.Time
getConnectionStartTime time.Time
getConnectionCompleteTime time.Time
}
type LoadGeneratingConnectionDownload struct {
Path string
downloaded uint64
client *http.Client
debug bool
valid bool
KeyLogger io.Writer
clientId uint64
tracer *httptrace.ClientTrace
stats LoadGeneratingConnectionStats
}
func generateHttpTimingTracer(
lgd *LoadGeneratingConnectionDownload,
) *httptrace.ClientTrace {
newTracer := httptrace.ClientTrace{
DNSStart: func(dnsStartInfo httptrace.DNSStartInfo) {
lgd.stats.dnsStartTime = time.Now()
lgd.stats.dnsStart = dnsStartInfo
if lgd.debug {
fmt.Printf(
"DNS Start for %v: %v\n",
lgd.ClientId(),
dnsStartInfo,
)
}
},
DNSDone: func(dnsDoneInfo httptrace.DNSDoneInfo) {
lgd.stats.dnsCompleteTime = time.Now()
lgd.stats.dnsDone = dnsDoneInfo
if lgd.debug {
fmt.Printf("DNS Done for %v: %v\n", lgd.ClientId(), dnsDoneInfo)
}
},
ConnectStart: func(network, address string) {
lgd.stats.connectStartTime = time.Now()
if lgd.debug {
fmt.Printf(
"TCP Start of %v: %v: %v\n",
lgd.ClientId(),
network,
address,
)
}
},
ConnectDone: func(network, address string, err error) {
lgd.stats.connectCompleteTime = time.Now()
if lgd.debug {
fmt.Printf(
"TCP Done for %v: %v: %v (%v)\n",
lgd.ClientId(),
network,
address,
err,
)
}
},
GetConn: func(hostPort string) {
lgd.stats.getConnectionStartTime = time.Now()
if lgd.debug {
fmt.Printf(
"GetConn host port for %v: %v\n",
lgd.ClientId(),
hostPort,
)
}
},
GotConn: func(connInfo httptrace.GotConnInfo) {
if connInfo.Reused {
panic(!connInfo.Reused)
}
lgd.stats.connInfo = connInfo
lgd.stats.getConnectionCompleteTime = time.Now()
if lgd.debug {
fmt.Printf(
"GetConn host port for %v: %v\n",
lgd.ClientId(),
connInfo,
)
}
},
TLSHandshakeStart: func() {
lgd.stats.tlsStartTime = time.Now()
if lgd.debug {
fmt.Printf("TLSHandshakeStart for %v\n", lgd.ClientId())
}
},
TLSHandshakeDone: func(tlsConnState tls.ConnectionState, err error) {
lgd.stats.tlsCompleteTime = time.Now()
lgd.stats.tlsConnInfo = tlsConnState
if lgd.debug {
fmt.Printf(
"TLSHandshakeDone for %v: %v\n",
lgd.ClientId(),
tlsConnState,
)
}
},
}
return &newTracer
}
func (lbd *LoadGeneratingConnectionDownload) ClientId() uint64 {
return lbd.clientId
}
func (lbd *LoadGeneratingConnectionDownload) Transferred() uint64 {
transferred := atomic.LoadUint64(&lbd.downloaded)
if lbd.debug {
fmt.Printf("download: Transferred: %v\n", transferred)
}
return transferred
}
func (lbd *LoadGeneratingConnectionDownload) Client() *http.Client {
return lbd.client
}
type countingReader struct {
n *uint64
ctx context.Context
readable io.Reader
}
func (cr *countingReader) Read(p []byte) (n int, err error) {
if cr.ctx.Err() != nil {
return 0, io.EOF
}
n, err = cr.readable.Read(p)
atomic.AddUint64(cr.n, uint64(n))
return
}
func (lbd *LoadGeneratingConnectionDownload) Start(
ctx context.Context,
debug bool,
) bool {
lbd.downloaded = 0
lbd.clientId = GenerateConnectionId()
transport := http2.Transport{}
if !utilities.IsInterfaceNil(lbd.KeyLogger) {
if debug {
fmt.Printf(
"Using an SSL Key Logger for this load-generating download.\n",
)
}
// The presence of a custom TLSClientConfig in a *generic* `transport`
// means that go will default to HTTP/1.1 and cowardly avoid HTTP/2:
// https://github.com/golang/go/blob/7ca6902c171b336d98adbb103d701a013229c806/src/net/http/transport.go#L278
// Also, it would appear that the API's choice of HTTP vs HTTP2 can
// depend on whether the url contains
// https:// or http://:
// https://github.com/golang/go/blob/7ca6902c171b336d98adbb103d701a013229c806/src/net/http/transport.go#L74
transport.TLSClientConfig = &tls.Config{
KeyLogWriter: lbd.KeyLogger,
InsecureSkipVerify: true,
}
}
lbd.client = &http.Client{Transport: &transport}
lbd.debug = debug
lbd.valid = true
lbd.tracer = generateHttpTimingTracer(lbd)
if debug {
fmt.Printf(
"Started a load-generating download (id: %v).\n",
lbd.clientId,
)
}
go lbd.doDownload(ctx)
return true
}
func (lbd *LoadGeneratingConnectionDownload) IsValid() bool {
return lbd.valid
}
func (lbd *LoadGeneratingConnectionDownload) doDownload(ctx context.Context) {
newRequest, err := http.NewRequestWithContext(
httptrace.WithClientTrace(ctx, lbd.tracer),
"GET",
lbd.Path,
nil,
)
get, err := lbd.client.Do(newRequest)
if err != nil {
lbd.valid = false
return
}
cr := &countingReader{n: &lbd.downloaded, ctx: ctx, readable: get.Body}
_, _ = io.Copy(ioutil.Discard, cr)
get.Body.Close()
if lbd.debug {
fmt.Printf("Ending a load-generating download.\n")
}
}
type LoadGeneratingConnectionUpload struct {
Path string
uploaded uint64
client *http.Client
debug bool
valid bool
KeyLogger io.Writer
clientId uint64
}
func (lbu *LoadGeneratingConnectionUpload) ClientId() uint64 {
return lbu.clientId
}
func (lbu *LoadGeneratingConnectionUpload) Transferred() uint64 {
transferred := atomic.LoadUint64(&lbu.uploaded)
if lbu.debug {
fmt.Printf("upload: Transferred: %v\n", transferred)
}
return transferred
}
func (lbu *LoadGeneratingConnectionUpload) Client() *http.Client {
return lbu.client
}
func (lbu *LoadGeneratingConnectionUpload) IsValid() bool {
return lbu.valid
}
type syntheticCountingReader struct {
n *uint64
ctx context.Context
}
func (s *syntheticCountingReader) Read(p []byte) (n int, err error) {
if s.ctx.Err() != nil {
return 0, io.EOF
}
err = nil
n = len(p)
atomic.AddUint64(s.n, uint64(n))
return
}
func (lbu *LoadGeneratingConnectionUpload) doUpload(ctx context.Context) bool {
lbu.uploaded = 0
s := &syntheticCountingReader{n: &lbu.uploaded, ctx: ctx}
var resp *http.Response = nil
var err error
if resp, err = lbu.client.Post(lbu.Path, "application/octet-stream", s); err != nil {
lbu.valid = false
}
resp.Body.Close()
if lbu.debug {
fmt.Printf("Ending a load-generating upload.\n")
}
return true
}
func (lbu *LoadGeneratingConnectionUpload) Start(
ctx context.Context,
debug bool,
) bool {
lbu.uploaded = 0
lbu.clientId = GenerateConnectionId()
// See above for the rationale of doing http2.Transport{} here
// to ensure that we are using h2.
transport := http2.Transport{}
if !utilities.IsInterfaceNil(lbu.KeyLogger) {
if debug {
fmt.Printf(
"Using an SSL Key Logger for this load-generating upload.\n",
)
}
transport.TLSClientConfig = &tls.Config{
KeyLogWriter: lbu.KeyLogger,
InsecureSkipVerify: true,
}
}
lbu.client = &http.Client{Transport: &transport}
lbu.debug = debug
lbu.valid = true
if debug {
fmt.Printf("Started a load-generating upload (id: %v).\n", lbu.clientId)
}
go lbu.doUpload(ctx)
return true
}
|