2015-07-15 21:19:46 -07:00
|
|
|
/*
|
2016-03-07 12:47:11 +00:00
|
|
|
* Identify is one of the protocols swarms speaks in order to
|
|
|
|
* broadcast and learn about the ip:port pairs a specific peer
|
2016-03-10 20:28:58 +00:00
|
|
|
* is available through and to know when a new stream muxer is
|
|
|
|
* established, so a conn can be reused
|
2015-07-15 21:19:46 -07:00
|
|
|
*/
|
|
|
|
|
2016-03-10 20:28:58 +00:00
|
|
|
const multistream = require('multistream-select')
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const Info = require('peer-info')
|
|
|
|
const Id = require('peer-id')
|
|
|
|
const multiaddr = require('multiaddr')
|
2016-03-07 12:47:11 +00:00
|
|
|
|
2016-03-16 00:32:13 +00:00
|
|
|
const isNode = !global.window
|
|
|
|
|
|
|
|
const identity = isNode
|
|
|
|
? fs.readFileSync(path.join(__dirname, 'identify.proto'))
|
|
|
|
: require('buffer!./identify.proto')
|
|
|
|
|
|
|
|
const pbStream = require('protocol-buffers-stream')(identity)
|
|
|
|
|
2016-03-07 12:47:11 +00:00
|
|
|
exports = module.exports
|
|
|
|
exports.multicodec = '/ipfs/identify/1.0.0'
|
|
|
|
|
2016-03-10 20:28:58 +00:00
|
|
|
exports.exec = (rawConn, muxer, peerInfo, callback) => {
|
2016-03-07 12:47:11 +00:00
|
|
|
// 1. open a stream
|
|
|
|
// 2. multistream into identify
|
2016-03-10 20:28:58 +00:00
|
|
|
// 3. send what I see from this other peer (extract fro conn)
|
2016-03-07 12:47:11 +00:00
|
|
|
// 4. receive what the other peer sees from me
|
|
|
|
// 4. callback with (err, peerInfo)
|
2015-07-15 21:19:46 -07:00
|
|
|
|
2016-03-10 20:28:58 +00:00
|
|
|
const conn = muxer.newStream()
|
2015-07-15 21:19:46 -07:00
|
|
|
|
2016-03-10 20:28:58 +00:00
|
|
|
var msI = new multistream.Interactive()
|
|
|
|
msI.handle(conn, () => {
|
|
|
|
msI.select(exports.multicodec, (err, ds) => {
|
2015-09-23 19:14:29 +01:00
|
|
|
if (err) {
|
2016-03-10 20:28:58 +00:00
|
|
|
return callback(err)
|
2015-09-23 19:14:29 +01:00
|
|
|
}
|
2015-07-15 21:19:46 -07:00
|
|
|
|
2016-03-10 20:28:58 +00:00
|
|
|
var pbs = pbStream()
|
2015-07-15 21:19:46 -07:00
|
|
|
|
2016-03-10 20:28:58 +00:00
|
|
|
pbs.on('identify', (msg) => {
|
|
|
|
peerInfo.multiaddr.addSafe(msg.observedAddr)
|
2015-09-23 19:14:29 +01:00
|
|
|
|
2016-03-10 20:28:58 +00:00
|
|
|
const peerId = Id.createFromPubKey(msg.publicKey)
|
|
|
|
const otherPeerInfo = new Info(peerId)
|
|
|
|
msg.listenAddrs.forEach((ma) => {
|
|
|
|
otherPeerInfo.multiaddr.add(multiaddr(ma))
|
|
|
|
})
|
2015-09-23 19:14:29 +01:00
|
|
|
|
2016-03-10 20:28:58 +00:00
|
|
|
callback(null, otherPeerInfo)
|
|
|
|
})
|
2015-09-23 19:14:29 +01:00
|
|
|
|
2016-03-10 20:28:58 +00:00
|
|
|
const obsMultiaddr = rawConn.getObservedAddrs()[0]
|
2015-09-23 19:14:29 +01:00
|
|
|
|
2016-03-10 20:28:58 +00:00
|
|
|
pbs.identify({
|
2015-07-17 12:05:02 -07:00
|
|
|
protocolVersion: 'na',
|
|
|
|
agentVersion: 'na',
|
2016-03-10 20:28:58 +00:00
|
|
|
publicKey: peerInfo.id.pubKey,
|
|
|
|
listenAddrs: peerInfo.multiaddrs.map((mh) => { return mh.buffer }),
|
|
|
|
observedAddr: obsMultiaddr ? obsMultiaddr.buffer : null
|
2015-07-17 12:05:02 -07:00
|
|
|
})
|
|
|
|
|
2016-03-10 20:28:58 +00:00
|
|
|
pbs.pipe(ds).pipe(pbs)
|
|
|
|
pbs.finalize()
|
2015-07-17 12:05:02 -07:00
|
|
|
})
|
2015-07-15 21:19:46 -07:00
|
|
|
})
|
2015-09-23 19:14:29 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 20:28:58 +00:00
|
|
|
exports.handler = (peerInfo, swarm) => {
|
2015-09-23 19:14:29 +01:00
|
|
|
return function (conn) {
|
2016-03-10 20:28:58 +00:00
|
|
|
// 1. receive incoming observed info about me
|
|
|
|
// 2. update my own information (on peerInfo)
|
|
|
|
// 3. send back what I see from the other (get from swarm.muxedConns[incPeerID].conn.getObservedAddrs()
|
|
|
|
var pbs = pbStream()
|
2015-09-23 19:14:29 +01:00
|
|
|
|
2016-03-10 20:28:58 +00:00
|
|
|
pbs.on('identify', function (msg) {
|
|
|
|
peerInfo.multiaddr.addSafe(msg.observedAddr)
|
2015-09-23 19:14:29 +01:00
|
|
|
|
2016-03-10 20:28:58 +00:00
|
|
|
const peerId = Id.createFromPubKey(msg.publicKey)
|
|
|
|
const conn = swarm.muxedConns[peerId.toB58String()].conn
|
|
|
|
const obsMultiaddr = conn.getObservedAddrs()[0]
|
2015-09-23 19:14:29 +01:00
|
|
|
|
2016-03-10 20:28:58 +00:00
|
|
|
pbs.identify({
|
2015-09-23 19:14:29 +01:00
|
|
|
protocolVersion: 'na',
|
|
|
|
agentVersion: 'na',
|
2016-03-10 20:28:58 +00:00
|
|
|
publicKey: peerInfo.id.pubKey,
|
|
|
|
listenAddrs: peerInfo.multiaddrs.map(function (ma) {
|
|
|
|
return ma.buffer
|
2015-09-23 19:14:29 +01:00
|
|
|
}),
|
2016-03-10 20:28:58 +00:00
|
|
|
observedAddr: obsMultiaddr ? obsMultiaddr.buffer : null
|
2015-07-15 21:19:46 -07:00
|
|
|
})
|
2016-03-10 20:28:58 +00:00
|
|
|
pbs.finalize()
|
2015-07-15 21:19:46 -07:00
|
|
|
})
|
2016-03-10 20:28:58 +00:00
|
|
|
pbs.pipe(conn).pipe(pbs)
|
2015-09-23 19:14:29 +01:00
|
|
|
}
|
2015-07-15 21:19:46 -07:00
|
|
|
}
|