forked from bigpigeon/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialchain.go
187 lines (156 loc) · 4.5 KB
/
dialchain.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
package dialchain
import (
"fmt"
"net"
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/outputs/transport"
"github.com/elastic/beats/heartbeat/look"
)
type DialLayerCallback interface {
Start()
Done(err error)
}
type MeasureLayerRTTCB struct {
Callback MeasureCallback
start time.Time
}
type MeasureCallback func(start, end time.Time)
type DialerChain struct {
Net NetDialer
Layers []Layer
}
type NetDialer struct {
Name string
Dialer transport.Dialer
}
type Layer struct {
Name string
Builder func(transport.Dialer) (transport.Dialer, error)
}
func (c *DialerChain) Clone() *DialerChain {
d := &DialerChain{
Net: c.Net,
Layers: make([]Layer, len(c.Layers)),
}
copy(d.Layers, c.Layers)
return d
}
func (c *DialerChain) BuildWith(makeCB func(string) DialLayerCallback) (d transport.Dialer, err error) {
d = LayerCBDialer(makeCB(c.Net.Name), c.Net.Dialer)
for _, layer := range c.Layers {
if d, err = LayerDeltaCBDialer(makeCB(layer.Name), d, layer.Builder); err != nil {
return nil, err
}
}
return
}
func (c *DialerChain) BuildWithMeasures(event common.MapStr) (transport.Dialer, error) {
return c.BuildWith(func(name string) DialLayerCallback {
return measureEventRTT(event, name)
})
}
func (c *DialerChain) Build() (d transport.Dialer, err error) {
d = c.Net.Dialer
for _, layer := range c.Layers {
if d, err = layer.Builder(d); err != nil {
return nil, err
}
}
return
}
func (c *DialerChain) TestBuild() error {
_, err := c.Build()
return err
}
func (c *DialerChain) DialWithMeasurements(network, host string) (fields common.MapStr, conn net.Conn, err error) {
var dialer transport.Dialer
fields = common.MapStr{}
if dialer, err = c.BuildWithMeasures(fields); err == nil {
conn, err = dialer.Dial(network, host)
}
return
}
func (c *DialerChain) Dial(network, host string) (conn net.Conn, err error) {
var dialer transport.Dialer
if dialer, err = c.Build(); err == nil {
return dialer.Dial(network, host)
}
return
}
func (c *DialerChain) AddLayer(l Layer) {
c.Layers = append(c.Layers, l)
}
func measureEventRTT(event common.MapStr, name string) DialLayerCallback {
return &MeasureLayerRTTCB{Callback: func(start, end time.Time) {
event[name] = look.RTT(end.Sub(start))
}}
}
func LayerCBDialer(cb DialLayerCallback, d transport.Dialer) transport.Dialer {
return transport.DialerFunc(func(network, address string) (net.Conn, error) {
cb.Start()
c, err := d.Dial(network, address)
cb.Done(err)
return c, err
})
}
func LayerDeltaCBDialer(
cb DialLayerCallback,
dialer transport.Dialer,
layer func(transport.Dialer) (transport.Dialer, error),
) (transport.DialerFunc, error) {
starter := transport.DialerFunc(func(network, address string) (net.Conn, error) {
c, err := dialer.Dial(network, address)
cb.Start()
return c, err
})
layerInstance, err := layer(starter)
if err != nil {
return nil, err
}
return func(network, address string) (net.Conn, error) {
c, err := layerInstance.Dial(network, address)
cb.Done(err)
return c, err
}, nil
}
func ConstAddrDialer(name, addr string, to time.Duration) NetDialer {
return NetDialer{name, transport.DialerFunc(func(network, _ string) (net.Conn, error) {
switch network {
case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6":
default:
return nil, fmt.Errorf("unsupported network type %v", network)
}
dialer := &net.Dialer{Timeout: to}
return dialer.Dial(network, addr)
})}
}
func ConstAddrLayer(addr string, l Layer) Layer {
return Layer{l.Name, func(d transport.Dialer) (transport.Dialer, error) {
forward, err := l.Builder(d)
if err != nil {
return nil, err
}
return transport.DialerFunc(func(network, _ string) (net.Conn, error) {
return forward.Dial(network, addr)
}), nil
}}
}
func TCPDialer(name string, to time.Duration) NetDialer {
return NetDialer{name, transport.NetDialer(to)}
}
func UDPDialer(name string, to time.Duration) NetDialer {
return NetDialer{name, transport.NetDialer(to)}
}
func SOCKS5Layer(name string, config *transport.ProxyConfig) Layer {
return Layer{name, func(d transport.Dialer) (transport.Dialer, error) {
return transport.ProxyDialer(config, d)
}}
}
func TLSLayer(name string, config *transport.TLSConfig, timeout time.Duration) Layer {
return Layer{name, func(d transport.Dialer) (transport.Dialer, error) {
return transport.TLSDialer(d, config, timeout)
}}
}
func (cb *MeasureLayerRTTCB) Start() { cb.start = time.Now() }
func (cb *MeasureLayerRTTCB) Done(_ error) { cb.Callback(cb.start, time.Now()) }