This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add swarm connect, peers and localAddrs
- Loading branch information
Showing
15 changed files
with
542 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,35 @@ | ||
const Command = require('ronin').Command | ||
const utils = require('../../utils') | ||
const bs58 = require('bs58') | ||
const debug = require('debug') | ||
const log = debug('cli:object') | ||
log.error = debug('cli:object:error') | ||
const log = debug('cli:swarm') | ||
log.error = debug('cli:swarm:error') | ||
|
||
module.exports = Command.extend({ | ||
desc: '', | ||
desc: 'Open connection to a given address', | ||
|
||
options: {}, | ||
|
||
run: () => { | ||
run: (address) => { | ||
if (!address) { | ||
throw new Error("Argument 'address' is required") | ||
} | ||
|
||
utils.getIPFS((err, ipfs) => { | ||
if (err) { | ||
throw err | ||
} | ||
// TODO | ||
|
||
if (!utils.isDaemonOn()) { | ||
throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.') | ||
} | ||
|
||
ipfs.swarm.connect(address, (err, res) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
console.log(res.Strings[0]) | ||
}) | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
'use strict' | ||
|
||
const ipfs = require('./../index.js').ipfs | ||
const debug = require('debug') | ||
const log = debug('http-api:block') | ||
log.error = debug('http-api:block:error') | ||
|
||
exports = module.exports | ||
|
||
// common pre request handler that parses the args and returns `addr` which is assigned to `request.pre.args` | ||
exports.parseAddrs = (request, reply) => { | ||
if (!request.query.arg) { | ||
return reply("Argument 'addr' is required").code(400).takeover() | ||
} | ||
|
||
return reply({ | ||
addr: request.query.arg | ||
}) | ||
} | ||
|
||
exports.peers = { | ||
// main route handler which is called after the above `parseArgs`, but only if the args were valid | ||
handler: (request, reply) => { | ||
ipfs.libp2p.swarm.peers((err, peers) => { | ||
if (err) { | ||
log.error(err) | ||
return reply({ | ||
Message: err.toString(), | ||
Code: 0 | ||
}).code(500) | ||
} | ||
|
||
return reply({ | ||
Strings: Object.keys(peers) | ||
.map((key) => | ||
`${peers[key].multiaddrs[0].toString()}/ipfs/${peers[key].id.toB58String()}`) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
exports.localAddrs = { | ||
// main route handler which is called after the above `parseArgs`, but only if the args were valid | ||
handler: (request, reply) => { | ||
ipfs.libp2p.swarm.localAddrs((err, addrs) => { | ||
if (err) { | ||
log.error(err) | ||
return reply({ | ||
Message: err.toString(), | ||
Code: 0 | ||
}).code(500) | ||
} | ||
|
||
return reply({ | ||
Strings: addrs.map((addr) => addr.toString()) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
exports.connect = { | ||
// uses common parseAddr method that returns a `addr` | ||
parseArgs: exports.parseAddrs, | ||
|
||
// main route handler which is called after the above `parseArgs`, but only if the args were valid | ||
handler: (request, reply) => { | ||
const addr = request.pre.args.addr | ||
|
||
ipfs.libp2p.swarm.connect(addr, (err, res) => { | ||
if (err) { | ||
log.error(err) | ||
return reply({ | ||
Message: err.toString(), | ||
Code: 0 | ||
}).code(500) | ||
} | ||
|
||
return reply({ | ||
Strings: [`connect ${res.toB58String()} success`] | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.