libp2p 2.1.1-c258b35af → 2.1.1
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 +15 -15
- package/dist/src/connection-manager/constants.defaults.d.ts +2 -6
- package/dist/src/connection-manager/constants.defaults.d.ts.map +1 -1
- package/dist/src/connection-manager/constants.defaults.js +2 -6
- package/dist/src/connection-manager/constants.defaults.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +4 -18
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +39 -30
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +1 -2
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/upgrader.d.ts +6 -32
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +121 -76
- 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/dist/typedoc-urls.json +19 -0
- package/package.json +17 -17
- package/src/connection-manager/constants.defaults.ts +2 -7
- package/src/connection-manager/index.ts +44 -52
- package/src/libp2p.ts +1 -2
- package/src/upgrader.ts +146 -116
- package/src/version.ts +1 -1
|
@@ -57,34 +57,17 @@ export interface ConnectionManagerInit {
|
|
|
57
57
|
/**
|
|
58
58
|
* How long a dial attempt is allowed to take, including DNS resolution
|
|
59
59
|
* of the multiaddr, opening a socket and upgrading it to a Connection.
|
|
60
|
-
*
|
|
61
|
-
* @default 5000
|
|
62
60
|
*/
|
|
63
61
|
dialTimeout?: number
|
|
64
62
|
|
|
65
63
|
/**
|
|
66
|
-
* When a new
|
|
67
|
-
*
|
|
64
|
+
* When a new inbound connection is opened, the upgrade process (e.g. protect,
|
|
65
|
+
* encrypt, multiplex etc) must complete within this number of ms.
|
|
68
66
|
*
|
|
69
|
-
* @default
|
|
67
|
+
* @default 30000
|
|
70
68
|
*/
|
|
71
69
|
inboundUpgradeTimeout?: number
|
|
72
70
|
|
|
73
|
-
/**
|
|
74
|
-
* When a new outbound connection is opened, the upgrade process (e.g.
|
|
75
|
-
* protect, encrypt, multiplex etc) must complete within this number of ms.
|
|
76
|
-
*
|
|
77
|
-
* @default 3000
|
|
78
|
-
*/
|
|
79
|
-
outboundUpgradeTimeout?: number
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Protocol negotiation must complete within this number of ms
|
|
83
|
-
*
|
|
84
|
-
* @default 2000
|
|
85
|
-
*/
|
|
86
|
-
protocolNegotiationTimeout?: number
|
|
87
|
-
|
|
88
71
|
/**
|
|
89
72
|
* Multiaddr resolvers to use when dialling
|
|
90
73
|
*/
|
|
@@ -181,6 +164,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
181
164
|
private readonly deny: Multiaddr[]
|
|
182
165
|
private readonly maxIncomingPendingConnections: number
|
|
183
166
|
private incomingPendingConnections: number
|
|
167
|
+
private outboundPendingConnections: number
|
|
184
168
|
private readonly maxConnections: number
|
|
185
169
|
|
|
186
170
|
public readonly dialQueue: DialQueue
|
|
@@ -219,6 +203,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
219
203
|
this.allow = (init.allow ?? []).map(ma => multiaddr(ma))
|
|
220
204
|
this.deny = (init.deny ?? []).map(ma => multiaddr(ma))
|
|
221
205
|
|
|
206
|
+
this.outboundPendingConnections = 0
|
|
222
207
|
this.incomingPendingConnections = 0
|
|
223
208
|
this.maxIncomingPendingConnections = init.maxIncomingPendingConnections ?? defaultOptions.maxIncomingPendingConnections
|
|
224
209
|
|
|
@@ -281,7 +266,8 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
281
266
|
const metric = {
|
|
282
267
|
inbound: 0,
|
|
283
268
|
'inbound pending': this.incomingPendingConnections,
|
|
284
|
-
outbound: 0
|
|
269
|
+
outbound: 0,
|
|
270
|
+
'outbound pending': this.outboundPendingConnections
|
|
285
271
|
}
|
|
286
272
|
|
|
287
273
|
for (const conns of this.connections.values()) {
|
|
@@ -482,48 +468,54 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
482
468
|
|
|
483
469
|
options.signal?.throwIfAborted()
|
|
484
470
|
|
|
485
|
-
|
|
471
|
+
try {
|
|
472
|
+
this.outboundPendingConnections++
|
|
486
473
|
|
|
487
|
-
|
|
488
|
-
this.log('dial %p', peerId)
|
|
489
|
-
const existingConnection = this.getConnections(peerId)
|
|
490
|
-
.find(conn => conn.limits == null)
|
|
474
|
+
const { peerId } = getPeerAddress(peerIdOrMultiaddr)
|
|
491
475
|
|
|
492
|
-
if (
|
|
493
|
-
this.log('
|
|
476
|
+
if (peerId != null && options.force !== true) {
|
|
477
|
+
this.log('dial %p', peerId)
|
|
478
|
+
const existingConnection = this.getConnections(peerId)
|
|
479
|
+
.find(conn => conn.limits == null)
|
|
494
480
|
|
|
495
|
-
|
|
496
|
-
|
|
481
|
+
if (existingConnection != null) {
|
|
482
|
+
this.log('had an existing non-limited connection to %p', peerId)
|
|
483
|
+
|
|
484
|
+
options.onProgress?.(new CustomProgressEvent('dial-queue:already-connected'))
|
|
485
|
+
return existingConnection
|
|
486
|
+
}
|
|
497
487
|
}
|
|
498
|
-
}
|
|
499
488
|
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
489
|
+
const connection = await this.dialQueue.dial(peerIdOrMultiaddr, {
|
|
490
|
+
...options,
|
|
491
|
+
priority: options.priority ?? DEFAULT_DIAL_PRIORITY
|
|
492
|
+
})
|
|
493
|
+
let peerConnections = this.connections.get(connection.remotePeer)
|
|
505
494
|
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
495
|
+
if (peerConnections == null) {
|
|
496
|
+
peerConnections = []
|
|
497
|
+
this.connections.set(connection.remotePeer, peerConnections)
|
|
498
|
+
}
|
|
510
499
|
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
500
|
+
// we get notified of connections via the Upgrader emitting "connection"
|
|
501
|
+
// events, double check we aren't already tracking this connection before
|
|
502
|
+
// storing it
|
|
503
|
+
let trackedConnection = false
|
|
515
504
|
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
505
|
+
for (const conn of peerConnections) {
|
|
506
|
+
if (conn.id === connection.id) {
|
|
507
|
+
trackedConnection = true
|
|
508
|
+
}
|
|
519
509
|
}
|
|
520
|
-
}
|
|
521
510
|
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
511
|
+
if (!trackedConnection) {
|
|
512
|
+
peerConnections.push(connection)
|
|
513
|
+
}
|
|
525
514
|
|
|
526
|
-
|
|
515
|
+
return connection
|
|
516
|
+
} finally {
|
|
517
|
+
this.outboundPendingConnections--
|
|
518
|
+
}
|
|
527
519
|
}
|
|
528
520
|
|
|
529
521
|
async closeConnections (peerId: PeerId, options: AbortOptions = {}): Promise<void> {
|
package/src/libp2p.ts
CHANGED
|
@@ -110,8 +110,7 @@ export class Libp2p<T extends ServiceMap = ServiceMap> extends TypedEventEmitter
|
|
|
110
110
|
this.components.upgrader = new DefaultUpgrader(this.components, {
|
|
111
111
|
connectionEncrypters: (init.connectionEncrypters ?? []).map((fn, index) => this.configureComponent(`connection-encryption-${index}`, fn(this.components))),
|
|
112
112
|
streamMuxers: (init.streamMuxers ?? []).map((fn, index) => this.configureComponent(`stream-muxers-${index}`, fn(this.components))),
|
|
113
|
-
inboundUpgradeTimeout: init.connectionManager?.inboundUpgradeTimeout
|
|
114
|
-
outboundUpgradeTimeout: init.connectionManager?.outboundUpgradeTimeout
|
|
113
|
+
inboundUpgradeTimeout: init.connectionManager?.inboundUpgradeTimeout
|
|
115
114
|
})
|
|
116
115
|
|
|
117
116
|
// Setup the transport manager
|
package/src/upgrader.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import { InvalidMultiaddrError, TooManyInboundProtocolStreamsError, TooManyOutboundProtocolStreamsError, LimitedConnectionError, setMaxListeners } from '@libp2p/interface'
|
|
1
|
+
import { InvalidMultiaddrError, InvalidPeerIdError, TooManyInboundProtocolStreamsError, TooManyOutboundProtocolStreamsError, LimitedConnectionError, TimeoutError, 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'
|
|
5
4
|
import { CustomProgressEvent } from 'progress-events'
|
|
6
5
|
import { createConnection } from './connection/index.js'
|
|
7
|
-
import {
|
|
6
|
+
import { INBOUND_UPGRADE_TIMEOUT } from './connection-manager/constants.js'
|
|
8
7
|
import { ConnectionDeniedError, ConnectionInterceptedError, EncryptionFailedError, MuxerUnavailableError } from './errors.js'
|
|
9
8
|
import { DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS } from './registrar.js'
|
|
10
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'
|
|
11
10
|
import type { ConnectionManager, Registrar } from '@libp2p/interface-internal'
|
|
12
11
|
|
|
12
|
+
const DEFAULT_PROTOCOL_SELECT_TIMEOUT = 30000
|
|
13
|
+
|
|
13
14
|
interface CreateConnectionOptions {
|
|
14
15
|
cryptoProtocol: string
|
|
15
16
|
direction: 'inbound' | 'outbound'
|
|
@@ -35,34 +36,10 @@ export interface UpgraderInit {
|
|
|
35
36
|
streamMuxers: StreamMuxerFactory[]
|
|
36
37
|
|
|
37
38
|
/**
|
|
38
|
-
* An amount of ms by which an inbound connection upgrade
|
|
39
|
-
*
|
|
40
|
-
* @default 3000
|
|
39
|
+
* An amount of ms by which an inbound connection upgrade
|
|
40
|
+
* must complete
|
|
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
|
|
66
43
|
}
|
|
67
44
|
|
|
68
45
|
function findIncomingStreamLimit (protocol: string, registrar: Registrar): number | undefined {
|
|
@@ -126,9 +103,6 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
126
103
|
private readonly connectionEncrypters: Map<string, ConnectionEncrypter>
|
|
127
104
|
private readonly streamMuxers: Map<string, StreamMuxerFactory>
|
|
128
105
|
private readonly inboundUpgradeTimeout: number
|
|
129
|
-
private readonly outboundUpgradeTimeout: number
|
|
130
|
-
private readonly inboundStreamProtocolNegotiationTimeout: number
|
|
131
|
-
private readonly outboundStreamProtocolNegotiationTimeout: number
|
|
132
106
|
private readonly events: TypedEventTarget<Libp2pEvents>
|
|
133
107
|
|
|
134
108
|
constructor (components: DefaultUpgraderComponents, init: UpgraderInit) {
|
|
@@ -145,46 +119,135 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
145
119
|
this.streamMuxers.set(muxer.protocol, muxer)
|
|
146
120
|
})
|
|
147
121
|
|
|
148
|
-
this.inboundUpgradeTimeout = init.inboundUpgradeTimeout ??
|
|
149
|
-
this.outboundUpgradeTimeout = init.outboundUpgradeTimeout ?? UPGRADE_TIMEOUT
|
|
150
|
-
this.inboundStreamProtocolNegotiationTimeout = init.inboundStreamProtocolNegotiationTimeout ?? PROTOCOL_NEGOTIATION_TIMEOUT
|
|
151
|
-
this.outboundStreamProtocolNegotiationTimeout = init.outboundStreamProtocolNegotiationTimeout ?? PROTOCOL_NEGOTIATION_TIMEOUT
|
|
122
|
+
this.inboundUpgradeTimeout = init.inboundUpgradeTimeout ?? INBOUND_UPGRADE_TIMEOUT
|
|
152
123
|
this.events = components.events
|
|
153
124
|
}
|
|
154
125
|
|
|
155
126
|
readonly [Symbol.toStringTag] = '@libp2p/upgrader'
|
|
156
127
|
|
|
157
|
-
async shouldBlockConnection (
|
|
158
|
-
|
|
159
|
-
async shouldBlockConnection (method: ConnectionDeniedType | 'denyInboundConnection', ...args: any[]): Promise<void> {
|
|
160
|
-
const denyOperation: any = this.components.connectionGater[method]
|
|
128
|
+
async shouldBlockConnection (remotePeer: PeerId, maConn: MultiaddrConnection, connectionType: ConnectionDeniedType): Promise<void> {
|
|
129
|
+
const connectionGater = this.components.connectionGater[connectionType]
|
|
161
130
|
|
|
162
|
-
if (
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const result = await denyOperation.apply(this.components.connectionGater, args)
|
|
167
|
-
|
|
168
|
-
if (result === true) {
|
|
169
|
-
throw new ConnectionInterceptedError(`The multiaddr connection is blocked by gater.${method}`)
|
|
131
|
+
if (connectionGater !== undefined) {
|
|
132
|
+
if (await connectionGater(remotePeer, maConn)) {
|
|
133
|
+
throw new ConnectionInterceptedError(`The multiaddr connection is blocked by gater.${connectionType}`)
|
|
134
|
+
}
|
|
170
135
|
}
|
|
171
136
|
}
|
|
172
137
|
|
|
173
138
|
/**
|
|
174
139
|
* Upgrades an inbound connection
|
|
175
140
|
*/
|
|
176
|
-
async upgradeInbound (maConn: MultiaddrConnection, opts
|
|
141
|
+
async upgradeInbound (maConn: MultiaddrConnection, opts?: UpgraderOptions): Promise<Connection> {
|
|
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
|
+
|
|
177
164
|
try {
|
|
178
|
-
|
|
165
|
+
if ((await this.components.connectionGater.denyInboundConnection?.(maConn)) === true) {
|
|
166
|
+
throw new ConnectionInterceptedError('The multiaddr connection is blocked by gater.acceptConnection')
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
this.components.metrics?.trackMultiaddrConnection(maConn)
|
|
179
170
|
|
|
180
|
-
|
|
181
|
-
|
|
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
|
+
}
|
|
182
183
|
}
|
|
183
184
|
|
|
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
|
+
}
|
|
185
201
|
|
|
186
|
-
|
|
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
|
+
}
|
|
215
|
+
|
|
216
|
+
upgradedConn = encryptedConn
|
|
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'))
|
|
221
|
+
|
|
222
|
+
// Multiplex the connection
|
|
223
|
+
const multiplexed = await this._multiplexInbound({
|
|
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
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
await this.shouldBlockConnection(remotePeer, maConn, 'denyInboundUpgradedConnection')
|
|
236
|
+
|
|
237
|
+
maConn.log('successfully upgraded inbound connection')
|
|
238
|
+
|
|
239
|
+
return this._createConnection({
|
|
240
|
+
cryptoProtocol,
|
|
241
|
+
direction: 'inbound',
|
|
242
|
+
maConn,
|
|
243
|
+
upgradedConn,
|
|
244
|
+
muxerFactory,
|
|
245
|
+
remotePeer,
|
|
246
|
+
limits: opts?.limits
|
|
247
|
+
})
|
|
187
248
|
} finally {
|
|
249
|
+
signal.removeEventListener('abort', onAbort)
|
|
250
|
+
|
|
188
251
|
this.components.connectionManager.afterUpgradeInbound()
|
|
189
252
|
}
|
|
190
253
|
}
|
|
@@ -192,43 +255,36 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
192
255
|
/**
|
|
193
256
|
* Upgrades an outbound connection
|
|
194
257
|
*/
|
|
195
|
-
async upgradeOutbound (maConn: MultiaddrConnection, opts
|
|
258
|
+
async upgradeOutbound (maConn: MultiaddrConnection, opts?: UpgraderOptions): Promise<Connection> {
|
|
196
259
|
const idStr = maConn.remoteAddr.getPeerId()
|
|
197
260
|
let remotePeerId: PeerId | undefined
|
|
198
261
|
|
|
199
262
|
if (idStr != null) {
|
|
200
263
|
remotePeerId = peerIdFromString(idStr)
|
|
201
|
-
await this.shouldBlockConnection('denyOutboundConnection', remotePeerId, maConn)
|
|
202
|
-
}
|
|
203
264
|
|
|
204
|
-
|
|
205
|
-
|
|
265
|
+
await this.shouldBlockConnection(remotePeerId, maConn, 'denyOutboundConnection')
|
|
266
|
+
}
|
|
206
267
|
|
|
207
|
-
private async _performUpgrade (maConn: MultiaddrConnection, direction: 'inbound' | 'outbound', opts: UpgraderOptions): Promise<Connection> {
|
|
208
268
|
let encryptedConn: MultiaddrConnection
|
|
209
269
|
let remotePeer: PeerId
|
|
210
270
|
let upgradedConn: MultiaddrConnection
|
|
211
|
-
let muxerFactory: StreamMuxerFactory | undefined
|
|
212
271
|
let cryptoProtocol
|
|
213
|
-
|
|
214
|
-
const upgradeTimeoutSignal = AbortSignal.timeout(direction === 'inbound' ? this.inboundUpgradeTimeout : this.outboundUpgradeTimeout)
|
|
215
|
-
const signal = anySignal([upgradeTimeoutSignal, opts.signal])
|
|
216
|
-
setMaxListeners(Infinity, upgradeTimeoutSignal, signal)
|
|
217
|
-
opts.signal = signal
|
|
272
|
+
let muxerFactory
|
|
218
273
|
|
|
219
274
|
this.components.metrics?.trackMultiaddrConnection(maConn)
|
|
220
275
|
|
|
221
|
-
maConn.log('starting the
|
|
276
|
+
maConn.log('starting the outbound connection upgrade')
|
|
277
|
+
|
|
278
|
+
// If the transport natively supports encryption, skip connection
|
|
279
|
+
// protector and encryption
|
|
222
280
|
|
|
223
281
|
// Protect
|
|
224
282
|
let protectedConn = maConn
|
|
225
|
-
|
|
226
283
|
if (opts?.skipProtection !== true) {
|
|
227
284
|
const protector = this.components.connectionProtector
|
|
228
285
|
|
|
229
286
|
if (protector != null) {
|
|
230
|
-
|
|
231
|
-
protectedConn = await protector.protect(maConn, opts)
|
|
287
|
+
protectedConn = await protector.protect(maConn)
|
|
232
288
|
}
|
|
233
289
|
}
|
|
234
290
|
|
|
@@ -236,38 +292,26 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
236
292
|
// Encrypt the connection
|
|
237
293
|
encryptedConn = protectedConn
|
|
238
294
|
if (opts?.skipEncryption !== true) {
|
|
239
|
-
opts?.onProgress?.(new CustomProgressEvent(`upgrader:encrypt-${direction}-connection`));
|
|
240
|
-
|
|
241
295
|
({
|
|
242
296
|
conn: encryptedConn,
|
|
243
297
|
remotePeer,
|
|
244
298
|
protocol: cryptoProtocol
|
|
245
|
-
} = await (
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
})
|
|
250
|
-
: this._encryptOutbound(protectedConn, {
|
|
251
|
-
...opts,
|
|
252
|
-
signal
|
|
253
|
-
})
|
|
254
|
-
))
|
|
299
|
+
} = await this._encryptOutbound(protectedConn, {
|
|
300
|
+
...opts,
|
|
301
|
+
remotePeer: remotePeerId
|
|
302
|
+
}))
|
|
255
303
|
|
|
256
304
|
const maConn: MultiaddrConnection = {
|
|
257
305
|
...protectedConn,
|
|
258
306
|
...encryptedConn
|
|
259
307
|
}
|
|
260
308
|
|
|
261
|
-
await this.shouldBlockConnection(
|
|
309
|
+
await this.shouldBlockConnection(remotePeer, maConn, 'denyOutboundEncryptedConnection')
|
|
262
310
|
} else {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
if (idStr == null) {
|
|
266
|
-
throw new InvalidMultiaddrError(`${direction} connection that skipped encryption must have a peer id`)
|
|
311
|
+
if (remotePeerId == null) {
|
|
312
|
+
throw new InvalidPeerIdError('Encryption was skipped but no peer id was passed')
|
|
267
313
|
}
|
|
268
314
|
|
|
269
|
-
const remotePeerId = peerIdFromString(idStr)
|
|
270
|
-
|
|
271
315
|
cryptoProtocol = 'native'
|
|
272
316
|
remotePeer = remotePeerId
|
|
273
317
|
}
|
|
@@ -276,33 +320,27 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
276
320
|
if (opts?.muxerFactory != null) {
|
|
277
321
|
muxerFactory = opts.muxerFactory
|
|
278
322
|
} else if (this.streamMuxers.size > 0) {
|
|
279
|
-
opts?.onProgress?.(new CustomProgressEvent(`upgrader:multiplex-${direction}-connection`))
|
|
280
|
-
|
|
281
323
|
// Multiplex the connection
|
|
282
|
-
const multiplexed = await (
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
}, this.streamMuxers, opts)
|
|
287
|
-
: this._multiplexOutbound({
|
|
288
|
-
...protectedConn,
|
|
289
|
-
...encryptedConn
|
|
290
|
-
}, this.streamMuxers, opts))
|
|
324
|
+
const multiplexed = await this._multiplexOutbound({
|
|
325
|
+
...protectedConn,
|
|
326
|
+
...encryptedConn
|
|
327
|
+
}, this.streamMuxers)
|
|
291
328
|
muxerFactory = multiplexed.muxerFactory
|
|
292
329
|
upgradedConn = multiplexed.stream
|
|
293
330
|
}
|
|
294
331
|
} catch (err: any) {
|
|
295
|
-
maConn.log.error('failed to upgrade
|
|
332
|
+
maConn.log.error('failed to upgrade outbound connection', err)
|
|
333
|
+
await maConn.close(err)
|
|
296
334
|
throw err
|
|
297
335
|
}
|
|
298
336
|
|
|
299
|
-
await this.shouldBlockConnection(
|
|
337
|
+
await this.shouldBlockConnection(remotePeer, maConn, 'denyOutboundUpgradedConnection')
|
|
300
338
|
|
|
301
|
-
maConn.log('successfully
|
|
339
|
+
maConn.log('successfully upgraded outbound connection')
|
|
302
340
|
|
|
303
341
|
return this._createConnection({
|
|
304
342
|
cryptoProtocol,
|
|
305
|
-
direction,
|
|
343
|
+
direction: 'outbound',
|
|
306
344
|
maConn,
|
|
307
345
|
upgradedConn,
|
|
308
346
|
muxerFactory,
|
|
@@ -342,11 +380,7 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
342
380
|
void Promise.resolve()
|
|
343
381
|
.then(async () => {
|
|
344
382
|
const protocols = this.components.registrar.getProtocols()
|
|
345
|
-
const signal = AbortSignal.timeout(this.inboundStreamProtocolNegotiationTimeout)
|
|
346
|
-
setMaxListeners(Infinity, signal)
|
|
347
|
-
|
|
348
383
|
const { stream, protocol } = await mss.handle(muxedStream, protocols, {
|
|
349
|
-
signal,
|
|
350
384
|
log: muxedStream.log,
|
|
351
385
|
yieldBytes: false
|
|
352
386
|
})
|
|
@@ -421,7 +455,7 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
421
455
|
if (options.signal == null) {
|
|
422
456
|
muxedStream.log('no abort signal was passed while trying to negotiate protocols %s falling back to default timeout', protocols)
|
|
423
457
|
|
|
424
|
-
const signal = AbortSignal.timeout(
|
|
458
|
+
const signal = AbortSignal.timeout(DEFAULT_PROTOCOL_SELECT_TIMEOUT)
|
|
425
459
|
setMaxListeners(Infinity, signal)
|
|
426
460
|
|
|
427
461
|
options = {
|
|
@@ -598,7 +632,6 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
598
632
|
|
|
599
633
|
try {
|
|
600
634
|
const { stream, protocol } = await mss.handle(connection, protocols, {
|
|
601
|
-
...options,
|
|
602
635
|
log: connection.log
|
|
603
636
|
})
|
|
604
637
|
const encrypter = this.connectionEncrypters.get(protocol)
|
|
@@ -623,7 +656,7 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
623
656
|
* Attempts to encrypt the given `connection` with the provided connection encrypters.
|
|
624
657
|
* The first `ConnectionEncrypter` module to succeed will be used
|
|
625
658
|
*/
|
|
626
|
-
async _encryptOutbound (connection: MultiaddrConnection, options
|
|
659
|
+
async _encryptOutbound (connection: MultiaddrConnection, options?: SecureConnectionOptions): Promise<CryptoResult> {
|
|
627
660
|
const protocols = Array.from(this.connectionEncrypters.keys())
|
|
628
661
|
connection.log('selecting outbound crypto protocol', protocols)
|
|
629
662
|
|
|
@@ -634,7 +667,6 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
634
667
|
stream,
|
|
635
668
|
protocol
|
|
636
669
|
} = await mss.select(connection, protocols, {
|
|
637
|
-
...options,
|
|
638
670
|
log: connection.log,
|
|
639
671
|
yieldBytes: true
|
|
640
672
|
})
|
|
@@ -661,7 +693,7 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
661
693
|
* Selects one of the given muxers via multistream-select. That
|
|
662
694
|
* muxer will be used for all future streams on the connection.
|
|
663
695
|
*/
|
|
664
|
-
async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory
|
|
696
|
+
async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{ stream: MultiaddrConnection, muxerFactory?: StreamMuxerFactory }> {
|
|
665
697
|
const protocols = Array.from(muxers.keys())
|
|
666
698
|
connection.log('outbound selecting muxer %s', protocols)
|
|
667
699
|
try {
|
|
@@ -671,7 +703,6 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
671
703
|
stream,
|
|
672
704
|
protocol
|
|
673
705
|
} = await mss.select(connection, protocols, {
|
|
674
|
-
...options,
|
|
675
706
|
log: connection.log,
|
|
676
707
|
yieldBytes: true
|
|
677
708
|
})
|
|
@@ -690,12 +721,11 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
690
721
|
* Registers support for one of the given muxers via multistream-select. The
|
|
691
722
|
* selected muxer will be used for all future streams on the connection.
|
|
692
723
|
*/
|
|
693
|
-
async _multiplexInbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory
|
|
724
|
+
async _multiplexInbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{ stream: MultiaddrConnection, muxerFactory?: StreamMuxerFactory }> {
|
|
694
725
|
const protocols = Array.from(muxers.keys())
|
|
695
726
|
connection.log('inbound handling muxers %s', protocols)
|
|
696
727
|
try {
|
|
697
728
|
const { stream, protocol } = await mss.handle(connection, protocols, {
|
|
698
|
-
...options,
|
|
699
729
|
log: connection.log
|
|
700
730
|
})
|
|
701
731
|
const muxerFactory = muxers.get(protocol)
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const version = '2.1.1
|
|
1
|
+
export const version = '2.1.1'
|
|
2
2
|
export const name = 'libp2p'
|