-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathdialer.go
269 lines (230 loc) · 6.36 KB
/
dialer.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
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
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2016 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package netext
import (
"context"
"fmt"
"net"
"strconv"
"sync/atomic"
"time"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/metrics"
"go.k6.io/k6/lib/types"
"go.k6.io/k6/stats"
)
// Dialer wraps net.Dialer and provides k6 specific functionality -
// tracing, blacklists and DNS cache and aliases.
type Dialer struct {
net.Dialer
Resolver Resolver
Blacklist []*lib.IPNet
BlockedHostnames *types.HostnameTrie
Hosts map[string]*lib.HostAddress
BytesRead int64
BytesWritten int64
}
// NewDialer constructs a new Dialer with the given DNS resolver.
func NewDialer(dialer net.Dialer, resolver Resolver) *Dialer {
return &Dialer{
Dialer: dialer,
Resolver: resolver,
}
}
// BlackListedIPError is an error that is returned when a given IP is blacklisted
type BlackListedIPError struct {
ip net.IP
net *lib.IPNet
}
func (b BlackListedIPError) Error() string {
return fmt.Sprintf("IP (%s) is in a blacklisted range (%s)", b.ip, b.net)
}
// BlockedHostError is returned when a given hostname is blocked
type BlockedHostError struct {
hostname string
match string
}
func (b BlockedHostError) Error() string {
return fmt.Sprintf("hostname (%s) is in a blocked pattern (%s)", b.hostname, b.match)
}
// DialContext wraps the net.Dialer.DialContext and handles the k6 specifics
func (d *Dialer) DialContext(ctx context.Context, proto, addr string) (net.Conn, error) {
dialAddr, err := d.getDialAddr(addr)
if err != nil {
return nil, err
}
conn, err := d.Dialer.DialContext(ctx, proto, dialAddr)
if err != nil {
return nil, err
}
conn = &Conn{conn, &d.BytesRead, &d.BytesWritten}
return conn, err
}
// GetTrail creates a new NetTrail instance with the Dialer
// sent and received data metrics and the supplied times and tags.
// TODO: Refactor this according to
// https://github.com/k6io/k6/pull/1203#discussion_r337938370
func (d *Dialer) GetTrail(
startTime, endTime time.Time, fullIteration bool, emitIterations bool, tags *stats.SampleTags,
builtinMetrics *metrics.BuiltinMetrics,
) *NetTrail {
bytesWritten := atomic.SwapInt64(&d.BytesWritten, 0)
bytesRead := atomic.SwapInt64(&d.BytesRead, 0)
samples := []stats.Sample{
{
Time: endTime,
Metric: builtinMetrics.DataSent,
Value: float64(bytesWritten),
Tags: tags,
},
{
Time: endTime,
Metric: builtinMetrics.DataReceived,
Value: float64(bytesRead),
Tags: tags,
},
}
if fullIteration {
samples = append(samples, stats.Sample{
Time: endTime,
Metric: builtinMetrics.IterationDuration,
Value: stats.D(endTime.Sub(startTime)),
Tags: tags,
})
if emitIterations {
samples = append(samples, stats.Sample{
Time: endTime,
Metric: builtinMetrics.Iterations,
Value: 1,
Tags: tags,
})
}
}
return &NetTrail{
BytesRead: bytesRead,
BytesWritten: bytesWritten,
FullIteration: fullIteration,
StartTime: startTime,
EndTime: endTime,
Tags: tags,
Samples: samples,
}
}
func (d *Dialer) getDialAddr(addr string) (string, error) {
remote, err := d.findRemote(addr)
if err != nil {
return "", err
}
for _, ipnet := range d.Blacklist {
if ipnet.Contains(remote.IP) {
return "", BlackListedIPError{ip: remote.IP, net: ipnet}
}
}
return remote.String(), nil
}
func (d *Dialer) findRemote(addr string) (*lib.HostAddress, error) {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
ip := net.ParseIP(host)
if d.BlockedHostnames != nil && ip == nil {
if match, blocked := d.BlockedHostnames.Contains(host); blocked {
return nil, BlockedHostError{hostname: host, match: match}
}
}
remote, err := d.getConfiguredHost(addr, host, port)
if err != nil || remote != nil {
return remote, err
}
if ip != nil {
return lib.NewHostAddress(ip, port)
}
ip, err = d.Resolver.LookupIP(host)
if err != nil {
return nil, err
}
if ip == nil {
return nil, fmt.Errorf("lookup %s: no such host", host)
}
return lib.NewHostAddress(ip, port)
}
func (d *Dialer) getConfiguredHost(addr, host, port string) (*lib.HostAddress, error) {
if remote, ok := d.Hosts[addr]; ok {
return remote, nil
}
if remote, ok := d.Hosts[host]; ok {
if remote.Port != 0 || port == "" {
return remote, nil
}
newPort, err := strconv.Atoi(port)
if err != nil {
return nil, err
}
newRemote := *remote
newRemote.Port = newPort
return &newRemote, nil
}
return nil, nil
}
// NetTrail contains information about the exchanged data size and length of a
// series of connections from a particular netext.Dialer
type NetTrail struct {
BytesRead int64
BytesWritten int64
FullIteration bool
StartTime time.Time
EndTime time.Time
Tags *stats.SampleTags
Samples []stats.Sample
}
// Ensure that interfaces are implemented correctly
var _ stats.ConnectedSampleContainer = &NetTrail{}
// GetSamples implements the stats.SampleContainer interface.
func (ntr *NetTrail) GetSamples() []stats.Sample {
return ntr.Samples
}
// GetTags implements the stats.ConnectedSampleContainer interface.
func (ntr *NetTrail) GetTags() *stats.SampleTags {
return ntr.Tags
}
// GetTime implements the stats.ConnectedSampleContainer interface.
func (ntr *NetTrail) GetTime() time.Time {
return ntr.EndTime
}
// Conn wraps net.Conn and keeps track of sent and received data size
type Conn struct {
net.Conn
BytesRead, BytesWritten *int64
}
func (c *Conn) Read(b []byte) (int, error) {
n, err := c.Conn.Read(b)
if n > 0 {
atomic.AddInt64(c.BytesRead, int64(n))
}
return n, err
}
func (c *Conn) Write(b []byte) (int, error) {
n, err := c.Conn.Write(b)
if n > 0 {
atomic.AddInt64(c.BytesWritten, int64(n))
}
return n, err
}