/
dial.go
376 lines (304 loc) · 8.98 KB
/
dial.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// This package is responsible for Client side for NAT Punching Protocol.
package npp
import (
"context"
"fmt"
"net"
"os"
"sync"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/prometheus/client_golang/prometheus"
prometheusIO "github.com/prometheus/client_model/go"
"github.com/sonm-io/core/insonmnia/auth"
"github.com/sonm-io/core/insonmnia/npp/relay"
"go.uber.org/zap"
)
// Dialer represents an NPP dialer.
//
// This structure acts like an usual dialer with an exception that the address
// must be an authenticated endpoint and the connection establishment process
// is done via NAT Punching Protocol.
type Dialer struct {
log *zap.Logger
puncherNew puncherFactory
puncherNewQUIC puncherFactory
relayDialer *relay.Dialer
mu sync.Mutex
metrics map[string]*dialMetrics
}
// NewDialer constructs a new dialer that is aware of NAT Punching Protocol.
func NewDialer(options ...Option) (*Dialer, error) {
opts := newOptions()
for _, o := range options {
if err := o(opts); err != nil {
return nil, err
}
}
return &Dialer{
log: opts.log,
puncherNew: opts.puncherNew,
puncherNewQUIC: opts.puncherNewQUIC,
relayDialer: opts.relayDialer,
metrics: map[string]*dialMetrics{},
}, nil
}
// Dial dials the given verified address using NPP.
func (m *Dialer) Dial(addr auth.Addr) (net.Conn, error) {
return m.DialContext(context.Background(), addr)
}
// DialContext connects to the given verified address using NPP and the
// provided context.
//
// The provided Context must be non-nil. If the context expires before
// the connection is complete, an error is returned. Once successfully
// connected, any expiration of the context will not affect the
// connection.
func (m *Dialer) DialContext(ctx context.Context, addr auth.Addr) (net.Conn, error) {
now := time.Now()
metric := m.metricHandle(addr)
metric.NumAttempts.Inc()
metric.LastTimeActive.SetToCurrentTime()
log := m.log.With(zap.Stringer("remote_addr", addr))
conn, err := m.dialContextExt(ctx, addr, metric)
if err != nil {
log.Warn("failed to connect using NPP - all methods failed")
metric.NumFailed.Inc()
return nil, err
}
log = log.With(zap.Stringer("remote_peer", conn.RemoteAddr()))
switch conn.Source {
case sourceDirectConnection:
metric.UsingTCPDirectHistogram.Observe(conn.Duration.Seconds())
log.Debug("successfully connected using direct TCP")
case sourceNPPConnection:
metric.UsingNATHistogram.Observe(conn.Duration.Seconds())
log.Debug("successfully connected using NPP")
case sourceNPPQUICConnection:
metric.UsingQNATHistogram.Observe(conn.Duration.Seconds())
log.Debug("successfully connected using QUIC NPP")
case sourceRelayedConnection:
metric.UsingRelayHistogram.Observe(conn.Duration.Seconds())
log.Debug("successfully connected using Relay")
}
metric.NumSuccess.Inc()
metric.SummaryHistogram.Observe(time.Since(now).Seconds())
metric.LastTimeSuccess.SetToCurrentTime()
return conn, nil
}
func (m *Dialer) dialContextExt(ctx context.Context, addr auth.Addr, metric *dialMetrics) (*nppConn, error) {
m.log.Debug("connecting to remote peer", zap.Stringer("remote_addr", addr))
if conn := m.dialDirect(ctx, addr); conn != nil {
return conn, nil
}
ethAddr, err := addr.ETH()
if err != nil {
return nil, err
}
// Currently we hide QUIC support under the feature gate.
if os.Getenv("SONM_ENABLE_QUIC") == "true" {
if conn := m.dialQUICNPP(ctx, ethAddr); conn != nil {
return conn, nil
}
}
if conn := m.dialNPP(ctx, ethAddr); conn != nil {
return conn, nil
}
return m.dialRelayed(ctx, ethAddr)
}
// Note, that this method acts as an optimization.
func (m *Dialer) dialDirect(ctx context.Context, addr auth.Addr) *nppConn {
now := time.Now()
log := m.log.With(zap.Stringer("remote_addr", addr))
log.Debug("connecting using direct TCP")
netAddr, err := addr.Addr()
if err != nil {
log.Debug("failed to connect using direct TCP", zap.Error(err))
return nil
}
dial := net.Dialer{}
conn, err := dial.DialContext(ctx, "tcp", netAddr)
if err != nil {
log.Debug("failed to connect using direct TCP", zap.Error(err))
return nil
}
return newDirectNPPConn(conn, time.Since(now))
}
func (m *Dialer) dialQUICNPP(ctx context.Context, addr common.Address) *nppConn {
if m.puncherNewQUIC == nil {
return nil
}
now := time.Now()
timeout := 5 * time.Second
log := m.log.With(zap.Stringer("remoteAddr", addr))
log.Debug("connecting using QUIC NPP", zap.Duration("timeout", timeout))
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
nppChannel := make(chan connTuple)
go func() {
defer close(nppChannel)
puncher, err := m.puncherNewQUIC(ctx)
if err != nil {
nppChannel <- newConnTuple(nil, err)
return
}
defer puncher.Close()
nppChannel <- newConnTuple(puncher.Dial(addr))
}()
select {
case conn := <-nppChannel:
err := conn.Error()
if err == nil {
return newPunchedQUICNPPConn(conn.conn, time.Since(now))
}
log.Warn("failed to connect using QUIC NPP", zap.Error(err))
case <-ctx.Done():
go drainConnChannel(nppChannel)
log.Warn("failed to connect using QUIC NPP", zap.Error(ctx.Err()))
}
return nil
}
func (m *Dialer) dialNPP(ctx context.Context, addr common.Address) *nppConn {
if m.puncherNew == nil {
return nil
}
now := time.Now()
timeout := 5 * time.Second
log := m.log.With(zap.Stringer("remote_addr", addr))
log.Debug("connecting using NPP", zap.Duration("timeout", timeout))
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
nppChannel := make(chan connTuple)
go func() {
defer close(nppChannel)
puncher, err := m.puncherNew(ctx)
if err != nil {
nppChannel <- newConnTuple(nil, err)
return
}
defer puncher.Close()
nppChannel <- newConnTuple(puncher.Dial(addr))
}()
select {
case conn := <-nppChannel:
err := conn.Error()
if err == nil {
return newPunchedNPPConn(conn.conn, time.Since(now))
}
log.Warn("failed to connect using NPP", zap.Error(err))
case <-ctx.Done():
go drainConnChannel(nppChannel)
log.Warn("failed to connect using NPP", zap.Error(ctx.Err()))
}
return nil
}
func (m *Dialer) dialRelayed(ctx context.Context, addr common.Address) (*nppConn, error) {
if m.relayDialer == nil {
return nil, fmt.Errorf("failed to connect using NPP: no Relay configured")
}
now := time.Now()
log := m.log.With(zap.Stringer("remote_addr", addr))
log.Debug("connecting using Relay")
channel := make(chan connTuple)
go func() {
defer close(channel)
channel <- newConnTuple(m.relayDialer.Dial(addr))
}()
select {
case conn := <-channel:
if err := conn.Error(); err != nil {
log.Warn("failed to connect using Relay", zap.Error(err))
return nil, err
}
return newRelayedNPPConn(conn.conn, time.Since(now)), nil
case <-ctx.Done():
log.Warn("failed to connect using Relay", zap.Error(ctx.Err()))
go drainConnChannel(channel)
return nil, ctx.Err()
}
}
func (m *Dialer) Metrics() (map[string][]*NamedMetric, error) {
m.mu.Lock()
defer m.mu.Unlock()
summary := map[string][]*NamedMetric{}
for addr, metric := range m.metrics {
metricNames := metric.MetricNames()
metricsToCollect := [...]prometheus.Metric{
metric.NumAttempts,
metric.NumSuccess,
metric.NumFailed,
metric.UsingTCPDirectHistogram,
metric.UsingNATHistogram,
metric.UsingQNATHistogram,
metric.UsingRelayHistogram,
metric.SummaryHistogram,
metric.LastTimeActive,
metric.LastTimeSuccess,
}
metricsCollected := make([]*NamedMetric, 0, len(metricsToCollect))
for id, metricToCollect := range metricsToCollect {
value := &prometheusIO.Metric{}
if err := metricToCollect.Write(value); err != nil {
return nil, err
}
metricsCollected = append(metricsCollected, &NamedMetric{Name: metricNames[id], Metric: value})
}
summary[addr] = metricsCollected
}
return summary, nil
}
// Close closes the dialer.
//
// Any blocked operations will be unblocked and return errors.
func (m *Dialer) Close() error {
return nil
}
func (m *Dialer) metricHandle(addr auth.Addr) *dialMetrics {
m.mu.Lock()
defer m.mu.Unlock()
k := addr.String()
if metric, ok := m.metrics[k]; ok {
return metric
}
metric := newDialMetrics()
m.metrics[k] = metric
return metric
}
type nppConn struct {
net.Conn
Source connSource
Duration time.Duration
}
func newDirectNPPConn(conn net.Conn, duration time.Duration) *nppConn {
return &nppConn{
Conn: conn,
Source: sourceDirectConnection,
Duration: duration,
}
}
func newPunchedNPPConn(conn net.Conn, duration time.Duration) *nppConn {
return &nppConn{
Conn: conn,
Source: sourceNPPConnection,
Duration: duration,
}
}
func newPunchedQUICNPPConn(conn net.Conn, duration time.Duration) *nppConn {
return &nppConn{
Conn: conn,
Source: sourceNPPQUICConnection,
Duration: duration,
}
}
func newRelayedNPPConn(conn net.Conn, duration time.Duration) *nppConn {
return &nppConn{
Conn: conn,
Source: sourceRelayedConnection,
Duration: duration,
}
}
func drainConnChannel(channel <-chan connTuple) {
for conn := range channel {
conn.Close()
}
}