libp2p 2.1.1 → 2.1.2-0c5957836
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +9 -9
- package/dist/src/connection-manager/constants.defaults.d.ts +6 -2
- package/dist/src/connection-manager/constants.defaults.d.ts.map +1 -1
- package/dist/src/connection-manager/constants.defaults.js +6 -2
- package/dist/src/connection-manager/constants.defaults.js.map +1 -1
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
- package/dist/src/connection-manager/dial-queue.js +4 -2
- package/dist/src/connection-manager/dial-queue.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +19 -4
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +46 -39
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +2 -1
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/upgrader.d.ts +33 -6
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +116 -137
- package/dist/src/upgrader.js.map +1 -1
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.d.ts.map +1 -1
- package/dist/src/version.js +1 -1
- package/dist/src/version.js.map +1 -1
- package/package.json +17 -17
- package/src/connection-manager/constants.defaults.ts +7 -2
- package/src/connection-manager/dial-queue.ts +4 -2
- package/src/connection-manager/index.ts +71 -42
- package/src/libp2p.ts +2 -1
- package/src/upgrader.ts +161 -165
- package/src/version.ts +1 -1
- package/dist/typedoc-urls.json +0 -19
package/src/upgrader.ts
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import { InvalidMultiaddrError,
|
|
1
|
+
import { InvalidMultiaddrError, TooManyInboundProtocolStreamsError, TooManyOutboundProtocolStreamsError, LimitedConnectionError, setMaxListeners } from '@libp2p/interface'
|
|
2
2
|
import * as mss from '@libp2p/multistream-select'
|
|
3
3
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
4
|
+
import { anySignal } from 'any-signal'
|
|
4
5
|
import { CustomProgressEvent } from 'progress-events'
|
|
5
6
|
import { createConnection } from './connection/index.js'
|
|
6
|
-
import {
|
|
7
|
+
import { PROTOCOL_NEGOTIATION_TIMEOUT, UPGRADE_TIMEOUT } from './connection-manager/constants.js'
|
|
7
8
|
import { ConnectionDeniedError, ConnectionInterceptedError, EncryptionFailedError, MuxerUnavailableError } from './errors.js'
|
|
8
9
|
import { DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS } from './registrar.js'
|
|
9
|
-
import type { Libp2pEvents, AbortOptions, ComponentLogger, MultiaddrConnection, Connection, Stream, ConnectionProtector, NewStreamOptions, ConnectionEncrypter, SecuredConnection, ConnectionGater, TypedEventTarget, Metrics, PeerId, PeerStore, StreamMuxer, StreamMuxerFactory, Upgrader, UpgraderOptions, ConnectionLimits, SecureConnectionOptions } from '@libp2p/interface'
|
|
10
|
+
import type { Libp2pEvents, AbortOptions, ComponentLogger, MultiaddrConnection, Connection, Stream, ConnectionProtector, NewStreamOptions, ConnectionEncrypter, SecuredConnection, ConnectionGater, TypedEventTarget, Metrics, PeerId, PeerStore, StreamMuxer, StreamMuxerFactory, Upgrader, UpgraderOptions, ConnectionLimits, SecureConnectionOptions, CounterGroup } from '@libp2p/interface'
|
|
10
11
|
import type { ConnectionManager, Registrar } from '@libp2p/interface-internal'
|
|
11
12
|
|
|
12
|
-
const DEFAULT_PROTOCOL_SELECT_TIMEOUT = 30000
|
|
13
|
-
|
|
14
13
|
interface CreateConnectionOptions {
|
|
15
14
|
cryptoProtocol: string
|
|
16
15
|
direction: 'inbound' | 'outbound'
|
|
@@ -36,10 +35,34 @@ export interface UpgraderInit {
|
|
|
36
35
|
streamMuxers: StreamMuxerFactory[]
|
|
37
36
|
|
|
38
37
|
/**
|
|
39
|
-
* An amount of ms by which an inbound connection upgrade
|
|
40
|
-
*
|
|
38
|
+
* An amount of ms by which an inbound connection upgrade must complete
|
|
39
|
+
*
|
|
40
|
+
* @default 3000
|
|
41
41
|
*/
|
|
42
42
|
inboundUpgradeTimeout?: number
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* An amount of ms by which an outbound connection upgrade must complete
|
|
46
|
+
*
|
|
47
|
+
* @default 3000
|
|
48
|
+
*/
|
|
49
|
+
outboundUpgradeTimeout?: number
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* When a new incoming stream is opened on a multiplexed connection, protocol
|
|
53
|
+
* negotiation on that stream must complete within this many ms
|
|
54
|
+
*
|
|
55
|
+
* @default 2000
|
|
56
|
+
*/
|
|
57
|
+
inboundStreamProtocolNegotiationTimeout?: number
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* When a new incoming stream is opened on a multiplexed connection, protocol
|
|
61
|
+
* negotiation on that stream must complete within this many ms
|
|
62
|
+
*
|
|
63
|
+
* @default 2000
|
|
64
|
+
*/
|
|
65
|
+
outboundStreamProtocolNegotiationTimeout?: number
|
|
43
66
|
}
|
|
44
67
|
|
|
45
68
|
function findIncomingStreamLimit (protocol: string, registrar: Registrar): number | undefined {
|
|
@@ -103,7 +126,14 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
103
126
|
private readonly connectionEncrypters: Map<string, ConnectionEncrypter>
|
|
104
127
|
private readonly streamMuxers: Map<string, StreamMuxerFactory>
|
|
105
128
|
private readonly inboundUpgradeTimeout: number
|
|
129
|
+
private readonly outboundUpgradeTimeout: number
|
|
130
|
+
private readonly inboundStreamProtocolNegotiationTimeout: number
|
|
131
|
+
private readonly outboundStreamProtocolNegotiationTimeout: number
|
|
106
132
|
private readonly events: TypedEventTarget<Libp2pEvents>
|
|
133
|
+
private readonly metrics: {
|
|
134
|
+
dials?: CounterGroup<'inbound' | 'outbound'>
|
|
135
|
+
errors?: CounterGroup<'inbound' | 'outbound'>
|
|
136
|
+
}
|
|
107
137
|
|
|
108
138
|
constructor (components: DefaultUpgraderComponents, init: UpgraderInit) {
|
|
109
139
|
this.components = components
|
|
@@ -119,135 +149,62 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
119
149
|
this.streamMuxers.set(muxer.protocol, muxer)
|
|
120
150
|
})
|
|
121
151
|
|
|
122
|
-
this.inboundUpgradeTimeout = init.inboundUpgradeTimeout ??
|
|
152
|
+
this.inboundUpgradeTimeout = init.inboundUpgradeTimeout ?? UPGRADE_TIMEOUT
|
|
153
|
+
this.outboundUpgradeTimeout = init.outboundUpgradeTimeout ?? UPGRADE_TIMEOUT
|
|
154
|
+
this.inboundStreamProtocolNegotiationTimeout = init.inboundStreamProtocolNegotiationTimeout ?? PROTOCOL_NEGOTIATION_TIMEOUT
|
|
155
|
+
this.outboundStreamProtocolNegotiationTimeout = init.outboundStreamProtocolNegotiationTimeout ?? PROTOCOL_NEGOTIATION_TIMEOUT
|
|
123
156
|
this.events = components.events
|
|
157
|
+
this.metrics = {
|
|
158
|
+
dials: components.metrics?.registerCounterGroup('libp2p_connection_manager_dials_total'),
|
|
159
|
+
errors: components.metrics?.registerCounterGroup('libp2p_connection_manager_dial_errors_total')
|
|
160
|
+
}
|
|
124
161
|
}
|
|
125
162
|
|
|
126
163
|
readonly [Symbol.toStringTag] = '@libp2p/upgrader'
|
|
127
164
|
|
|
128
|
-
async shouldBlockConnection (
|
|
129
|
-
|
|
165
|
+
async shouldBlockConnection (connectionType: 'denyInboundConnection', maConn: MultiaddrConnection): Promise<void>
|
|
166
|
+
async shouldBlockConnection (connectionType: ConnectionDeniedType, remotePeer: PeerId, maConn: MultiaddrConnection): Promise<void>
|
|
167
|
+
async shouldBlockConnection (method: ConnectionDeniedType | 'denyInboundConnection', ...args: any[]): Promise<void> {
|
|
168
|
+
const denyOperation: any = this.components.connectionGater[method]
|
|
130
169
|
|
|
131
|
-
if (
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
170
|
+
if (denyOperation == null) {
|
|
171
|
+
return
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const result = await denyOperation.apply(this.components.connectionGater, args)
|
|
175
|
+
|
|
176
|
+
if (result === true) {
|
|
177
|
+
throw new ConnectionInterceptedError(`The multiaddr connection is blocked by gater.${method}`)
|
|
135
178
|
}
|
|
136
179
|
}
|
|
137
180
|
|
|
138
181
|
/**
|
|
139
182
|
* Upgrades an inbound connection
|
|
140
183
|
*/
|
|
141
|
-
async upgradeInbound (maConn: MultiaddrConnection, opts
|
|
142
|
-
const accept = await this.components.connectionManager.acceptIncomingConnection(maConn)
|
|
143
|
-
|
|
144
|
-
if (!accept) {
|
|
145
|
-
throw new ConnectionDeniedError('connection denied')
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
let encryptedConn: MultiaddrConnection
|
|
149
|
-
let remotePeer
|
|
150
|
-
let upgradedConn: MultiaddrConnection
|
|
151
|
-
let muxerFactory: StreamMuxerFactory | undefined
|
|
152
|
-
let cryptoProtocol
|
|
153
|
-
|
|
154
|
-
const signal = AbortSignal.timeout(this.inboundUpgradeTimeout)
|
|
155
|
-
|
|
156
|
-
const onAbort = (): void => {
|
|
157
|
-
maConn.abort(new TimeoutError('inbound upgrade timeout'))
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
signal.addEventListener('abort', onAbort, { once: true })
|
|
161
|
-
|
|
162
|
-
setMaxListeners(Infinity, signal)
|
|
163
|
-
|
|
184
|
+
async upgradeInbound (maConn: MultiaddrConnection, opts: UpgraderOptions = {}): Promise<Connection> {
|
|
164
185
|
try {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
this.components.metrics?.trackMultiaddrConnection(maConn)
|
|
170
|
-
|
|
171
|
-
maConn.log('starting the inbound connection upgrade')
|
|
172
|
-
|
|
173
|
-
// Protect
|
|
174
|
-
let protectedConn = maConn
|
|
175
|
-
|
|
176
|
-
if (opts?.skipProtection !== true) {
|
|
177
|
-
const protector = this.components.connectionProtector
|
|
178
|
-
|
|
179
|
-
if (protector != null) {
|
|
180
|
-
maConn.log('protecting the inbound connection')
|
|
181
|
-
protectedConn = await protector.protect(maConn)
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
try {
|
|
186
|
-
// Encrypt the connection
|
|
187
|
-
encryptedConn = protectedConn
|
|
188
|
-
if (opts?.skipEncryption !== true) {
|
|
189
|
-
opts?.onProgress?.(new CustomProgressEvent('upgrader:encrypt-inbound-connection'));
|
|
190
|
-
|
|
191
|
-
({
|
|
192
|
-
conn: encryptedConn,
|
|
193
|
-
remotePeer,
|
|
194
|
-
protocol: cryptoProtocol
|
|
195
|
-
} = await this._encryptInbound(protectedConn))
|
|
196
|
-
|
|
197
|
-
const maConn: MultiaddrConnection = {
|
|
198
|
-
...protectedConn,
|
|
199
|
-
...encryptedConn
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
await this.shouldBlockConnection(remotePeer, maConn, 'denyInboundEncryptedConnection')
|
|
203
|
-
} else {
|
|
204
|
-
const idStr = maConn.remoteAddr.getPeerId()
|
|
205
|
-
|
|
206
|
-
if (idStr == null) {
|
|
207
|
-
throw new InvalidMultiaddrError('inbound connection that skipped encryption must have a peer id')
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const remotePeerId = peerIdFromString(idStr)
|
|
211
|
-
|
|
212
|
-
cryptoProtocol = 'native'
|
|
213
|
-
remotePeer = remotePeerId
|
|
214
|
-
}
|
|
186
|
+
this.metrics.dials?.increment({
|
|
187
|
+
inbound: true
|
|
188
|
+
})
|
|
215
189
|
|
|
216
|
-
|
|
217
|
-
if (opts?.muxerFactory != null) {
|
|
218
|
-
muxerFactory = opts.muxerFactory
|
|
219
|
-
} else if (this.streamMuxers.size > 0) {
|
|
220
|
-
opts?.onProgress?.(new CustomProgressEvent('upgrader:multiplex-inbound-connection'))
|
|
190
|
+
const accept = await this.components.connectionManager.acceptIncomingConnection(maConn)
|
|
221
191
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
...protectedConn,
|
|
225
|
-
...encryptedConn
|
|
226
|
-
}, this.streamMuxers)
|
|
227
|
-
muxerFactory = multiplexed.muxerFactory
|
|
228
|
-
upgradedConn = multiplexed.stream
|
|
229
|
-
}
|
|
230
|
-
} catch (err: any) {
|
|
231
|
-
maConn.log.error('failed to upgrade inbound connection', err)
|
|
232
|
-
throw err
|
|
192
|
+
if (!accept) {
|
|
193
|
+
throw new ConnectionDeniedError('connection denied')
|
|
233
194
|
}
|
|
234
195
|
|
|
235
|
-
await this.shouldBlockConnection(
|
|
196
|
+
await this.shouldBlockConnection('denyInboundConnection', maConn)
|
|
236
197
|
|
|
237
|
-
|
|
198
|
+
const conn = await this._performUpgrade(maConn, 'inbound', opts)
|
|
238
199
|
|
|
239
|
-
return
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
upgradedConn,
|
|
244
|
-
muxerFactory,
|
|
245
|
-
remotePeer,
|
|
246
|
-
limits: opts?.limits
|
|
200
|
+
return conn
|
|
201
|
+
} catch (err) {
|
|
202
|
+
this.metrics.errors?.increment({
|
|
203
|
+
inbound: true
|
|
247
204
|
})
|
|
248
|
-
} finally {
|
|
249
|
-
signal.removeEventListener('abort', onAbort)
|
|
250
205
|
|
|
206
|
+
throw err
|
|
207
|
+
} finally {
|
|
251
208
|
this.components.connectionManager.afterUpgradeInbound()
|
|
252
209
|
}
|
|
253
210
|
}
|
|
@@ -255,36 +212,55 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
255
212
|
/**
|
|
256
213
|
* Upgrades an outbound connection
|
|
257
214
|
*/
|
|
258
|
-
async upgradeOutbound (maConn: MultiaddrConnection, opts
|
|
259
|
-
|
|
260
|
-
|
|
215
|
+
async upgradeOutbound (maConn: MultiaddrConnection, opts: UpgraderOptions = {}): Promise<Connection> {
|
|
216
|
+
try {
|
|
217
|
+
this.metrics.dials?.increment({
|
|
218
|
+
outbound: true
|
|
219
|
+
})
|
|
261
220
|
|
|
262
|
-
|
|
263
|
-
remotePeerId
|
|
221
|
+
const idStr = maConn.remoteAddr.getPeerId()
|
|
222
|
+
let remotePeerId: PeerId | undefined
|
|
264
223
|
|
|
265
|
-
|
|
224
|
+
if (idStr != null) {
|
|
225
|
+
remotePeerId = peerIdFromString(idStr)
|
|
226
|
+
await this.shouldBlockConnection('denyOutboundConnection', remotePeerId, maConn)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return await this._performUpgrade(maConn, 'outbound', opts)
|
|
230
|
+
} catch (err) {
|
|
231
|
+
this.metrics.errors?.increment({
|
|
232
|
+
outbound: true
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
throw err
|
|
266
236
|
}
|
|
237
|
+
}
|
|
267
238
|
|
|
239
|
+
private async _performUpgrade (maConn: MultiaddrConnection, direction: 'inbound' | 'outbound', opts: UpgraderOptions): Promise<Connection> {
|
|
268
240
|
let encryptedConn: MultiaddrConnection
|
|
269
241
|
let remotePeer: PeerId
|
|
270
242
|
let upgradedConn: MultiaddrConnection
|
|
243
|
+
let muxerFactory: StreamMuxerFactory | undefined
|
|
271
244
|
let cryptoProtocol
|
|
272
|
-
let muxerFactory
|
|
273
245
|
|
|
274
|
-
this.
|
|
246
|
+
const upgradeTimeoutSignal = AbortSignal.timeout(direction === 'inbound' ? this.inboundUpgradeTimeout : this.outboundUpgradeTimeout)
|
|
247
|
+
const signal = anySignal([upgradeTimeoutSignal, opts.signal])
|
|
248
|
+
setMaxListeners(Infinity, upgradeTimeoutSignal, signal)
|
|
249
|
+
opts.signal = signal
|
|
275
250
|
|
|
276
|
-
|
|
251
|
+
this.components.metrics?.trackMultiaddrConnection(maConn)
|
|
277
252
|
|
|
278
|
-
|
|
279
|
-
// protector and encryption
|
|
253
|
+
maConn.log.trace('starting the %s connection upgrade', direction)
|
|
280
254
|
|
|
281
255
|
// Protect
|
|
282
256
|
let protectedConn = maConn
|
|
257
|
+
|
|
283
258
|
if (opts?.skipProtection !== true) {
|
|
284
259
|
const protector = this.components.connectionProtector
|
|
285
260
|
|
|
286
261
|
if (protector != null) {
|
|
287
|
-
|
|
262
|
+
maConn.log('protecting the %s connection', direction)
|
|
263
|
+
protectedConn = await protector.protect(maConn, opts)
|
|
288
264
|
}
|
|
289
265
|
}
|
|
290
266
|
|
|
@@ -292,26 +268,38 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
292
268
|
// Encrypt the connection
|
|
293
269
|
encryptedConn = protectedConn
|
|
294
270
|
if (opts?.skipEncryption !== true) {
|
|
271
|
+
opts?.onProgress?.(new CustomProgressEvent(`upgrader:encrypt-${direction}-connection`));
|
|
272
|
+
|
|
295
273
|
({
|
|
296
274
|
conn: encryptedConn,
|
|
297
275
|
remotePeer,
|
|
298
276
|
protocol: cryptoProtocol
|
|
299
|
-
} = await
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
277
|
+
} = await (direction === 'inbound'
|
|
278
|
+
? this._encryptInbound(protectedConn, {
|
|
279
|
+
...opts,
|
|
280
|
+
signal
|
|
281
|
+
})
|
|
282
|
+
: this._encryptOutbound(protectedConn, {
|
|
283
|
+
...opts,
|
|
284
|
+
signal
|
|
285
|
+
})
|
|
286
|
+
))
|
|
303
287
|
|
|
304
288
|
const maConn: MultiaddrConnection = {
|
|
305
289
|
...protectedConn,
|
|
306
290
|
...encryptedConn
|
|
307
291
|
}
|
|
308
292
|
|
|
309
|
-
await this.shouldBlockConnection(
|
|
293
|
+
await this.shouldBlockConnection(direction === 'inbound' ? 'denyInboundEncryptedConnection' : 'denyOutboundEncryptedConnection', remotePeer, maConn)
|
|
310
294
|
} else {
|
|
311
|
-
|
|
312
|
-
|
|
295
|
+
const idStr = maConn.remoteAddr.getPeerId()
|
|
296
|
+
|
|
297
|
+
if (idStr == null) {
|
|
298
|
+
throw new InvalidMultiaddrError(`${direction} connection that skipped encryption must have a peer id`)
|
|
313
299
|
}
|
|
314
300
|
|
|
301
|
+
const remotePeerId = peerIdFromString(idStr)
|
|
302
|
+
|
|
315
303
|
cryptoProtocol = 'native'
|
|
316
304
|
remotePeer = remotePeerId
|
|
317
305
|
}
|
|
@@ -320,27 +308,33 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
320
308
|
if (opts?.muxerFactory != null) {
|
|
321
309
|
muxerFactory = opts.muxerFactory
|
|
322
310
|
} else if (this.streamMuxers.size > 0) {
|
|
311
|
+
opts?.onProgress?.(new CustomProgressEvent(`upgrader:multiplex-${direction}-connection`))
|
|
312
|
+
|
|
323
313
|
// Multiplex the connection
|
|
324
|
-
const multiplexed = await
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
314
|
+
const multiplexed = await (direction === 'inbound'
|
|
315
|
+
? this._multiplexInbound({
|
|
316
|
+
...protectedConn,
|
|
317
|
+
...encryptedConn
|
|
318
|
+
}, this.streamMuxers, opts)
|
|
319
|
+
: this._multiplexOutbound({
|
|
320
|
+
...protectedConn,
|
|
321
|
+
...encryptedConn
|
|
322
|
+
}, this.streamMuxers, opts))
|
|
328
323
|
muxerFactory = multiplexed.muxerFactory
|
|
329
324
|
upgradedConn = multiplexed.stream
|
|
330
325
|
}
|
|
331
326
|
} catch (err: any) {
|
|
332
|
-
maConn.log.error('failed to upgrade
|
|
333
|
-
await maConn.close(err)
|
|
327
|
+
maConn.log.error('failed to upgrade inbound connection %s %a - %e', direction === 'inbound' ? 'from' : 'to', maConn.remoteAddr, err)
|
|
334
328
|
throw err
|
|
335
329
|
}
|
|
336
330
|
|
|
337
|
-
await this.shouldBlockConnection(
|
|
331
|
+
await this.shouldBlockConnection(direction === 'inbound' ? 'denyInboundUpgradedConnection' : 'denyOutboundUpgradedConnection', remotePeer, maConn)
|
|
338
332
|
|
|
339
|
-
maConn.log('successfully upgraded
|
|
333
|
+
maConn.log('successfully upgraded %s connection', direction)
|
|
340
334
|
|
|
341
335
|
return this._createConnection({
|
|
342
336
|
cryptoProtocol,
|
|
343
|
-
direction
|
|
337
|
+
direction,
|
|
344
338
|
maConn,
|
|
345
339
|
upgradedConn,
|
|
346
340
|
muxerFactory,
|
|
@@ -380,7 +374,11 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
380
374
|
void Promise.resolve()
|
|
381
375
|
.then(async () => {
|
|
382
376
|
const protocols = this.components.registrar.getProtocols()
|
|
377
|
+
const signal = AbortSignal.timeout(this.inboundStreamProtocolNegotiationTimeout)
|
|
378
|
+
setMaxListeners(Infinity, signal)
|
|
379
|
+
|
|
383
380
|
const { stream, protocol } = await mss.handle(muxedStream, protocols, {
|
|
381
|
+
signal,
|
|
384
382
|
log: muxedStream.log,
|
|
385
383
|
yieldBytes: false
|
|
386
384
|
})
|
|
@@ -433,7 +431,7 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
433
431
|
this._onStream({ connection, stream: muxedStream, protocol })
|
|
434
432
|
})
|
|
435
433
|
.catch(async err => {
|
|
436
|
-
connection.log.error('error handling incoming stream id %s', muxedStream.id, err
|
|
434
|
+
connection.log.error('error handling incoming stream id %s - %e', muxedStream.id, err)
|
|
437
435
|
|
|
438
436
|
if (muxedStream.timeline.close == null) {
|
|
439
437
|
await muxedStream.close()
|
|
@@ -447,7 +445,7 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
447
445
|
throw new MuxerUnavailableError('Connection is not multiplexed')
|
|
448
446
|
}
|
|
449
447
|
|
|
450
|
-
connection.log('starting new stream for protocols %s', protocols)
|
|
448
|
+
connection.log.trace('starting new stream for protocols %s', protocols)
|
|
451
449
|
const muxedStream = await muxer.newStream()
|
|
452
450
|
connection.log.trace('started new stream %s for protocols %s', muxedStream.id, protocols)
|
|
453
451
|
|
|
@@ -455,7 +453,7 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
455
453
|
if (options.signal == null) {
|
|
456
454
|
muxedStream.log('no abort signal was passed while trying to negotiate protocols %s falling back to default timeout', protocols)
|
|
457
455
|
|
|
458
|
-
const signal = AbortSignal.timeout(
|
|
456
|
+
const signal = AbortSignal.timeout(this.outboundStreamProtocolNegotiationTimeout)
|
|
459
457
|
setMaxListeners(Infinity, signal)
|
|
460
458
|
|
|
461
459
|
options = {
|
|
@@ -475,7 +473,7 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
475
473
|
yieldBytes: true
|
|
476
474
|
})
|
|
477
475
|
|
|
478
|
-
muxedStream.log('selected protocol %s', protocol)
|
|
476
|
+
muxedStream.log.trace('selected protocol %s', protocol)
|
|
479
477
|
|
|
480
478
|
const outgoingLimit = findOutgoingStreamLimit(protocol, this.components.registrar, options)
|
|
481
479
|
const streamCount = countStreams(protocol, 'outbound', connection)
|
|
@@ -518,7 +516,7 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
518
516
|
|
|
519
517
|
return muxedStream
|
|
520
518
|
} catch (err: any) {
|
|
521
|
-
connection.log.error('could not create new stream for protocols %s', protocols, err)
|
|
519
|
+
connection.log.error('could not create new outbound stream on connection %s %a for protocols %s - %e', direction === 'inbound' ? 'from' : 'to', opts.maConn.remoteAddr, protocols, err)
|
|
522
520
|
|
|
523
521
|
if (muxedStream.timeline.close == null) {
|
|
524
522
|
muxedStream.abort(err)
|
|
@@ -533,7 +531,7 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
533
531
|
muxer.sink(upgradedConn.source),
|
|
534
532
|
upgradedConn.sink(muxer.source)
|
|
535
533
|
]).catch(err => {
|
|
536
|
-
connection.log.error('error piping data through muxer', err)
|
|
534
|
+
connection.log.error('error piping data through muxer - %e', err)
|
|
537
535
|
})
|
|
538
536
|
}
|
|
539
537
|
|
|
@@ -628,26 +626,26 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
628
626
|
*/
|
|
629
627
|
async _encryptInbound (connection: MultiaddrConnection, options?: AbortOptions): Promise<CryptoResult> {
|
|
630
628
|
const protocols = Array.from(this.connectionEncrypters.keys())
|
|
631
|
-
connection.log('handling inbound crypto protocol selection', protocols)
|
|
632
629
|
|
|
633
630
|
try {
|
|
634
631
|
const { stream, protocol } = await mss.handle(connection, protocols, {
|
|
632
|
+
...options,
|
|
635
633
|
log: connection.log
|
|
636
634
|
})
|
|
637
635
|
const encrypter = this.connectionEncrypters.get(protocol)
|
|
638
636
|
|
|
639
637
|
if (encrypter == null) {
|
|
640
|
-
throw new
|
|
638
|
+
throw new EncryptionFailedError(`no crypto module found for ${protocol}`)
|
|
641
639
|
}
|
|
642
640
|
|
|
643
|
-
connection.log('encrypting inbound connection using', protocol)
|
|
641
|
+
connection.log('encrypting inbound connection to %a using %s', connection.remoteAddr, protocol)
|
|
644
642
|
|
|
645
643
|
return {
|
|
646
644
|
...await encrypter.secureInbound(stream, options),
|
|
647
645
|
protocol
|
|
648
646
|
}
|
|
649
647
|
} catch (err: any) {
|
|
650
|
-
connection.log.error('encrypting inbound connection failed', err)
|
|
648
|
+
connection.log.error('encrypting inbound connection from %a failed', connection.remoteAddr, err)
|
|
651
649
|
throw new EncryptionFailedError(err.message)
|
|
652
650
|
}
|
|
653
651
|
}
|
|
@@ -656,35 +654,31 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
656
654
|
* Attempts to encrypt the given `connection` with the provided connection encrypters.
|
|
657
655
|
* The first `ConnectionEncrypter` module to succeed will be used
|
|
658
656
|
*/
|
|
659
|
-
async _encryptOutbound (connection: MultiaddrConnection, options
|
|
657
|
+
async _encryptOutbound (connection: MultiaddrConnection, options: SecureConnectionOptions): Promise<CryptoResult> {
|
|
660
658
|
const protocols = Array.from(this.connectionEncrypters.keys())
|
|
661
|
-
connection.log('selecting outbound crypto protocol', protocols)
|
|
662
659
|
|
|
663
660
|
try {
|
|
664
661
|
connection.log.trace('selecting encrypter from %s', protocols)
|
|
665
662
|
|
|
666
|
-
const {
|
|
667
|
-
|
|
668
|
-
protocol
|
|
669
|
-
} = await mss.select(connection, protocols, {
|
|
663
|
+
const { stream, protocol } = await mss.select(connection, protocols, {
|
|
664
|
+
...options,
|
|
670
665
|
log: connection.log,
|
|
671
666
|
yieldBytes: true
|
|
672
667
|
})
|
|
673
|
-
|
|
674
668
|
const encrypter = this.connectionEncrypters.get(protocol)
|
|
675
669
|
|
|
676
670
|
if (encrypter == null) {
|
|
677
|
-
throw new
|
|
671
|
+
throw new EncryptionFailedError(`no crypto module found for ${protocol}`)
|
|
678
672
|
}
|
|
679
673
|
|
|
680
|
-
connection.log('encrypting outbound connection to %
|
|
674
|
+
connection.log('encrypting outbound connection to %a using %s', connection.remoteAddr, protocol)
|
|
681
675
|
|
|
682
676
|
return {
|
|
683
677
|
...await encrypter.secureOutbound(stream, options),
|
|
684
678
|
protocol
|
|
685
679
|
}
|
|
686
680
|
} catch (err: any) {
|
|
687
|
-
connection.log.error('encrypting outbound connection to %
|
|
681
|
+
connection.log.error('encrypting outbound connection to %a failed', connection.remoteAddr, err)
|
|
688
682
|
throw new EncryptionFailedError(err.message)
|
|
689
683
|
}
|
|
690
684
|
}
|
|
@@ -693,7 +687,7 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
693
687
|
* Selects one of the given muxers via multistream-select. That
|
|
694
688
|
* muxer will be used for all future streams on the connection.
|
|
695
689
|
*/
|
|
696
|
-
async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory
|
|
690
|
+
async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>, options: AbortOptions): Promise<{ stream: MultiaddrConnection, muxerFactory?: StreamMuxerFactory }> {
|
|
697
691
|
const protocols = Array.from(muxers.keys())
|
|
698
692
|
connection.log('outbound selecting muxer %s', protocols)
|
|
699
693
|
try {
|
|
@@ -703,6 +697,7 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
703
697
|
stream,
|
|
704
698
|
protocol
|
|
705
699
|
} = await mss.select(connection, protocols, {
|
|
700
|
+
...options,
|
|
706
701
|
log: connection.log,
|
|
707
702
|
yieldBytes: true
|
|
708
703
|
})
|
|
@@ -721,11 +716,12 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
721
716
|
* Registers support for one of the given muxers via multistream-select. The
|
|
722
717
|
* selected muxer will be used for all future streams on the connection.
|
|
723
718
|
*/
|
|
724
|
-
async _multiplexInbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory
|
|
719
|
+
async _multiplexInbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>, options: AbortOptions): Promise<{ stream: MultiaddrConnection, muxerFactory?: StreamMuxerFactory }> {
|
|
725
720
|
const protocols = Array.from(muxers.keys())
|
|
726
721
|
connection.log('inbound handling muxers %s', protocols)
|
|
727
722
|
try {
|
|
728
723
|
const { stream, protocol } = await mss.handle(connection, protocols, {
|
|
724
|
+
...options,
|
|
729
725
|
log: connection.log
|
|
730
726
|
})
|
|
731
727
|
const muxerFactory = muxers.get(protocol)
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const version = '2.1.
|
|
1
|
+
export const version = '2.1.2-0c5957836'
|
|
2
2
|
export const name = 'libp2p'
|
package/dist/typedoc-urls.json
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"AddressFilter": "https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.AddressFilter.html",
|
|
3
|
-
"AddressManagerInit": "https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.AddressManagerInit.html",
|
|
4
|
-
"ConnectionManagerInit": "https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.ConnectionManagerInit.html",
|
|
5
|
-
"ConnectionMonitorInit": "https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.ConnectionMonitorInit.html",
|
|
6
|
-
"Libp2pInit": "https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.Libp2pInit.html",
|
|
7
|
-
".:Libp2pInit": "https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.Libp2pInit.html",
|
|
8
|
-
"TransportManagerInit": "https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.TransportManagerInit.html",
|
|
9
|
-
"Libp2pOptions": "https://libp2p.github.io/js-libp2p/types/libp2p.index.Libp2pOptions.html",
|
|
10
|
-
".:Libp2pOptions": "https://libp2p.github.io/js-libp2p/types/libp2p.index.Libp2pOptions.html",
|
|
11
|
-
"ServiceFactoryMap": "https://libp2p.github.io/js-libp2p/types/libp2p.index.ServiceFactoryMap.html",
|
|
12
|
-
".:ServiceFactoryMap": "https://libp2p.github.io/js-libp2p/types/libp2p.index.ServiceFactoryMap.html",
|
|
13
|
-
"createLibp2p": "https://libp2p.github.io/js-libp2p/functions/libp2p.index.createLibp2p.html",
|
|
14
|
-
".:createLibp2p": "https://libp2p.github.io/js-libp2p/functions/libp2p.index.createLibp2p.html",
|
|
15
|
-
"name": "https://libp2p.github.io/js-libp2p/variables/libp2p.version.name.html",
|
|
16
|
-
"./version:name": "https://libp2p.github.io/js-libp2p/variables/libp2p.version.name.html",
|
|
17
|
-
"version": "https://libp2p.github.io/js-libp2p/variables/libp2p.version.version.html",
|
|
18
|
-
"./version:version": "https://libp2p.github.io/js-libp2p/variables/libp2p.version.version.html"
|
|
19
|
-
}
|