Skip to content

Commit

Permalink
feat: allow skipping encryption and custom muxer factory in upgrader (#…
Browse files Browse the repository at this point in the history
…1411)

Updates the `DefaultUpgrader` to use the new `UpgraderOptions` which allows skipping encryption and/or adding a custom muxer factory for transports which inherently support encryption and muxing.

Also updates all deps.

Co-authored-by: Chinmay Kousik <chinmaykousik1@gmail.com>
Co-authored-by: achingbrain <alex@achingbrain.net>
  • Loading branch information
3 people authored Oct 7, 2022
1 parent c185ef5 commit 6615efa
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 56 deletions.
14 changes: 7 additions & 7 deletions examples/delegated-routing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@chainsafe/libp2p-noise": "^8.0.2",
"@chainsafe/libp2p-noise": "^9.0.0",
"ipfs-core": "^0.15.4",
"libp2p": "../../",
"@libp2p/delegated-content-routing": "^2.0.1",
"@libp2p/delegated-peer-routing": "^2.0.1",
"@libp2p/kad-dht": "^3.0.0",
"@libp2p/mplex": "^5.2.3",
"@libp2p/webrtc-star": "^3.0.3",
"@libp2p/websockets": "^3.0.4",
"@libp2p/delegated-content-routing": "^2.0.2",
"@libp2p/delegated-peer-routing": "^2.0.2",
"@libp2p/kad-dht": "^4.0.0",
"@libp2p/mplex": "^6.0.2",
"@libp2p/webrtc-star": "^4.0.1",
"@libp2p/websockets": "^4.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0"
Expand Down
8 changes: 4 additions & 4 deletions examples/libp2p-in-the-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
},
"license": "ISC",
"dependencies": {
"@chainsafe/libp2p-noise": "^8.0.2",
"@libp2p/bootstrap": "^2.0.1",
"@libp2p/mplex": "^5.2.3",
"@libp2p/webrtc-star": "^3.0.3",
"@chainsafe/libp2p-noise": "^9.0.0",
"@libp2p/bootstrap": "^4.0.0",
"@libp2p/mplex": "^6.0.2",
"@libp2p/webrtc-star": "^4.0.1",
"@libp2p/websockets": "^3.0.4",
"libp2p": "../../"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"license": "MIT",
"dependencies": {
"@libp2p/pubsub-peer-discovery": "^6.0.2",
"@libp2p/floodsub": "^3.0.3",
"@libp2p/floodsub": "^4.0.0",
"@nodeutils/defaults-deep": "^1.1.0",
"execa": "^6.1.0",
"fs-extra": "^10.1.0",
Expand Down
8 changes: 4 additions & 4 deletions examples/webrtc-direct/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
},
"license": "ISC",
"dependencies": {
"@libp2p/webrtc-direct": "^2.0.0",
"@chainsafe/libp2p-noise": "^8.0.2",
"@libp2p/bootstrap": "^2.0.1",
"@libp2p/mplex": "^5.2.3",
"@libp2p/webrtc-direct": "^2.0.3",
"@chainsafe/libp2p-noise": "^9.0.0",
"@libp2p/bootstrap": "^4.0.0",
"@libp2p/mplex": "^6.0.2",
"libp2p": "../../",
"wrtc": "^0.4.7"
},
Expand Down
36 changes: 18 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@
},
"dependencies": {
"@achingbrain/nat-port-mapper": "^1.0.3",
"@libp2p/components": "^2.1.0",
"@libp2p/components": "^3.0.0",
"@libp2p/connection": "^4.0.2",
"@libp2p/crypto": "^1.0.4",
"@libp2p/interface-address-manager": "^1.0.3",
"@libp2p/interface-address-manager": "^2.0.0",
"@libp2p/interface-connection": "^3.0.2",
"@libp2p/interface-connection-encrypter": "^3.0.1",
"@libp2p/interface-connection-manager": "^1.1.1",
Expand All @@ -115,16 +115,16 @@
"@libp2p/interface-peer-store": "^1.2.2",
"@libp2p/interface-pubsub": "^2.1.0",
"@libp2p/interface-registrar": "^2.0.3",
"@libp2p/interface-stream-muxer": "^2.0.2",
"@libp2p/interface-transport": "^1.0.4",
"@libp2p/interface-stream-muxer": "^3.0.0",
"@libp2p/interface-transport": "^2.0.0",
"@libp2p/interfaces": "^3.0.3",
"@libp2p/logger": "^2.0.1",
"@libp2p/multistream-select": "^3.0.0",
"@libp2p/peer-collections": "^2.0.0",
"@libp2p/peer-id": "^1.1.15",
"@libp2p/peer-id-factory": "^1.0.18",
"@libp2p/peer-record": "^4.0.3",
"@libp2p/peer-store": "^3.1.5",
"@libp2p/peer-store": "^4.0.0",
"@libp2p/tracked-map": "^2.0.1",
"@libp2p/utils": "^3.0.2",
"@multiformats/mafmt": "^11.0.2",
Expand Down Expand Up @@ -169,24 +169,24 @@
"xsalsa20": "^1.1.0"
},
"devDependencies": {
"@chainsafe/libp2p-noise": "^8.0.2",
"@chainsafe/libp2p-yamux": "^1.0.0",
"@libp2p/bootstrap": "^3.0.0",
"@chainsafe/libp2p-noise": "^9.0.0",
"@chainsafe/libp2p-yamux": "^2.0.0",
"@libp2p/bootstrap": "^4.0.0",
"@libp2p/daemon-client": "^3.0.1",
"@libp2p/daemon-server": "^3.0.1",
"@libp2p/floodsub": "^3.0.0",
"@libp2p/floodsub": "^4.0.0",
"@libp2p/interface-compliance-tests": "^3.0.2",
"@libp2p/interface-connection-encrypter-compliance-tests": "^2.0.2",
"@libp2p/interface-mocks": "^5.1.0",
"@libp2p/interface-mocks": "^6.0.1",
"@libp2p/interop": "^3.0.1",
"@libp2p/kad-dht": "^3.0.5",
"@libp2p/mdns": "^3.0.1",
"@libp2p/mplex": "^5.2.3",
"@libp2p/pubsub": "^3.1.3",
"@libp2p/tcp": "^3.1.1",
"@libp2p/kad-dht": "^4.0.0",
"@libp2p/mdns": "^4.0.0",
"@libp2p/mplex": "^6.0.2",
"@libp2p/pubsub": "^4.0.0",
"@libp2p/tcp": "^4.0.0",
"@libp2p/topology": "^3.0.1",
"@libp2p/webrtc-star": "^3.0.3",
"@libp2p/websockets": "^3.0.4",
"@libp2p/webrtc-star": "^4.0.1",
"@libp2p/websockets": "^4.0.0",
"@types/node-forge": "^1.0.0",
"@types/p-fifo": "^1.0.0",
"@types/varint": "^6.0.0",
Expand All @@ -211,4 +211,4 @@
"browser": {
"nat-api": false
}
}
}
17 changes: 17 additions & 0 deletions src/address-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ export class DefaultAddressManager extends EventEmitter<AddressManagerEvents> {
return Array.from(this.observed).map((a) => multiaddr(a))
}

/**
* Add peer observed addresses
* Signal that we have confidence an observed multiaddr is publicly dialable -
* this will make it appear in the output of getAddresses()
*/
confirmObservedAddr (addr: Multiaddr): void {

}

/**
* Signal that we do not have confidence an observed multiaddr is publicly dialable -
* this will remove it from the output of getObservedAddrs()
*/
removeObservedAddr (addr: Multiaddr): void {

}

/**
* Add peer observed addresses
*/
Expand Down
56 changes: 34 additions & 22 deletions src/upgrader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -235,7 +235,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
/**
* Upgrades an outbound connection
*/
async upgradeOutbound (maConn: MultiaddrConnection): Promise<Connection> {
async upgradeOutbound (maConn: MultiaddrConnection, opts?: UpgraderOptions): Promise<Connection> {
const idStr = maConn.remoteAddr.getPeerId()
if (idStr == null) {
throw errCode(new Error('outbound connection must have a peer id'), codes.ERR_INVALID_MULTIADDR)
Expand Down Expand Up @@ -265,39 +265,51 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> 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 (opts?.skipProtection !== true) {
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))
encryptedConn = protectedConn
if (opts?.skipEncryption !== true) {
({
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)
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)
}
} else {
cryptoProtocol = 'native'
remotePeer = remotePeerId
}

// Multiplex the connection
if (this.muxers.size > 0) {
upgradedConn = encryptedConn
if (opts?.muxerFactory != null) {
muxerFactory = opts.muxerFactory
} else if (this.muxers.size > 0) {
// Multiplex the connection
const multiplexed = await this._multiplexOutbound({
...protectedConn,
...encryptedConn
}, this.muxers)
muxerFactory = multiplexed.muxerFactory
upgradedConn = multiplexed.stream
} else {
upgradedConn = encryptedConn
}
} catch (err: any) {
log.error('Failed to upgrade outbound connection', err)
Expand Down Expand Up @@ -418,7 +430,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> 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

Expand Down Expand Up @@ -616,7 +628,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> 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<string, StreamMuxerFactory>): Promise<{ stream: Duplex<Uint8Array>, muxerFactory?: StreamMuxerFactory}> {
async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{stream: Duplex<Uint8Array>, muxerFactory?: StreamMuxerFactory}> {
const protocols = Array.from(muxers.keys())
log('outbound selecting muxer %s', protocols)
try {
Expand All @@ -636,7 +648,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> 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<string, StreamMuxerFactory>): Promise<{ stream: Duplex<Uint8Array>, muxerFactory?: StreamMuxerFactory}> {
async _multiplexInbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{stream: Duplex<Uint8Array>, muxerFactory?: StreamMuxerFactory}> {
const protocols = Array.from(muxers.keys())
log('inbound handling muxers %s', protocols)
try {
Expand Down

0 comments on commit 6615efa

Please sign in to comment.