diff --git a/package.json b/package.json index 47d861fa06..14f256d181 100644 --- a/package.json +++ b/package.json @@ -156,6 +156,7 @@ "progress": "^2.0.0", "promisify-es6": "^1.0.3", "pull-abortable": "^4.1.1", + "pull-catch": "^1.0.0", "pull-defer": "^0.2.2", "pull-file": "^1.1.0", "pull-ndjson": "^0.1.1", diff --git a/src/cli/commands/ping.js b/src/cli/commands/ping.js new file mode 100644 index 0000000000..ab9d73666e --- /dev/null +++ b/src/cli/commands/ping.js @@ -0,0 +1,41 @@ +'use strict' + +const pull = require('pull-stream/pull') +const drain = require('pull-stream/sinks/drain') +const pullCatch = require('pull-catch') + +const print = require('../utils').print + +module.exports = { + command: 'ping ', + + description: 'Measure the latency of a connection', + + builder: { + count: { + alias: 'n', + type: 'integer', + default: 10 + } + }, + + handler (argv) { + const peerId = argv.peerId + const count = argv.count || 10 + pull( + argv.ipfs.pingPullStream(peerId, { count }), + pullCatch(err => { + throw err + }), + drain(({ Time, Text }) => { + // Check if it's a pong + if (Time) { + print(`Pong received: time=${Time} ms`) + // Status response + } else { + print(Text) + } + }) + ) + } +} diff --git a/src/core/components/ping.js b/src/core/components/ping.js index eb7a45b34f..6804acf83d 100644 --- a/src/core/components/ping.js +++ b/src/core/components/ping.js @@ -1,9 +1,96 @@ 'use strict' const promisify = require('promisify-es6') +const debug = require('debug') +const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR +const PeerId = require('peer-id') +const pull = require('pull-stream/pull') +const Pushable = require('pull-pushable') +const ndjson = require('pull-ndjson') +const waterfall = require('async/waterfall') + +const log = debug('jsipfs:ping') +log.error = debug('jsipfs:ping:error') module.exports = function ping (self) { - return promisify((callback) => { - callback(new Error('Not implemented')) + return promisify((peerId, count, cb) => { + if (!self.isOnline()) { + return cb(new Error(OFFLINE_ERROR)) + } + + const source = Pushable() + + const response = pull( + source, + ndjson.serialize() + ) + waterfall([ + getPeer.bind(null, self._libp2pNode, source, peerId), + runPing.bind(null, self._libp2pNode, source, count) + ], (err) => { + if (err) { + log.error(err) + source.push(getPacket({Text: err.toString()})) + source.end(err) + } + }) + + cb(null, response) + }) +} + +function getPacket (msg) { + // Default msg + const basePacket = {Success: false, Time: 0, Text: ''} + return Object.assign({}, basePacket, msg) +} + +function getPeer (libp2pNode, statusStream, peerId, cb) { + let peer + try { + peer = libp2pNode.peerBook.get(peerId) + return cb(null, peer) + } catch (err) { + log('Peer not found in peer book, trying peer routing') + // Share lookup status just as in the go implemmentation + statusStream.push(getPacket({Success: true, Text: `Looking up peer ${peerId}`})) + // Try to use peerRouting + libp2pNode.peerRouting.findPeer(PeerId.createFromB58String(peerId), cb) + } +} + +function runPing (libp2pNode, statusStream, count, peer, cb) { + libp2pNode.ping(peer, (err, p) => { + log('Got peer', peer) + if (err) { + return cb(err) + } + + let packetCount = 0 + let totalTime = 0 + statusStream.push(getPacket({Success: true, Text: `PING ${peer.id.toB58String()}`})) + + p.on('ping', (time) => { + statusStream.push(getPacket({ Success: true, Time: time })) + totalTime += time + packetCount++ + if (packetCount >= count) { + const average = totalTime / count + p.stop() + statusStream.push(getPacket({ Success: true, Text: `Average latency: ${average}ms` })) + statusStream.end() + } + }) + + p.on('error', (err) => { + log.error(err) + p.stop() + statusStream.push(getPacket({Text: err.toString()})) + statusStream.end(err) + }) + + p.start() + + return cb() }) } diff --git a/src/http/api/resources/index.js b/src/http/api/resources/index.js index 08d8d7f2a1..37f38f246b 100644 --- a/src/http/api/resources/index.js +++ b/src/http/api/resources/index.js @@ -3,6 +3,7 @@ exports.version = require('./version') exports.shutdown = require('./shutdown') exports.id = require('./id') +exports.ping = require('./ping') exports.bootstrap = require('./bootstrap') exports.repo = require('./repo') exports.object = require('./object') diff --git a/src/http/api/resources/ping.js b/src/http/api/resources/ping.js new file mode 100644 index 0000000000..3bc0b74b48 --- /dev/null +++ b/src/http/api/resources/ping.js @@ -0,0 +1,38 @@ +'use strict' + +const Joi = require('joi') +const boom = require('boom') +const toStream = require('pull-stream-to-stream') +const PassThrough = require('readable-stream').PassThrough +const pump = require('pump') + +exports = module.exports + +exports.get = { + validate: { + query: Joi.object().keys({ + n: Joi.number().greater(0), + count: Joi.number().greater(0), + arg: Joi.string().required() + }).xor('n', 'count').unknown() + }, + handler: (request, reply) => { + const ipfs = request.server.app.ipfs + const peerId = request.query.arg + // Default count to 10 + const count = request.query.n || request.query.count || 10 + ipfs.ping(peerId, count, (err, pullStream) => { + if (err) { + return reply(boom.badRequest(err)) + } + // Streams from pull-stream-to-stream don't seem to be compatible + // with the stream2 readable interface + // see: https://github.com/hapijs/hapi/blob/c23070a3de1b328876d5e64e679a147fafb04b38/lib/response.js#L533 + // and: https://github.com/pull-stream/pull-stream-to-stream/blob/e436acee18b71af8e71d1b5d32eee642351517c7/index.js#L28 + const responseStream = toStream.source(pullStream) + const stream2 = new PassThrough() + pump(responseStream, stream2) + return reply(stream2).type('application/json').header('X-Chunked-Output', '1') + }) + } +} diff --git a/src/http/api/routes/index.js b/src/http/api/routes/index.js index 2eeb4b137a..d7c30851f7 100644 --- a/src/http/api/routes/index.js +++ b/src/http/api/routes/index.js @@ -9,6 +9,7 @@ module.exports = (server) => { require('./object')(server) require('./repo')(server) require('./config')(server) + require('./ping')(server) require('./swarm')(server) require('./bitswap')(server) require('./file')(server) diff --git a/src/http/api/routes/ping.js b/src/http/api/routes/ping.js new file mode 100644 index 0000000000..92b081862f --- /dev/null +++ b/src/http/api/routes/ping.js @@ -0,0 +1,16 @@ +'use strict' + +const resources = require('./../resources') + +module.exports = (server) => { + const api = server.select('API') + + api.route({ + method: '*', + path: '/api/v0/ping', + config: { + handler: resources.ping.get.handler, + validate: resources.ping.get.validate + } + }) +} diff --git a/test/cli/commands.js b/test/cli/commands.js index 06154cfac6..1e194eb143 100644 --- a/test/cli/commands.js +++ b/test/cli/commands.js @@ -4,7 +4,8 @@ const expect = require('chai').expect const runOnAndOff = require('../utils/on-and-off') -const commandCount = 73 +const commandCount = 74 + describe('commands', () => runOnAndOff((thing) => { let ipfs diff --git a/test/cli/ping.js b/test/cli/ping.js new file mode 100644 index 0000000000..a571b2e380 --- /dev/null +++ b/test/cli/ping.js @@ -0,0 +1,149 @@ +/* eslint max-nested-callbacks: ["error", 8] */ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const series = require('async/series') +const DaemonFactory = require('ipfsd-ctl') +const ipfsExec = require('../utils/ipfs-exec') + +const df = DaemonFactory.create({ type: 'js' }) +const expect = chai.expect +chai.use(dirtyChai) + +const config = { + Bootstrap: [], + Discovery: { + MDNS: { + Enabled: + false + } + } +} + +describe('ping', function () { + this.timeout(60 * 1000) + let ipfsdA + let ipfsdB + let bMultiaddr + let ipfsdBId + let cli + + before((done) => { + this.timeout(60 * 1000) + series([ + (cb) => { + df.spawn({ + exec: `./src/cli/bin.js`, + config, + initOptions: { bits: 512 } + }, (err, _ipfsd) => { + expect(err).to.not.exist() + ipfsdB = _ipfsd + cb() + }) + }, + (cb) => { + ipfsdB.api.id((err, peerInfo) => { + expect(err).to.not.exist() + ipfsdBId = peerInfo.id + bMultiaddr = peerInfo.addresses[0] + cb() + }) + } + ], done) + }) + + before(function (done) { + this.timeout(60 * 1000) + + df.spawn({ + exec: './src/cli/bin.js', + config, + initoptions: { bits: 512 } + }, (err, _ipfsd) => { + expect(err).to.not.exist() + ipfsdA = _ipfsd + // Without DHT we need to have an already established connection + ipfsdA.api.swarm.connect(bMultiaddr, done) + }) + }) + + before((done) => { + this.timeout(60 * 1000) + cli = ipfsExec(ipfsdA.repoPath) + done() + }) + + after((done) => ipfsdA.stop(done)) + after((done) => ipfsdB.stop(done)) + + it('ping host', (done) => { + this.timeout(60 * 1000) + const ping = cli(`ping ${ipfsdBId}`) + const result = [] + ping.stdout.on('data', (output) => { + const packets = output.toString().split('\n').slice(0, -1) + result.push(...packets) + }) + + ping.stdout.on('end', () => { + expect(result).to.have.lengthOf(12) + expect(result[0]).to.equal(`PING ${ipfsdBId}`) + for (let i = 1; i < 11; i++) { + expect(result[i]).to.match(/^Pong received: time=\d+ ms$/) + } + expect(result[11]).to.match(/^Average latency: \d+(.\d+)?ms$/) + done() + }) + + ping.catch((err) => { + expect(err).to.not.exist() + }) + }) + + it('ping host with --n option', (done) => { + this.timeout(60 * 1000) + const ping = cli(`ping --n 1 ${ipfsdBId}`) + const result = [] + ping.stdout.on('data', (output) => { + const packets = output.toString().split('\n').slice(0, -1) + result.push(...packets) + }) + + ping.stdout.on('end', () => { + expect(result).to.have.lengthOf(3) + expect(result[0]).to.equal(`PING ${ipfsdBId}`) + expect(result[1]).to.match(/^Pong received: time=\d+ ms$/) + expect(result[2]).to.match(/^Average latency: \d+(.\d+)?ms$/) + done() + }) + + ping.catch((err) => { + expect(err).to.not.exist() + }) + }) + + it('ping host with --count option', (done) => { + this.timeout(60 * 1000) + const ping = cli(`ping --count 1 ${ipfsdBId}`) + const result = [] + ping.stdout.on('data', (output) => { + const packets = output.toString().split('\n').slice(0, -1) + result.push(...packets) + }) + + ping.stdout.on('end', () => { + expect(result).to.have.lengthOf(3) + expect(result[0]).to.equal(`PING ${ipfsdBId}`) + expect(result[1]).to.match(/^Pong received: time=\d+ ms$/) + expect(result[2]).to.match(/^Average latency: \d+(.\d+)?ms$/) + done() + }) + + ping.catch((err) => { + expect(err).to.not.exist() + }) + }) +}) diff --git a/test/core/ping.spec.js b/test/core/ping.spec.js new file mode 100644 index 0000000000..b94299ba80 --- /dev/null +++ b/test/core/ping.spec.js @@ -0,0 +1,220 @@ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const pull = require('pull-stream/pull') +const drain = require('pull-stream/sinks/drain') +const pullCatch = require('pull-catch') +const parallel = require('async/parallel') +const DaemonFactory = require('ipfsd-ctl') +const isNode = require('detect-node') + +const expect = chai.expect +chai.use(dirtyChai) +const df = DaemonFactory.create({ exec: 'src/cli/bin.js' }) + +const config = { + Bootstrap: [], + Discovery: { + MDNS: { + Enabled: + false + } + } +} + +function spawnNode ({ dht = false }, cb) { + const args = dht ? ['--enable-dht-experiment'] : [] + df.spawn({ + args, + config, + initOptions: { bits: 512 } + }, cb) +} + +describe('ping', function () { + this.timeout(60 * 1000) + + if (!isNode) return + + describe('DHT disabled', function () { + // Without DHT nodes need to be previously connected + let ipfsdA + let ipfsdB + let bMultiaddr + let ipfsdBId + + // Spawn nodes + before(function (done) { + this.timeout(60 * 1000) + + parallel([ + spawnNode.bind(null, { dht: false }), + spawnNode.bind(null, { dht: false }) + ], (err, ipfsd) => { + expect(err).to.not.exist() + ipfsdA = ipfsd[0] + ipfsdB = ipfsd[1] + done() + }) + }) + + // Get the peer info object + before(function (done) { + this.timeout(60 * 1000) + + ipfsdB.api.id((err, peerInfo) => { + expect(err).to.not.exist() + ipfsdBId = peerInfo.id + bMultiaddr = peerInfo.addresses[0] + done() + }) + }) + + // Connect the nodes + before(function (done) { + this.timeout(60 * 1000) + ipfsdA.api.swarm.connect(bMultiaddr, done) + }) + + after((done) => ipfsdA.stop(done)) + after((done) => ipfsdB.stop(done)) + + it('sends the specified number of packets', (done) => { + let packetNum = 0 + const count = 3 + pull( + ipfsdA.api.pingPullStream(ipfsdBId, { count }), + pullCatch(err => { + expect(err).to.not.exist() + }), + drain(({ Success, Time, Text }) => { + expect(Success).to.be.true() + // It's a pong + if (Time) { + packetNum++ + } + }, () => { + expect(packetNum).to.equal(count) + done() + }) + ) + }) + + it('pinging an unknown peer will fail accordingly', (done) => { + let messageNum = 0 + const count = 2 + pull( + ipfsdA.api.pingPullStream('unknown', { count }), + pullCatch(err => { + expect(err).to.not.exist() + }), + drain(({ Success, Time, Text }) => { + messageNum++ + // Assert that the ping command falls back to the peerRouting + if (messageNum === 1) { + expect(Text).to.include('Looking up') + } + + // Fails accordingly while trying to use peerRouting + if (messageNum === 2) { + expect(Success).to.be.false() + } + }, () => { + expect(messageNum).to.equal(count) + done() + }) + ) + }) + }) + + describe('DHT enabled', function () { + // Our bootstrap process will run 3 IPFS daemons where + // A ----> B ----> C + // Allowing us to test the ping command using the DHT peer routing + let ipfsdA + let ipfsdB + let ipfsdC + let bMultiaddr + let cMultiaddr + let ipfsdCId + + // Spawn nodes + before(function (done) { + this.timeout(60 * 1000) + + parallel([ + spawnNode.bind(null, { dht: true }), + spawnNode.bind(null, { dht: true }), + spawnNode.bind(null, { dht: true }) + ], (err, ipfsd) => { + expect(err).to.not.exist() + ipfsdA = ipfsd[0] + ipfsdB = ipfsd[1] + ipfsdC = ipfsd[2] + done() + }) + }) + + // Get the peer info objects + before(function (done) { + this.timeout(60 * 1000) + + parallel([ + ipfsdB.api.id.bind(ipfsdB.api), + ipfsdC.api.id.bind(ipfsdC.api) + ], (err, peerInfo) => { + expect(err).to.not.exist() + bMultiaddr = peerInfo[0].addresses[0] + ipfsdCId = peerInfo[1].id + cMultiaddr = peerInfo[1].addresses[0] + done() + }) + }) + + // Connect the nodes + before(function (done) { + this.timeout(60 * 1000) + + parallel([ + ipfsdA.api.swarm.connect.bind(ipfsdA.api, bMultiaddr), + ipfsdB.api.swarm.connect.bind(ipfsdB.api, cMultiaddr) + ], done) + }) + + // FIXME timeout needed for connections to succeed + before((done) => setTimeout(done, 100)) + + after((done) => ipfsdA.stop(done)) + after((done) => ipfsdB.stop(done)) + after((done) => ipfsdC.stop(done)) + + it('if enabled uses the DHT peer routing to find peer', (done) => { + let messageNum = 0 + let packetNum = 0 + const count = 3 + pull( + ipfsdA.api.pingPullStream(ipfsdCId, { count }), + pullCatch(err => { + expect(err).to.not.exist() + }), + drain(({ Success, Time, Text }) => { + messageNum++ + expect(Success).to.be.true() + // Assert that the ping command falls back to the peerRouting + if (messageNum === 1) { + expect(Text).to.include('Looking up') + } + // It's a pong + if (Time) { + packetNum++ + } + }, () => { + expect(packetNum).to.equal(count) + done() + }) + ) + }) + }) +}) diff --git a/test/http-api/inject/ping.js b/test/http-api/inject/ping.js new file mode 100644 index 0000000000..8cda3836c3 --- /dev/null +++ b/test/http-api/inject/ping.js @@ -0,0 +1,52 @@ +/* eslint max-nested-callbacks: ["error", 8] */ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +const dirtyChai = require('dirty-chai') + +const expect = chai.expect +chai.use(dirtyChai) + +module.exports = (http) => { + describe('/ping', function () { + let api + + before(() => { + api = http.api.server.select('API') + }) + + it('returns 400 if both n and count are provided', (done) => { + api.inject({ + method: 'GET', + url: `/api/v0/ping?arg=someRandomId&n=1&count=1` + }, (res) => { + expect(res.statusCode).to.equal(400) + done() + }) + }) + + it('returns 400 if arg is not provided', (done) => { + api.inject({ + method: 'GET', + url: `/api/v0/ping?count=1` + }, (res) => { + expect(res.statusCode).to.equal(400) + done() + }) + }) + + it('returns 200 and the response stream with the ping result', (done) => { + api.inject({ + method: 'GET', + url: `/api/v0/ping?arg=someRandomId` + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.headers['x-chunked-output']).to.equal('1') + expect(res.headers['transfer-encoding']).to.equal('chunked') + expect(res.headers['content-type']).to.include('application/json') + done() + }) + }) + }) +}