Skip to content

Commit ba47c95

Browse files
authored
fix: Refactor Upgrader connection gating (#1622)
1 parent 17eb162 commit ba47c95

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/upgrader.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ export interface DefaultUpgraderComponents {
107107
events: EventEmitter<Libp2pEvents>
108108
}
109109

110+
type EncryptedConn = Duplex<AsyncGenerator<Uint8Array, any, unknown>, Source<Uint8Array>, Promise<void>>
111+
112+
type ConnectionDeniedType = keyof Pick<ConnectionGater, 'denyOutboundConnection' | 'denyInboundEncryptedConnection' | 'denyOutboundEncryptedConnection' | 'denyInboundUpgradedConnection' | 'denyOutboundUpgradedConnection'>
113+
110114
export class DefaultUpgrader implements Upgrader {
111115
private readonly components: DefaultUpgraderComponents
112116
private readonly connectionEncryption: Map<string, ConnectionEncrypter>
@@ -132,6 +136,16 @@ export class DefaultUpgrader implements Upgrader {
132136
this.events = components.events
133137
}
134138

139+
async shouldBlockConnection (remotePeer: PeerId, maConn: MultiaddrConnection, connectionType: ConnectionDeniedType): Promise<void> {
140+
const connectionGater = this.components.connectionGater[connectionType]
141+
142+
if (connectionGater !== undefined) {
143+
if (await connectionGater(remotePeer, maConn)) {
144+
throw new CodeError(`The multiaddr connection is blocked by gater.${connectionType}`, codes.ERR_CONNECTION_INTERCEPTED)
145+
}
146+
}
147+
}
148+
135149
/**
136150
* Upgrades an inbound connection
137151
*/
@@ -142,7 +156,7 @@ export class DefaultUpgrader implements Upgrader {
142156
throw new CodeError('connection denied', codes.ERR_CONNECTION_DENIED)
143157
}
144158

145-
let encryptedConn
159+
let encryptedConn: EncryptedConn
146160
let remotePeer
147161
let upgradedConn: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>
148162
let muxerFactory: StreamMuxerFactory | undefined
@@ -190,12 +204,12 @@ export class DefaultUpgrader implements Upgrader {
190204
protocol: cryptoProtocol
191205
} = await this._encryptInbound(protectedConn))
192206

193-
if ((await this.components.connectionGater.denyInboundEncryptedConnection?.(remotePeer, {
207+
const maConn: MultiaddrConnection = {
194208
...protectedConn,
195209
...encryptedConn
196-
})) === true) {
197-
throw new CodeError('The multiaddr connection is blocked by gater.acceptEncryptedConnection', codes.ERR_CONNECTION_INTERCEPTED)
198210
}
211+
212+
await this.shouldBlockConnection(remotePeer, maConn, 'denyInboundEncryptedConnection')
199213
} else {
200214
const idStr = maConn.remoteAddr.getPeerId()
201215

@@ -226,12 +240,7 @@ export class DefaultUpgrader implements Upgrader {
226240
throw err
227241
}
228242

229-
if ((await this.components.connectionGater.denyInboundUpgradedConnection?.(remotePeer, {
230-
...protectedConn,
231-
...encryptedConn
232-
})) === true) {
233-
throw new CodeError('The multiaddr connection is blocked by gater.acceptEncryptedConnection', codes.ERR_CONNECTION_INTERCEPTED)
234-
}
243+
await this.shouldBlockConnection(remotePeer, maConn, 'denyInboundUpgradedConnection')
235244

236245
log('Successfully upgraded inbound connection')
237246

@@ -259,9 +268,7 @@ export class DefaultUpgrader implements Upgrader {
259268
if (idStr != null) {
260269
remotePeerId = peerIdFromString(idStr)
261270

262-
if ((await this.components.connectionGater.denyOutboundConnection?.(remotePeerId, maConn)) === true) {
263-
throw new CodeError('The multiaddr connection is blocked by connectionGater.denyOutboundConnection', codes.ERR_CONNECTION_INTERCEPTED)
264-
}
271+
await this.shouldBlockConnection(remotePeerId, maConn, 'denyOutboundConnection')
265272
}
266273

267274
let encryptedConn
@@ -297,12 +304,12 @@ export class DefaultUpgrader implements Upgrader {
297304
protocol: cryptoProtocol
298305
} = await this._encryptOutbound(protectedConn, remotePeerId))
299306

300-
if ((await this.components.connectionGater.denyOutboundEncryptedConnection?.(remotePeer, {
307+
const maConn: MultiaddrConnection = {
301308
...protectedConn,
302309
...encryptedConn
303-
})) === true) {
304-
throw new CodeError('The multiaddr connection is blocked by gater.acceptEncryptedConnection', codes.ERR_CONNECTION_INTERCEPTED)
305310
}
311+
312+
await this.shouldBlockConnection(remotePeer, maConn, 'denyOutboundEncryptedConnection')
306313
} else {
307314
if (remotePeerId == null) {
308315
throw new CodeError('Encryption was skipped but no peer id was passed', codes.ERR_INVALID_PEER)
@@ -330,12 +337,7 @@ export class DefaultUpgrader implements Upgrader {
330337
throw err
331338
}
332339

333-
if ((await this.components.connectionGater.denyOutboundUpgradedConnection?.(remotePeer, {
334-
...protectedConn,
335-
...encryptedConn
336-
})) === true) {
337-
throw new CodeError('The multiaddr connection is blocked by gater.acceptEncryptedConnection', codes.ERR_CONNECTION_INTERCEPTED)
338-
}
340+
await this.shouldBlockConnection(remotePeer, maConn, 'denyOutboundUpgradedConnection')
339341

340342
log('Successfully upgraded outbound connection')
341343

0 commit comments

Comments
 (0)