libp2p-switch
Advanced tools
Comparing version
@@ -17,5 +17,5 @@ { | ||
"--timeout", | ||
"5000", | ||
"99999", | ||
"--colors", | ||
"${workspaceFolder}/test/pnet.node.js" | ||
"${workspaceFolder}/test/identify.node.js" | ||
], | ||
@@ -22,0 +22,0 @@ "internalConsoleOptions": "openOnSessionStart" |
@@ -0,1 +1,11 @@ | ||
<a name="0.40.7"></a> | ||
## [0.40.7](https://github.com/libp2p/js-libp2p-switch/compare/v0.40.6...v0.40.7) (2018-07-23) | ||
### Bug Fixes | ||
* improve erroring around invalid peers ([a62a72b](https://github.com/libp2p/js-libp2p-switch/commit/a62a72b)) | ||
<a name="0.40.6"></a> | ||
@@ -2,0 +12,0 @@ ## [0.40.6](https://github.com/libp2p/js-libp2p-switch/compare/v0.40.5...v0.40.6) (2018-07-17) |
{ | ||
"name": "libp2p-switch", | ||
"version": "0.40.6", | ||
"version": "0.40.7", | ||
"description": "libp2p switch implementation in JavaScript", | ||
@@ -40,8 +40,7 @@ "leadMaintainer": "Jacob Heun <jacobheun@gmail.com>", | ||
"devDependencies": { | ||
"aegir": "^13.1.0", | ||
"buffer-loader": "0.0.1", | ||
"aegir": "^15.0.0", | ||
"chai": "^4.1.2", | ||
"dirty-chai": "^2.0.1", | ||
"gulp": "^3.9.1", | ||
"libp2p-mplex": "~0.7.0", | ||
"libp2p-pnet": "~0.1.0", | ||
"libp2p-secio": "~0.10.0", | ||
@@ -53,3 +52,2 @@ "libp2p-spdy": "~0.12.1", | ||
"peer-book": "~0.7.0", | ||
"pull-goodbye": "0.0.2", | ||
"sinon": "^5.0.2", | ||
@@ -66,3 +64,3 @@ "webrtcsupport": "^2.2.0" | ||
"libp2p-circuit": "~0.2.0", | ||
"libp2p-identify": "~0.7.1", | ||
"libp2p-identify": "~0.7.2", | ||
"lodash.includes": "^4.3.0", | ||
@@ -89,2 +87,3 @@ "moving-average": "^1.0.0", | ||
"Jacob Heun <jacobheun@gmail.com>", | ||
"Jacob Heun <jake@andyet.net>", | ||
"Kevin Kwok <antimatter15@gmail.com>", | ||
@@ -91,0 +90,0 @@ "Kobi Gurkan <kobigurk@gmail.com>", |
@@ -78,3 +78,8 @@ libp2p-switch JavaScript implementation | ||
### Private Networks | ||
libp2p-switch supports private networking. In order to enabled private networks, the `switch.protector` must be | ||
set and must contain a `protect` method. You can see an example of this in the [private network | ||
tests]([./test/pnet.node.js]). | ||
## API | ||
@@ -81,0 +86,0 @@ |
@@ -46,55 +46,73 @@ 'use strict' | ||
if (this.switch.identify) { | ||
// overload peerInfo to use Identify instead | ||
conn.getPeerInfo = (callback) => { | ||
const conn = muxedConn.newStream() | ||
const ms = new multistream.Dialer() | ||
callback = once(callback) | ||
// Get the peer info from the crypto exchange | ||
conn.getPeerInfo((err, cryptoPI) => { | ||
if (err || !cryptoPI) { | ||
log('crypto peerInfo wasnt found') | ||
} | ||
waterfall([ | ||
(cb) => ms.handle(conn, cb), | ||
(cb) => ms.select(identify.multicodec, cb), | ||
(conn, cb) => identify.dialer(conn, cb), | ||
(peerInfo, observedAddrs, cb) => { | ||
observedAddrs.forEach((oa) => { | ||
this.switch._peerInfo.multiaddrs.addSafe(oa) | ||
}) | ||
cb(null, peerInfo) | ||
} | ||
], (err, peerInfo) => { | ||
if (peerInfo) { | ||
conn.setPeerInfo(peerInfo) | ||
} | ||
callback(err, peerInfo) | ||
}) | ||
} | ||
// overload peerInfo to use Identify instead | ||
conn.getPeerInfo = (callback) => { | ||
const conn = muxedConn.newStream() | ||
const ms = new multistream.Dialer() | ||
callback = once(callback) | ||
conn.getPeerInfo((err, peerInfo) => { | ||
if (err) { | ||
return log('Identify not successful') | ||
waterfall([ | ||
(cb) => ms.handle(conn, cb), | ||
(cb) => ms.select(identify.multicodec, cb), | ||
// run identify and verify the peer has the same info from crypto | ||
(conn, cb) => identify.dialer(conn, cryptoPI, cb), | ||
(peerInfo, observedAddrs, cb) => { | ||
observedAddrs.forEach((oa) => { | ||
this.switch._peerInfo.multiaddrs.addSafe(oa) | ||
}) | ||
cb(null, peerInfo) | ||
} | ||
], (err, peerInfo) => { | ||
if (err) { | ||
return muxedConn.end(() => { | ||
if (peerInfo) { | ||
setImmediate(() => this.switch.emit('peer-mux-closed', peerInfo)) | ||
} | ||
callback(err, null) | ||
}) | ||
} | ||
if (peerInfo) { | ||
conn.setPeerInfo(peerInfo) | ||
} | ||
callback(err, peerInfo) | ||
}) | ||
} | ||
const b58Str = peerInfo.id.toB58String() | ||
this.switch.muxedConns[b58Str] = { muxer: muxedConn } | ||
conn.getPeerInfo((err, peerInfo) => { | ||
if (err) { | ||
return log('identify not successful') | ||
} | ||
const b58Str = peerInfo.id.toB58String() | ||
if (peerInfo.multiaddrs.size > 0) { | ||
// with incomming conn and through identify, going to pick one | ||
// of the available multiaddrs from the other peer as the one | ||
// I'm connected to as we really can't be sure at the moment | ||
// TODO add this consideration to the connection abstraction! | ||
peerInfo.connect(peerInfo.multiaddrs.toArray()[0]) | ||
} else { | ||
// for the case of websockets in the browser, where peers have | ||
// no addr, use just their IPFS id | ||
peerInfo.connect(`/ipfs/${b58Str}`) | ||
} | ||
peerInfo = this.switch._peerBook.put(peerInfo) | ||
this.switch.muxedConns[b58Str] = { muxer: muxedConn } | ||
muxedConn.on('close', () => { | ||
delete this.switch.muxedConns[b58Str] | ||
peerInfo.disconnect() | ||
if (peerInfo.multiaddrs.size > 0) { | ||
// with incomming conn and through identify, going to pick one | ||
// of the available multiaddrs from the other peer as the one | ||
// I'm connected to as we really can't be sure at the moment | ||
// TODO add this consideration to the connection abstraction! | ||
peerInfo.connect(peerInfo.multiaddrs.toArray()[0]) | ||
} else { | ||
// for the case of websockets in the browser, where peers have | ||
// no addr, use just their IPFS id | ||
peerInfo.connect(`/ipfs/${b58Str}`) | ||
} | ||
peerInfo = this.switch._peerBook.put(peerInfo) | ||
setImmediate(() => this.switch.emit('peer-mux-closed', peerInfo)) | ||
muxedConn.on('close', () => { | ||
delete this.switch.muxedConns[b58Str] | ||
peerInfo.disconnect() | ||
peerInfo = this.switch._peerBook.put(peerInfo) | ||
log(`closed connection to ${b58Str}`) | ||
setImmediate(() => this.switch.emit('peer-mux-closed', peerInfo)) | ||
}) | ||
setImmediate(() => this.switch.emit('peer-mux-established', peerInfo)) | ||
}) | ||
setImmediate(() => this.switch.emit('peer-mux-established', peerInfo)) | ||
}) | ||
@@ -101,0 +119,0 @@ } |
@@ -146,5 +146,14 @@ 'use strict' | ||
} | ||
// If we have a muxer, create a new stream, otherwise it's a standard connection | ||
const connection = muxer.newStream ? muxer.newStream() : muxer | ||
this._performProtocolHandshake(connection, cb) | ||
if (muxer.newStream) { | ||
muxer.newStream((err, conn) => { | ||
if (err) return cb(err) | ||
this._performProtocolHandshake(conn, cb) | ||
}) | ||
return | ||
} | ||
this._performProtocolHandshake(muxer, cb) | ||
} | ||
@@ -185,4 +194,8 @@ ], (err, connection) => { | ||
(baseConnection, cb) => { | ||
// Create a private connection if it's needed | ||
this._createPrivateConnection(baseConnection, cb) | ||
}, | ||
(connection, cb) => { | ||
// Add the Switch's crypt encryption to the connection | ||
this._encryptConnection(baseConnection, cb) | ||
this._encryptConnection(connection, cb) | ||
} | ||
@@ -199,2 +212,27 @@ ], (err, encryptedConnection) => { | ||
/** | ||
* If the switch has a private network protector, `switch.protector`, its `protect` | ||
* method will be called with the given connection. The resulting, wrapped connection | ||
* will be returned via the callback. | ||
* | ||
* @param {Connection} connection The connection to protect | ||
* @param {function(Error, Connection)} callback | ||
* @returns {void} | ||
*/ | ||
_createPrivateConnection (connection, callback) { | ||
if (this.switch.protector === null) { | ||
return callback(null, connection) | ||
} | ||
// If the switch has a protector, be private | ||
const protectedConnection = this.switch.protector.protect(connection, (err) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
protectedConnection.setPeerInfo(this.peerInfo) | ||
callback(null, protectedConnection) | ||
}) | ||
} | ||
/** | ||
* If the given PeerId key, `b58Id`, has an existing muxed connection | ||
@@ -285,2 +323,3 @@ * it will be returned via the callback, otherwise the connection | ||
this.switch._peerInfo.disconnect() | ||
log(`closed connection to ${b58Id}`) | ||
setImmediate(() => this.switch.emit('peer-mux-closed', this.peerInfo)) | ||
@@ -287,0 +326,0 @@ }) |
@@ -15,2 +15,3 @@ 'use strict' | ||
const assert = require('assert') | ||
const Errors = require('./errors') | ||
@@ -56,2 +57,4 @@ class Switch extends EE { | ||
this.protector = this._options.protector || null | ||
this.transport = new TransportManager(this) | ||
@@ -202,1 +205,2 @@ this.connection = new ConnectionManager(this) | ||
module.exports = Switch | ||
module.exports.errors = Errors |
@@ -89,2 +89,4 @@ 'use strict' | ||
listen (key, options, handler, callback) { | ||
let muxerHandler | ||
// if no handler is passed, we pass conns to protocolMuxer | ||
@@ -95,2 +97,13 @@ if (!handler) { | ||
// If we have a protector make the connection private | ||
if (this.switch.protector) { | ||
muxerHandler = handler | ||
handler = (parentConnection) => { | ||
const connection = this.switch.protector.protect(parentConnection, () => { | ||
// If we get an error here, we should stop talking to this peer | ||
muxerHandler(connection) | ||
}) | ||
} | ||
} | ||
const transport = this.switch.transports[key] | ||
@@ -97,0 +110,0 @@ const multiaddrs = TransportManager.dialables( |
Sorry, the diff of this file is not supported yet
6663348
0.06%13
-13.33%81191
0.08%391
1.3%27
-3.57%Updated