Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Commit

Permalink
start integrating secio
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Aug 9, 2016
1 parent 6cbcf23 commit d1e337e
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 29,569 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@
"libp2p-tcp": "^0.7.4",
"libp2p-webrtc-star": "^0.3.2",
"libp2p-websockets": "^0.7.1",
"pre-commit": "^1.1.2",
"pre-commit": "^1.1.3",
"stream-pair": "^1.0.3",
"webrtcsupport": "^2.2.0"
},
"dependencies": {
"babel-runtime": "^6.6.1",
"babel-runtime": "^6.9.0",
"bl": "^1.1.2",
"browserify-zlib": "github:ipfs/browserify-zlib",
"debug": "^2.2.0",
Expand All @@ -61,8 +61,9 @@
"ip-address": "^5.8.0",
"length-prefixed-stream": "^1.5.0",
"libp2p-identify": "^0.1.3",
"libp2p-secio": "^0.3.0",
"lodash.contains": "^2.4.3",
"multiaddr": "^2.0.0",
"multiaddr": "^2.0.2",
"multistream-select": "^0.9.0",
"peer-id": "^0.7.0",
"peer-info": "^0.7.0",
Expand All @@ -77,4 +78,4 @@
"Richard Littauer <[email protected]>",
"dignifiedquire <[email protected]>"
]
}
}
18 changes: 16 additions & 2 deletions src/dial.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const multistream = require('multistream-select')
const Connection = require('interface-connection').Connection

const protocolMuxer = require('./protocol-muxer')
const secio = require('./secio')
const tags = require('./tags')

module.exports = function dial (swarm) {
return (pi, protocol, callback) => {
Expand Down Expand Up @@ -97,13 +99,24 @@ module.exports = function dial (swarm) {
cryptoDial()

function cryptoDial () {
// currently, no crypto channel is implemented
const ms = new multistream.Dialer()
ms.handle(conn, (err) => {
if (err) {
return cb(err)
}
ms.select('/plaintext/1.0.0', cb)

const id = swarm._peerInfo.id
if (id.privKey == null || swarm.encrypt === false) {
return ms.select(tags.plaintex, cb)
}

ms.select(tags.secio, (err, conn) => {
if (err) {
return cb(err)
}

cb(null, secio.create(id, conn))
})
})
}
})
Expand Down Expand Up @@ -148,6 +161,7 @@ module.exports = function dial (swarm) {

muxedConn.once('close', () => {
delete swarm.muxedConns[pi.id.toB58String()]
conn.end()
swarm.emit('peer-mux-closed', pi)
})

Expand Down
20 changes: 17 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const transport = require('./transport')
const connection = require('./connection')
const dial = require('./dial')
const protocolMuxer = require('./protocol-muxer')
const secio = require('./secio')
const tags = require('./tags')

exports = module.exports = Swarm

Expand Down Expand Up @@ -50,6 +52,9 @@ function Swarm (peerInfo) {
// is the Identify protocol enabled?
this.identify = false

// is encryption enabled?
this.encrypt = true

this.transport = transport(this)
this.connection = connection(this)

Expand Down Expand Up @@ -90,9 +95,18 @@ function Swarm (peerInfo) {
this.protocols[protocol] = handler
}

// our crypto handshake :)
this.handle('/plaintext/1.0.0', (conn) => {
protocolMuxer(this.protocols, conn)
let cryptoTag = tags.secio
if (this.encrypt === false) {
cryptoTag = tags.plaintext
}

this.handle(cryptoTag, (conn) => {
if (this.encrypt === false) {
return protocolMuxer(this.protocols, conn)
}

const secure = secio.create(this._peerInfo.id, conn)
protocolMuxer(this.protocols, secure)
})

this.unhandle = (protocol, handler) => {
Expand Down
10 changes: 10 additions & 0 deletions src/secio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict'

const SecureSession = require('libp2p-secio').SecureSession

exports = module.exports

exports.create = (local, insecure) => {
const session = new SecureSession(local, local.privKey, insecure)
return session.secureStream()
}
6 changes: 6 additions & 0 deletions src/tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict'

module.exports = {
secio: '/secio/1.0.0',
plaintext: '/plaintext/1.0.0'
}
3 changes: 0 additions & 3 deletions src/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

const Connection = require('interface-connection').Connection
const parallel = require('run-parallel')
const debug = require('debug')
const log = debug('libp2p:swarm')

const protocolMuxer = require('./protocol-muxer')

Expand Down Expand Up @@ -105,7 +103,6 @@ module.exports = function (swarm) {
return (cb) => {
const listener = transport.createListener(handler)
listener.listen(ma, () => {
log('Listener started on:', ma.toString())
listener.getAddrs((err, addrs) => {
if (err) {
return cb(err)
Expand Down
128 changes: 123 additions & 5 deletions test/06-conn-upgrade-secio.node.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,128 @@
/* eslint-env mocha */
'use strict'

describe('secio conn upgrade (on TCP)', function () {
this.timeout(20000)
const expect = require('chai').expect

it.skip('add', (done) => {})
it.skip('dial', (done) => {})
it.skip('tls on a muxed stream (not the full conn)', (done) => {})
const parallel = require('run-parallel')
const multiaddr = require('multiaddr')
const Peer = require('peer-info')
const TCP = require('libp2p-tcp')
const multiplex = require('libp2p-spdy')

const Swarm = require('../src')

describe.skip('secio conn upgrade (on TCP)', function () {
this.timeout(60 * 1000)

var swarmA
var peerA
var swarmB
var peerB
var swarmC
var peerC

before((done) => {
peerA = new Peer()
peerB = new Peer()
peerC = new Peer()

// console.log('peer A', peerA.id.toB58String())
// console.log('peer B', peerB.id.toB58String())
// console.log('peer C', peerC.id.toB58String())

peerA.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/9001'))
peerB.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/9002'))
peerC.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/9003'))

swarmA = new Swarm(peerA)
swarmB = new Swarm(peerB)
swarmC = new Swarm(peerC)

swarmA.encrypt = true
swarmB.encrypt = true
swarmC.encrypt = true

swarmA.transport.add('tcp', new TCP())
swarmB.transport.add('tcp', new TCP())
swarmC.transport.add('tcp', new TCP())

parallel([
(cb) => swarmA.transport.listen('tcp', {}, null, cb),
(cb) => swarmB.transport.listen('tcp', {}, null, cb),
(cb) => swarmC.transport.listen('tcp', {}, null, cb)
], done)
})

after((done) => {
console.log('closing connections')
parallel([
(cb) => swarmA.close(cb),
(cb) => swarmB.close(cb),
(cb) => swarmC.close(cb)
], (err) => {
console.log('after', err)
done()
})
})

it('add', () => {
swarmA.connection.addStreamMuxer(multiplex)
swarmB.connection.addStreamMuxer(multiplex)
swarmC.connection.addStreamMuxer(multiplex)
})

it('handle + dial on protocol', (done) => {
swarmB.handle('/abacaxi/1.0.0', (conn) => {
conn.pipe(conn)
})

swarmA.dial(peerB, '/abacaxi/1.0.0', (err, conn) => {
expect(err).to.not.exist
expect(Object.keys(swarmA.muxedConns).length).to.equal(1)
conn.end()

conn.on('data', () => {}) // let it flow.. let it flooooow
conn.on('end', done)
})
})

it.skip('dial to warm conn', (done) => {
swarmB.dial(peerA, (err) => {
expect(err).to.not.exist
expect(Object.keys(swarmB.conns).length).to.equal(0)
expect(Object.keys(swarmB.muxedConns).length).to.equal(1)
done()
})
})

it.skip('dial on protocol, reuse warmed conn', (done) => {
swarmA.handle('/papaia/1.0.0', (conn) => {
conn.pipe(conn)
conn.on('error', (err) => { throw err })
})

swarmB.dial(peerA, '/papaia/1.0.0', (err, conn) => {
expect(err).to.not.exist
expect(Object.keys(swarmB.conns).length).to.equal(0)
expect(Object.keys(swarmB.muxedConns).length).to.equal(1)
conn.end()
conn.on('error', (err) => { throw err })
conn.on('data', () => {}) // let it flow.. let it flooooow
conn.on('end', done)
})
})

it.skip('enable identify to reuse incomming muxed conn', (done) => {
swarmA.connection.reuse()
swarmC.connection.reuse()

swarmC.dial(peerA, (err) => {
expect(err).to.not.exist
setTimeout(() => {
expect(Object.keys(swarmC.muxedConns).length).to.equal(1)
expect(Object.keys(swarmA.muxedConns).length).to.equal(2)
done()
}, 500)
})
})
})
Loading

0 comments on commit d1e337e

Please sign in to comment.