From 6769f5b0b033900d6689bf91252872483252428a Mon Sep 17 00:00:00 2001 From: mzdws <8580712+mzdws@user.noreply.gitee.com> Date: Wed, 8 Sep 2021 12:36:45 +0800 Subject: [PATCH 1/8] feat: gater --- doc/CONFIGURATION.md | 35 ++++++++++++++++++++++++++- doc/CONNECTION_MANAGER.md | 37 +++++++++++++++++++++++++++++ src/config.js | 4 +++- src/connection-manager/index.js | 42 +++++++++++++++++++++++++++++++++ src/dialer/index.js | 13 +++++++++- src/index.js | 2 ++ src/upgrader.js | 15 ++++++++++++ 7 files changed, 145 insertions(+), 3 deletions(-) diff --git a/doc/CONFIGURATION.md b/doc/CONFIGURATION.md index 135390eb1e..4ff4bb6476 100644 --- a/doc/CONFIGURATION.md +++ b/doc/CONFIGURATION.md @@ -586,7 +586,40 @@ const node = await Libp2p.create({ maxSentData: Infinity, maxReceivedData: Infinity, maxEventLoopDelay: Infinity, - movingAverageInterval: 60000 + movingAverageInterval: 60000, + gater: { + + // InterceptPeerDial tests whether we're permitted to Dial the specified peer. + // + // This is called by the dialer.connectToPeer implementation when dialling a peer. + interceptPeerDial: async (/** @type {PeerId} */ peerId) => false, + + // InterceptAddrDial tests whether we're permitted to dial the specified + // multiaddr for the given peer. + // + // This is called by the dialer.connectToPeer implementation after it has + // resolved the peer's addrs, and prior to dialling each. + interceptAddrDial: async (/** @type {PeerId} */ peerId, /** @type {Multiaddr} */ multiaddr) => false, + + + // InterceptAccept tests whether an incipient inbound connection is allowed. + // + // This is called by the upgrader, or by the transport directly (e.g. QUIC, + // Bluetooth), straight after it has accepted a connection from its socket. + interceptAccept: async (/** @type {MultiaddrConnection} */ maConn) => false, + + // InterceptSecured tests whether a given connection, now authenticated, + // is allowed. + // + // This is called by the upgrader, after it has performed the security + // handshake, and before it negotiates the muxer, or by the directly by the + // transport, at the exact same checkpoint. + interceptSecured: async (/** @type {'inbound' | 'outbound'}*/ direction, /** @type {PeerId} */ peerId, /** @type {MultiaddrConnection} */ maConn) => false, + + // InterceptUpgraded tests whether a fully capable connection is allowed. + interceptUpgraded: async (/** @type {MultiaddrConnection | MuxedStream} */ maConn) => false, + + } } }) ``` diff --git a/doc/CONNECTION_MANAGER.md b/doc/CONNECTION_MANAGER.md index 8d0a3aaf47..4086fab83c 100644 --- a/doc/CONNECTION_MANAGER.md +++ b/doc/CONNECTION_MANAGER.md @@ -18,3 +18,40 @@ The following is a list of available options for setting limits for the Connecti - `pollInterval`: sets the poll interval (in milliseconds) for assessing the current state and determining if this peer needs to force a disconnect. Defaults to `2000` (2 seconds). - `movingAverageInterval`: the interval used to calculate moving averages (in milliseconds). Defaults to `60000` (1 minute). This must be an available interval configured in `Metrics` - `defaultPeerValue`: number between 0 and 1. Defaults to 1. +- `gater`: gater options. + +### Gater Options +``` +gater = { + // InterceptPeerDial tests whether we're permitted to Dial the specified peer. + // + // This is called by the dialer.connectToPeer implementation when dialling a peer. + interceptPeerDial: async (/** @type {PeerId} */ peerId) => false, + + // InterceptAddrDial tests whether we're permitted to dial the specified + // multiaddr for the given peer. + // + // This is called by the dialer.connectToPeer implementation after it has + // resolved the peer's addrs, and prior to dialling each. + interceptAddrDial: async (/** @type {PeerId} */ peerId, /** @type {Multiaddr} */ multiaddr) => false, + + + // InterceptAccept tests whether an incipient inbound connection is allowed. + // + // This is called by the upgrader, or by the transport directly (e.g. QUIC, + // Bluetooth), straight after it has accepted a connection from its socket. + interceptAccept: async (/** @type {MultiaddrConnection} */ maConn) => false, + + // InterceptSecured tests whether a given connection, now authenticated, + // is allowed. + // + // This is called by the upgrader, after it has performed the security + // handshake, and before it negotiates the muxer, or by the directly by the + // transport, at the exact same checkpoint. + interceptSecured: async (/** @type {'inbound' | 'outbound'}*/ direction, /** @type {PeerId} */ peerId, /** @type {MultiaddrConnection} */ maConn) => false, + + // InterceptUpgraded tests whether a fully capable connection is allowed. + interceptUpgraded: async (/** @type {MultiaddrConnection | MuxedStream} */ maConn) => false, + +} +``` \ No newline at end of file diff --git a/src/config.js b/src/config.js index 9542c70036..db74211e90 100644 --- a/src/config.js +++ b/src/config.js @@ -13,6 +13,7 @@ const { FaultTolerance } = require('./transport-manager') /** * @typedef {import('multiaddr').Multiaddr} Multiaddr + * @typedef {import('./connection-manager') } ConnectionManager * @typedef {import('.').Libp2pOptions} Libp2pOptions * @typedef {import('.').constructorOptions} constructorOptions */ @@ -25,7 +26,8 @@ const DefaultConfig = { announceFilter: (/** @type {Multiaddr[]} */ multiaddrs) => multiaddrs }, connectionManager: { - minConnections: 25 + minConnections: 25, + gater: /** @type {ConnectionManager.gater} */ {}, }, transportManager: { faultTolerance: FaultTolerance.FATAL_ALL diff --git a/src/connection-manager/index.js b/src/connection-manager/index.js index d63ebf7581..40d5eee38d 100644 --- a/src/connection-manager/index.js +++ b/src/connection-manager/index.js @@ -34,6 +34,9 @@ const defaultOptions = { /** * @typedef {import('../')} Libp2p + * @typedef {import('multiaddr').Multiaddr} Multiaddr + * @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream + * @typedef {import('libp2p-interfaces/src/transport/types').MultiaddrConnection} MultiaddrConnection * @typedef {import('libp2p-interfaces/src/connection').Connection} Connection */ @@ -102,6 +105,45 @@ class ConnectionManager extends EventEmitter { latencyCheckIntervalMs: this._options.pollInterval, dataEmitIntervalMs: this._options.pollInterval }) + + this.gater = { + ...this.gater, + ...libp2p._options.connectionManager.gater, + } + } + + gater = { + + // InterceptPeerDial tests whether we're permitted to Dial the specified peer. + // + // This is called by the dialer.connectToPeer implementation when dialling a peer. + interceptPeerDial: async (/** @type {PeerId} */ peerId) => false, + + // InterceptAddrDial tests whether we're permitted to dial the specified + // multiaddr for the given peer. + // + // This is called by the dialer.connectToPeer implementation after it has + // resolved the peer's addrs, and prior to dialling each. + interceptAddrDial: async (/** @type {PeerId} */ peerId, /** @type {Multiaddr} */ multiaddr) => false, + + + // InterceptAccept tests whether an incipient inbound connection is allowed. + // + // This is called by the upgrader, or by the transport directly (e.g. QUIC, + // Bluetooth), straight after it has accepted a connection from its socket. + interceptAccept: async (/** @type {MultiaddrConnection} */ maConn) => false, + + // InterceptSecured tests whether a given connection, now authenticated, + // is allowed. + // + // This is called by the upgrader, after it has performed the security + // handshake, and before it negotiates the muxer, or by the directly by the + // transport, at the exact same checkpoint. + interceptSecured: async (/** @type {'inbound' | 'outbound'}*/ direction, /** @type {PeerId} */ peerId, /** @type {MultiaddrConnection} */ maConn) => false, + + // InterceptUpgraded tests whether a fully capable connection is allowed. + interceptUpgraded: async (/** @type {MultiaddrConnection | MuxedStream} */ maConn) => false, + } /** diff --git a/src/dialer/index.js b/src/dialer/index.js index 65afe266e7..b5e7b61351 100644 --- a/src/dialer/index.js +++ b/src/dialer/index.js @@ -1,6 +1,8 @@ 'use strict' const debug = require('debug') +const all = require('it-all') +const filter = require('it-filter') const log = Object.assign(debug('libp2p:dialer'), { error: debug('libp2p:dialer:err') }) @@ -29,12 +31,14 @@ const { * @typedef {import('../peer-store')} PeerStore * @typedef {import('../peer-store/address-book').Address} Address * @typedef {import('../transport-manager')} TransportManager + * @typedef {import('../connection-manager')} ConnectionManager */ /** * @typedef {Object} DialerProperties * @property {PeerStore} peerStore * @property {TransportManager} transportManager + * @property {ConnectionManager} connectionManager * * @typedef {(addr:Multiaddr) => Promise} Resolver * @@ -65,6 +69,7 @@ class Dialer { constructor ({ transportManager, peerStore, + connectionManager, addressSorter = publicAddressesFirst, maxParallelDials = MAX_PARALLEL_DIALS, maxAddrsToDial = MAX_ADDRS_TO_DIAL, @@ -72,6 +77,7 @@ class Dialer { maxDialsPerPeer = MAX_PER_PEER_DIALS, resolvers = {} }) { + this.connectionManager = connectionManager; this.transportManager = transportManager this.peerStore = peerStore this.addressSorter = addressSorter @@ -118,6 +124,10 @@ class Dialer { * @returns {Promise} */ async connectToPeer (peer, options = {}) { + const { id } = getPeer(peer) + if (await this.connectionManager.gater.interceptPeerDial(id)) { + throw errCode(new Error('The dial request is blocked by connection gater'), codes.ERR_INVALID_PEER) + } const dialTarget = await this._createCancellableDialTarget(peer) if (!dialTarget.addrs.length) { @@ -177,9 +187,10 @@ class Dialer { * @returns {Promise} */ async _createDialTarget (peer) { - const { id, multiaddrs } = getPeer(peer) + let { id, multiaddrs } = getPeer(peer) if (multiaddrs) { + multiaddrs = await all(filter(multiaddrs, async (multiaddr) => !(await this.connectionManager.gater.interceptAddrDial(id, multiaddr)))) this.peerStore.addressBook.add(id, multiaddrs) } diff --git a/src/index.js b/src/index.js index 3bd1fbfe77..e726bbdd17 100644 --- a/src/index.js +++ b/src/index.js @@ -221,6 +221,7 @@ class Libp2p extends EventEmitter { // Setup the Upgrader this.upgrader = new Upgrader({ + connectionManager: this.connectionManager, localPeer: this.peerId, metrics: this.metrics, onConnection: (connection) => this.connectionManager.onConnect(connection), @@ -263,6 +264,7 @@ class Libp2p extends EventEmitter { this.dialer = new Dialer({ transportManager: this.transportManager, + connectionManager: this.connectionManager, peerStore: this.peerStore, ...this._options.dialer }) diff --git a/src/upgrader.js b/src/upgrader.js index 8ee6c65412..0df3b03416 100644 --- a/src/upgrader.js +++ b/src/upgrader.js @@ -36,6 +36,7 @@ class Upgrader { /** * @param {object} options * @param {PeerId} options.localPeer + * @param {import('./connection-manager')} options.connectionManager * @param {import('./metrics')} [options.metrics] * @param {Map} [options.cryptos] * @param {Map} [options.muxers] @@ -45,11 +46,13 @@ class Upgrader { constructor ({ localPeer, metrics, + connectionManager, cryptos = new Map(), muxers = new Map(), onConnectionEnd = () => {}, onConnection = () => {} }) { + this.connectionManager = connectionManager this.localPeer = localPeer this.metrics = metrics this.cryptos = cryptos @@ -77,6 +80,10 @@ class Upgrader { let setPeer let proxyPeer + if (await this.connectionManager.gater.interceptAccept(maConn)) { + throw errCode(new Error('The multiaddr connection is blocked by gater.interceptAccept'), codes.ERR_CONNECTION_FAILED) + } + if (this.metrics) { ({ setTarget: setPeer, proxy: proxyPeer } = mutableProxy()) const idString = (Math.random() * 1e9).toString(36) + Date.now() @@ -100,6 +107,10 @@ class Upgrader { protocol: cryptoProtocol } = await this._encryptInbound(this.localPeer, protectedConn, this.cryptos)) + if (await this.connectionManager.gater.interceptSecured('inbound', remotePeer, encryptedConn)) { + throw errCode(new Error('The multiaddr connection is blocked by gater.interceptSecured'), codes.ERR_CONNECTION_FAILED) + } + // Multiplex the connection if (this.muxers.size) { ({ stream: upgradedConn, Muxer } = await this._multiplexInbound(encryptedConn, this.muxers)) @@ -112,6 +123,10 @@ class Upgrader { throw err } + if (await this.connectionManager.gater.interceptUpgraded(upgradedConn)) { + throw errCode(new Error('The multiaddr connection is blocked by gater.interceptUpgraded'), codes.ERR_CONNECTION_FAILED) + } + if (this.metrics) { this.metrics.updatePlaceholder(proxyPeer, remotePeer) setPeer(remotePeer) From 5de5be05b27ce0d059bd347584bddabe8b64804d Mon Sep 17 00:00:00 2001 From: mzdws <8580712+mzdws@user.noreply.gitee.com> Date: Wed, 8 Sep 2021 16:08:47 +0800 Subject: [PATCH 2/8] chore: gater test --- doc/CONFIGURATION.md | 9 +- doc/CONNECTION_MANAGER.md | 1 - package.json | 1 + src/connection-manager/index.js | 1 - src/dialer/index.js | 11 +- src/errors.js | 2 + src/upgrader.js | 10 +- test/connection-manager/index.node.js | 198 ++++++++++++++++++++++++++ test/nat-manager/nat-manager.node.js | 2 +- test/upgrading/upgrader.spec.js | 14 ++ 10 files changed, 233 insertions(+), 16 deletions(-) diff --git a/doc/CONFIGURATION.md b/doc/CONFIGURATION.md index 4ff4bb6476..8691f0a6cf 100644 --- a/doc/CONFIGURATION.md +++ b/doc/CONFIGURATION.md @@ -592,21 +592,20 @@ const node = await Libp2p.create({ // InterceptPeerDial tests whether we're permitted to Dial the specified peer. // // This is called by the dialer.connectToPeer implementation when dialling a peer. - interceptPeerDial: async (/** @type {PeerId} */ peerId) => false, + interceptPeerDial: async (/** @type {PeerId} */ peerId) => false, - // InterceptAddrDial tests whether we're permitted to dial the specified + // InterceptAddrDial tests whether we're permitted to dial the specified // multiaddr for the given peer. // // This is called by the dialer.connectToPeer implementation after it has // resolved the peer's addrs, and prior to dialling each. - interceptAddrDial: async (/** @type {PeerId} */ peerId, /** @type {Multiaddr} */ multiaddr) => false, - + interceptAddrDial: async (/** @type {PeerId} */ peerId, /** @type {Multiaddr} */ multiaddr) => false, // InterceptAccept tests whether an incipient inbound connection is allowed. // // This is called by the upgrader, or by the transport directly (e.g. QUIC, // Bluetooth), straight after it has accepted a connection from its socket. - interceptAccept: async (/** @type {MultiaddrConnection} */ maConn) => false, + interceptAccept: async (/** @type {MultiaddrConnection} */ maConn) => false, // InterceptSecured tests whether a given connection, now authenticated, // is allowed. diff --git a/doc/CONNECTION_MANAGER.md b/doc/CONNECTION_MANAGER.md index 4086fab83c..a697eb0b18 100644 --- a/doc/CONNECTION_MANAGER.md +++ b/doc/CONNECTION_MANAGER.md @@ -35,7 +35,6 @@ gater = { // resolved the peer's addrs, and prior to dialling each. interceptAddrDial: async (/** @type {PeerId} */ peerId, /** @type {Multiaddr} */ multiaddr) => false, - // InterceptAccept tests whether an incipient inbound connection is allowed. // // This is called by the upgrader, or by the transport directly (e.g. QUIC, diff --git a/package.json b/package.json index 602a8656d1..a8ec41e645 100644 --- a/package.json +++ b/package.json @@ -124,6 +124,7 @@ "set-delayed-interval": "^1.0.0", "streaming-iterables": "^6.0.0", "timeout-abort-controller": "^1.1.1", + "typescript": "^3.7.7", "uint8arrays": "^3.0.0", "varint": "^6.0.0", "wherearewe": "^1.0.0", diff --git a/src/connection-manager/index.js b/src/connection-manager/index.js index 40d5eee38d..2023ffcbff 100644 --- a/src/connection-manager/index.js +++ b/src/connection-manager/index.js @@ -126,7 +126,6 @@ class ConnectionManager extends EventEmitter { // resolved the peer's addrs, and prior to dialling each. interceptAddrDial: async (/** @type {PeerId} */ peerId, /** @type {Multiaddr} */ multiaddr) => false, - // InterceptAccept tests whether an incipient inbound connection is allowed. // // This is called by the upgrader, or by the transport directly (e.g. QUIC, diff --git a/src/dialer/index.js b/src/dialer/index.js index b5e7b61351..fcc732b7b7 100644 --- a/src/dialer/index.js +++ b/src/dialer/index.js @@ -125,8 +125,8 @@ class Dialer { */ async connectToPeer (peer, options = {}) { const { id } = getPeer(peer) - if (await this.connectionManager.gater.interceptPeerDial(id)) { - throw errCode(new Error('The dial request is blocked by connection gater'), codes.ERR_INVALID_PEER) + if (this.connectionManager && await this.connectionManager.gater.interceptPeerDial(id)) { + throw errCode(new Error('The dial request is blocked by gater.interceptPeerDial'), codes.ERR_PEER_DIAL_INTERCEPTED) } const dialTarget = await this._createCancellableDialTarget(peer) @@ -187,14 +187,15 @@ class Dialer { * @returns {Promise} */ async _createDialTarget (peer) { - let { id, multiaddrs } = getPeer(peer) + const { id, multiaddrs } = getPeer(peer) if (multiaddrs) { - multiaddrs = await all(filter(multiaddrs, async (multiaddr) => !(await this.connectionManager.gater.interceptAddrDial(id, multiaddr)))) - this.peerStore.addressBook.add(id, multiaddrs) + const filteredMultiaddrs = await all(filter(multiaddrs, async (multiaddr) => !(this.connectionManager && await this.connectionManager.gater.interceptAddrDial(id, multiaddr)))) + this.peerStore.addressBook.add(id, filteredMultiaddrs) } let knownAddrs = this.peerStore.addressBook.getMultiaddrsForPeer(id, this.addressSorter) || [] + knownAddrs = await all(filter(knownAddrs, async (multiaddr) => !(this.connectionManager && await this.connectionManager.gater.interceptAddrDial(id, multiaddr)))) // If received a multiaddr to dial, it should be the first to use // But, if we know other multiaddrs for the peer, we should try them too. diff --git a/src/errors.js b/src/errors.js index 5b4d070fb2..bf5d19992f 100644 --- a/src/errors.js +++ b/src/errors.js @@ -11,6 +11,8 @@ exports.codes = { PUBSUB_NOT_STARTED: 'ERR_PUBSUB_NOT_STARTED', DHT_NOT_STARTED: 'ERR_DHT_NOT_STARTED', CONN_ENCRYPTION_REQUIRED: 'ERR_CONN_ENCRYPTION_REQUIRED', + ERR_PEER_DIAL_INTERCEPTED: 'ERR_PEER_DIAL_INTERCEPTED', + ERR_CONNECTION_INTERCEPTED: 'ERR_CONNECTION_INTERCEPTED', ERR_INVALID_PROTOCOLS_FOR_STREAM: 'ERR_INVALID_PROTOCOLS_FOR_STREAM', ERR_CONNECTION_ENDED: 'ERR_CONNECTION_ENDED', ERR_CONNECTION_FAILED: 'ERR_CONNECTION_FAILED', diff --git a/src/upgrader.js b/src/upgrader.js index 0df3b03416..28ee51c809 100644 --- a/src/upgrader.js +++ b/src/upgrader.js @@ -81,7 +81,7 @@ class Upgrader { let proxyPeer if (await this.connectionManager.gater.interceptAccept(maConn)) { - throw errCode(new Error('The multiaddr connection is blocked by gater.interceptAccept'), codes.ERR_CONNECTION_FAILED) + throw errCode(new Error('The multiaddr connection is blocked by gater.interceptAccept'), codes.ERR_CONNECTION_INTERCEPTED) } if (this.metrics) { @@ -108,7 +108,7 @@ class Upgrader { } = await this._encryptInbound(this.localPeer, protectedConn, this.cryptos)) if (await this.connectionManager.gater.interceptSecured('inbound', remotePeer, encryptedConn)) { - throw errCode(new Error('The multiaddr connection is blocked by gater.interceptSecured'), codes.ERR_CONNECTION_FAILED) + throw errCode(new Error('The multiaddr connection is blocked by gater.interceptSecured'), codes.ERR_CONNECTION_INTERCEPTED) } // Multiplex the connection @@ -124,7 +124,7 @@ class Upgrader { } if (await this.connectionManager.gater.interceptUpgraded(upgradedConn)) { - throw errCode(new Error('The multiaddr connection is blocked by gater.interceptUpgraded'), codes.ERR_CONNECTION_FAILED) + throw errCode(new Error('The multiaddr connection is blocked by gater.interceptUpgraded'), codes.ERR_CONNECTION_INTERCEPTED) } if (this.metrics) { @@ -190,6 +190,10 @@ class Upgrader { protocol: cryptoProtocol } = await this._encryptOutbound(this.localPeer, protectedConn, remotePeerId, this.cryptos)) + if (await this.connectionManager.gater.interceptSecured('outbound', remotePeer, encryptedConn)) { + throw errCode(new Error('The multiaddr connection is blocked by gater.interceptSecured'), codes.ERR_CONNECTION_INTERCEPTED) + } + // Multiplex the connection if (this.muxers.size) { ({ stream: upgradedConn, Muxer } = await this._multiplexOutbound(encryptedConn, this.muxers)) diff --git a/test/connection-manager/index.node.js b/test/connection-manager/index.node.js index aae0b3462b..d0a244e43b 100644 --- a/test/connection-manager/index.node.js +++ b/test/connection-manager/index.node.js @@ -11,6 +11,8 @@ const pWaitFor = require('p-wait-for') const peerUtils = require('../utils/creators/peer') const mockConnection = require('../utils/mockConnection') const baseOptions = require('../utils/base-options.browser') +const { codes } = require('../../src/errors') +const { Multiaddr } = require('multiaddr') const listenMultiaddr = '/ip4/127.0.0.1/tcp/15002/ws' @@ -305,4 +307,200 @@ describe('libp2p.connections', () => { await remoteLibp2p.stop() }) }) + + describe('connection gater', () => { + let remoteLibp2p + let port = 15004 + + beforeEach(async () => { + [remoteLibp2p] = await peerUtils.createPeer({ + config: { + peerId: peerIds[1], + addresses: { + listen: ['/ip4/127.0.0.1/tcp/' + (port++) + '/ws'] + }, + modules: baseOptions.modules + } + }) + }) + it('intercept peer dial', async () => { + const [libp2p] = await peerUtils.createPeer({ + config: { + peerId: peerIds[0], + addresses: { + listen: ['/ip4/127.0.0.1/tcp/' + (port++) + '/ws'] + }, + modules: baseOptions.modules, + connectionManager: { + gater: { + interceptPeerDial: async (peer) => { + return peer.toB58String() === remoteLibp2p.peerId.toB58String() + } + } + } + }, + }) + try{ + await libp2p.dial(remoteLibp2p.peerId) + } catch (e) { + expect(e.code).to.equal(codes.ERR_PEER_DIAL_INTERCEPTED) + } + }) + it('intercept addr dial', async () => { + const testAddr = new Multiaddr('/ip4/99.99.99.88/tcp/12345/ws/p2p/' + remoteLibp2p.peerId.toB58String()); + const [libp2p] = await peerUtils.createPeer({ + config: { + peerId: peerIds[0], + addresses: { + listen: ['/ip4/127.0.0.1/tcp/' + (port++) + '/ws'] + }, + modules: baseOptions.modules, + connectionManager: { + gater: { + interceptAddrDial: async (peerId, maddr) => { + return peerId.toB58String() === remoteLibp2p.peerId.toB58String() && maddr.toString() == testAddr.toString() + } + } + } + }, + }) + libp2p.peerStore.addressBook.set(remoteLibp2p.peerId, [ + ...remoteLibp2p.multiaddrs, + testAddr + ]) + const { addrs } = await libp2p.dialer._createCancellableDialTarget(remoteLibp2p.peerId); + expect(addrs.length > 0 && addrs.filter(i => i.toString() === testAddr.toString()).length === 0).to.equal(true) + }) + it('intercept accept', async () => { + const [libp2p] = await peerUtils.createPeer({ + config: { + peerId: peerIds[0], + addresses: { + listen: ['/ip4/127.0.0.1/tcp/' + (port++) + '/ws'] + }, + modules: baseOptions.modules, + connectionManager: { + gater: { + interceptAccept: async (maConn) => { + return maConn.remoteAddr.toString().indexOf('127.0.0.1') >= 0 + } + } + } + }, + }) + remoteLibp2p.peerStore.addressBook.set(libp2p.peerId, libp2p.multiaddrs); + const upgradeInbound = libp2p.upgrader.upgradeInbound.bind(libp2p.upgrader); + expect(await new Promise(async resolve => { + libp2p.upgrader.upgradeInbound = async function(maConn) { + try { + return await upgradeInbound(maConn) + } catch (e) { + if (e.toString().indexOf('interceptAccept') >= 0 && e.code === codes.ERR_CONNECTION_INTERCEPTED) { + resolve(true) + } + } + } + try { + await remoteLibp2p.dial(libp2p.peerId) + } catch (e) { + } + })).to.equal(true); + }) + it('intercept secured (inbound)', async () => { + const [libp2p] = await peerUtils.createPeer({ + config: { + peerId: peerIds[0], + addresses: { + listen: ['/ip4/127.0.0.1/tcp/' + (port++) + '/ws'] + }, + modules: baseOptions.modules, + connectionManager: { + gater: { + interceptSecured: async (type, remotePeer, encryptedConn) => { + return remotePeer.toB58String() === remoteLibp2p.peerId.toB58String() && type === 'inbound' + } + } + } + }, + }) + remoteLibp2p.peerStore.addressBook.set(libp2p.peerId, libp2p.multiaddrs); + const upgradeInbound = libp2p.upgrader.upgradeInbound.bind(libp2p.upgrader); + expect(await new Promise(async resolve => { + libp2p.upgrader.upgradeInbound = async function(maConn) { + try { + return await upgradeInbound(maConn) + } catch (e) { + if (e.toString().indexOf('interceptSecured') >= 0 && e.code === codes.ERR_CONNECTION_INTERCEPTED) { + resolve(true) + } + } + } + try { + await remoteLibp2p.dial(libp2p.peerId) + } catch (e) { + } + })).to.equal(true); + }) + it('intercept secured (outbound)', async () => { + const [libp2p] = await peerUtils.createPeer({ + config: { + peerId: peerIds[0], + addresses: { + listen: ['/ip4/127.0.0.1/tcp/' + (port++) + '/ws'] + }, + modules: baseOptions.modules, + connectionManager: { + gater: { + interceptSecured: async (type, remotePeer, encryptedConn) => { + return remotePeer.toB58String() === remoteLibp2p.peerId.toB58String() && type === 'outbound' + } + } + } + }, + }) + libp2p.peerStore.addressBook.set(remoteLibp2p.peerId, remoteLibp2p.multiaddrs); + expect(await new Promise(async resolve => { + try { + await libp2p.dial(remoteLibp2p.peerId) + } catch (e) { + resolve(e.toString().indexOf('interceptSecured') >= 0) + } + })).to.equal(true); + }) + it('intercept upgraded', async () => { + const [libp2p] = await peerUtils.createPeer({ + config: { + peerId: peerIds[0], + addresses: { + listen: ['/ip4/127.0.0.1/tcp/' + (port++) + '/ws'] + }, + modules: baseOptions.modules, + connectionManager: { + gater: { + interceptUpgraded: async (upgradedConn) => { + return true + } + } + } + }, + }) + remoteLibp2p.peerStore.addressBook.set(libp2p.peerId, libp2p.multiaddrs); + const upgradeInbound = libp2p.upgrader.upgradeInbound.bind(libp2p.upgrader); + expect(await new Promise(async resolve => { + libp2p.upgrader.upgradeInbound = async function(maConn) { + try { + return await upgradeInbound(maConn) + } catch (e) { + if (e.toString().indexOf('interceptUpgraded') >= 0 && e.code === codes.ERR_CONNECTION_INTERCEPTED) { + resolve(true) + } + } + } + try { + await remoteLibp2p.dial(libp2p.peerId) + } catch (e) { + } + })).to.equal(true); + }) + }); }) diff --git a/test/nat-manager/nat-manager.node.js b/test/nat-manager/nat-manager.node.js index 5f5562e0b0..2faa51e088 100644 --- a/test/nat-manager/nat-manager.node.js +++ b/test/nat-manager/nat-manager.node.js @@ -243,7 +243,7 @@ describe('Nat Manager (TCP)', () => { }).to.throw().with.property('code', ERR_INVALID_PARAMETERS) }) - it('shuts the nat api down when stopping', async function () { + it.only('shuts the nat api down when stopping', async function () { function findRoutableAddress () { const interfaces = networkInterfaces() diff --git a/test/upgrading/upgrader.spec.js b/test/upgrading/upgrader.spec.js index e78f7007c3..beaad1d26c 100644 --- a/test/upgrading/upgrader.spec.js +++ b/test/upgrading/upgrader.spec.js @@ -32,6 +32,16 @@ describe('Upgrader', () => { let localPeer let remotePeer + const mockConnectionManager = { + gater: { + interceptPeerDial: async () => false, + interceptAddrDial: async () => false, + interceptAccept: async () => false, + interceptSecured: async () => false, + interceptUpgraded: async () => false, + } + } + before(async () => { ([ localPeer, @@ -42,9 +52,11 @@ describe('Upgrader', () => { ])) localUpgrader = new Upgrader({ + connectionManager: mockConnectionManager, localPeer }) remoteUpgrader = new Upgrader({ + connectionManager: mockConnectionManager, localPeer: remotePeer }) @@ -392,6 +404,7 @@ describe('libp2p.upgrader', () => { const remoteUpgrader = new Upgrader({ localPeer: remotePeer, + connectionManager: libp2p.connectionManager, muxers: new Map([[Muxer.multicodec, Muxer]]), cryptos: new Map([[Crypto.protocol, Crypto]]) }) @@ -424,6 +437,7 @@ describe('libp2p.upgrader', () => { const remoteUpgrader = new Upgrader({ localPeer: remotePeer, + connectionManager: libp2p.connectionManager, muxers: new Map([[Muxer.multicodec, Muxer]]), cryptos: new Map([[Crypto.protocol, Crypto]]) }) From 723be7681f232a75beb6c8025568437cf3aef0ed Mon Sep 17 00:00:00 2001 From: mzdws <8580712+mzdws@user.noreply.gitee.com> Date: Wed, 8 Sep 2021 16:32:44 +0800 Subject: [PATCH 3/8] chore: lint --- doc/CONFIGURATION.md | 42 +++++----- doc/CONNECTION_MANAGER.md | 40 +++++----- src/connection-manager/index.js | 29 +------ test/connection-manager/index.node.js | 108 +++++++++++--------------- test/nat-manager/nat-manager.node.js | 2 +- test/upgrading/upgrader.spec.js | 4 +- 6 files changed, 93 insertions(+), 132 deletions(-) diff --git a/doc/CONFIGURATION.md b/doc/CONFIGURATION.md index 8691f0a6cf..e8847c3a1c 100644 --- a/doc/CONFIGURATION.md +++ b/doc/CONFIGURATION.md @@ -589,34 +589,34 @@ const node = await Libp2p.create({ movingAverageInterval: 60000, gater: { - // InterceptPeerDial tests whether we're permitted to Dial the specified peer. - // - // This is called by the dialer.connectToPeer implementation when dialling a peer. + // InterceptPeerDial tests whether we're permitted to Dial the specified peer. + // + // This is called by the dialer.connectToPeer implementation when dialling a peer. interceptPeerDial: async (/** @type {PeerId} */ peerId) => false, - // InterceptAddrDial tests whether we're permitted to dial the specified - // multiaddr for the given peer. - // - // This is called by the dialer.connectToPeer implementation after it has - // resolved the peer's addrs, and prior to dialling each. + // InterceptAddrDial tests whether we're permitted to dial the specified + // multiaddr for the given peer. + // + // This is called by the dialer.connectToPeer implementation after it has + // resolved the peer's addrs, and prior to dialling each. interceptAddrDial: async (/** @type {PeerId} */ peerId, /** @type {Multiaddr} */ multiaddr) => false, - // InterceptAccept tests whether an incipient inbound connection is allowed. - // - // This is called by the upgrader, or by the transport directly (e.g. QUIC, - // Bluetooth), straight after it has accepted a connection from its socket. + // InterceptAccept tests whether an incipient inbound connection is allowed. + // + // This is called by the upgrader, or by the transport directly (e.g. QUIC, + // Bluetooth), straight after it has accepted a connection from its socket. interceptAccept: async (/** @type {MultiaddrConnection} */ maConn) => false, - // InterceptSecured tests whether a given connection, now authenticated, - // is allowed. - // - // This is called by the upgrader, after it has performed the security - // handshake, and before it negotiates the muxer, or by the directly by the - // transport, at the exact same checkpoint. - interceptSecured: async (/** @type {'inbound' | 'outbound'}*/ direction, /** @type {PeerId} */ peerId, /** @type {MultiaddrConnection} */ maConn) => false, + // InterceptSecured tests whether a given connection, now authenticated, + // is allowed. + // + // This is called by the upgrader, after it has performed the security + // handshake, and before it negotiates the muxer, or by the directly by the + // transport, at the exact same checkpoint. + interceptSecured: async (/** @type {'inbound' | 'outbound'}*/ direction, /** @type {PeerId} */ peerId, /** @type {MultiaddrConnection} */ maConn) => false, - // InterceptUpgraded tests whether a fully capable connection is allowed. - interceptUpgraded: async (/** @type {MultiaddrConnection | MuxedStream} */ maConn) => false, + // InterceptUpgraded tests whether a fully capable connection is allowed. + interceptUpgraded: async (/** @type {MultiaddrConnection | MuxedStream} */ maConn) => false, } } diff --git a/doc/CONNECTION_MANAGER.md b/doc/CONNECTION_MANAGER.md index a697eb0b18..8f896ae04a 100644 --- a/doc/CONNECTION_MANAGER.md +++ b/doc/CONNECTION_MANAGER.md @@ -23,34 +23,34 @@ The following is a list of available options for setting limits for the Connecti ### Gater Options ``` gater = { - // InterceptPeerDial tests whether we're permitted to Dial the specified peer. - // - // This is called by the dialer.connectToPeer implementation when dialling a peer. + // InterceptPeerDial tests whether we're permitted to Dial the specified peer. + // + // This is called by the dialer.connectToPeer implementation when dialling a peer. interceptPeerDial: async (/** @type {PeerId} */ peerId) => false, // InterceptAddrDial tests whether we're permitted to dial the specified - // multiaddr for the given peer. - // - // This is called by the dialer.connectToPeer implementation after it has - // resolved the peer's addrs, and prior to dialling each. + // multiaddr for the given peer. + // + // This is called by the dialer.connectToPeer implementation after it has + // resolved the peer's addrs, and prior to dialling each. interceptAddrDial: async (/** @type {PeerId} */ peerId, /** @type {Multiaddr} */ multiaddr) => false, - // InterceptAccept tests whether an incipient inbound connection is allowed. - // - // This is called by the upgrader, or by the transport directly (e.g. QUIC, - // Bluetooth), straight after it has accepted a connection from its socket. + // InterceptAccept tests whether an incipient inbound connection is allowed. + // + // This is called by the upgrader, or by the transport directly (e.g. QUIC, + // Bluetooth), straight after it has accepted a connection from its socket. interceptAccept: async (/** @type {MultiaddrConnection} */ maConn) => false, - // InterceptSecured tests whether a given connection, now authenticated, - // is allowed. - // - // This is called by the upgrader, after it has performed the security - // handshake, and before it negotiates the muxer, or by the directly by the - // transport, at the exact same checkpoint. - interceptSecured: async (/** @type {'inbound' | 'outbound'}*/ direction, /** @type {PeerId} */ peerId, /** @type {MultiaddrConnection} */ maConn) => false, + // InterceptSecured tests whether a given connection, now authenticated, + // is allowed. + // + // This is called by the upgrader, after it has performed the security + // handshake, and before it negotiates the muxer, or by the directly by the + // transport, at the exact same checkpoint. + interceptSecured: async (/** @type {'inbound' | 'outbound'}*/ direction, /** @type {PeerId} */ peerId, /** @type {MultiaddrConnection} */ maConn) => false, - // InterceptUpgraded tests whether a fully capable connection is allowed. - interceptUpgraded: async (/** @type {MultiaddrConnection | MuxedStream} */ maConn) => false, + // InterceptUpgraded tests whether a fully capable connection is allowed. + interceptUpgraded: async (/** @type {MultiaddrConnection | MuxedStream} */ maConn) => false, } ``` \ No newline at end of file diff --git a/src/connection-manager/index.js b/src/connection-manager/index.js index 2023ffcbff..3cdc6d026a 100644 --- a/src/connection-manager/index.js +++ b/src/connection-manager/index.js @@ -113,36 +113,11 @@ class ConnectionManager extends EventEmitter { } gater = { - - // InterceptPeerDial tests whether we're permitted to Dial the specified peer. - // - // This is called by the dialer.connectToPeer implementation when dialling a peer. interceptPeerDial: async (/** @type {PeerId} */ peerId) => false, - - // InterceptAddrDial tests whether we're permitted to dial the specified - // multiaddr for the given peer. - // - // This is called by the dialer.connectToPeer implementation after it has - // resolved the peer's addrs, and prior to dialling each. interceptAddrDial: async (/** @type {PeerId} */ peerId, /** @type {Multiaddr} */ multiaddr) => false, - - // InterceptAccept tests whether an incipient inbound connection is allowed. - // - // This is called by the upgrader, or by the transport directly (e.g. QUIC, - // Bluetooth), straight after it has accepted a connection from its socket. interceptAccept: async (/** @type {MultiaddrConnection} */ maConn) => false, - - // InterceptSecured tests whether a given connection, now authenticated, - // is allowed. - // - // This is called by the upgrader, after it has performed the security - // handshake, and before it negotiates the muxer, or by the directly by the - // transport, at the exact same checkpoint. - interceptSecured: async (/** @type {'inbound' | 'outbound'}*/ direction, /** @type {PeerId} */ peerId, /** @type {MultiaddrConnection} */ maConn) => false, - - // InterceptUpgraded tests whether a fully capable connection is allowed. - interceptUpgraded: async (/** @type {MultiaddrConnection | MuxedStream} */ maConn) => false, - + interceptSecured: async (/** @type {'inbound' | 'outbound'}*/ direction, /** @type {PeerId} */ peerId, /** @type {MultiaddrConnection} */ maConn) => false, + interceptUpgraded: async (/** @type {MultiaddrConnection | MuxedStream} */ maConn) => false, } /** diff --git a/test/connection-manager/index.node.js b/test/connection-manager/index.node.js index d0a244e43b..6e6eaa0ceb 100644 --- a/test/connection-manager/index.node.js +++ b/test/connection-manager/index.node.js @@ -338,16 +338,16 @@ describe('libp2p.connections', () => { } } } - }, + } }) - try{ + try { await libp2p.dial(remoteLibp2p.peerId) } catch (e) { expect(e.code).to.equal(codes.ERR_PEER_DIAL_INTERCEPTED) } }) it('intercept addr dial', async () => { - const testAddr = new Multiaddr('/ip4/99.99.99.88/tcp/12345/ws/p2p/' + remoteLibp2p.peerId.toB58String()); + const testAddr = new Multiaddr('/ip4/99.99.99.88/tcp/12345/ws/p2p/' + remoteLibp2p.peerId.toB58String()) const [libp2p] = await peerUtils.createPeer({ config: { peerId: peerIds[0], @@ -358,17 +358,17 @@ describe('libp2p.connections', () => { connectionManager: { gater: { interceptAddrDial: async (peerId, maddr) => { - return peerId.toB58String() === remoteLibp2p.peerId.toB58String() && maddr.toString() == testAddr.toString() + return peerId.toB58String() === remoteLibp2p.peerId.toB58String() && maddr.toString() === testAddr.toString() } } } - }, + } }) libp2p.peerStore.addressBook.set(remoteLibp2p.peerId, [ ...remoteLibp2p.multiaddrs, testAddr ]) - const { addrs } = await libp2p.dialer._createCancellableDialTarget(remoteLibp2p.peerId); + const { addrs } = await libp2p.dialer._createCancellableDialTarget(remoteLibp2p.peerId) expect(addrs.length > 0 && addrs.filter(i => i.toString() === testAddr.toString()).length === 0).to.equal(true) }) it('intercept accept', async () => { @@ -386,25 +386,21 @@ describe('libp2p.connections', () => { } } } - }, - }) - remoteLibp2p.peerStore.addressBook.set(libp2p.peerId, libp2p.multiaddrs); - const upgradeInbound = libp2p.upgrader.upgradeInbound.bind(libp2p.upgrader); - expect(await new Promise(async resolve => { - libp2p.upgrader.upgradeInbound = async function(maConn) { - try { - return await upgradeInbound(maConn) - } catch (e) { - if (e.toString().indexOf('interceptAccept') >= 0 && e.code === codes.ERR_CONNECTION_INTERCEPTED) { - resolve(true) - } - } } + }) + remoteLibp2p.peerStore.addressBook.set(libp2p.peerId, libp2p.multiaddrs) + const upgradeInbound = libp2p.upgrader.upgradeInbound.bind(libp2p.upgrader) + libp2p.upgrader.upgradeInbound = async function (maConn) { try { - await remoteLibp2p.dial(libp2p.peerId) + return await upgradeInbound(maConn) } catch (e) { + expect(e.toString().indexOf('interceptAccept') >= 0 && e.code === codes.ERR_CONNECTION_INTERCEPTED).to.equal(true) } - })).to.equal(true); + } + try { + await remoteLibp2p.dial(libp2p.peerId) + } catch (e) { + } }) it('intercept secured (inbound)', async () => { const [libp2p] = await peerUtils.createPeer({ @@ -421,25 +417,21 @@ describe('libp2p.connections', () => { } } } - }, - }) - remoteLibp2p.peerStore.addressBook.set(libp2p.peerId, libp2p.multiaddrs); - const upgradeInbound = libp2p.upgrader.upgradeInbound.bind(libp2p.upgrader); - expect(await new Promise(async resolve => { - libp2p.upgrader.upgradeInbound = async function(maConn) { - try { - return await upgradeInbound(maConn) - } catch (e) { - if (e.toString().indexOf('interceptSecured') >= 0 && e.code === codes.ERR_CONNECTION_INTERCEPTED) { - resolve(true) - } - } } + }) + remoteLibp2p.peerStore.addressBook.set(libp2p.peerId, libp2p.multiaddrs) + const upgradeInbound = libp2p.upgrader.upgradeInbound.bind(libp2p.upgrader) + libp2p.upgrader.upgradeInbound = async function (maConn) { try { - await remoteLibp2p.dial(libp2p.peerId) + return await upgradeInbound(maConn) } catch (e) { + expect(e.toString().indexOf('interceptSecured') >= 0 && e.code === codes.ERR_CONNECTION_INTERCEPTED).to.equal(true) } - })).to.equal(true); + } + try { + remoteLibp2p.dial(libp2p.peerId) + } catch (e) { + } }) it('intercept secured (outbound)', async () => { const [libp2p] = await peerUtils.createPeer({ @@ -456,16 +448,14 @@ describe('libp2p.connections', () => { } } } - }, - }) - libp2p.peerStore.addressBook.set(remoteLibp2p.peerId, remoteLibp2p.multiaddrs); - expect(await new Promise(async resolve => { - try { - await libp2p.dial(remoteLibp2p.peerId) - } catch (e) { - resolve(e.toString().indexOf('interceptSecured') >= 0) } - })).to.equal(true); + }) + libp2p.peerStore.addressBook.set(remoteLibp2p.peerId, remoteLibp2p.multiaddrs) + try { + await libp2p.dial(remoteLibp2p.peerId) + } catch (e) { + expect(e.toString().indexOf('interceptSecured') >= 0).to.equal(true) + } }) it('intercept upgraded', async () => { const [libp2p] = await peerUtils.createPeer({ @@ -482,25 +472,21 @@ describe('libp2p.connections', () => { } } } - }, - }) - remoteLibp2p.peerStore.addressBook.set(libp2p.peerId, libp2p.multiaddrs); - const upgradeInbound = libp2p.upgrader.upgradeInbound.bind(libp2p.upgrader); - expect(await new Promise(async resolve => { - libp2p.upgrader.upgradeInbound = async function(maConn) { - try { - return await upgradeInbound(maConn) - } catch (e) { - if (e.toString().indexOf('interceptUpgraded') >= 0 && e.code === codes.ERR_CONNECTION_INTERCEPTED) { - resolve(true) - } - } } + }) + remoteLibp2p.peerStore.addressBook.set(libp2p.peerId, libp2p.multiaddrs) + const upgradeInbound = libp2p.upgrader.upgradeInbound.bind(libp2p.upgrader) + libp2p.upgrader.upgradeInbound = async function (maConn) { try { - await remoteLibp2p.dial(libp2p.peerId) + return await upgradeInbound(maConn) } catch (e) { + expect(e.toString().indexOf('interceptUpgraded') >= 0 && e.code === codes.ERR_CONNECTION_INTERCEPTED).to.equal(true) } - })).to.equal(true); + } + try { + remoteLibp2p.dial(libp2p.peerId) + } catch (e) { + } }) - }); + }) }) diff --git a/test/nat-manager/nat-manager.node.js b/test/nat-manager/nat-manager.node.js index 2faa51e088..5f5562e0b0 100644 --- a/test/nat-manager/nat-manager.node.js +++ b/test/nat-manager/nat-manager.node.js @@ -243,7 +243,7 @@ describe('Nat Manager (TCP)', () => { }).to.throw().with.property('code', ERR_INVALID_PARAMETERS) }) - it.only('shuts the nat api down when stopping', async function () { + it('shuts the nat api down when stopping', async function () { function findRoutableAddress () { const interfaces = networkInterfaces() diff --git a/test/upgrading/upgrader.spec.js b/test/upgrading/upgrader.spec.js index beaad1d26c..6549b626b2 100644 --- a/test/upgrading/upgrader.spec.js +++ b/test/upgrading/upgrader.spec.js @@ -37,8 +37,8 @@ describe('Upgrader', () => { interceptPeerDial: async () => false, interceptAddrDial: async () => false, interceptAccept: async () => false, - interceptSecured: async () => false, - interceptUpgraded: async () => false, + interceptSecured: async () => false, + interceptUpgraded: async () => false } } From d9aceda6a68772b1da394e1336cf8f0a75338240 Mon Sep 17 00:00:00 2001 From: mzdws <8580712+mzdws@user.noreply.gitee.com> Date: Wed, 8 Sep 2021 16:42:33 +0800 Subject: [PATCH 4/8] fix: lint --- src/connection-manager/index.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/connection-manager/index.js b/src/connection-manager/index.js index 3cdc6d026a..ccabbbfdcb 100644 --- a/src/connection-manager/index.js +++ b/src/connection-manager/index.js @@ -106,20 +106,25 @@ class ConnectionManager extends EventEmitter { dataEmitIntervalMs: this._options.pollInterval }) + /** + * @type {{ + * interceptPeerDial: (peerId: PeerId) => Promise, + * interceptAddrDial: (peerId: PeerId, maddr: Multiaddr) => Promise, + * interceptAccept: (maConn: MultiaddrConnection) => Promise, + * interceptSecured: (direction: 'inbound' | 'outbound', peerId: PeerId, maConn: MultiaddrConnection) => Promise, + * interceptUpgraded: (maConn: MultiaddrConnection | MuxedStream) => Promise, + * }} + */ this.gater = { - ...this.gater, + interceptPeerDial: async (peerId) => false, + interceptAddrDial: async (peerId, multiaddr) => false, + interceptAccept: async (maConn) => false, + interceptSecured: async (direction, peerId, maConn) => false, + interceptUpgraded: async (maConn) => false, ...libp2p._options.connectionManager.gater, } } - gater = { - interceptPeerDial: async (/** @type {PeerId} */ peerId) => false, - interceptAddrDial: async (/** @type {PeerId} */ peerId, /** @type {Multiaddr} */ multiaddr) => false, - interceptAccept: async (/** @type {MultiaddrConnection} */ maConn) => false, - interceptSecured: async (/** @type {'inbound' | 'outbound'}*/ direction, /** @type {PeerId} */ peerId, /** @type {MultiaddrConnection} */ maConn) => false, - interceptUpgraded: async (/** @type {MultiaddrConnection | MuxedStream} */ maConn) => false, - } - /** * Get current number of open connections. */ From 0b33489ed30d1aee7c95fd87ca8a9c4302192511 Mon Sep 17 00:00:00 2001 From: mzdws <8580712+mzdws@user.noreply.gitee.com> Date: Wed, 8 Sep 2021 16:44:38 +0800 Subject: [PATCH 5/8] chore: description --- src/connection-manager/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/connection-manager/index.js b/src/connection-manager/index.js index ccabbbfdcb..c590e490d4 100644 --- a/src/connection-manager/index.js +++ b/src/connection-manager/index.js @@ -107,6 +107,8 @@ class ConnectionManager extends EventEmitter { }) /** + * Connection Gater + * * @type {{ * interceptPeerDial: (peerId: PeerId) => Promise, * interceptAddrDial: (peerId: PeerId, maddr: Multiaddr) => Promise, From 8b96b31cd660f9a626830d94d07c606451c4ed1c Mon Sep 17 00:00:00 2001 From: mzdws <8580712+mzdws@user.noreply.gitee.com> Date: Wed, 8 Sep 2021 16:46:41 +0800 Subject: [PATCH 6/8] chore: lint --- src/config.js | 2 +- src/connection-manager/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config.js b/src/config.js index db74211e90..8a757480f6 100644 --- a/src/config.js +++ b/src/config.js @@ -27,7 +27,7 @@ const DefaultConfig = { }, connectionManager: { minConnections: 25, - gater: /** @type {ConnectionManager.gater} */ {}, + gater: /** @type {ConnectionManager.gater} */ {} }, transportManager: { faultTolerance: FaultTolerance.FATAL_ALL diff --git a/src/connection-manager/index.js b/src/connection-manager/index.js index c590e490d4..6d35183d7f 100644 --- a/src/connection-manager/index.js +++ b/src/connection-manager/index.js @@ -108,7 +108,7 @@ class ConnectionManager extends EventEmitter { /** * Connection Gater - * + * * @type {{ * interceptPeerDial: (peerId: PeerId) => Promise, * interceptAddrDial: (peerId: PeerId, maddr: Multiaddr) => Promise, From 24d81f347d7d573c0de2e64fdd08be65ac785266 Mon Sep 17 00:00:00 2001 From: mzdws <8580712+mzdws@user.noreply.gitee.com> Date: Wed, 8 Sep 2021 16:50:02 +0800 Subject: [PATCH 7/8] chore: lint --- src/connection-manager/index.js | 12 ++++++------ src/dialer/index.js | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/connection-manager/index.js b/src/connection-manager/index.js index 6d35183d7f..15563b7349 100644 --- a/src/connection-manager/index.js +++ b/src/connection-manager/index.js @@ -110,11 +110,11 @@ class ConnectionManager extends EventEmitter { * Connection Gater * * @type {{ - * interceptPeerDial: (peerId: PeerId) => Promise, - * interceptAddrDial: (peerId: PeerId, maddr: Multiaddr) => Promise, - * interceptAccept: (maConn: MultiaddrConnection) => Promise, - * interceptSecured: (direction: 'inbound' | 'outbound', peerId: PeerId, maConn: MultiaddrConnection) => Promise, - * interceptUpgraded: (maConn: MultiaddrConnection | MuxedStream) => Promise, + * interceptPeerDial: (peerId: PeerId) => Promise, + * interceptAddrDial: (peerId: PeerId, maddr: Multiaddr) => Promise, + * interceptAccept: (maConn: MultiaddrConnection) => Promise, + * interceptSecured: (direction: 'inbound' | 'outbound', peerId: PeerId, maConn: MultiaddrConnection) => Promise, + * interceptUpgraded: (maConn: MultiaddrConnection | MuxedStream) => Promise, * }} */ this.gater = { @@ -123,7 +123,7 @@ class ConnectionManager extends EventEmitter { interceptAccept: async (maConn) => false, interceptSecured: async (direction, peerId, maConn) => false, interceptUpgraded: async (maConn) => false, - ...libp2p._options.connectionManager.gater, + ...libp2p._options.connectionManager.gater } } diff --git a/src/dialer/index.js b/src/dialer/index.js index fcc732b7b7..60b7ff1dc1 100644 --- a/src/dialer/index.js +++ b/src/dialer/index.js @@ -77,7 +77,7 @@ class Dialer { maxDialsPerPeer = MAX_PER_PEER_DIALS, resolvers = {} }) { - this.connectionManager = connectionManager; + this.connectionManager = connectionManager this.transportManager = transportManager this.peerStore = peerStore this.addressSorter = addressSorter From 737181b0eb0b48a1d0ea88ec01c00c5c7f776318 Mon Sep 17 00:00:00 2001 From: mzdws <8580712+mzdws@user.noreply.gitee.com> Date: Sat, 11 Sep 2021 05:13:52 +0800 Subject: [PATCH 8/8] chore: fix ts --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index a8ec41e645..602a8656d1 100644 --- a/package.json +++ b/package.json @@ -124,7 +124,6 @@ "set-delayed-interval": "^1.0.0", "streaming-iterables": "^6.0.0", "timeout-abort-controller": "^1.1.1", - "typescript": "^3.7.7", "uint8arrays": "^3.0.0", "varint": "^6.0.0", "wherearewe": "^1.0.0",