From 20f068da2784916728ffc508427e5bc7660a046d Mon Sep 17 00:00:00 2001 From: Dan Flettre Date: Thu, 28 May 2015 14:34:27 -0500 Subject: [PATCH 1/3] Switch to friends-swarm --- lib/bot.js | 2 +- lib/swarm.js | 165 --------------------------------------------------- package.json | 1 + schema.proto | 11 ---- 4 files changed, 2 insertions(+), 177 deletions(-) delete mode 100644 lib/swarm.js delete mode 100644 schema.proto diff --git a/lib/bot.js b/lib/bot.js index ee4c108..ab4b36a 100644 --- a/lib/bot.js +++ b/lib/bot.js @@ -2,7 +2,7 @@ var _ = require('lodash') var irc = require('irc') var logger = require('winston') var subleveldown = require('subleveldown') -var createSwarm = require('./swarm') +var createSwarm = require('friends-swarm') var errors = require('./errors') var validateChannelMapping = require('./validators').validateChannelMapping var emojis = require('./emoji') diff --git a/lib/swarm.js b/lib/swarm.js deleted file mode 100644 index 34bc6e2..0000000 --- a/lib/swarm.js +++ /dev/null @@ -1,165 +0,0 @@ -var events = require('events') -var subleveldown = require('subleveldown') -var swarm = require('webrtc-swarm') -var signalhub = require('signalhub') -var hyperlog = require('hyperlog') -var ghsign = require('ghsign') -var through = require('through2') -var protobuf = require('protocol-buffers') -var fs = require('fs') -var request = require('request') -var wrtc = require('wrtc') - -var messages = protobuf(fs.readFileSync(__dirname + '/../schema.proto')) - -module.exports = createSwarm - -ghsign = ghsign(function (username, cb) { - fs.readFile('./public-keys/' + username + '.keys', 'utf-8', function (_, keys) { - if (keys) return cb(null, keys) - request('https://github.com/' + username + '.keys', function (_, response) { - var keys = response.statusCode === 200 && response.body - if (!keys) return cb(new Error('Could not find public keys for ' + username)) - fs.mkdir('./public-keys', function () { - fs.writeFile('./public-keys/' + username + '.keys', keys, function () { - cb(null, keys) - }) - }) - }) - }) -}) - -var verifiers = {} - -function createSwarm (db) { - var emitter = new events.EventEmitter() - var logs = {} - - emitter.logs = logs - - var sign - var processor - - emitter.changes = function (name) { - return logs[name] ? logs[name].changes : 0 - } - - emitter.process = function (fn) { - processor = fn - Object.keys(logs).forEach(function (name) { - startProcessor(logs[name]) - }) - } - - emitter.removeChannel = function (name) { - var log = logs[name] - if (!log) return - delete logs[name] - - if (log.processing) log.processing.destroy() - log.peers.forEach(function (p) { - p.destroy() - }) - } - - emitter.addChannel = function (name) { - if (logs[name]) return - - var log = logs[name] = hyperlog(subleveldown(db, name)) - var id = 'friends-' + name - var hub = signalhub('http://dev.mathiasbuus.eu:8080', id) - var sw = swarm(hub, { wrtc: wrtc }) - - log.peers = [] - - sw.on('peer', function (p, id) { - var stream = log.replicate({live: true}) - - log.peers.push(p) - p.on('close', function () { - var i = log.peers.indexOf(p) - if (i > -1) log.peers.splice(i, 1) - }) - - emitter.emit('peer', p, name, id, stream) - - stream.on('push', function () { - emitter.emit('push', name) - }) - - stream.on('pull', function () { - emitter.emit('pull', name) - }) - - p.pipe(stream).pipe(p) - }) - - if (processor) startProcessor(log) - } - - emitter.send = function (message, cb) { - if (!sign && emitter.username) { - sign = ghsign.signer(emitter.username) - } - - var addMessage = function (m, sig) { - var ch = message.channel || 'friends' - emitter.addChannel(ch) - var log = logs[ch] - log.heads(function (err, heads) { - if (err) return cb(err) - log.add(heads, messages.SignedMessage.encode({signature: sig, message: m}), cb) - }) - } - - console.log(JSON.stringify(message, null, 4)) - var m = messages.Message.encode(message) - - if (sign) { - sign(m, function (err, sig) { - if (err) return cb(err) - addMessage(m, sig) - }) - return - } - - addMessage(m, null) - } - - function startProcessor (log) { - log.ready(function () { - if (log.processing) return - - var rs = log.processing = log.createReadStream({ - live: true, - since: Math.max(log.changes - 500, 0) - }) - - rs.pipe(through.obj(function (data, enc, cb) { - if (data.value.toString()[0] === '{') return cb() // old stuff - - var val = messages.SignedMessage.decode(data.value) - var m = messages.Message.decode(val.message) - var u = m.username - var verify = verifiers[u] - - if (u && !verifiers[u]) verifiers[u] = verify = ghsign.verifier(u) - - m.change = data.change - - if (verify && val.signature) { - verify(val.message, val.signature, function (_, valid) { - m.valid = !!valid - processor(m, cb) - }) - return - } - - m.valid = false - processor(m, cb) - })) - }) - } - - return emitter -} diff --git a/package.json b/package.json index 16873a1..31b9566 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "body-parser": "~1.11.0", "check-env": "~1.2.0", "commander": "~2.6.0", + "friends-swarm": "^1.0.0", "ghsign": "^1.2.2", "hyperlog": "^3.3.1", "irc": "~0.3.9", diff --git a/schema.proto b/schema.proto deleted file mode 100644 index 1db5c26..0000000 --- a/schema.proto +++ /dev/null @@ -1,11 +0,0 @@ -message SignedMessage { - optional bytes signature = 1; - required bytes message = 2; -} - -message Message { - optional string username = 1; - optional string channel = 2; - optional uint64 timestamp = 3; - optional string text = 4; -} \ No newline at end of file From 0ec3f1724dc1d013101210fd0773e6f935ac4beb Mon Sep 17 00:00:00 2001 From: ekmartin Date: Thu, 28 May 2015 23:11:36 +0200 Subject: [PATCH 2/3] Make Travis run tests on iojs-1.8.1 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f5e048b..c69c346 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: node_js node_js: - - "iojs" + - "iojs-v1.8.1" before_install: - sudo apt-get update -qq From c7e9db41c0384855ae6a558f26ec6a9d6361f41f Mon Sep 17 00:00:00 2001 From: Dan Flettre Date: Thu, 28 May 2015 18:10:52 -0500 Subject: [PATCH 3/3] use wrtc with friends-swarm --- lib/bot.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/bot.js b/lib/bot.js index ab4b36a..e68405f 100644 --- a/lib/bot.js +++ b/lib/bot.js @@ -3,6 +3,7 @@ var irc = require('irc') var logger = require('winston') var subleveldown = require('subleveldown') var createSwarm = require('friends-swarm') +var wrtc = require('wrtc') var errors = require('./errors') var validateChannelMapping = require('./validators').validateChannelMapping var emojis = require('./emoji') @@ -43,7 +44,7 @@ function Bot (db, options) { Bot.prototype.connect = function () { logger.debug('Connecting to IRC and Friends') - this.swarm = createSwarm(subleveldown(this.db, 'swarm')) + this.swarm = createSwarm(subleveldown(this.db, 'swarm'), {wrtc: wrtc}) Object.keys(this.channelMapping).forEach(this.swarm.addChannel) var ircOptions = _.assign({