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

Commit bf1574d

Browse files
start integrating secio
1 parent 896fe7a commit bf1574d

File tree

10 files changed

+240
-29571
lines changed

10 files changed

+240
-29571
lines changed

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"node": "^4.3.0"
3838
},
3939
"devDependencies": {
40-
"aegir": "^3.0.4",
40+
"aegir": "^3.1.1",
4141
"bl": "^1.1.2",
4242
"buffer-loader": "0.0.1",
4343
"chai": "^3.5.0",
@@ -48,17 +48,19 @@
4848
"libp2p-tcp": "^0.6.0",
4949
"libp2p-webrtc-star": "^0.2.0",
5050
"libp2p-websockets": "^0.6.0",
51-
"pre-commit": "^1.1.2",
51+
"pre-commit": "^1.1.3",
5252
"stream-pair": "^1.0.3",
5353
"webrtcsupport": "^2.2.0"
5454
},
5555
"dependencies": {
56-
"babel-runtime": "^6.6.1",
56+
"babel-runtime": "^6.9.0",
5757
"browserify-zlib": "github:ipfs/browserify-zlib",
58+
"debug": "^2.2.0",
5859
"duplex-passthrough": "github:diasdavid/duplex-passthrough",
5960
"ip-address": "^5.8.0",
61+
"libp2p-secio": "^0.3.0",
6062
"lodash.contains": "^2.4.3",
61-
"multiaddr": "^2.0.0",
63+
"multiaddr": "^2.0.2",
6264
"multistream-select": "^0.9.0",
6365
"peer-id": "^0.7.0",
6466
"peer-info": "^0.7.0",
@@ -73,4 +75,4 @@
7375
"Richard Littauer <[email protected]>",
7476
"dignifiedquire <[email protected]>"
7577
]
76-
}
78+
}

src/default-handler.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
'use strict'
22

33
const multistream = require('multistream-select')
4+
const debug = require('debug')
5+
6+
const log = debug('libp2p:swarm:handler')
7+
log.error = debug('libp2p:swarm:handler:error')
48

59
// incomming connection handler
610
module.exports = function connHandler (protocols, conn) {
711
const ms = new multistream.Listener()
8-
Object.keys(protocols).forEach((protocol) => {
12+
const keys = Object.keys(protocols)
13+
14+
log('handling', keys)
15+
keys.forEach((protocol) => {
916
if (!protocol) {
1017
return
1118
}
1219

1320
ms.addHandler(protocol, protocols[protocol])
1421
})
1522

23+
conn.on('error', (err) => {
24+
log.error(err)
25+
})
26+
1627
ms.handle(conn, (err) => {
1728
if (err) {
29+
log.error(err)
1830
return // the multistream handshake failed
1931
}
2032
})

src/dial.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
const multistream = require('multistream-select')
44
const DuplexPassThrough = require('duplex-passthrough')
5+
const debug = require('debug')
56

67
const connHandler = require('./default-handler')
8+
const secio = require('./secio')
9+
const tags = require('./tags')
10+
11+
const log = debug('libp2p:swarm:dial')
12+
log.error = debug('libp2p:swarm:dial:error')
713

814
module.exports = function dial (swarm) {
915
return (pi, protocol, callback) => {
@@ -17,9 +23,9 @@ module.exports = function dial (swarm) {
1723
}
1824

1925
const pt = new DuplexPassThrough()
20-
2126
const b58Id = pi.id.toB58String()
2227

28+
log('dialing from %s to %s', swarm._peerInfo.id.toB58String(), b58Id)
2329
if (!swarm.muxedConns[b58Id]) {
2430
if (!swarm.conns[b58Id]) {
2531
attemptDial(pi, (err, conn) => {
@@ -88,13 +94,35 @@ module.exports = function dial (swarm) {
8894
cryptoDial()
8995

9096
function cryptoDial () {
91-
// currently, js-libp2p-swarm doesn't implement any crypto
9297
const ms = new multistream.Dialer()
9398
ms.handle(conn, (err) => {
9499
if (err) {
95100
return cb(err)
96101
}
97-
ms.select('/plaintext/1.0.0', cb)
102+
103+
const id = swarm._peerInfo.id
104+
if (id.privKey == null || swarm.encrypt === false) {
105+
log(
106+
'dialer %s dialing INSECURELY %s!',
107+
id.toB58String(),
108+
pi.id.toB58String()
109+
)
110+
111+
return ms.select(tags.plaintex, cb)
112+
}
113+
114+
ms.select(tags.secio, (err, conn) => {
115+
if (err) {
116+
return cb(err)
117+
}
118+
119+
log(
120+
'dialer %s dialing secure to %s!',
121+
id.toB58String(),
122+
pi.id.toB58String()
123+
)
124+
cb(null, secio.create(id, conn))
125+
})
98126
})
99127
}
100128
})
@@ -104,6 +132,7 @@ module.exports = function dial (swarm) {
104132
function attemptMuxerUpgrade (conn, cb) {
105133
const muxers = Object.keys(swarm.muxers)
106134
if (muxers.length === 0) {
135+
log.error('no muxers available')
107136
return cb(new Error('no muxers available'))
108137
}
109138

@@ -115,14 +144,18 @@ module.exports = function dial (swarm) {
115144
nextMuxer(muxers.shift())
116145

117146
function nextMuxer (key) {
147+
log('attempting muxer upgrade %s', key)
118148
const ms = new multistream.Dialer()
119149
ms.handle(conn, (err) => {
120150
if (err) {
151+
log.error(err)
152+
log.error('multistream not supported')
121153
return callback(new Error('multistream not supported'))
122154
}
123155
ms.select(key, (err, conn) => {
124156
if (err) {
125157
if (muxers.length === 0) {
158+
log.error('could not upgrade to stream muxing')
126159
cb(new Error('could not upgrade to stream muxing'))
127160
} else {
128161
nextMuxer(muxers.shift())

src/identify.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ exports.exec = (rawConn, muxer, peerInfo, callback) => {
5858
})
5959

6060
const obsMultiaddr = rawConn.getObservedAddrs()[0]
61+
let publicKey = new Buffer(0)
62+
if (peerInfo.id.pubKey) {
63+
publicKey = peerInfo.id.pubKey.bytes
64+
}
6165

6266
let publicKey = new Buffer(0)
6367
if (peerInfo.id.pubKey) {

src/index.js

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ const util = require('util')
44
const EE = require('events').EventEmitter
55
const parallel = require('run-parallel')
66
const contains = require('lodash.contains')
7+
const debug = require('debug')
78

89
const transport = require('./transport')
910
const connection = require('./connection')
1011
const dial = require('./dial')
1112
const connHandler = require('./default-handler')
13+
const tags = require('./tags')
14+
const secio = require('./secio')
15+
16+
const log = debug('libp2p:swarm')
1217

1318
exports = module.exports = Swarm
1419

@@ -52,6 +57,9 @@ function Swarm (peerInfo) {
5257
// is the Identify protocol enabled?
5358
this.identify = false
5459

60+
// is encryption enabled?
61+
this.encrypt = true
62+
5563
this.transport = transport(this)
5664
this.connection = connection(this)
5765

@@ -82,34 +90,55 @@ function Swarm (peerInfo) {
8290

8391
// Start listening on all available transports
8492
this.listen = (callback) => {
93+
log('listen')
8594
parallel(this.availableTransports(peerInfo).map((ts) => (cb) => {
8695
// Listen on the given transport
8796
this.transport.listen(ts, {}, null, cb)
8897
}), callback)
8998
}
9099

91100
this.handle = (protocol, handler) => {
101+
log('handling %s', protocol)
92102
this.protocols[protocol] = handler
93103
}
94104

95-
// our crypto handshake :)
96-
this.handle('/plaintext/1.0.0', (conn) => {
97-
connHandler(this.protocols, conn)
105+
let cryptoTag = tags.secio
106+
if (this.encrypt === false) {
107+
cryptoTag = tags.plaintext
108+
}
109+
110+
this.handle(cryptoTag, (conn) => {
111+
if (this.encrypt === false) {
112+
log(
113+
'listener %s listening INSECURELY!',
114+
this._peerInfo.id.toB58String()
115+
)
116+
return connHandler(this.protocols, conn)
117+
}
118+
119+
log('securing connection %s', this._peerInfo.id.toB58String())
120+
const secure = secio.create(this._peerInfo.id, conn)
121+
connHandler(this.protocols, secure)
98122
})
99123

100124
this.unhandle = (protocol, handler) => {
125+
log('unhandling %s', protocol)
101126
if (this.protocols[protocol]) {
102127
delete this.protocols[protocol]
103128
}
104129
}
105130

106131
this.close = (callback) => {
132+
log('closing')
107133
Object.keys(this.muxedConns).forEach((key) => {
108134
this.muxedConns[key].muxer.end()
109135
})
110136

111137
parallel(Object.keys(this.transports).map((key) => {
112-
return (cb) => this.transports[key].close(cb)
113-
}), callback)
138+
return (cb) => this.transport.close(key, cb)
139+
}), (err) => {
140+
log('closed', err)
141+
callback(err)
142+
})
114143
}
115144
}

src/secio.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
const SecureSession = require('libp2p-secio').SecureSession
4+
5+
exports = module.exports
6+
7+
exports.create = (local, insecure) => {
8+
const session = new SecureSession(local, local.privKey, insecure)
9+
return session.secureStream()
10+
}

src/tags.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict'
2+
3+
module.exports = {
4+
secio: '/secio/1.0.0',
5+
plaintext: '/plaintext/1.0.0'
6+
}

src/transport.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
const contains = require('lodash.contains')
44
const DuplexPassThrough = require('duplex-passthrough')
5+
const debug = require('debug')
56

67
const connHandler = require('./default-handler')
78

9+
const log = debug('libp2p:swarm:transport')
10+
811
module.exports = function (swarm) {
912
return {
1013
add (key, transport, options, callback) {
14+
log('add %s', key)
1115
if (typeof options === 'function') {
1216
callback = options
1317
options = {}
@@ -22,6 +26,7 @@ module.exports = function (swarm) {
2226
},
2327

2428
dial (key, multiaddrs, callback) {
29+
log('dial %s', key)
2530
const t = swarm.transports[key]
2631

2732
if (!Array.isArray(multiaddrs)) {
@@ -69,6 +74,7 @@ module.exports = function (swarm) {
6974
},
7075

7176
listen (key, options, handler, callback) {
77+
log('listen %s', key)
7278
// if no callback is passed, we pass conns to connHandler
7379
if (!handler) {
7480
handler = connHandler.bind(null, swarm.protocols)
@@ -90,6 +96,7 @@ module.exports = function (swarm) {
9096
},
9197

9298
close (key, callback) {
99+
log('close %s', key)
93100
const transport = swarm.transports[key]
94101

95102
if (!transport) {

0 commit comments

Comments
 (0)