-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathtransport.go
666 lines (591 loc) · 19.6 KB
/
transport.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
// Package libp2pwebrtc implements the WebRTC transport for go-libp2p,
// as described in https://github.com/libp2p/specs/tree/master/webrtc.
package libp2pwebrtc
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"errors"
"fmt"
"net"
"time"
mrand "math/rand/v2"
"google.golang.org/protobuf/proto"
"github.com/libp2p/go-libp2p/core/connmgr"
ic "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/pnet"
"github.com/libp2p/go-libp2p/core/sec"
tpt "github.com/libp2p/go-libp2p/core/transport"
"github.com/libp2p/go-libp2p/p2p/security/noise"
libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/webrtc/pb"
"github.com/libp2p/go-msgio"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/multiformats/go-multihash"
"github.com/pion/datachannel"
"github.com/pion/webrtc/v4"
)
var webrtcComponent ma.Component
func init() {
var err error
webrtcComponent, err = ma.NewComponent(ma.ProtocolWithCode(ma.P_WEBRTC_DIRECT).Name, "")
if err != nil {
log.Fatal(err)
}
}
const (
// handshakeChannelNegotiated is used to specify that the
// handshake data channel does not need negotiation via DCEP.
// A constant is used since the `DataChannelInit` struct takes
// references instead of values.
handshakeChannelNegotiated = true
// handshakeChannelID is the agreed ID for the handshake data
// channel. A constant is used since the `DataChannelInit` struct takes
// references instead of values. We specify the type here as this
// value is only ever copied and passed by reference
handshakeChannelID = uint16(0)
)
// timeout values for the peerconnection
// https://github.com/pion/webrtc/blob/v3.1.50/settingengine.go#L102-L109
const (
DefaultDisconnectedTimeout = 20 * time.Second
DefaultFailedTimeout = 30 * time.Second
DefaultKeepaliveTimeout = 15 * time.Second
sctpReceiveBufferSize = 100_000
)
type WebRTCTransport struct {
webrtcConfig webrtc.Configuration
rcmgr network.ResourceManager
gater connmgr.ConnectionGater
privKey ic.PrivKey
noiseTpt *noise.Transport
localPeerId peer.ID
listenUDP func(network string, laddr *net.UDPAddr) (net.PacketConn, error)
// timeouts
peerConnectionTimeouts iceTimeouts
// in-flight connections
maxInFlightConnections uint32
}
var _ tpt.Transport = &WebRTCTransport{}
type Option func(*WebRTCTransport) error
type iceTimeouts struct {
Disconnect time.Duration
Failed time.Duration
Keepalive time.Duration
}
type ListenUDPFn func(network string, laddr *net.UDPAddr) (net.PacketConn, error)
func New(privKey ic.PrivKey, psk pnet.PSK, gater connmgr.ConnectionGater, rcmgr network.ResourceManager, listenUDP ListenUDPFn, opts ...Option) (*WebRTCTransport, error) {
if psk != nil {
log.Error("WebRTC doesn't support private networks yet.")
return nil, fmt.Errorf("WebRTC doesn't support private networks yet")
}
if rcmgr == nil {
rcmgr = &network.NullResourceManager{}
}
localPeerID, err := peer.IDFromPrivateKey(privKey)
if err != nil {
return nil, fmt.Errorf("get local peer ID: %w", err)
}
// We use elliptic P-256 since it is widely supported by browsers.
//
// Implementation note: Testing with the browser,
// it seems like Chromium only supports ECDSA P-256 or RSA key signatures in the webrtc TLS certificate.
// We tried using P-228 and P-384 which caused the DTLS handshake to fail with Illegal Parameter
//
// Please refer to this is a list of suggested algorithms for the WebCrypto API.
// The algorithm for generating a certificate for an RTCPeerConnection
// must adhere to the WebCrpyto API. From my observation,
// RSA and ECDSA P-256 is supported on almost all browsers.
// Ed25519 is not present on the list.
pk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, fmt.Errorf("generate key for cert: %w", err)
}
cert, err := webrtc.GenerateCertificate(pk)
if err != nil {
return nil, fmt.Errorf("generate certificate: %w", err)
}
config := webrtc.Configuration{
Certificates: []webrtc.Certificate{*cert},
}
noiseTpt, err := noise.New(noise.ID, privKey, nil)
if err != nil {
return nil, fmt.Errorf("unable to create noise transport: %w", err)
}
transport := &WebRTCTransport{
rcmgr: rcmgr,
gater: gater,
webrtcConfig: config,
privKey: privKey,
noiseTpt: noiseTpt,
localPeerId: localPeerID,
listenUDP: listenUDP,
peerConnectionTimeouts: iceTimeouts{
Disconnect: DefaultDisconnectedTimeout,
Failed: DefaultFailedTimeout,
Keepalive: DefaultKeepaliveTimeout,
},
maxInFlightConnections: DefaultMaxInFlightConnections,
}
for _, opt := range opts {
if err := opt(transport); err != nil {
return nil, err
}
}
return transport, nil
}
func (t *WebRTCTransport) ListenOrder() int {
return libp2pquic.ListenOrder + 1 // We want to listen after QUIC listens so we can possibly reuse the same port.
}
func (t *WebRTCTransport) Protocols() []int {
return []int{ma.P_WEBRTC_DIRECT}
}
func (t *WebRTCTransport) Proxy() bool {
return false
}
func (t *WebRTCTransport) CanDial(addr ma.Multiaddr) bool {
isValid, n := IsWebRTCDirectMultiaddr(addr)
return isValid && n > 0
}
// Listen returns a listener for addr.
//
// The IP, Port combination for addr must be exclusive to this listener as a WebRTC listener cannot
// be multiplexed on the same port as other UDP based transports like QUIC and WebTransport.
// See https://github.com/libp2p/go-libp2p/issues/2446 for details.
func (t *WebRTCTransport) Listen(addr ma.Multiaddr) (tpt.Listener, error) {
addr, wrtcComponent := ma.SplitLast(addr)
isWebrtc := wrtcComponent.Equal(webrtcComponent)
if !isWebrtc {
return nil, fmt.Errorf("must listen on webrtc multiaddr")
}
nw, host, err := manet.DialArgs(addr)
if err != nil {
return nil, fmt.Errorf("listener could not fetch dialargs: %w", err)
}
udpAddr, err := net.ResolveUDPAddr(nw, host)
if err != nil {
return nil, fmt.Errorf("listener could not resolve udp address: %w", err)
}
socket, err := t.listenUDP(nw, udpAddr)
if err != nil {
return nil, fmt.Errorf("listen on udp: %w", err)
}
listener, err := t.listenSocket(socket)
if err != nil {
socket.Close()
return nil, err
}
return listener, nil
}
func (t *WebRTCTransport) listenSocket(socket net.PacketConn) (tpt.Listener, error) {
listenerMultiaddr, err := manet.FromNetAddr(socket.LocalAddr())
if err != nil {
return nil, err
}
listenerFingerprint, err := t.getCertificateFingerprint()
if err != nil {
return nil, err
}
encodedLocalFingerprint, err := encodeDTLSFingerprint(listenerFingerprint)
if err != nil {
return nil, err
}
certComp, err := ma.NewComponent(ma.ProtocolWithCode(ma.P_CERTHASH).Name, encodedLocalFingerprint)
if err != nil {
return nil, err
}
listenerMultiaddr = append(listenerMultiaddr, webrtcComponent, certComp)
return newListener(
t,
listenerMultiaddr,
socket,
t.webrtcConfig,
)
}
func (t *WebRTCTransport) Dial(ctx context.Context, remoteMultiaddr ma.Multiaddr, p peer.ID) (tpt.CapableConn, error) {
scope, err := t.rcmgr.OpenConnection(network.DirOutbound, false, remoteMultiaddr)
if err != nil {
return nil, err
}
if err := scope.SetPeer(p); err != nil {
scope.Done()
return nil, err
}
conn, err := t.dial(ctx, scope, remoteMultiaddr, p)
if err != nil {
scope.Done()
return nil, err
}
return conn, nil
}
func (t *WebRTCTransport) dial(ctx context.Context, scope network.ConnManagementScope, remoteMultiaddr ma.Multiaddr, p peer.ID) (tConn tpt.CapableConn, err error) {
var w webRTCConnection
defer func() {
if err != nil {
if w.PeerConnection != nil {
_ = w.PeerConnection.Close()
}
if tConn != nil {
_ = tConn.Close()
tConn = nil
}
}
}()
remoteMultihash, err := decodeRemoteFingerprint(remoteMultiaddr)
if err != nil {
return nil, fmt.Errorf("decode fingerprint: %w", err)
}
remoteHashFunction, ok := getSupportedSDPHash(remoteMultihash.Code)
if !ok {
return nil, fmt.Errorf("unsupported hash function: %w", nil)
}
rnw, rhost, err := manet.DialArgs(remoteMultiaddr)
if err != nil {
return nil, fmt.Errorf("generate dial args: %w", err)
}
raddr, err := net.ResolveUDPAddr(rnw, rhost)
if err != nil {
return nil, fmt.Errorf("resolve udp address: %w", err)
}
// Instead of encoding the local fingerprint we
// generate a random UUID as the connection ufrag.
// The only requirement here is that the ufrag and password
// must be equal, which will allow the server to determine
// the password using the STUN message.
ufrag := genUfrag()
settingEngine := webrtc.SettingEngine{
LoggerFactory: pionLoggerFactory,
}
settingEngine.SetICECredentials(ufrag, ufrag)
settingEngine.DetachDataChannels()
// use the first best address candidate
settingEngine.SetPrflxAcceptanceMinWait(0)
settingEngine.SetICETimeouts(
t.peerConnectionTimeouts.Disconnect,
t.peerConnectionTimeouts.Failed,
t.peerConnectionTimeouts.Keepalive,
)
// By default, webrtc will not collect candidates on the loopback address.
// This is disallowed in the ICE specification. However, implementations
// do not strictly follow this, for eg. Chrome gathers TCP loopback candidates.
// If you run pion on a system with only the loopback interface UP,
// it will not connect to anything.
settingEngine.SetIncludeLoopbackCandidate(true)
settingEngine.SetSCTPMaxReceiveBufferSize(sctpReceiveBufferSize)
if err := scope.ReserveMemory(sctpReceiveBufferSize, network.ReservationPriorityMedium); err != nil {
return nil, err
}
w, err = newWebRTCConnection(settingEngine, t.webrtcConfig)
if err != nil {
return nil, fmt.Errorf("instantiating peer connection failed: %w", err)
}
errC := addOnConnectionStateChangeCallback(w.PeerConnection)
// do offer-answer exchange
offer, err := w.PeerConnection.CreateOffer(nil)
if err != nil {
return nil, fmt.Errorf("create offer: %w", err)
}
err = w.PeerConnection.SetLocalDescription(offer)
if err != nil {
return nil, fmt.Errorf("set local description: %w", err)
}
answerSDPString, err := createServerSDP(raddr, ufrag, *remoteMultihash)
if err != nil {
return nil, fmt.Errorf("render server SDP: %w", err)
}
answer := webrtc.SessionDescription{SDP: answerSDPString, Type: webrtc.SDPTypeAnswer}
err = w.PeerConnection.SetRemoteDescription(answer)
if err != nil {
return nil, fmt.Errorf("set remote description: %w", err)
}
// await peerconnection opening
select {
case err := <-errC:
if err != nil {
return nil, err
}
case <-ctx.Done():
return nil, errors.New("peerconnection opening timed out")
}
// We are connected, run the noise handshake
detached, err := detachHandshakeDataChannel(ctx, w.HandshakeDataChannel)
if err != nil {
return nil, err
}
channel := newStream(w.HandshakeDataChannel, detached, func() {})
remotePubKey, err := t.noiseHandshake(ctx, w.PeerConnection, channel, p, remoteHashFunction, false)
if err != nil {
return nil, err
}
// Setup local and remote address for the connection
cp, err := w.HandshakeDataChannel.Transport().Transport().ICETransport().GetSelectedCandidatePair()
if cp == nil {
return nil, errors.New("ice connection did not have selected candidate pair: nil result")
}
if err != nil {
return nil, fmt.Errorf("ice connection did not have selected candidate pair: error: %w", err)
}
// the local address of the selected candidate pair should be the local address for the connection
localAddr, err := manet.FromNetAddr(&net.UDPAddr{IP: net.ParseIP(cp.Local.Address), Port: int(cp.Local.Port)})
if err != nil {
return nil, err
}
remoteMultiaddrWithoutCerthash, _ := ma.SplitFunc(remoteMultiaddr, func(c ma.Component) bool { return c.Protocol().Code == ma.P_CERTHASH })
conn, err := newConnection(
network.DirOutbound,
w.PeerConnection,
t,
scope,
t.localPeerId,
localAddr,
p,
remotePubKey,
remoteMultiaddrWithoutCerthash,
w.IncomingDataChannels,
w.PeerConnectionClosedCh,
)
if err != nil {
return nil, err
}
if t.gater != nil && !t.gater.InterceptSecured(network.DirOutbound, p, conn) {
return nil, fmt.Errorf("secured connection gated")
}
return conn, nil
}
func genUfrag() string {
const (
uFragAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
uFragPrefix = "libp2p+webrtc+v1/"
uFragIdLength = 32
uFragLength = len(uFragPrefix) + uFragIdLength
)
seed := [32]byte{}
rand.Read(seed[:])
r := mrand.New(mrand.New(mrand.NewChaCha8(seed)))
b := make([]byte, uFragLength)
for i := 0; i < len(uFragPrefix); i++ {
b[i] = uFragPrefix[i]
}
for i := len(uFragPrefix); i < uFragLength; i++ {
b[i] = uFragAlphabet[r.IntN(len(uFragAlphabet))]
}
return string(b)
}
func (t *WebRTCTransport) getCertificateFingerprint() (webrtc.DTLSFingerprint, error) {
fps, err := t.webrtcConfig.Certificates[0].GetFingerprints()
if err != nil {
return webrtc.DTLSFingerprint{}, err
}
return fps[0], nil
}
func (t *WebRTCTransport) generateNoisePrologue(pc *webrtc.PeerConnection, hash crypto.Hash, inbound bool) ([]byte, error) {
raw := pc.SCTP().Transport().GetRemoteCertificate()
cert, err := x509.ParseCertificate(raw)
if err != nil {
return nil, err
}
// NOTE: should we want we can fork the cert code as well to avoid
// all the extra allocations due to unneeded string interspersing (hex)
localFp, err := t.getCertificateFingerprint()
if err != nil {
return nil, err
}
remoteFpBytes, err := parseFingerprint(cert, hash)
if err != nil {
return nil, err
}
localFpBytes, err := decodeInterspersedHexFromASCIIString(localFp.Value)
if err != nil {
return nil, err
}
localEncoded, err := multihash.Encode(localFpBytes, multihash.SHA2_256)
if err != nil {
log.Debugf("could not encode multihash for local fingerprint")
return nil, err
}
remoteEncoded, err := multihash.Encode(remoteFpBytes, multihash.SHA2_256)
if err != nil {
log.Debugf("could not encode multihash for remote fingerprint")
return nil, err
}
result := []byte("libp2p-webrtc-noise:")
if inbound {
result = append(result, remoteEncoded...)
result = append(result, localEncoded...)
} else {
result = append(result, localEncoded...)
result = append(result, remoteEncoded...)
}
return result, nil
}
func (t *WebRTCTransport) noiseHandshake(ctx context.Context, pc *webrtc.PeerConnection, s *stream, peer peer.ID, hash crypto.Hash, inbound bool) (ic.PubKey, error) {
prologue, err := t.generateNoisePrologue(pc, hash, inbound)
if err != nil {
return nil, fmt.Errorf("generate prologue: %w", err)
}
opts := make([]noise.SessionOption, 0, 2)
opts = append(opts, noise.Prologue(prologue))
if peer == "" {
opts = append(opts, noise.DisablePeerIDCheck())
}
sessionTransport, err := t.noiseTpt.WithSessionOptions(opts...)
if err != nil {
return nil, fmt.Errorf("failed to instantiate Noise transport: %w", err)
}
var secureConn sec.SecureConn
if inbound {
secureConn, err = sessionTransport.SecureOutbound(ctx, netConnWrapper{s}, peer)
if err != nil {
return nil, fmt.Errorf("failed to secure inbound connection: %w", err)
}
} else {
secureConn, err = sessionTransport.SecureInbound(ctx, netConnWrapper{s}, peer)
if err != nil {
return nil, fmt.Errorf("failed to secure outbound connection: %w", err)
}
}
return secureConn.RemotePublicKey(), nil
}
func (t *WebRTCTransport) AddCertHashes(addr ma.Multiaddr) (ma.Multiaddr, bool) {
listenerFingerprint, err := t.getCertificateFingerprint()
if err != nil {
return nil, false
}
encodedLocalFingerprint, err := encodeDTLSFingerprint(listenerFingerprint)
if err != nil {
return nil, false
}
certComp, err := ma.NewComponent(ma.ProtocolWithCode(ma.P_CERTHASH).Name, encodedLocalFingerprint)
if err != nil {
return nil, false
}
return append(addr, certComp), true
}
type netConnWrapper struct {
*stream
}
func (netConnWrapper) LocalAddr() net.Addr { return nil }
func (netConnWrapper) RemoteAddr() net.Addr { return nil }
func (w netConnWrapper) Close() error {
// Close called while running the security handshake is an error and we should Reset the
// stream in that case rather than gracefully closing
w.stream.Reset()
return nil
}
// detachHandshakeDataChannel detaches the handshake data channel
func detachHandshakeDataChannel(ctx context.Context, dc *webrtc.DataChannel) (datachannel.ReadWriteCloser, error) {
done := make(chan struct{})
var rwc datachannel.ReadWriteCloser
var err error
dc.OnOpen(func() {
defer close(done)
rwc, err = dc.Detach()
})
// this is safe since for detached datachannels, the peerconnection runs the onOpen
// callback immediately if the SCTP transport is also connected.
select {
case <-done:
return rwc, err
case <-ctx.Done():
return nil, ctx.Err()
}
}
// webRTCConnection holds the webrtc.PeerConnection with the handshake channel and the queue for
// incoming data channels created by the peer.
//
// When creating a webrtc.PeerConnection, It is important to set the OnDataChannel handler upfront
// before connecting with the peer. If the handler's set up after connecting with the peer, there's
// a small window of time where datachannels created by the peer may not surface to us and cause a
// memory leak.
type webRTCConnection struct {
PeerConnection *webrtc.PeerConnection
HandshakeDataChannel *webrtc.DataChannel
IncomingDataChannels chan dataChannel
PeerConnectionClosedCh chan struct{}
}
func newWebRTCConnection(settings webrtc.SettingEngine, config webrtc.Configuration) (webRTCConnection, error) {
api := webrtc.NewAPI(webrtc.WithSettingEngine(settings))
pc, err := api.NewPeerConnection(config)
if err != nil {
return webRTCConnection{}, fmt.Errorf("failed to create peer connection: %w", err)
}
negotiated, id := handshakeChannelNegotiated, handshakeChannelID
handshakeDataChannel, err := pc.CreateDataChannel("", &webrtc.DataChannelInit{
Negotiated: &negotiated,
ID: &id,
})
if err != nil {
pc.Close()
return webRTCConnection{}, fmt.Errorf("failed to create handshake channel: %w", err)
}
incomingDataChannels := make(chan dataChannel, maxAcceptQueueLen)
pc.OnDataChannel(func(dc *webrtc.DataChannel) {
dc.OnOpen(func() {
rwc, err := dc.Detach()
if err != nil {
log.Warnf("could not detach datachannel: id: %d", *dc.ID())
return
}
select {
case incomingDataChannels <- dataChannel{rwc, dc}:
default:
log.Warnf("connection busy, rejecting stream")
b, _ := proto.Marshal(&pb.Message{Flag: pb.Message_RESET.Enum()})
w := msgio.NewWriter(rwc)
w.WriteMsg(b)
rwc.Close()
}
})
})
connectionClosedCh := make(chan struct{}, 1)
pc.SCTP().OnClose(func(err error) {
// We only need one message. Closing a connection is a problem as pion might invoke the callback more than once.
select {
case connectionClosedCh <- struct{}{}:
default:
}
})
return webRTCConnection{
PeerConnection: pc,
HandshakeDataChannel: handshakeDataChannel,
IncomingDataChannels: incomingDataChannels,
PeerConnectionClosedCh: connectionClosedCh,
}, nil
}
// IsWebRTCDirectMultiaddr returns whether addr is a /webrtc-direct multiaddr with the count of certhashes
// in addr
func IsWebRTCDirectMultiaddr(addr ma.Multiaddr) (bool, int) {
var foundUDP, foundWebRTC bool
certHashCount := 0
ma.ForEach(addr, func(c ma.Component) bool {
if !foundUDP {
if c.Protocol().Code == ma.P_UDP {
foundUDP = true
}
return true
}
if !foundWebRTC && foundUDP {
// protocol after udp must be webrtc-direct
if c.Protocol().Code != ma.P_WEBRTC_DIRECT {
return false
}
foundWebRTC = true
return true
}
if foundWebRTC {
if c.Protocol().Code == ma.P_CERTHASH {
certHashCount++
} else {
return false
}
}
return true
})
return foundUDP && foundWebRTC, certHashCount
}