From 257f92f7b22bf5070bec99da5d50aa00869c5c90 Mon Sep 17 00:00:00 2001 From: mkg20001 Date: Mon, 27 Nov 2017 13:56:46 +0100 Subject: [PATCH] feat: allowJoinWithDisabledChallenge See https://github.com/ipfs/js-ipfs/issues/1095#issuecomment-347163822 --- .aegir.js | 2 +- src/index.js | 4 +++- src/listener.js | 2 ++ test/dial.spec.js | 6 +++--- test/discovery.spec.js | 4 ++-- test/listen.spec.js | 2 +- test/reconnect.node.js | 6 +++--- test/strict.spec.js | 7 +++++-- test/valid-connection.spec.js | 4 ++-- 9 files changed, 22 insertions(+), 15 deletions(-) diff --git a/.aegir.js b/.aegir.js index 6e5af2f..137dd2f 100644 --- a/.aegir.js +++ b/.aegir.js @@ -14,7 +14,7 @@ function boot (done) { refreshPeerListIntervalMS: 1000 }, v) - parallel([['r1', {port: 15001, metrics: f}], ['r2', {port: 15002}], ['r3', {port: 15003, host: '::'}]].map((v) => (cb) => { + parallel([['r1', {port: 15001, metrics: f}], ['r2', {port: 15002}], ['r3', {port: 15003, host: '::'}], ['r4', {port: 15004, cryptoChallenge: true}]].map((v) => (cb) => { rendezvous.start(base(v.pop()), (err, r) => { if (err) { return cb(err) } _r.push(r) diff --git a/src/index.js b/src/index.js index d2315c4..5c2e0f9 100644 --- a/src/index.js +++ b/src/index.js @@ -24,6 +24,7 @@ class WebsocketStar { options = options || {} this.id = options.id + this.flag = options.allowJoinWithDisabledChallenge // let's just refer to it as "flag" this.discovery = new EE() this.discovery.start = (callback) => { @@ -84,7 +85,8 @@ class WebsocketStar { const listener = new Listener({ id: this.id, handler, - listeners: this.listeners_list + listeners: this.listeners_list, + flag: this.flag }) listener.on('peer', this._peerDiscovered) diff --git a/src/listener.js b/src/listener.js index d67a315..8e6d3d2 100644 --- a/src/listener.js +++ b/src/listener.js @@ -37,6 +37,7 @@ class Listener extends EE { this.canCrypto = Boolean(options.id) this._handler = options.handler || noop this.listeners_list = options.listeners || {} + this.flag = options.flag } // "private" functions @@ -116,6 +117,7 @@ class Listener extends EE { this._join(callback) }) } else { + if (!this.flag) { return callback(new Error('Tried to listen on a server with crypto challenge disabled!\n This is prohibited by default and can lead to security issues!\n Please set "allowJoinWithDisabledChallenge" to true in the constructor options (but only if you know what you are doing)!')) } this.signature = '_' callback() } diff --git a/test/dial.spec.js b/test/dial.spec.js index a3be912..21933d7 100644 --- a/test/dial.spec.js +++ b/test/dial.spec.js @@ -36,7 +36,7 @@ describe('dial', () => { const maLocalIP4 = '/ip4/127.0.0.1/tcp/15001' // const maLocalIP6 = '/ip6/::1/tcp/15003' - const maGen = (base, id, sec) => multiaddr(`/${base}/${sec ? "wss" : "ws"}/p2p-websocket-star/ipfs/${id}`) + const maGen = (base, id, sec) => multiaddr(`/${base}/${sec ? 'wss' : 'ws'}/p2p-websocket-star/ipfs/${id}`) if (process.env.REMOTE_DNS) { // test with deployed signalling server using DNS @@ -65,8 +65,8 @@ describe('dial', () => { before((done) => { map(require('./ids.json'), PeerId.createFromJSON, (err, ids) => { if (err) return done(err) - ws1 = new WebSocketsStar({ id: ids[0] }) - ws2 = new WebSocketsStar({ id: ids[1] }) + ws1 = new WebSocketsStar({ id: ids[0], allowJoinWithDisabledChallenge: true }) + ws2 = new WebSocketsStar({ id: ids[1], allowJoinWithDisabledChallenge: true }) each([ [ws1, ma1], diff --git a/test/discovery.spec.js b/test/discovery.spec.js index 4f20e6a..9b2f46c 100644 --- a/test/discovery.spec.js +++ b/test/discovery.spec.js @@ -20,7 +20,7 @@ describe('peer discovery', () => { const ma2 = multiaddr('/ip4/127.0.0.1/tcp/15003/ws/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooo3B') it('listen on the first', (done) => { - ws1 = new WebSocketStar() + ws1 = new WebSocketStar({ allowJoinWithDisabledChallenge: true }) const listener = ws1.createListener((/* conn */) => {}) @@ -32,7 +32,7 @@ describe('peer discovery', () => { }) it('listen on the second, discover the first', (done) => { - ws2 = new WebSocketStar() + ws2 = new WebSocketStar({ allowJoinWithDisabledChallenge: true }) ws1.discovery.once('peer', (peerInfo) => { expect(peerInfo.multiaddrs.has(ma2)).to.equal(true) diff --git a/test/listen.spec.js b/test/listen.spec.js index 16b72e4..9d195e9 100644 --- a/test/listen.spec.js +++ b/test/listen.spec.js @@ -20,7 +20,7 @@ describe('listen', () => { const mav6 = multiaddr('/ip6/::1/tcp/15003/ws/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooooB') before(() => { - ws = new WebSocketStar() + ws = new WebSocketStar({ allowJoinWithDisabledChallenge: true }) }) it('listen, check for callback', (done) => { diff --git a/test/reconnect.node.js b/test/reconnect.node.js index f127b8b..5d15552 100644 --- a/test/reconnect.node.js +++ b/test/reconnect.node.js @@ -35,7 +35,7 @@ describe('reconnect to signaling server', () => { after((done) => r.stop(done)) it('listen on the first', (done) => { - ws1 = new WebSocketStar() + ws1 = new WebSocketStar({ allowJoinWithDisabledChallenge: true }) const listener = ws1.createListener((conn) => {}) listener.listen(ma1, (err) => { @@ -45,7 +45,7 @@ describe('reconnect to signaling server', () => { }) it('listen on the second, discover the first', (done) => { - ws2 = new WebSocketStar() + ws2 = new WebSocketStar({ allowJoinWithDisabledChallenge: true }) ws1.discovery.once('peer', (peerInfo) => { expect(peerInfo.multiaddrs.has(ma2)).to.equal(true) @@ -71,7 +71,7 @@ describe('reconnect to signaling server', () => { }) it('listen on the third, first discovers it', (done) => { - ws3 = new WebSocketStar() + ws3 = new WebSocketStar({ allowJoinWithDisabledChallenge: true }) const listener = ws3.createListener((conn) => {}) listener.listen(ma3, (err) => expect(err).to.not.exist()) diff --git a/test/strict.spec.js b/test/strict.spec.js index 6c3ac44..86305af 100644 --- a/test/strict.spec.js +++ b/test/strict.spec.js @@ -15,6 +15,8 @@ const pull = require('pull-stream') const WebSocketStar = require('../src') +const SERVER_PORT = 15004 + describe('strict', () => { let id1 let ma1 @@ -32,8 +34,9 @@ describe('strict', () => { id1 = keys.shift() id2 = keys.shift() - ma1 = multiaddr('/ip4/127.0.0.1/tcp/15002/ws/p2p-websocket-star/ipfs/QmS8BL7M8jrXYhHo2ofEVeiq5aDKTr29ksmpcqWxjZGvpX') - ma2 = multiaddr('/ip4/127.0.0.1/tcp/15002/ws/p2p-websocket-star/ipfs/QmeJGHUQ4hsMvPzAoXCdkT1Z9NBgjT7BenVPENUgpufENP') + ma1 = multiaddr('/ip4/127.0.0.1/tcp/' + SERVER_PORT + '/ws/p2p-websocket-star/ipfs/' + id1.toB58String()) + ma2 = multiaddr('/ip4/127.0.0.1/tcp/' + SERVER_PORT + '/ws/p2p-websocket-star/ipfs/' + id2.toB58String()) + done() }) }) diff --git a/test/valid-connection.spec.js b/test/valid-connection.spec.js index ffbb066..cd803e1 100644 --- a/test/valid-connection.spec.js +++ b/test/valid-connection.spec.js @@ -26,14 +26,14 @@ describe('valid Connection', () => { series([first, second], dial) function first (next) { - ws1 = new WebSocketStar() + ws1 = new WebSocketStar({ allowJoinWithDisabledChallenge: true }) const listener = ws1.createListener((conn) => pull(conn, conn)) listener.listen(ma1, next) } function second (next) { - ws2 = new WebSocketStar() + ws2 = new WebSocketStar({ allowJoinWithDisabledChallenge: true }) const listener = ws2.createListener((conn) => pull(conn, conn)) listener.listen(ma2, next)