/
puncher.go
326 lines (266 loc) · 8.04 KB
/
puncher.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
// TODO: Collect metrics.
package npp
import (
"context"
"fmt"
"net"
"sync"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/libp2p/go-reuseport"
"github.com/noxiouz/zapctx/ctxlog"
"github.com/sonm-io/core/insonmnia/npp/rendezvous"
"github.com/sonm-io/core/proto"
"github.com/sonm-io/core/util/multierror"
"github.com/sonm-io/core/util/netutil"
"go.uber.org/zap"
)
// NATPuncher describes an interface of NAT Punching Protocol.
//
// It should be used to penetrate a NAT while connecting two peers located
// either under the same or different firewalls with network address
// translation enabled.
type NATPuncher interface {
// Dial dials the given address.
// Should be used only on client side.
Dial(addr common.Address) (net.Conn, error)
// DialContext connects to the address using the provided context.
// Should be used only on client side.
DialContext(ctx context.Context, addr common.Address) (net.Conn, error)
// Accept blocks the current execution context until a new connection
// arrives.
//
// Indented to be used on server side.
Accept() (net.Conn, error)
// @antmat said that this method is clearly self-descriptive and much obvious. Wow.
AcceptContext(ctx context.Context) (net.Conn, error)
// RemoteAddr returns rendezvous remote address.
RemoteAddr() net.Addr
// Close closes the puncher.
// Any blocked operations will be unblocked and return errors.
Close() error
}
type natPuncher struct {
ctx context.Context
log *zap.Logger
client *rendezvousClient
listener net.Listener
listenerChannel chan connTuple
maxAttempts int
timeout time.Duration
}
func newNATPuncher(ctx context.Context, cfg rendezvous.Config, client *rendezvousClient) (NATPuncher, error) {
// It's important here to reuse the Rendezvous client local address for
// successful NAT penetration in the case of cone NAT.
listener, err := reuseport.Listen(protocol, client.LocalAddr().String())
if err != nil {
return nil, err
}
channel := make(chan connTuple, 1)
m := &natPuncher{
ctx: ctx,
log: ctxlog.G(ctx),
client: client,
listenerChannel: channel,
listener: listener,
maxAttempts: cfg.MaxConnectionAttempts,
timeout: cfg.Timeout,
}
go m.listen()
return m, nil
}
func (m *natPuncher) listen() error {
for {
conn, err := m.listener.Accept()
m.listenerChannel <- newConnTuple(conn, err)
if err != nil {
m.log.Error("failed to listen NPP", zap.Error(err))
return err
}
}
}
func (m *natPuncher) Dial(addr common.Address) (net.Conn, error) {
ctx, cancel := context.WithTimeout(m.ctx, m.timeout)
defer cancel()
return m.DialContext(ctx, addr)
}
func (m *natPuncher) DialContext(ctx context.Context, addr common.Address) (net.Conn, error) {
addrs, err := m.resolve(ctx, addr)
if err != nil {
m.log.Warn("failed to resolve remote peer using rendezvous", zap.Stringer("remote_addr", addr), zap.Error(err))
return nil, err
}
return m.punch(ctx, addrs)
}
func (m *natPuncher) Accept() (net.Conn, error) {
return m.AcceptContext(m.ctx)
}
func (m *natPuncher) AcceptContext(ctx context.Context) (net.Conn, error) {
// Check for acceptor listenerChannel, if there is a connection - return immediately.
select {
case conn := <-m.listenerChannel:
m.log.Info("received acceptor peer", zap.Any("conn", conn))
return conn.unwrap()
default:
}
addrs, err := m.publish(ctx)
if err != nil {
m.log.Warn("failed to publish itself on the rendezvous", zap.Error(err))
return nil, newRendezvousError(err)
}
m.log.Info("received remote peer endpoints", zap.Any("addrs", *addrs))
ctx, cancel := context.WithTimeout(ctx, m.timeout)
defer cancel()
// Here the race begins! We're simultaneously trying to connect to ALL
// provided endpoints with a reasonable timeout. The first winner will
// be the champion, while others die in agony. Life is cruel.
conn, err := m.punch(ctx, addrs)
if err != nil {
return nil, newRendezvousError(err)
}
return conn, nil
}
func (m *natPuncher) resolve(ctx context.Context, addr common.Address) (*sonm.RendezvousReply, error) {
privateAddrs, err := m.privateAddrs()
if err != nil {
return nil, err
}
request := &sonm.ConnectRequest{
Protocol: protocol,
PrivateAddrs: []*sonm.Addr{},
ID: addr.Bytes(),
}
request.PrivateAddrs, err = convertAddrs(privateAddrs)
if err != nil {
return nil, err
}
return m.client.Resolve(ctx, request)
}
func (m *natPuncher) publish(ctx context.Context) (*sonm.RendezvousReply, error) {
privateAddrs, err := m.privateAddrs()
if err != nil {
return nil, err
}
request := &sonm.PublishRequest{
PrivateAddrs: []*sonm.Addr{},
}
request.PrivateAddrs, err = convertAddrs(privateAddrs)
if err != nil {
return nil, err
}
return m.client.Publish(ctx, request)
}
func convertAddrs(addrs []net.Addr) ([]*sonm.Addr, error) {
var result []*sonm.Addr
for _, addr := range addrs {
host, port, err := netutil.SplitHostPort(addr.String())
if err != nil {
return nil, err
}
result = append(result, &sonm.Addr{
Protocol: protocol,
Addr: &sonm.SocketAddr{
Addr: host.String(),
Port: uint32(port),
},
})
}
return result, nil
}
func (m *natPuncher) punch(ctx context.Context, addrs *sonm.RendezvousReply) (net.Conn, error) {
if addrs.Empty() {
return nil, fmt.Errorf("no addresses resolved")
}
channel := make(chan connTuple, 1)
go m.doPunch(ctx, addrs, channel)
select {
case conn := <-channel:
return conn.unwrap()
case conn := <-m.listenerChannel:
return conn.unwrap()
}
}
func (m *natPuncher) doPunch(ctx context.Context, addrs *sonm.RendezvousReply, channel chan<- connTuple) {
m.log.Debug("punching", zap.Any("addrs", *addrs))
pending := make(chan connTuple, 1+len(addrs.PrivateAddrs))
wg := sync.WaitGroup{}
wg.Add(len(addrs.PrivateAddrs))
if addrs.PublicAddr.IsValid() {
wg.Add(1)
go func() {
defer wg.Done()
conn, err := m.punchAddr(ctx, addrs.PublicAddr)
m.log.Debug("received NPP NAT connection candidate", zap.Any("remote_addr", *addrs.PublicAddr), zap.Error(err))
pending <- newConnTuple(conn, err)
}()
}
for _, addr := range addrs.PrivateAddrs {
go func(addr *sonm.Addr) {
defer wg.Done()
conn, err := m.punchAddr(ctx, addr)
m.log.Debug("received NPP internet connection candidate", zap.Any("remote_addr", *addr), zap.Error(err))
pending <- newConnTuple(conn, err)
}(addr)
}
go func() {
wg.Wait()
close(pending)
}()
var peer net.Conn
var errs = multierror.NewMultiError()
for conn := range pending {
m.log.Debug("received NPP connection candidate", zap.Any("remote_addr", conn.RemoteAddr()), zap.Error(conn.err))
if conn.Error() != nil {
errs = multierror.AppendUnique(errs, conn.Error())
continue
}
if peer != nil {
conn.Close()
} else {
peer = conn.conn
// Do not return here - still need to close possibly successful connections.
channel <- newConnTuple(peer, nil)
}
}
if peer == nil {
channel <- newConnTuple(nil, fmt.Errorf("failed to punch the network using NPP: all attempts has failed - %s", errs.Error()))
}
}
func (m *natPuncher) punchAddr(ctx context.Context, addr *sonm.Addr) (net.Conn, error) {
peerAddr, err := addr.IntoTCP()
if err != nil {
return nil, err
}
var conn net.Conn
var errs = multierror.NewMultiError()
for i := 0; i < m.maxAttempts; i++ {
conn, err = DialContext(ctx, protocol, m.client.LocalAddr().String(), peerAddr.String())
if err == nil {
return conn, nil
}
errs = multierror.AppendUnique(errs, err)
}
return nil, errs.ErrorOrNil()
}
func (m *natPuncher) RemoteAddr() net.Addr {
return m.client.RemoteAddr()
}
func (m *natPuncher) Close() error {
var errs []error
if err := m.listener.Close(); err != nil {
errs = append(errs, err)
}
if err := m.client.Close(); err != nil {
errs = append(errs, err)
}
if len(errs) > 0 {
return fmt.Errorf("failed to close listener: %+v", errs)
} else {
return nil
}
}
// PrivateAddrs collects and returns private addresses of a network interfaces
// the listening socket bind on.
func (m *natPuncher) privateAddrs() ([]net.Addr, error) {
return privateAddrs(m.listener.Addr())
}