@@ -107,6 +107,10 @@ export interface DefaultUpgraderComponents {
107
107
events : EventEmitter < Libp2pEvents >
108
108
}
109
109
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
+
110
114
export class DefaultUpgrader implements Upgrader {
111
115
private readonly components : DefaultUpgraderComponents
112
116
private readonly connectionEncryption : Map < string , ConnectionEncrypter >
@@ -132,6 +136,16 @@ export class DefaultUpgrader implements Upgrader {
132
136
this . events = components . events
133
137
}
134
138
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
+
135
149
/**
136
150
* Upgrades an inbound connection
137
151
*/
@@ -142,7 +156,7 @@ export class DefaultUpgrader implements Upgrader {
142
156
throw new CodeError ( 'connection denied' , codes . ERR_CONNECTION_DENIED )
143
157
}
144
158
145
- let encryptedConn
159
+ let encryptedConn : EncryptedConn
146
160
let remotePeer
147
161
let upgradedConn : Duplex < AsyncGenerator < Uint8Array > , Source < Uint8Array > , Promise < void > >
148
162
let muxerFactory : StreamMuxerFactory | undefined
@@ -190,12 +204,12 @@ export class DefaultUpgrader implements Upgrader {
190
204
protocol : cryptoProtocol
191
205
} = await this . _encryptInbound ( protectedConn ) )
192
206
193
- if ( ( await this . components . connectionGater . denyInboundEncryptedConnection ?. ( remotePeer , {
207
+ const maConn : MultiaddrConnection = {
194
208
...protectedConn ,
195
209
...encryptedConn
196
- } ) ) === true ) {
197
- throw new CodeError ( 'The multiaddr connection is blocked by gater.acceptEncryptedConnection' , codes . ERR_CONNECTION_INTERCEPTED )
198
210
}
211
+
212
+ await this . shouldBlockConnection ( remotePeer , maConn , 'denyInboundEncryptedConnection' )
199
213
} else {
200
214
const idStr = maConn . remoteAddr . getPeerId ( )
201
215
@@ -226,12 +240,7 @@ export class DefaultUpgrader implements Upgrader {
226
240
throw err
227
241
}
228
242
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' )
235
244
236
245
log ( 'Successfully upgraded inbound connection' )
237
246
@@ -259,9 +268,7 @@ export class DefaultUpgrader implements Upgrader {
259
268
if ( idStr != null ) {
260
269
remotePeerId = peerIdFromString ( idStr )
261
270
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' )
265
272
}
266
273
267
274
let encryptedConn
@@ -297,12 +304,12 @@ export class DefaultUpgrader implements Upgrader {
297
304
protocol : cryptoProtocol
298
305
} = await this . _encryptOutbound ( protectedConn , remotePeerId ) )
299
306
300
- if ( ( await this . components . connectionGater . denyOutboundEncryptedConnection ?. ( remotePeer , {
307
+ const maConn : MultiaddrConnection = {
301
308
...protectedConn ,
302
309
...encryptedConn
303
- } ) ) === true ) {
304
- throw new CodeError ( 'The multiaddr connection is blocked by gater.acceptEncryptedConnection' , codes . ERR_CONNECTION_INTERCEPTED )
305
310
}
311
+
312
+ await this . shouldBlockConnection ( remotePeer , maConn , 'denyOutboundEncryptedConnection' )
306
313
} else {
307
314
if ( remotePeerId == null ) {
308
315
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 {
330
337
throw err
331
338
}
332
339
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' )
339
341
340
342
log ( 'Successfully upgraded outbound connection' )
341
343
0 commit comments