forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
111 lines (94 loc) · 3.3 KB
/
util.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
package dialchain
import (
"net"
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/outputs/transport"
)
type timer struct {
s, e time.Time
}
// IDLayer creates an empty placeholder layer.
func IDLayer() Layer {
return _idLayer
}
var _idLayer = Layer(func(event common.MapStr, next transport.Dialer) (transport.Dialer, error) {
return next, nil
})
// ConstAddrLayer introduces a network layer always passing a constant address
// to the underlying layer.
func ConstAddrLayer(address string) Layer {
build := constAddr(address)
return func(event common.MapStr, next transport.Dialer) (transport.Dialer, error) {
return build(next), nil
}
}
// MakeConstAddrLayer always passes the same address to the original Layer.
// This is useful if a lookup did return multiple IPs for the same hostname,
// but the IP use to connect shall be fixed.
func MakeConstAddrLayer(addr string, origLayer Layer) Layer {
return withLayerDialer(origLayer, constAddr(addr))
}
// MakeConstAddrDialer always passes the same address to the original NetDialer.
// This is useful if a lookup did return multiple IPs for the same hostname,
// but the IP use to connect shall be fixed.
func MakeConstAddrDialer(addr string, origNet NetDialer) NetDialer {
return withNetDialer(origNet, constAddr(addr))
}
func (t *timer) start() { t.s = time.Now() }
func (t *timer) stop() { t.e = time.Now() }
func (t *timer) duration() time.Duration { return t.e.Sub(t.s) }
// makeDialer aliases transport.DialerFunc
func makeDialer(fn func(network, address string) (net.Conn, error)) transport.Dialer {
return transport.DialerFunc(fn)
}
// beforeDial will always call fn before executing the underlying dialer.
// The callback must return the original or a new address to be used with
// the dialer.
func beforeDial(dialer transport.Dialer, fn func(string) string) transport.Dialer {
return makeDialer(func(network, address string) (net.Conn, error) {
address = fn(address)
return dialer.Dial(network, address)
})
}
// afterDial will run fn after the dialer did successfully return a connection.
func afterDial(dialer transport.Dialer, fn func(net.Conn) (net.Conn, error)) transport.Dialer {
return makeDialer(func(network, address string) (net.Conn, error) {
conn, err := dialer.Dial(network, address)
if err == nil {
conn, err = fn(conn)
}
return conn, err
})
}
func startTimerAfterDial(t *timer, dialer transport.Dialer) transport.Dialer {
return afterDial(dialer, func(c net.Conn) (net.Conn, error) {
t.start()
return c, nil
})
}
func constAddr(addr string) func(transport.Dialer) transport.Dialer {
return func(dialer transport.Dialer) transport.Dialer {
return beforeDial(dialer, func(_ string) string {
return addr
})
}
}
func withNetDialer(layer NetDialer, fn func(transport.Dialer) transport.Dialer) NetDialer {
return func(event common.MapStr) (transport.Dialer, error) {
origDialer, err := layer.build(event)
if err != nil {
return nil, err
}
return fn(origDialer), nil
}
}
func withLayerDialer(layer Layer, fn func(transport.Dialer) transport.Dialer) Layer {
return func(event common.MapStr, next transport.Dialer) (transport.Dialer, error) {
origDialer, err := layer.build(event, next)
if err != nil {
return nil, err
}
return fn(origDialer), nil
}
}