Skip to content

Commit 3833353

Browse files
authored
feat: add skip muxer negotiation (#3081)
Some transports have their own muxers but use existing encrypers to do a handshake so skip sending muxer lists as it is not necessary.
1 parent ae7d867 commit 3833353

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

packages/connection-encrypter-tls/src/tls.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ export class TLS implements ConnectionEncrypter {
9090
async _encrypt <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (conn: Stream, isServer: boolean, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
9191
let streamMuxer: StreamMuxerFactory | undefined
9292

93+
let streamMuxers: string[] = []
94+
95+
if (options?.skipStreamMuxerNegotiation !== true) {
96+
streamMuxers = [...this.components.upgrader.getStreamMuxers().keys()]
97+
}
98+
9399
const opts: TLSSocketOptions = {
94100
...await generateCertificate(this.components.privateKey),
95101
isServer,
@@ -101,7 +107,7 @@ export class TLS implements ConnectionEncrypter {
101107

102108
// early negotiation of muxer via ALPN protocols
103109
ALPNProtocols: [
104-
...this.components.upgrader.getStreamMuxers().keys(),
110+
...streamMuxers,
105111
'libp2p'
106112
],
107113
ALPNCallback: ({ protocols }) => {
@@ -158,17 +164,9 @@ export class TLS implements ConnectionEncrypter {
158164
.then(remotePeer => {
159165
this.log('remote certificate ok, remote peer %p', remotePeer)
160166

161-
if (!isServer && typeof socket.alpnProtocol === 'string') {
162-
streamMuxer = this.components.upgrader.getStreamMuxers().get(socket.alpnProtocol)
163-
164-
if (streamMuxer == null) {
165-
this.log.error('selected muxer that did not exist')
166-
}
167-
}
168-
169167
// 'libp2p' is a special protocol - if it's sent the remote does not
170168
// support early muxer negotiation
171-
if (!isServer && typeof socket.alpnProtocol === 'string' && socket.alpnProtocol !== 'libp2p') {
169+
if (!isServer && typeof socket.alpnProtocol === 'string' && socket.alpnProtocol !== 'libp2p' && options?.skipStreamMuxerNegotiation !== true) {
172170
this.log.trace('got early muxer', socket.alpnProtocol)
173171
streamMuxer = this.components.upgrader.getStreamMuxers().get(socket.alpnProtocol)
174172

packages/connection-encrypter-tls/test/index.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,26 @@ describe('tls', () => {
113113
expect(result).to.have.nested.property('[0].streamMuxer.protocol', '/test/muxer')
114114
expect(result).to.have.nested.property('[1].streamMuxer.protocol', '/test/muxer')
115115
})
116+
117+
it('should not select an early muxer when it is skipped', async () => {
118+
const [inbound, outbound] = duplexPair<any>()
119+
120+
const result = await Promise.all([
121+
encrypter.secureInbound(stubInterface<MultiaddrConnection>({
122+
...inbound
123+
}), {
124+
remotePeer: localPeer,
125+
skipStreamMuxerNegotiation: true
126+
}),
127+
encrypter.secureOutbound(stubInterface<MultiaddrConnection>({
128+
...outbound
129+
}), {
130+
remotePeer: localPeer,
131+
skipStreamMuxerNegotiation: true
132+
})
133+
])
134+
135+
expect(result).to.have.nested.property('[0].streamMuxer', undefined)
136+
expect(result).to.have.nested.property('[1].streamMuxer', undefined)
137+
})
116138
})

packages/interface/src/connection-encrypter.ts

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ import type { Uint8ArrayList } from 'uint8arraylist'
1111
*/
1212
export interface SecureConnectionOptions extends AbortOptions {
1313
remotePeer?: PeerId
14+
15+
/**
16+
* Some encryption protocols allow negotiating application protocols as part
17+
* of the initial handshake. The negotiated stream muxer protocol will be
18+
* included as part of the from the `secureOutbound`/`secureInbound` methods
19+
* unless `false` is passed here.
20+
*/
21+
skipStreamMuxerNegotiation?: boolean
1422
}
1523

1624
/**

0 commit comments

Comments
 (0)