-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
dialer.go
197 lines (165 loc) · 4.33 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
package netext
import (
"context"
"fmt"
"net"
"strconv"
"sync/atomic"
"time"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/types"
"go.k6.io/k6/metrics"
)
// 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 *types.Hosts
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
}
// IOSamples returns samples for data send and received since it last call and zeros out.
// It uses the provided time as the sample time and tags and builtinMetrics to build the samples.
func (d *Dialer) IOSamples(
sampleTime time.Time, ctm metrics.TagsAndMeta, builtinMetrics *metrics.BuiltinMetrics,
) metrics.SampleContainer {
bytesWritten := atomic.SwapInt64(&d.BytesWritten, 0)
bytesRead := atomic.SwapInt64(&d.BytesRead, 0)
return metrics.Samples([]metrics.Sample{
{
TimeSeries: metrics.TimeSeries{
Metric: builtinMetrics.DataSent,
Tags: ctm.Tags,
},
Time: sampleTime,
Metadata: ctm.Metadata,
Value: float64(bytesWritten),
},
{
TimeSeries: metrics.TimeSeries{
Metric: builtinMetrics.DataReceived,
Tags: ctm.Tags,
},
Time: sampleTime,
Metadata: ctm.Metadata,
Value: float64(bytesRead),
},
})
}
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) (*types.Host, 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}
}
}
if d.Hosts != nil {
remote, e := d.getConfiguredHost(addr, host, port)
if e != nil || remote != nil {
return remote, e
}
}
if ip != nil {
return types.NewHost(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 types.NewHost(ip, port)
}
func (d *Dialer) getConfiguredHost(addr, host, port string) (*types.Host, error) {
if remote := d.Hosts.Match(addr); remote != nil {
return remote, nil
}
if remote := d.Hosts.Match(host); remote != nil {
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 //nolint:nilnil
}
// 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
}