From e280a3914b9b73ff944367a13bcb48543e965936 Mon Sep 17 00:00:00 2001 From: Chinmay Kousik Date: Mon, 26 Sep 2022 18:16:58 +0530 Subject: [PATCH 1/3] update upgrader interface --- package.json | 4 +-- src/upgrader.ts | 76 ++++++++++++++++++++++++++++++------------------- 2 files changed, 48 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index 9861754814..2a79cab10a 100644 --- a/package.json +++ b/package.json @@ -78,7 +78,7 @@ }, "scripts": { "clean": "aegir clean", - "lint": "aegir lint", + "lint": "aegir lint --fix", "dep-check": "aegir dep-check", "prepublishOnly": "node scripts/update-version.js", "build": "aegir build", @@ -116,7 +116,7 @@ "@libp2p/interface-pubsub": "^2.0.1", "@libp2p/interface-registrar": "^2.0.3", "@libp2p/interface-stream-muxer": "^2.0.2", - "@libp2p/interface-transport": "^1.0.3", + "@libp2p/interface-transport": "file:../js-libp2p-interfaces/packages/interface-transport", "@libp2p/interfaces": "^3.0.3", "@libp2p/logger": "^2.0.1", "@libp2p/multistream-select": "^3.0.0", diff --git a/src/upgrader.ts b/src/upgrader.ts index 6b12304721..cacf389a5b 100644 --- a/src/upgrader.ts +++ b/src/upgrader.ts @@ -12,7 +12,7 @@ import type { MultiaddrConnection, Connection, Stream } from '@libp2p/interface- import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface-connection-encrypter' import type { StreamMuxer, StreamMuxerFactory } from '@libp2p/interface-stream-muxer' import type { PeerId } from '@libp2p/interface-peer-id' -import type { Upgrader, UpgraderEvents } from '@libp2p/interface-transport' +import type { Upgrader, UpgraderEvents, UpgraderOptions } from '@libp2p/interface-transport' import type { Duplex } from 'it-stream-types' import { Components, isInitializable } from '@libp2p/components' import type { AbortOptions } from '@libp2p/interfaces' @@ -228,7 +228,7 @@ export class DefaultUpgrader extends EventEmitter implements Upg /** * Upgrades an outbound connection */ - async upgradeOutbound (maConn: MultiaddrConnection): Promise { + async upgradeOutbound (maConn: MultiaddrConnection, opts?: UpgraderOptions): Promise { const idStr = maConn.remoteAddr.getPeerId() if (idStr == null) { throw errCode(new Error('outbound connection must have a peer id'), codes.ERR_INVALID_MULTIADDR) @@ -240,6 +240,8 @@ export class DefaultUpgrader extends EventEmitter implements Upg throw errCode(new Error('The multiaddr connection is blocked by connectionGater.denyOutboundConnection'), codes.ERR_CONNECTION_INTERCEPTED) } + const skipEncryption = opts?.skipEncryption === true + let encryptedConn let remotePeer let upgradedConn @@ -258,39 +260,53 @@ export class DefaultUpgrader extends EventEmitter implements Upg log('Starting the outbound connection upgrade') + // If the transport natively supports encryption, skip connection + // protector and encryption + // Protect let protectedConn = maConn - const protector = this.components.getConnectionProtector() + if (!skipEncryption) { + const protector = this.components.getConnectionProtector() - if (protector != null) { - protectedConn = await protector.protect(maConn) + if (protector != null) { + protectedConn = await protector.protect(maConn) + } } try { // Encrypt the connection - ({ - conn: encryptedConn, - remotePeer, - protocol: cryptoProtocol - } = await this._encryptOutbound(protectedConn, remotePeerId)) - - if (await this.components.getConnectionGater().denyOutboundEncryptedConnection(remotePeer, { - ...protectedConn, - ...encryptedConn - })) { - throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED) - } + encryptedConn = protectedConn + if (!skipEncryption) { + ({ + conn: encryptedConn, + remotePeer, + protocol: cryptoProtocol + } = await this._encryptOutbound(protectedConn, remotePeerId)) - // Multiplex the connection - if (this.muxers.size > 0) { - const multiplexed = await this._multiplexOutbound({ + if (await this.components.getConnectionGater().denyOutboundEncryptedConnection(remotePeer, { ...protectedConn, ...encryptedConn - }, this.muxers) - muxerFactory = multiplexed.muxerFactory - upgradedConn = multiplexed.stream + })) { + throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED) + } } else { - upgradedConn = encryptedConn + // specify this somehow + cryptoProtocol = 'custom' + } + + upgradedConn = encryptedConn + if (opts?.muxerFactory) { + muxerFactory = opts.muxerFactory + } else { + // Multiplex the connection + if (this.muxers.size > 0) { + const multiplexed = await this._multiplexOutbound({ + ...protectedConn, + ...encryptedConn + }, this.muxers) + muxerFactory = multiplexed.muxerFactory + upgradedConn = multiplexed.stream + } } } catch (err: any) { log.error('Failed to upgrade outbound connection', err) @@ -298,7 +314,7 @@ export class DefaultUpgrader extends EventEmitter implements Upg throw err } - if (await this.components.getConnectionGater().denyOutboundUpgradedConnection(remotePeer, { + if (await this.components.getConnectionGater().denyOutboundUpgradedConnection(remotePeerId, { ...protectedConn, ...encryptedConn })) { @@ -306,8 +322,8 @@ export class DefaultUpgrader extends EventEmitter implements Upg } if (metrics != null) { - metrics.updatePlaceholder(proxyPeer, remotePeer) - setPeer(remotePeer) + metrics.updatePlaceholder(proxyPeer, remotePeerId) + setPeer(remotePeerId) } log('Successfully upgraded outbound connection') @@ -318,7 +334,7 @@ export class DefaultUpgrader extends EventEmitter implements Upg maConn, upgradedConn, muxerFactory, - remotePeer + remotePeer: remotePeerId }) } @@ -609,7 +625,7 @@ export class DefaultUpgrader extends EventEmitter implements Upg * Selects one of the given muxers via multistream-select. That * muxer will be used for all future streams on the connection. */ - async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map): Promise<{ stream: Duplex, muxerFactory?: StreamMuxerFactory}> { + async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map): Promise<{stream: Duplex, muxerFactory?: StreamMuxerFactory}> { const protocols = Array.from(muxers.keys()) log('outbound selecting muxer %s', protocols) try { @@ -629,7 +645,7 @@ export class DefaultUpgrader extends EventEmitter implements Upg * Registers support for one of the given muxers via multistream-select. The * selected muxer will be used for all future streams on the connection. */ - async _multiplexInbound (connection: MultiaddrConnection, muxers: Map): Promise<{ stream: Duplex, muxerFactory?: StreamMuxerFactory}> { + async _multiplexInbound (connection: MultiaddrConnection, muxers: Map): Promise<{stream: Duplex, muxerFactory?: StreamMuxerFactory}> { const protocols = Array.from(muxers.keys()) log('inbound handling muxers %s', protocols) try { From 14ca807861ffbe2ec8bd378f72a1e996cbba009d Mon Sep 17 00:00:00 2001 From: Chinmay Kousik Date: Fri, 30 Sep 2022 17:01:31 +0530 Subject: [PATCH 2/3] address review --- package.json | 4 ++-- src/upgrader.ts | 29 ++++++++++++++--------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 2a79cab10a..3b0600c44e 100644 --- a/package.json +++ b/package.json @@ -78,7 +78,7 @@ }, "scripts": { "clean": "aegir clean", - "lint": "aegir lint --fix", + "lint": "aegir lint", "dep-check": "aegir dep-check", "prepublishOnly": "node scripts/update-version.js", "build": "aegir build", @@ -115,7 +115,7 @@ "@libp2p/interface-peer-store": "^1.2.1", "@libp2p/interface-pubsub": "^2.0.1", "@libp2p/interface-registrar": "^2.0.3", - "@libp2p/interface-stream-muxer": "^2.0.2", + "@libp2p/interface-stream-muxer": "file:../js-libp2p-interfaces/packages/interface-stream-muxer", "@libp2p/interface-transport": "file:../js-libp2p-interfaces/packages/interface-transport", "@libp2p/interfaces": "^3.0.3", "@libp2p/logger": "^2.0.1", diff --git a/src/upgrader.ts b/src/upgrader.ts index cacf389a5b..e837025f3f 100644 --- a/src/upgrader.ts +++ b/src/upgrader.ts @@ -265,7 +265,7 @@ export class DefaultUpgrader extends EventEmitter implements Upg // Protect let protectedConn = maConn - if (!skipEncryption) { + if ((opts?.muxerFactory) == null) { const protector = this.components.getConnectionProtector() if (protector != null) { @@ -291,22 +291,21 @@ export class DefaultUpgrader extends EventEmitter implements Upg } } else { // specify this somehow - cryptoProtocol = 'custom' + cryptoProtocol = 'native' + remotePeer = remotePeerId } upgradedConn = encryptedConn - if (opts?.muxerFactory) { + if ((opts?.muxerFactory) != null) { muxerFactory = opts.muxerFactory - } else { + } else if (this.muxers.size > 0) { // Multiplex the connection - if (this.muxers.size > 0) { - const multiplexed = await this._multiplexOutbound({ - ...protectedConn, - ...encryptedConn - }, this.muxers) - muxerFactory = multiplexed.muxerFactory - upgradedConn = multiplexed.stream - } + const multiplexed = await this._multiplexOutbound({ + ...protectedConn, + ...encryptedConn + }, this.muxers) + muxerFactory = multiplexed.muxerFactory + upgradedConn = multiplexed.stream } } catch (err: any) { log.error('Failed to upgrade outbound connection', err) @@ -314,7 +313,7 @@ export class DefaultUpgrader extends EventEmitter implements Upg throw err } - if (await this.components.getConnectionGater().denyOutboundUpgradedConnection(remotePeerId, { + if (await this.components.getConnectionGater().denyOutboundUpgradedConnection(remotePeer, { ...protectedConn, ...encryptedConn })) { @@ -322,7 +321,7 @@ export class DefaultUpgrader extends EventEmitter implements Upg } if (metrics != null) { - metrics.updatePlaceholder(proxyPeer, remotePeerId) + metrics.updatePlaceholder(proxyPeer, remotePeer) setPeer(remotePeerId) } @@ -427,7 +426,7 @@ export class DefaultUpgrader extends EventEmitter implements Upg } log('%s: starting new stream on %s', direction, protocols) - const muxedStream = muxer.newStream() + const muxedStream = await muxer.newStream() const metrics = this.components.getMetrics() let controller: TimeoutController | undefined From 8131cf9c2f4a5d9103bae2c879afd30642d30a40 Mon Sep 17 00:00:00 2001 From: Chinmay Kousik Date: Fri, 30 Sep 2022 20:04:21 +0530 Subject: [PATCH 3/3] address review --- src/upgrader.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/upgrader.ts b/src/upgrader.ts index e837025f3f..03d7d87837 100644 --- a/src/upgrader.ts +++ b/src/upgrader.ts @@ -240,8 +240,6 @@ export class DefaultUpgrader extends EventEmitter implements Upg throw errCode(new Error('The multiaddr connection is blocked by connectionGater.denyOutboundConnection'), codes.ERR_CONNECTION_INTERCEPTED) } - const skipEncryption = opts?.skipEncryption === true - let encryptedConn let remotePeer let upgradedConn @@ -276,7 +274,7 @@ export class DefaultUpgrader extends EventEmitter implements Upg try { // Encrypt the connection encryptedConn = protectedConn - if (!skipEncryption) { + if (!opts?.skipEncryption) { ({ conn: encryptedConn, remotePeer, @@ -290,7 +288,6 @@ export class DefaultUpgrader extends EventEmitter implements Upg throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED) } } else { - // specify this somehow cryptoProtocol = 'native' remotePeer = remotePeerId } @@ -333,7 +330,7 @@ export class DefaultUpgrader extends EventEmitter implements Upg maConn, upgradedConn, muxerFactory, - remotePeer: remotePeerId + remotePeer, }) }