Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 218e8f8

Browse files
committed
Merge pull request #151 from xicombd/feature/swarm
Add swarm connect, peers and localAddrs
2 parents 7143942 + 0d1329d commit 218e8f8

File tree

15 files changed

+542
-41
lines changed

15 files changed

+542
-41
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"debug": "^2.2.0",
8181
"fs-blob-store": "^5.2.1",
8282
"hapi": "^13.3.0",
83-
"ipfs-api": "github:ipfs/js-ipfs-api#f3e2d42",
83+
"ipfs-api": "^3.0.1",
8484
"ipfs-blocks": "^0.1.0",
8585
"ipfs-data-importing": "^0.3.3",
8686
"ipfs-merkle-dag": "^0.4.0",

src/cli/commands/swarm/addrs.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const Command = require('ronin').Command
22
const utils = require('../../utils')
3-
const bs58 = require('bs58')
43
const debug = require('debug')
54
const log = debug('cli:object')
65
log.error = debug('cli:object:error')

src/cli/commands/swarm/addrs/local.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
const Command = require('ronin').Command
22
const utils = require('../../../utils')
3-
const bs58 = require('bs58')
43
const debug = require('debug')
54
const log = debug('cli:object')
65
log.error = debug('cli:object:error')
76

87
module.exports = Command.extend({
9-
desc: '',
8+
desc: 'List local addresses',
109

1110
options: {},
1211

@@ -15,7 +14,20 @@ module.exports = Command.extend({
1514
if (err) {
1615
throw err
1716
}
18-
// TODO
17+
18+
if (!utils.isDaemonOn()) {
19+
throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.')
20+
}
21+
22+
ipfs.swarm.localAddrs((err, res) => {
23+
if (err) {
24+
throw err
25+
}
26+
27+
res.Strings.forEach((addr) => {
28+
console.log(addr)
29+
})
30+
})
1931
})
2032
}
2133
})

src/cli/commands/swarm/connect.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
const Command = require('ronin').Command
22
const utils = require('../../utils')
3-
const bs58 = require('bs58')
43
const debug = require('debug')
5-
const log = debug('cli:object')
6-
log.error = debug('cli:object:error')
4+
const log = debug('cli:swarm')
5+
log.error = debug('cli:swarm:error')
76

87
module.exports = Command.extend({
9-
desc: '',
8+
desc: 'Open connection to a given address',
109

1110
options: {},
1211

13-
run: () => {
12+
run: (address) => {
13+
if (!address) {
14+
throw new Error("Argument 'address' is required")
15+
}
16+
1417
utils.getIPFS((err, ipfs) => {
1518
if (err) {
1619
throw err
1720
}
18-
// TODO
21+
22+
if (!utils.isDaemonOn()) {
23+
throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.')
24+
}
25+
26+
ipfs.swarm.connect(address, (err, res) => {
27+
if (err) {
28+
throw err
29+
}
30+
31+
console.log(res.Strings[0])
32+
})
1933
})
2034
}
2135
})

src/cli/commands/swarm/disconnect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Command = require('ronin').Command
22
const utils = require('../../utils')
3-
const bs58 = require('bs58')
3+
// const bs58 = require('bs58')
44
const debug = require('debug')
55
const log = debug('cli:object')
66
log.error = debug('cli:object:error')

src/cli/commands/swarm/peers.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
const Command = require('ronin').Command
22
const utils = require('../../utils')
3-
const bs58 = require('bs58')
43
const debug = require('debug')
54
const log = debug('cli:object')
65
log.error = debug('cli:object:error')
76

87
module.exports = Command.extend({
9-
desc: '',
8+
desc: 'List peers with open connections',
109

1110
options: {},
1211

@@ -15,7 +14,20 @@ module.exports = Command.extend({
1514
if (err) {
1615
throw err
1716
}
18-
// TODO
17+
18+
if (!utils.isDaemonOn()) {
19+
throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.')
20+
}
21+
22+
ipfs.swarm.peers((err, res) => {
23+
if (err) {
24+
throw err
25+
}
26+
27+
res.Strings.forEach((addr) => {
28+
console.log(addr)
29+
})
30+
})
1931
})
2032
}
2133
})

src/core/index.js

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function IPFS (repo) {
2929
const dagS = new DAGService(blockS)
3030
var peerInfo
3131
var libp2pNode
32+
var peers = {}
3233

3334
this.load = (callback) => {
3435
repo.exists((err, exists) => {
@@ -308,6 +309,8 @@ function IPFS (repo) {
308309
}
309310
}
310311

312+
const OFFLINE_ERROR = new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.')
313+
311314
this.libp2p = {
312315
start: (callback) => {
313316
libp2pNode = new libp2p.Node(peerInfo)
@@ -324,13 +327,54 @@ function IPFS (repo) {
324327
},
325328
swarm: {
326329
peers: (callback) => {
327-
callback(null, [])
330+
if (!libp2pNode) {
331+
return callback(OFFLINE_ERROR)
332+
}
333+
334+
callback(null, peers)
328335
},
329336
// all the addrs we know
330-
addrs: notImpl,
331-
localAddrs: notImpl,
332-
connect: notImpl,
333-
disconnect: notImpl,
337+
addrs: (callback) => {
338+
if (!libp2pNode) {
339+
return callback(OFFLINE_ERROR)
340+
}
341+
342+
notImpl()
343+
},
344+
localAddrs: (callback) => {
345+
if (!libp2pNode) {
346+
return callback(OFFLINE_ERROR)
347+
}
348+
349+
callback(null, peerInfo.multiaddrs)
350+
},
351+
connect: (ma, callback) => {
352+
if (!libp2pNode) {
353+
return callback(OFFLINE_ERROR)
354+
}
355+
356+
const idStr = ma.toString().match(/\/ipfs\/(.*)/)
357+
if (!idStr) {
358+
return callback(new Error('invalid multiaddr'))
359+
}
360+
const id = peerId.createFromB58String(idStr[1])
361+
const peer = new PeerInfo(id)
362+
363+
ma = ma.toString().replace(/\/ipfs\/(.*)/, '') // FIXME remove this when multiaddr supports ipfs
364+
365+
peer.multiaddr.add(multiaddr(ma))
366+
peers[peer.id.toB58String()] = peer
367+
libp2pNode.swarm.dial(peer, (err) => {
368+
callback(err, id)
369+
})
370+
},
371+
disconnect: (callback) => {
372+
if (!libp2pNode) {
373+
return callback(OFFLINE_ERROR)
374+
}
375+
376+
notImpl()
377+
},
334378
filters: notImpl // TODO
335379
},
336380
routing: {},

src/http-api/resources/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ exports.repo = require('./repo')
55
exports.object = require('./object')
66
exports.config = require('./config')
77
exports.block = require('./block')
8+
exports.swarm = require('./swarm')

src/http-api/resources/swarm.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
'use strict'
2+
3+
const ipfs = require('./../index.js').ipfs
4+
const debug = require('debug')
5+
const log = debug('http-api:block')
6+
log.error = debug('http-api:block:error')
7+
8+
exports = module.exports
9+
10+
// common pre request handler that parses the args and returns `addr` which is assigned to `request.pre.args`
11+
exports.parseAddrs = (request, reply) => {
12+
if (!request.query.arg) {
13+
return reply("Argument 'addr' is required").code(400).takeover()
14+
}
15+
16+
return reply({
17+
addr: request.query.arg
18+
})
19+
}
20+
21+
exports.peers = {
22+
// main route handler which is called after the above `parseArgs`, but only if the args were valid
23+
handler: (request, reply) => {
24+
ipfs.libp2p.swarm.peers((err, peers) => {
25+
if (err) {
26+
log.error(err)
27+
return reply({
28+
Message: err.toString(),
29+
Code: 0
30+
}).code(500)
31+
}
32+
33+
return reply({
34+
Strings: Object.keys(peers)
35+
.map((key) =>
36+
`${peers[key].multiaddrs[0].toString()}/ipfs/${peers[key].id.toB58String()}`)
37+
})
38+
})
39+
}
40+
}
41+
42+
exports.localAddrs = {
43+
// main route handler which is called after the above `parseArgs`, but only if the args were valid
44+
handler: (request, reply) => {
45+
ipfs.libp2p.swarm.localAddrs((err, addrs) => {
46+
if (err) {
47+
log.error(err)
48+
return reply({
49+
Message: err.toString(),
50+
Code: 0
51+
}).code(500)
52+
}
53+
54+
return reply({
55+
Strings: addrs.map((addr) => addr.toString())
56+
})
57+
})
58+
}
59+
}
60+
61+
exports.connect = {
62+
// uses common parseAddr method that returns a `addr`
63+
parseArgs: exports.parseAddrs,
64+
65+
// main route handler which is called after the above `parseArgs`, but only if the args were valid
66+
handler: (request, reply) => {
67+
const addr = request.pre.args.addr
68+
69+
ipfs.libp2p.swarm.connect(addr, (err, res) => {
70+
if (err) {
71+
log.error(err)
72+
return reply({
73+
Message: err.toString(),
74+
Code: 0
75+
}).code(500)
76+
}
77+
78+
return reply({
79+
Strings: [`connect ${res.toB58String()} success`]
80+
})
81+
})
82+
}
83+
}

src/http-api/routes/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ module.exports = (server) => {
66
require('./object')(server)
77
// require('./repo')(server)
88
require('./config')(server)
9+
require('./swarm')(server)
910
}

src/http-api/routes/swarm.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@ module.exports = (server) => {
55

66
api.route({
77
method: 'GET',
8-
path: '/api/v0/swarm/addrs',
8+
path: '/api/v0/swarm/peers',
99
config: {
10-
handler: resources.swarm.addrs.handler
10+
handler: resources.swarm.peers.handler
1111
}
1212
})
1313

14+
// api.route({
15+
// method: 'GET',
16+
// path: '/api/v0/swarm/addrs',
17+
// config: {
18+
// handler: resources.swarm.addrs.handler
19+
// }
20+
// })
21+
1422
api.route({
1523
method: 'GET',
1624
path: '/api/v0/swarm/addrs/local',
@@ -23,17 +31,20 @@ module.exports = (server) => {
2331
method: 'GET',
2432
path: '/api/v0/swarm/connect',
2533
config: {
26-
handler: resources.swarm.connect
34+
pre: [
35+
{ method: resources.swarm.connect.parseArgs, assign: 'args' }
36+
],
37+
handler: resources.swarm.connect.handler
2738
}
2839
})
2940

30-
api.route({
31-
method: 'GET',
32-
path: '/api/v0/swarm/disconnect',
33-
config: {
34-
handler: resources.swarm.disconnect
35-
}
36-
})
41+
// api.route({
42+
// method: 'GET',
43+
// path: '/api/v0/swarm/disconnect',
44+
// config: {
45+
// handler: resources.swarm.disconnect
46+
// }
47+
// })
3748

3849
// TODO
3950
// api.route({

0 commit comments

Comments
 (0)