Skip to content

Commit e7e01f5

Browse files
authored
fix: pass abort signal to noise and the upgrader (#2992)
To allow the noise handshake to time out, pass the abort signal to the upgrade methods
1 parent 8ac12c2 commit e7e01f5

File tree

5 files changed

+24
-33
lines changed

5 files changed

+24
-33
lines changed

packages/transport-webrtc/src/constants.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ export const DEFAULT_ICE_SERVERS = [
1616
export const UFRAG_ALPHABET = Array.from('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890')
1717

1818
export const UFRAG_PREFIX = 'libp2p+webrtc+v1/'
19+
20+
/**
21+
* The time to wait, in milliseconds, for the data channel handshake to complete
22+
*/
23+
export const HANDSHAKE_TIMEOUT_MS = 10_000
24+
export const CODEC_WEBRTC_DIRECT = 0x0118
25+
export const CODEC_CERTHASH = 0x01d2

packages/transport-webrtc/src/private-to-public/listener.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { IP4, WebRTCDirect } from '@multiformats/multiaddr-matcher'
66
import { Crypto } from '@peculiar/webcrypto'
77
import getPort from 'get-port'
88
import pWaitFor from 'p-wait-for'
9+
import { CODEC_CERTHASH, CODEC_WEBRTC_DIRECT, HANDSHAKE_TIMEOUT_MS } from '../constants.js'
910
import { connect } from './utils/connect.js'
1011
import { generateTransportCertificate } from './utils/generate-certificates.js'
1112
import { createDialerRTCPeerConnection } from './utils/get-rtcpeerconnection.js'
@@ -18,13 +19,6 @@ import type { Multiaddr } from '@multiformats/multiaddr'
1819

1920
const crypto = new Crypto()
2021

21-
/**
22-
* The time to wait, in milliseconds, for the data channel handshake to complete
23-
*/
24-
const HANDSHAKE_TIMEOUT_MS = 10_000
25-
const CODEC_WEBRTC_DIRECT = 0x0118
26-
const CODEC_CERTHASH = 0x01d2
27-
2822
export interface WebRTCDirectListenerComponents {
2923
peerId: PeerId
3024
privateKey: PrivateKey

packages/transport-webrtc/src/private-to-public/transport.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { serviceCapabilities, transportSymbol } from '@libp2p/interface'
22
import { peerIdFromString } from '@libp2p/peer-id'
3-
import { protocols } from '@multiformats/multiaddr'
43
import { WebRTCDirect } from '@multiformats/multiaddr-matcher'
54
import { raceSignal } from 'race-signal'
5+
import { HANDSHAKE_TIMEOUT_MS } from '../constants.js'
66
import { genUfrag } from '../util.js'
77
import { WebRTCDirectListener } from './listener.js'
88
import { connect } from './utils/connect.js'
@@ -13,25 +13,6 @@ import type { CreateListenerOptions, Transport, Listener, ComponentLogger, Logge
1313
import type { TransportManager } from '@libp2p/interface-internal'
1414
import type { Multiaddr } from '@multiformats/multiaddr'
1515

16-
/**
17-
* The time to wait, in milliseconds, for the data channel handshake to complete
18-
*/
19-
const HANDSHAKE_TIMEOUT_MS = 10_000
20-
21-
/**
22-
* Created by converting the hexadecimal protocol code to an integer.
23-
*
24-
* {@link https://github.com/multiformats/multiaddr/blob/master/protocols.csv}
25-
*/
26-
export const WEBRTC_CODE: number = protocols('webrtc-direct').code
27-
28-
/**
29-
* Created by converting the hexadecimal protocol code to an integer.
30-
*
31-
* {@link https://github.com/multiformats/multiaddr/blob/master/protocols.csv}
32-
*/
33-
export const CERTHASH_CODE: number = protocols('certhash').code
34-
3516
/**
3617
* The peer for this transport
3718
*/

packages/transport-webrtc/src/private-to-public/utils/connect.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export async function connect (peerConnection: DirectRTCPeerConnection, ufrag: s
146146
case 'closed':
147147
maConn.close().catch((err) => {
148148
options.log.error('error closing connection', err)
149+
maConn.abort(err)
149150
})
150151
break
151152
default:
@@ -167,18 +168,25 @@ export async function connect (peerConnection: DirectRTCPeerConnection, ufrag: s
167168
// Therefore, we need to secure an inbound noise connection from the remote.
168169
options.log.trace('%s secure inbound', options.role)
169170
await connectionEncrypter.secureInbound(wrappedDuplex, {
170-
remotePeer: options.remotePeerId
171+
remotePeer: options.remotePeerId,
172+
signal: options.signal
171173
})
172174

173175
options.log.trace('%s upgrade outbound', options.role)
174-
return options.upgrader.upgradeOutbound(maConn, { skipProtection: true, skipEncryption: true, muxerFactory })
176+
return options.upgrader.upgradeOutbound(maConn, {
177+
skipProtection: true,
178+
skipEncryption: true,
179+
muxerFactory,
180+
signal: options.signal
181+
})
175182
}
176183

177184
// For inbound connections, we are expected to start the noise handshake.
178185
// Therefore, we need to secure an outbound noise connection from the remote.
179186
options.log.trace('%s secure outbound', options.role)
180187
const result = await connectionEncrypter.secureOutbound(wrappedDuplex, {
181-
remotePeer: options.remotePeerId
188+
remotePeer: options.remotePeerId,
189+
signal: options.signal
182190
})
183191
maConn.remoteAddr = maConn.remoteAddr.encapsulate(`/p2p/${result.remotePeer}`)
184192

@@ -187,6 +195,7 @@ export async function connect (peerConnection: DirectRTCPeerConnection, ufrag: s
187195
await options.upgrader.upgradeInbound(maConn, {
188196
skipProtection: true,
189197
skipEncryption: true,
190-
muxerFactory
198+
muxerFactory,
199+
signal: options.signal
191200
})
192201
}

packages/transport-webrtc/src/private-to-public/utils/sdp.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { base64url } from 'multiformats/bases/base64'
44
import { bases, digest } from 'multiformats/basics'
55
import * as Digest from 'multiformats/hashes/digest'
66
import { sha256 } from 'multiformats/hashes/sha2'
7+
import { CODEC_CERTHASH } from '../../constants.js'
78
import { InvalidFingerprintError, UnsupportedHashAlgorithmError } from '../../error.js'
89
import { MAX_MESSAGE_SIZE } from '../../stream.js'
9-
import { CERTHASH_CODE } from '../transport.js'
1010
import type { Multiaddr } from '@multiformats/multiaddr'
1111
import type { MultihashDigest } from 'multiformats/hashes/interface'
1212

@@ -29,7 +29,7 @@ export function getFingerprintFromSdp (sdp: string | undefined): string | undefi
2929
// Extract the certhash from a multiaddr
3030
export function certhash (ma: Multiaddr): string {
3131
const tups = ma.stringTuples()
32-
const certhash = tups.filter((tup) => tup[0] === CERTHASH_CODE).map((tup) => tup[1])[0]
32+
const certhash = tups.filter((tup) => tup[0] === CODEC_CERTHASH).map((tup) => tup[1])[0]
3333

3434
if (certhash === undefined || certhash === '') {
3535
throw new InvalidParametersError(`Couldn't find a certhash component of multiaddr: ${ma.toString()}`)

0 commit comments

Comments
 (0)