2019-08-16 17:30:03 +02:00
|
|
|
'use strict'
|
|
|
|
|
2019-11-29 16:41:08 +01:00
|
|
|
const EventEmitter = require('events')
|
2019-08-16 17:30:03 +02:00
|
|
|
const multiaddr = require('multiaddr')
|
|
|
|
|
|
|
|
const debug = require('debug')
|
|
|
|
const log = debug('libp2p:circuit:listener')
|
|
|
|
log.err = debug('libp2p:circuit:error:listener')
|
|
|
|
|
2019-11-29 16:41:08 +01:00
|
|
|
/**
|
2020-09-16 16:43:09 +02:00
|
|
|
* @param {Libp2p} libp2p
|
2019-11-29 16:41:08 +01:00
|
|
|
* @returns {Listener} a transport listener
|
|
|
|
*/
|
2020-09-16 16:43:09 +02:00
|
|
|
module.exports = (libp2p) => {
|
2019-11-29 16:41:08 +01:00
|
|
|
const listener = new EventEmitter()
|
|
|
|
const listeningAddrs = new Map()
|
2019-08-16 17:30:03 +02:00
|
|
|
|
2020-09-16 16:43:09 +02:00
|
|
|
// Remove listeningAddrs when a peer disconnects
|
|
|
|
libp2p.connectionManager.on('peer:disconnect', (connection) => {
|
|
|
|
const deleted = listeningAddrs.delete(connection.remotePeer.toB58String())
|
|
|
|
|
|
|
|
if (deleted) {
|
2020-09-23 13:14:53 +02:00
|
|
|
// Announce listen addresses change
|
|
|
|
listener.emit('listening')
|
2020-09-16 16:43:09 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-08-16 17:30:03 +02:00
|
|
|
/**
|
|
|
|
* Add swarm handler and listen for incoming connections
|
|
|
|
*
|
2019-11-29 16:41:08 +01:00
|
|
|
* @param {Multiaddr} addr
|
2020-10-06 14:59:43 +02:00
|
|
|
* @returns {void}
|
2019-08-16 17:30:03 +02:00
|
|
|
*/
|
2019-11-29 16:41:08 +01:00
|
|
|
listener.listen = async (addr) => {
|
2020-04-14 12:37:52 +02:00
|
|
|
const addrString = String(addr).split('/p2p-circuit').find(a => a !== '')
|
2019-08-16 17:30:03 +02:00
|
|
|
|
2020-09-16 16:43:09 +02:00
|
|
|
const relayConn = await libp2p.dial(multiaddr(addrString))
|
2019-11-29 16:41:08 +01:00
|
|
|
const relayedAddr = relayConn.remoteAddr.encapsulate('/p2p-circuit')
|
2019-08-16 17:30:03 +02:00
|
|
|
|
2019-11-29 16:41:08 +01:00
|
|
|
listeningAddrs.set(relayConn.remotePeer.toB58String(), relayedAddr)
|
|
|
|
listener.emit('listening')
|
2019-08-16 17:30:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-29 16:41:08 +01:00
|
|
|
* TODO: Remove the peers from our topology
|
2019-08-16 17:30:03 +02:00
|
|
|
*
|
2020-10-06 14:59:43 +02:00
|
|
|
* @returns {void}
|
2019-08-16 17:30:03 +02:00
|
|
|
*/
|
2019-11-29 16:41:08 +01:00
|
|
|
listener.close = () => {}
|
2019-08-16 17:30:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get fixed up multiaddrs
|
|
|
|
*
|
|
|
|
* NOTE: This method will grab the peers multiaddrs and expand them such that:
|
|
|
|
*
|
|
|
|
* a) If it's an existing /p2p-circuit address for a specific relay i.e.
|
2020-10-06 14:59:43 +02:00
|
|
|
* `/ip4/0.0.0.0/tcp/0/ipfs/QmRelay/p2p-circuit` this method will expand the
|
|
|
|
* address to `/ip4/0.0.0.0/tcp/0/ipfs/QmRelay/p2p-circuit/ipfs/QmPeer` where
|
|
|
|
* `QmPeer` is this peers id
|
2019-08-16 17:30:03 +02:00
|
|
|
* b) If it's not a /p2p-circuit address, it will encapsulate the address as a /p2p-circuit
|
2020-10-06 14:59:43 +02:00
|
|
|
* addr, such when dialing over a relay with this address, it will create the circuit using
|
|
|
|
* the encapsulated transport address. This is useful when for example, a peer should only
|
|
|
|
* be dialed over TCP rather than any other transport
|
2019-08-16 17:30:03 +02:00
|
|
|
*
|
2020-10-06 14:59:43 +02:00
|
|
|
* @returns {Multiaddr[]}
|
2019-08-16 17:30:03 +02:00
|
|
|
*/
|
2019-11-29 16:41:08 +01:00
|
|
|
listener.getAddrs = () => {
|
|
|
|
const addrs = []
|
|
|
|
for (const addr of listeningAddrs.values()) {
|
|
|
|
addrs.push(addr)
|
2019-08-16 17:30:03 +02:00
|
|
|
}
|
2019-11-29 16:41:08 +01:00
|
|
|
return addrs
|
2019-08-16 17:30:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return listener
|
|
|
|
}
|