2016-08-05 14:22:18 +02:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const multiaddr = require('multiaddr')
|
|
|
|
const Connection = require('interface-connection').Connection
|
|
|
|
const os = require('os')
|
2016-12-04 20:28:18 +08:00
|
|
|
const includes = require('lodash.includes')
|
2018-08-02 17:23:27 +02:00
|
|
|
const {createServer} = require('pull-net')
|
2016-08-31 06:41:34 -04:00
|
|
|
const EventEmitter = require('events').EventEmitter
|
|
|
|
const debug = require('debug')
|
|
|
|
const log = debug('libp2p:tcp:listen')
|
2016-08-05 14:22:18 +02:00
|
|
|
|
|
|
|
const getMultiaddr = require('./get-multiaddr')
|
|
|
|
const IPFS_CODE = 421
|
2017-09-03 10:01:16 +01:00
|
|
|
|
2017-04-07 11:51:56 -04:00
|
|
|
function noop () {}
|
2016-08-05 14:22:18 +02:00
|
|
|
|
|
|
|
module.exports = (handler) => {
|
2016-08-31 06:41:34 -04:00
|
|
|
const listener = new EventEmitter()
|
|
|
|
|
2018-08-02 17:23:27 +02:00
|
|
|
const server = createServer((stream) => {
|
|
|
|
const addr = getMultiaddr(stream)
|
2018-07-23 03:48:26 +00:00
|
|
|
if (!addr) {
|
2018-08-02 17:23:27 +02:00
|
|
|
if (stream.remoteAddress === undefined || stream.remoteAddress.address === 'undefined') {
|
2018-07-23 03:48:26 +00:00
|
|
|
log('connection closed before p2p connection made')
|
|
|
|
} else {
|
|
|
|
log('error interpreting incoming p2p connection')
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-31 06:41:34 -04:00
|
|
|
log('new connection', addr.toString())
|
|
|
|
|
2018-08-02 17:23:27 +02:00
|
|
|
stream.getObservedAddrs = (cb) => {
|
2017-04-07 11:51:56 -04:00
|
|
|
cb(null, [addr])
|
2016-08-05 14:22:18 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 17:23:27 +02:00
|
|
|
const conn = new Connection(stream)
|
2016-08-05 14:22:18 +02:00
|
|
|
handler(conn)
|
2016-08-31 06:41:34 -04:00
|
|
|
listener.emit('connection', conn)
|
2016-08-05 14:22:18 +02:00
|
|
|
})
|
|
|
|
|
2018-08-02 17:23:27 +02:00
|
|
|
listener.emit('listening')
|
2016-08-31 06:41:34 -04:00
|
|
|
|
2017-04-07 11:51:56 -04:00
|
|
|
listener.close = (options, callback) => {
|
2016-08-05 14:22:18 +02:00
|
|
|
if (typeof options === 'function') {
|
2017-04-07 11:51:56 -04:00
|
|
|
callback = options
|
2016-08-05 14:22:18 +02:00
|
|
|
options = {}
|
|
|
|
}
|
2017-04-07 11:51:56 -04:00
|
|
|
callback = callback || noop
|
2016-08-05 14:22:18 +02:00
|
|
|
options = options || {}
|
|
|
|
|
2018-08-02 17:23:27 +02:00
|
|
|
server.close((err, ...a) => {
|
|
|
|
listener.emit('close')
|
|
|
|
callback(err, ...a)
|
2018-02-07 06:43:15 +01:00
|
|
|
})
|
2016-08-05 14:22:18 +02:00
|
|
|
}
|
2016-08-31 06:41:34 -04:00
|
|
|
|
2016-08-05 14:22:18 +02:00
|
|
|
let ipfsId
|
|
|
|
let listeningAddr
|
|
|
|
|
2017-04-07 11:51:56 -04:00
|
|
|
listener.listen = (ma, callback) => {
|
2016-08-05 14:22:18 +02:00
|
|
|
listeningAddr = ma
|
2016-12-04 20:28:18 +08:00
|
|
|
if (includes(ma.protoNames(), 'ipfs')) {
|
2016-08-05 14:22:18 +02:00
|
|
|
ipfsId = getIpfsId(ma)
|
|
|
|
listeningAddr = ma.decapsulate('ipfs')
|
|
|
|
}
|
|
|
|
|
|
|
|
const lOpts = listeningAddr.toOptions()
|
|
|
|
log('Listening on %s %s', lOpts.port, lOpts.host)
|
2017-04-07 11:51:56 -04:00
|
|
|
return server.listen(lOpts.port, lOpts.host, callback)
|
2016-08-05 14:22:18 +02:00
|
|
|
}
|
|
|
|
|
2017-04-07 11:51:56 -04:00
|
|
|
listener.getAddrs = (callback) => {
|
2016-08-05 14:22:18 +02:00
|
|
|
const multiaddrs = []
|
2016-08-31 06:41:34 -04:00
|
|
|
const address = server.address()
|
2016-08-05 14:22:18 +02:00
|
|
|
|
|
|
|
if (!address) {
|
2017-04-07 11:51:56 -04:00
|
|
|
return callback(new Error('Listener is not ready yet'))
|
2016-08-05 14:22:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Because TCP will only return the IPv6 version
|
|
|
|
// we need to capture from the passed multiaddr
|
|
|
|
if (listeningAddr.toString().indexOf('ip4') !== -1) {
|
|
|
|
let m = listeningAddr.decapsulate('tcp')
|
|
|
|
m = m.encapsulate('/tcp/' + address.port)
|
|
|
|
if (ipfsId) {
|
|
|
|
m = m.encapsulate('/ipfs/' + ipfsId)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m.toString().indexOf('0.0.0.0') !== -1) {
|
|
|
|
const netInterfaces = os.networkInterfaces()
|
|
|
|
Object.keys(netInterfaces).forEach((niKey) => {
|
|
|
|
netInterfaces[niKey].forEach((ni) => {
|
|
|
|
if (ni.family === 'IPv4') {
|
|
|
|
multiaddrs.push(multiaddr(m.toString().replace('0.0.0.0', ni.address)))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
multiaddrs.push(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address.family === 'IPv6') {
|
|
|
|
let ma = multiaddr('/ip6/' + address.address + '/tcp/' + address.port)
|
|
|
|
if (ipfsId) {
|
|
|
|
ma = ma.encapsulate('/ipfs/' + ipfsId)
|
|
|
|
}
|
|
|
|
|
|
|
|
multiaddrs.push(ma)
|
|
|
|
}
|
|
|
|
|
2017-04-07 11:51:56 -04:00
|
|
|
callback(null, multiaddrs)
|
2016-08-05 14:22:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return listener
|
|
|
|
}
|
|
|
|
|
|
|
|
function getIpfsId (ma) {
|
|
|
|
return ma.stringTuples().filter((tuple) => {
|
|
|
|
return tuple[0] === IPFS_CODE
|
|
|
|
})[0][1]
|
|
|
|
}
|