2020-04-18 23:26:46 +02:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const debug = require('debug')
|
|
|
|
const log = debug('libp2p:addresses')
|
|
|
|
log.error = debug('libp2p:addresses:error')
|
|
|
|
|
|
|
|
const multiaddr = require('multiaddr')
|
|
|
|
|
|
|
|
/**
|
2020-04-28 16:21:25 +02:00
|
|
|
* Responsible for managing this peers addresses.
|
2020-04-18 23:26:46 +02:00
|
|
|
* Peers can specify their listen, announce and noAnnounce addresses.
|
|
|
|
* The listen addresses will be used by the libp2p transports to listen for new connections,
|
|
|
|
* while the announce an noAnnounce addresses will be combined with the listen addresses for
|
|
|
|
* address adverstising to other peers in the network.
|
|
|
|
*/
|
|
|
|
class AddressManager {
|
|
|
|
/**
|
2020-10-06 14:59:43 +02:00
|
|
|
* @class
|
2020-04-18 23:26:46 +02:00
|
|
|
* @param {object} [options]
|
2020-10-06 14:59:43 +02:00
|
|
|
* @param {Array<string>} [options.listen = []] - list of multiaddrs string representation to listen.
|
|
|
|
* @param {Array<string>} [options.announce = []] - list of multiaddrs string representation to announce.
|
|
|
|
* @param {Array<string>} [options.noAnnounce = []] - list of multiaddrs string representation to not announce.
|
2020-04-18 23:26:46 +02:00
|
|
|
*/
|
|
|
|
constructor ({ listen = [], announce = [], noAnnounce = [] } = {}) {
|
|
|
|
this.listen = new Set(listen)
|
|
|
|
this.announce = new Set(announce)
|
|
|
|
this.noAnnounce = new Set(noAnnounce)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get peer listen multiaddrs.
|
2020-10-06 14:59:43 +02:00
|
|
|
*
|
|
|
|
* @returns {Array<Multiaddr>}
|
2020-04-18 23:26:46 +02:00
|
|
|
*/
|
2020-04-28 16:21:25 +02:00
|
|
|
getListenAddrs () {
|
2020-04-18 23:26:46 +02:00
|
|
|
return Array.from(this.listen).map((a) => multiaddr(a))
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get peer announcing multiaddrs.
|
2020-10-06 14:59:43 +02:00
|
|
|
*
|
|
|
|
* @returns {Array<Multiaddr>}
|
2020-04-18 23:26:46 +02:00
|
|
|
*/
|
2020-04-28 16:21:25 +02:00
|
|
|
getAnnounceAddrs () {
|
2020-04-18 23:26:46 +02:00
|
|
|
return Array.from(this.announce).map((a) => multiaddr(a))
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get peer noAnnouncing multiaddrs.
|
2020-10-06 14:59:43 +02:00
|
|
|
*
|
|
|
|
* @returns {Array<Multiaddr>}
|
2020-04-18 23:26:46 +02:00
|
|
|
*/
|
2020-04-28 16:21:25 +02:00
|
|
|
getNoAnnounceAddrs () {
|
2020-04-18 23:26:46 +02:00
|
|
|
return Array.from(this.noAnnounce).map((a) => multiaddr(a))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AddressManager
|