diff --git a/examples/delegated-routing/package.json b/examples/delegated-routing/package.json index fc3cae70c8..a136e8433a 100644 --- a/examples/delegated-routing/package.json +++ b/examples/delegated-routing/package.json @@ -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" diff --git a/examples/libp2p-in-the-browser/package.json b/examples/libp2p-in-the-browser/package.json index 2abd84345f..3e24693520 100644 --- a/examples/libp2p-in-the-browser/package.json +++ b/examples/libp2p-in-the-browser/package.json @@ -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": "../../" }, diff --git a/examples/package.json b/examples/package.json index c3baa68c9d..a43e2a9df1 100644 --- a/examples/package.json +++ b/examples/package.json @@ -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", diff --git a/examples/webrtc-direct/package.json b/examples/webrtc-direct/package.json index 37c08976b2..b42ffa5d4a 100644 --- a/examples/webrtc-direct/package.json +++ b/examples/webrtc-direct/package.json @@ -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" }, diff --git a/package.json b/package.json index 4ce25eeb36..2ad2304a60 100644 --- a/package.json +++ b/package.json @@ -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", @@ -115,8 +115,8 @@ "@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", @@ -124,7 +124,7 @@ "@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", @@ -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", @@ -211,4 +211,4 @@ "browser": { "nat-api": false } -} +} \ No newline at end of file diff --git a/src/address-manager/index.ts b/src/address-manager/index.ts index b6726994c3..907f30a8c5 100644 --- a/src/address-manager/index.ts +++ b/src/address-manager/index.ts @@ -76,6 +76,23 @@ export class DefaultAddressManager extends EventEmitter { 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 */ diff --git a/src/upgrader.ts b/src/upgrader.ts index 4057bd3117..6f2ba74c84 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' @@ -235,7 +235,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) @@ -265,39 +265,51 @@ 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 (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) @@ -418,7 +430,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 @@ -616,7 +628,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 { @@ -636,7 +648,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 {