|
4 | 4 | */
|
5 | 5 |
|
6 | 6 | var Interactive = require('multistream-select').Interactive
|
7 |
| -var EventEmmiter = require('events').EventEmitter |
8 |
| -var util = require('util') |
9 | 7 | var protobufs = require('protocol-buffers-stream')
|
10 | 8 | var fs = require('fs')
|
11 | 9 | var schema = fs.readFileSync(__dirname + '/identify.proto')
|
12 | 10 | var v6 = require('ip-address').v6
|
13 |
| -var Id = require('ipfs-peer-id') |
| 11 | +var Id = require('peer-id') |
14 | 12 | var multiaddr = require('multiaddr')
|
15 | 13 |
|
16 |
| -exports = module.exports = Identify |
| 14 | +exports = module.exports = identify |
17 | 15 |
|
18 |
| -util.inherits(Identify, EventEmmiter) |
| 16 | +var protoId = '/ipfs/identify/1.0.0' |
19 | 17 |
|
20 |
| -function Identify (swarm, peerSelf) { |
21 |
| - var self = this |
22 |
| - self.createProtoStream = protobufs(schema) |
| 18 | +exports.protoId = protoId |
| 19 | +var createProtoStream = protobufs(schema) |
23 | 20 |
|
24 |
| - swarm.registerHandler('/ipfs/identify/1.0.0', function (stream) { |
25 |
| - var ps = self.createProtoStream() |
| 21 | +function identify (muxedConns, peerInfoSelf, socket, conn, muxer) { |
| 22 | + var msi = new Interactive() |
| 23 | + msi.handle(conn, function () { |
| 24 | + msi.select(protoId, function (err, ds) { |
| 25 | + if (err) { |
| 26 | + return console.log(err) // TODO Treat error |
| 27 | + } |
26 | 28 |
|
27 |
| - ps.on('identify', function (msg) { |
28 |
| - updateSelf(peerSelf, msg.observedAddr) |
| 29 | + var ps = createProtoStream() |
29 | 30 |
|
30 |
| - var peerId = Id.createFromPubKey(msg.publicKey) |
| 31 | + ps.on('identify', function (msg) { |
| 32 | + var peerId = Id.createFromPubKey(msg.publicKey) |
| 33 | + |
| 34 | + updateSelf(peerInfoSelf, msg.observedAddr) |
| 35 | + |
| 36 | + muxedConns[peerId.toB58String()] = { |
| 37 | + muxer: muxer, |
| 38 | + socket: socket |
| 39 | + } |
| 40 | + |
| 41 | + // TODO: Pass the new discovered info about the peer that contacted us |
| 42 | + // to something like the Kademlia Router, so the peerInfo for this peer |
| 43 | + // is fresh |
| 44 | + // - before this was exectued through a event emitter |
| 45 | + // self.emit('peer-update', { |
| 46 | + // peerId: peerId, |
| 47 | + // listenAddrs: msg.listenAddrs.map(function (mhb) {return multiaddr(mhb)}) |
| 48 | + // }) |
| 49 | + }) |
31 | 50 |
|
32 |
| - var socket = swarm.connections[peerId.toB58String()].socket |
33 | 51 | var mh = getMultiaddr(socket)
|
| 52 | + |
34 | 53 | ps.identify({
|
35 | 54 | protocolVersion: 'na',
|
36 | 55 | agentVersion: 'na',
|
37 |
| - publicKey: peerSelf.id.pubKey, |
38 |
| - listenAddrs: peerSelf.multiaddrs.map(function (mh) {return mh.buffer}), |
| 56 | + publicKey: peerInfoSelf.id.pubKey, |
| 57 | + listenAddrs: peerInfoSelf.multiaddrs.map(function (mh) { |
| 58 | + return mh.buffer |
| 59 | + }), |
39 | 60 | observedAddr: mh.buffer
|
40 | 61 | })
|
41 | 62 |
|
42 |
| - self.emit('peer-update', { |
43 |
| - peerId: peerId, |
44 |
| - listenAddrs: msg.listenAddrs.map(function (mhb) {return multiaddr(mhb)}) |
45 |
| - }) |
46 |
| - |
| 63 | + ps.pipe(ds).pipe(ps) |
47 | 64 | ps.finalize()
|
48 | 65 | })
|
49 |
| - ps.pipe(stream).pipe(ps) |
50 | 66 | })
|
| 67 | +} |
| 68 | + |
| 69 | +exports.getHandlerFunction = function (peerInfoSelf, muxedConns) { |
| 70 | + return function (conn) { |
| 71 | + // wait for the other peer to identify itself |
| 72 | + // update our multiaddr with observed addr list |
| 73 | + // then get the socket from our list of muxedConns and send the reply back |
| 74 | + |
| 75 | + var ps = createProtoStream() |
| 76 | + |
| 77 | + ps.on('identify', function (msg) { |
| 78 | + updateSelf(peerInfoSelf, msg.observedAddr) |
51 | 79 |
|
52 |
| - swarm.on('connection-unknown', function (conn, socket) { |
53 |
| - conn.dialStream(function (err, stream) { |
54 |
| - if (err) { return console.log(err) } |
55 |
| - var msi = new Interactive() |
56 |
| - msi.handle(stream, function () { |
57 |
| - msi.select('/ipfs/identify/1.0.0', function (err, ds) { |
58 |
| - if (err) { return console.log(err) } |
59 |
| - |
60 |
| - var ps = self.createProtoStream() |
61 |
| - |
62 |
| - ps.on('identify', function (msg) { |
63 |
| - var peerId = Id.createFromPubKey(msg.publicKey) |
64 |
| - |
65 |
| - updateSelf(peerSelf, msg.observedAddr) |
66 |
| - |
67 |
| - swarm.connections[peerId.toB58String()] = { |
68 |
| - conn: conn, |
69 |
| - socket: socket |
70 |
| - } |
71 |
| - |
72 |
| - self.emit('peer-update', { |
73 |
| - peerId: peerId, |
74 |
| - listenAddrs: msg.listenAddrs.map(function (mhb) {return multiaddr(mhb)}) |
75 |
| - }) |
76 |
| - }) |
77 |
| - |
78 |
| - var mh = getMultiaddr(socket) |
79 |
| - |
80 |
| - ps.identify({ |
81 |
| - protocolVersion: 'na', |
82 |
| - agentVersion: 'na', |
83 |
| - publicKey: peerSelf.id.pubKey, |
84 |
| - listenAddrs: peerSelf.multiaddrs.map(function (mh) {return mh.buffer}), |
85 |
| - observedAddr: mh.buffer |
86 |
| - }) |
87 |
| - |
88 |
| - ps.pipe(ds).pipe(ps) |
89 |
| - ps.finalize() |
90 |
| - }) |
| 80 | + var peerId = Id.createFromPubKey(msg.publicKey) |
| 81 | + |
| 82 | + var socket = muxedConns[peerId.toB58String()].socket |
| 83 | + |
| 84 | + var mh = getMultiaddr(socket) |
| 85 | + |
| 86 | + ps.identify({ |
| 87 | + protocolVersion: 'na', |
| 88 | + agentVersion: 'na', |
| 89 | + publicKey: peerInfoSelf.id.pubKey, |
| 90 | + listenAddrs: peerInfoSelf.multiaddrs.map(function (mh) { |
| 91 | + return mh.buffer |
| 92 | + }), |
| 93 | + observedAddr: mh.buffer |
91 | 94 | })
|
| 95 | + |
| 96 | + // TODO: Pass the new discovered info about the peer that contacted us |
| 97 | + // to something like the Kademlia Router, so the peerInfo for this peer |
| 98 | + // is fresh |
| 99 | + // - before this was exectued through a event emitter |
| 100 | + // self.emit('peer-update', { |
| 101 | + // peerId: peerId, |
| 102 | + // listenAddrs: msg.listenAddrs.map(function (mhb) { |
| 103 | + // return multiaddr(mhb) |
| 104 | + // }) |
| 105 | + // }) |
| 106 | + |
| 107 | + ps.finalize() |
92 | 108 | })
|
93 |
| - }) |
| 109 | + ps.pipe(conn).pipe(ps) |
| 110 | + } |
94 | 111 | }
|
95 | 112 |
|
96 | 113 | function getMultiaddr (socket) {
|
|
0 commit comments