Skip to content

Commit

Permalink
feat: custom announce filter
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Dec 10, 2020
1 parent 186f9b7 commit 48476d5
Show file tree
Hide file tree
Showing 43 changed files with 75 additions and 147 deletions.
1 change: 1 addition & 0 deletions doc/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ Besides the `modules` and `config`, libp2p allows other internal options and con
- `listen` addresses will be provided to the libp2p underlying transports for listening on them.
- `announce` addresses will be used to compute the advertises that the node should advertise to the network.
- `noAnnounce` addresses will be used as a filter to compute the advertises that the node should advertise to the network.
- `announceFilter`: filter function used to filter announced addresses programmatically: `(ma: Array<multiaddr>) => Array<multiaddr>`. Default: bypass all addresses. [`libp2p-utils`](https://github.com/libp2p/js-libp2p-utils) provides useful [multiaddr utilities](https://github.com/libp2p/js-libp2p-utils/blob/master/API.md#multiaddr-isloopbackma) to create your filters.

### Examples

Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,9 @@
"@nodeutils/defaults-deep": "^1.1.0",
"abortable-iterator": "^3.0.0",
"aegir": "^27.0.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"chai-bytes": "^0.1.2",
"chai-string": "^1.5.0",
"delay": "^4.3.0",
"dirty-chai": "^2.0.1",
"interop-libp2p": "^0.3.0",
"ipfs-http-client": "^47.0.1",
"it-concat": "^1.0.0",
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,13 @@ class Libp2p extends EventEmitter {
* @returns {Array<Multiaddr>}
*/
get multiaddrs () {
const announceFilter = this._options.addresses.announceFilter || ((multiaddrs) => multiaddrs)

// Filter noAnnounce multiaddrs
const filterMa = this.addressManager.getNoAnnounceAddrs()

// Create advertising list
return this.transportManager.getAddrs()
return announceFilter(this.transportManager.getAddrs()
.concat(this.addressManager.getAnnounceAddrs())
.filter((ma, index, array) => {
// Filter out if repeated
Expand All @@ -385,7 +387,7 @@ class Libp2p extends EventEmitter {
}

return true
})
}))
}

/**
Expand Down
6 changes: 1 addition & 5 deletions test/addresses/address-manager.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
const { expect } = chai

const { expect } = require('aegir/utils/chai')
const multiaddr = require('multiaddr')

const AddressManager = require('../../src/address-manager')
Expand Down
35 changes: 30 additions & 5 deletions test/addresses/addresses.node.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
const { expect } = chai
const { expect } = require('aegir/utils/chai')
const sinon = require('sinon')

const isLoopback = require('libp2p-utils/src/multiaddr/is-loopback')

const { AddressesOptions } = require('./utils')
const peerUtils = require('../utils/creators/peer')

const listenAddresses = ['/ip4/127.0.0.1/tcp/0', '/ip4/127.0.0.1/tcp/8000/ws']
const announceAddreses = ['/dns4/peer.io']
const announceAddreses = ['/dns4/peer.io/tcp/433/p2p/12D3KooWNvSZnPi3RrhrTwEY4LuuBeB6K6facKUCJcyWG1aoDd2p']

describe('libp2p.multiaddrs', () => {
let libp2p
Expand Down Expand Up @@ -123,4 +122,30 @@ describe('libp2p.multiaddrs', () => {
expect(advertiseMultiaddrs).to.not.include(m)
})
})

it('can filter out loopback addresses to announced by the announce filter', async () => {
[libp2p] = await peerUtils.createPeer({
started: false,
config: {
...AddressesOptions,
addresses: {
listen: listenAddresses,
announce: announceAddreses,
announceFilter: (multiaddrs) => multiaddrs.filter(m => !isLoopback(m))
}
}
})

const listenAddrs = libp2p.addressManager.listen
expect(listenAddrs.size).to.equal(listenAddresses.length)
expect(listenAddrs.has(listenAddresses[0])).to.equal(true)
expect(listenAddrs.has(listenAddresses[1])).to.equal(true)

await libp2p.start()

const multiaddrs = libp2p.multiaddrs
expect(multiaddrs.length).to.equal(announceAddreses.length)
expect(multiaddrs.includes(listenAddresses[0])).to.equal(false)
expect(multiaddrs.includes(listenAddresses[1])).to.equal(false)
})
})
5 changes: 1 addition & 4 deletions test/connection-manager/index.node.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
const { expect } = chai
const { expect } = require('aegir/utils/chai')
const sinon = require('sinon')

const delay = require('delay')
Expand Down
5 changes: 1 addition & 4 deletions test/connection-manager/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
const { expect } = chai
const { expect } = require('aegir/utils/chai')
const sinon = require('sinon')

const peerUtils = require('../utils/creators/peer')
Expand Down
5 changes: 1 addition & 4 deletions test/content-routing/content-routing.node.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
const { expect } = chai
const { expect } = require('aegir/utils/chai')
const nock = require('nock')
const sinon = require('sinon')

Expand Down
5 changes: 1 addition & 4 deletions test/content-routing/dht/configuration.node.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
const { expect } = chai

const { expect } = require('aegir/utils/chai')
const mergeOptions = require('merge-options')

const { create } = require('../../../src')
Expand Down
5 changes: 1 addition & 4 deletions test/core/encryption.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
const { expect } = chai
const { expect } = require('aegir/utils/chai')

const Transport = require('libp2p-websockets')
const { NOISE: Crypto } = require('libp2p-noise')
Expand Down
4 changes: 1 addition & 3 deletions test/core/listening.node.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
const { expect } = chai
const { expect } = require('aegir/utils/chai')

const Transport = require('libp2p-tcp')
const { NOISE: Crypto } = require('libp2p-noise')
Expand Down
4 changes: 1 addition & 3 deletions test/core/ping.node.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
const { expect } = chai
const { expect } = require('aegir/utils/chai')

const pTimes = require('p-times')
const pipe = require('it-pipe')
Expand Down
5 changes: 1 addition & 4 deletions test/dialing/dial-request.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
const { expect } = chai
const { expect } = require('aegir/utils/chai')
const sinon = require('sinon')

const { AbortError } = require('libp2p-interfaces/src/transport/errors')
Expand Down
5 changes: 1 addition & 4 deletions test/dialing/direct.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
const { expect } = chai
const { expect } = require('aegir/utils/chai')
const sinon = require('sinon')
const pDefer = require('p-defer')
const pWaitFor = require('p-wait-for')
Expand Down
Empty file added test/dialing/utils.js
Empty file.
5 changes: 1 addition & 4 deletions test/identify/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
const { expect } = chai
const { expect } = require('aegir/utils/chai')
const sinon = require('sinon')

const { EventEmitter } = require('events')
Expand Down
4 changes: 1 addition & 3 deletions test/insecure/plaintext.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
const { expect } = chai
const { expect } = require('aegir/utils/chai')
const sinon = require('sinon')

const PeerId = require('peer-id')
Expand Down
5 changes: 1 addition & 4 deletions test/keychain/cms-interop.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { chai, expect } = require('aegir/utils/chai')
chai.use(require('chai-string'))
const uint8ArrayFromString = require('uint8arrays/from-string')
const uint8ArrayToString = require('uint8arrays/to-string')
Expand Down
4 changes: 2 additions & 2 deletions test/keychain/keychain.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ describe('keychain', () => {
it('can find a key without a password', async () => {
const keychain = new Keychain(datastore2)
const keychainWithPassword = new Keychain(datastore2, { passPhrase: `hello-${Date.now()}-${Date.now()}` })
const id = `key-${Math.random()}`
const name = `key-${Math.random()}`

await keychainWithPassword.createKey(id, 'ed25519')
const { id } = await keychainWithPassword.createKey(name, 'ed25519')

await expect(keychain.findKeyById(id)).to.eventually.be.ok()
})
Expand Down
5 changes: 1 addition & 4 deletions test/keychain/peerid.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { expect } = require('aegir/utils/chai')
const PeerId = require('peer-id')
const multihash = require('multihashes')
const crypto = require('libp2p-crypto')
Expand Down
5 changes: 1 addition & 4 deletions test/metrics/index.node.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
const { expect } = chai
const { expect } = require('aegir/utils/chai')
const sinon = require('sinon')

const { randomBytes } = require('libp2p-crypto')
Expand Down
5 changes: 1 addition & 4 deletions test/metrics/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
const { expect } = chai
const { expect } = require('aegir/utils/chai')
const sinon = require('sinon')

const { EventEmitter } = require('events')
Expand Down
4 changes: 1 addition & 3 deletions test/peer-discovery/index.node.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
const { expect } = chai
const { expect } = require('aegir/utils/chai')
const sinon = require('sinon')
const defer = require('p-defer')
const mergeOptions = require('merge-options')
Expand Down
4 changes: 1 addition & 3 deletions test/peer-discovery/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
const { expect } = chai
const { expect } = require('aegir/utils/chai')
const sinon = require('sinon')

const defer = require('p-defer')
Expand Down
4 changes: 1 addition & 3 deletions test/peer-routing/peer-routing.node.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
const { expect } = chai
const { expect } = require('aegir/utils/chai')
const nock = require('nock')
const sinon = require('sinon')

Expand Down
5 changes: 1 addition & 4 deletions test/peer-store/address-book.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 6] */

const chai = require('chai')
chai.use(require('dirty-chai'))
const { expect } = chai

const { expect } = require('aegir/utils/chai')
const { Buffer } = require('buffer')
const multiaddr = require('multiaddr')
const arrayEquals = require('libp2p-utils/src/array-equals')
Expand Down
4 changes: 1 addition & 3 deletions test/peer-store/key-book.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
const { chai, expect } = require('aegir/utils/chai')
chai.use(require('chai-bytes'))
const { expect } = chai
const sinon = require('sinon')

const PeerStore = require('../../src/peer-store')
Expand Down
4 changes: 1 addition & 3 deletions test/peer-store/metadata-book.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
const { chai, expect } = require('aegir/utils/chai')
chai.use(require('chai-bytes'))
const { expect } = chai
const uint8ArrayFromString = require('uint8arrays/from-string')

const pDefer = require('p-defer')
Expand Down
4 changes: 1 addition & 3 deletions test/peer-store/peer-store.node.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
const { chai, expect } = require('aegir/utils/chai')
chai.use(require('chai-bytes'))
const { expect } = chai
const sinon = require('sinon')

const baseOptions = require('../utils/base-options')
Expand Down
4 changes: 1 addition & 3 deletions test/peer-store/peer-store.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
const { expect } = chai
const { expect } = require('aegir/utils/chai')

const PeerStore = require('../../src/peer-store')
const multiaddr = require('multiaddr')
Expand Down
Loading

0 comments on commit 48476d5

Please sign in to comment.