forked from libp2p/go-libp2p
/
listener.go
366 lines (319 loc) · 10.2 KB
/
listener.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
package libp2pwebrtc
import (
"context"
"crypto"
"encoding/hex"
"errors"
"fmt"
"net"
"strings"
"sync"
"time"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
tpt "github.com/libp2p/go-libp2p/core/transport"
"github.com/libp2p/go-libp2p/p2p/transport/webrtc/udpmux"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/multiformats/go-multibase"
"github.com/multiformats/go-multihash"
pionlogger "github.com/pion/logging"
"github.com/pion/webrtc/v3"
"go.uber.org/zap/zapcore"
)
type connMultiaddrs struct {
local, remote ma.Multiaddr
}
var _ network.ConnMultiaddrs = &connMultiaddrs{}
func (c *connMultiaddrs) LocalMultiaddr() ma.Multiaddr { return c.local }
func (c *connMultiaddrs) RemoteMultiaddr() ma.Multiaddr { return c.remote }
const (
candidateSetupTimeout = 20 * time.Second
DefaultMaxInFlightConnections = 10
)
type listener struct {
transport *WebRTCTransport
mux *udpmux.UDPMux
config webrtc.Configuration
localFingerprint webrtc.DTLSFingerprint
localFingerprintMultibase string
localAddr net.Addr
localMultiaddr ma.Multiaddr
// buffered incoming connections
acceptQueue chan tpt.CapableConn
// used to control the lifecycle of the listener
ctx context.Context
cancel context.CancelFunc
}
var _ tpt.Listener = &listener{}
func newListener(transport *WebRTCTransport, laddr ma.Multiaddr, socket net.PacketConn, config webrtc.Configuration) (*listener, error) {
localFingerprints, err := config.Certificates[0].GetFingerprints()
if err != nil {
return nil, err
}
localMh, err := hex.DecodeString(strings.ReplaceAll(localFingerprints[0].Value, ":", ""))
if err != nil {
return nil, err
}
localMhBuf, err := multihash.Encode(localMh, multihash.SHA2_256)
if err != nil {
return nil, err
}
localFpMultibase, err := multibase.Encode(multibase.Base64url, localMhBuf)
if err != nil {
return nil, err
}
l := &listener{
transport: transport,
config: config,
localFingerprint: localFingerprints[0],
localFingerprintMultibase: localFpMultibase,
localMultiaddr: laddr,
localAddr: socket.LocalAddr(),
acceptQueue: make(chan tpt.CapableConn),
}
l.ctx, l.cancel = context.WithCancel(context.Background())
mux := udpmux.NewUDPMux(socket)
l.mux = mux
mux.Start()
go l.listen()
return l, err
}
func (l *listener) listen() {
// Accepting a connection requires instantiating a peerconnection
// and a noise connection which is expensive. We therefore limit
// the number of in-flight connection requests. A connection
// is considered to be in flight from the instant it is handled
// until it is dequeued by a call to Accept, or errors out in some
// way.
inFlightQueueCh := make(chan struct{}, l.transport.maxInFlightConnections)
for i := uint32(0); i < l.transport.maxInFlightConnections; i++ {
inFlightQueueCh <- struct{}{}
}
for {
select {
case <-inFlightQueueCh:
case <-l.ctx.Done():
return
}
candidate, err := l.mux.Accept(l.ctx)
if err != nil {
if l.ctx.Err() == nil {
log.Debugf("accepting candidate failed: %s", err)
}
return
}
go func() {
defer func() { inFlightQueueCh <- struct{}{} }() // free this spot once again
ctx, cancel := context.WithTimeout(l.ctx, candidateSetupTimeout)
defer cancel()
conn, err := l.handleCandidate(ctx, candidate)
if err != nil {
l.mux.RemoveConnByUfrag(candidate.Ufrag)
log.Debugf("could not accept connection: %s: %v", candidate.Ufrag, err)
return
}
select {
case <-ctx.Done():
log.Warn("could not push connection: ctx done")
conn.Close()
case l.acceptQueue <- conn:
// acceptQueue is an unbuffered channel, so this block until the connection is accepted.
}
}()
}
}
func (l *listener) handleCandidate(ctx context.Context, candidate udpmux.Candidate) (tpt.CapableConn, error) {
remoteMultiaddr, err := manet.FromNetAddr(candidate.Addr)
if err != nil {
return nil, err
}
if l.transport.gater != nil {
localAddr, _ := ma.SplitFunc(l.localMultiaddr, func(c ma.Component) bool { return c.Protocol().Code == ma.P_CERTHASH })
if !l.transport.gater.InterceptAccept(&connMultiaddrs{local: localAddr, remote: remoteMultiaddr}) {
// The connection attempt is rejected before we can send the client an error.
// This means that the connection attempt will time out.
return nil, errors.New("connection gated")
}
}
scope, err := l.transport.rcmgr.OpenConnection(network.DirInbound, false, remoteMultiaddr)
if err != nil {
return nil, err
}
conn, err := l.setupConnection(ctx, scope, remoteMultiaddr, candidate)
if err != nil {
scope.Done()
return nil, err
}
if l.transport.gater != nil && !l.transport.gater.InterceptSecured(network.DirInbound, conn.RemotePeer(), conn) {
conn.Close()
return nil, errors.New("connection gated")
}
return conn, nil
}
func (l *listener) setupConnection(
ctx context.Context, scope network.ConnManagementScope,
remoteMultiaddr ma.Multiaddr, candidate udpmux.Candidate,
) (tConn tpt.CapableConn, err error) {
var pc *webrtc.PeerConnection
defer func() {
if err != nil {
if pc != nil {
_ = pc.Close()
}
if tConn != nil {
_ = tConn.Close()
}
}
}()
loggerFactory := pionlogger.NewDefaultLoggerFactory()
pionLogLevel := pionlogger.LogLevelDisabled
switch log.Level() {
case zapcore.DebugLevel:
pionLogLevel = pionlogger.LogLevelDebug
case zapcore.InfoLevel:
pionLogLevel = pionlogger.LogLevelInfo
case zapcore.WarnLevel:
pionLogLevel = pionlogger.LogLevelWarn
case zapcore.ErrorLevel:
pionLogLevel = pionlogger.LogLevelError
}
loggerFactory.DefaultLogLevel = pionLogLevel
settingEngine := webrtc.SettingEngine{LoggerFactory: loggerFactory}
settingEngine.SetAnsweringDTLSRole(webrtc.DTLSRoleServer)
settingEngine.SetICECredentials(candidate.Ufrag, candidate.Ufrag)
settingEngine.SetLite(true)
settingEngine.SetICEUDPMux(l.mux)
settingEngine.SetIncludeLoopbackCandidate(true)
settingEngine.DisableCertificateFingerprintVerification(true)
settingEngine.SetICETimeouts(
l.transport.peerConnectionTimeouts.Disconnect,
l.transport.peerConnectionTimeouts.Failed,
l.transport.peerConnectionTimeouts.Keepalive,
)
settingEngine.DetachDataChannels()
api := webrtc.NewAPI(webrtc.WithSettingEngine(settingEngine))
pc, err = api.NewPeerConnection(l.config)
if err != nil {
return nil, err
}
negotiated, id := handshakeChannelNegotiated, handshakeChannelID
rawDatachannel, err := pc.CreateDataChannel("", &webrtc.DataChannelInit{
Negotiated: &negotiated,
ID: &id,
})
if err != nil {
return nil, err
}
errC := addOnConnectionStateChangeCallback(pc)
// Infer the client SDP from the incoming STUN message by setting the ice-ufrag.
if err := pc.SetRemoteDescription(webrtc.SessionDescription{
SDP: createClientSDP(candidate.Addr, candidate.Ufrag),
Type: webrtc.SDPTypeOffer,
}); err != nil {
return nil, err
}
answer, err := pc.CreateAnswer(nil)
if err != nil {
return nil, err
}
if err := pc.SetLocalDescription(answer); err != nil {
return nil, err
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case err := <-errC:
if err != nil {
return nil, fmt.Errorf("peer connection failed for ufrag: %s", candidate.Ufrag)
}
}
rwc, err := getDetachedChannel(ctx, rawDatachannel)
if err != nil {
return nil, err
}
localMultiaddrWithoutCerthash, _ := ma.SplitFunc(l.localMultiaddr, func(c ma.Component) bool { return c.Protocol().Code == ma.P_CERTHASH })
handshakeChannel := newStream(rawDatachannel, rwc, func() {})
// The connection is instantiated before performing the Noise handshake. This is
// to handle the case where the remote is faster and attempts to initiate a stream
// before the ondatachannel callback can be set.
conn, err := newConnection(
network.DirInbound,
pc,
l.transport,
scope,
l.transport.localPeerId,
localMultiaddrWithoutCerthash,
"", // remotePeer
nil, // remoteKey
remoteMultiaddr,
)
if err != nil {
return nil, err
}
// we do not yet know A's peer ID so accept any inbound
remotePubKey, err := l.transport.noiseHandshake(ctx, pc, handshakeChannel, "", crypto.SHA256, true)
if err != nil {
return nil, err
}
remotePeer, err := peer.IDFromPublicKey(remotePubKey)
if err != nil {
return nil, err
}
// earliest point where we know the remote's peerID
if err := scope.SetPeer(remotePeer); err != nil {
return nil, err
}
conn.setRemotePeer(remotePeer)
conn.setRemotePublicKey(remotePubKey)
return conn, err
}
func (l *listener) Accept() (tpt.CapableConn, error) {
select {
case <-l.ctx.Done():
return nil, tpt.ErrListenerClosed
case conn := <-l.acceptQueue:
return conn, nil
}
}
func (l *listener) Close() error {
select {
case <-l.ctx.Done():
default:
l.cancel()
}
return nil
}
func (l *listener) Addr() net.Addr {
return l.localAddr
}
func (l *listener) Multiaddr() ma.Multiaddr {
return l.localMultiaddr
}
// addOnConnectionStateChangeCallback adds the OnConnectionStateChange to the PeerConnection.
// The channel returned here:
// * is closed when the state changes to Connection
// * receives an error when the state changes to Failed
// * doesn't receive anything (nor is closed) when the state changes to Disconnected
func addOnConnectionStateChangeCallback(pc *webrtc.PeerConnection) <-chan error {
errC := make(chan error, 1)
var once sync.Once
pc.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
switch state {
case webrtc.PeerConnectionStateConnected:
once.Do(func() { close(errC) })
case webrtc.PeerConnectionStateFailed:
once.Do(func() {
errC <- errors.New("peerconnection failed")
close(errC)
})
case webrtc.PeerConnectionStateDisconnected:
// the connection can move to a disconnected state and back to a connected state without ICE renegotiation.
// This could happen when underlying UDP packets are lost, and therefore the connection moves to the disconnected state.
// If the connection then receives packets on the connection, it can move back to the connected state.
// If no packets are received until the failed timeout is triggered, the connection moves to the failed state.
log.Warn("peerconnection disconnected")
}
})
return errC
}