tape('connect remote master', function (t) { var keys = ssbKeys.generate() var aliceDb = u.createDB('test-alice', { port: 45451, host: 'localhost', timeout: 2000, master: keys.id }) var rpc = client({port: 45451}) rpc.auth(ssbKeys.signObj(keys, { role: 'client', ts: Date.now(), public: keys.public }), function (err, res) { if(err) throw err t.equal(res.role, 'master') rpc.publish({ type: 'msg', value: 'written by bob', from: keys.id }, function (err) { if(err) throw err aliceDb.close(function () { t.end() }) }) }) })
Gossip.prototype.publish = function (msg) { var data = msg msg = { data: ssbkeys.signObj(this.keys, data), public: this.keys.public, seq: this.seq++, } this.store.push(JSON.stringify(msg) + '\n') }
function attachSession (stream, incoming, cb) { var rpc = peerApi(server.manifest, api) .permissions({allow: ['auth']}) var rpcStream = rpc.createStream() rpcStream = inactive(rpcStream, server.config.timeout) pull(stream, rpcStream, stream) rpc.incoming = incoming rpc.outgoing = !incoming rpc._sessid = ++sessCounter rpc._remoteAddress = stream.remoteAddress rpc.task = multicb() server.emit('rpc:connect', rpc) server.emit('rpc:' + (incoming ? 'incoming' : 'outgoing'), rpc) rpc.on('remote:authorized', function (authed) { server.emit('remote:authorized', rpc, authed) server.emit('log:info', ['remote', rpc._sessid, 'remote-authed', authed]) if(authed.type === 'client') rpcStream.setTTL(null) //don't abort the stream on timeout. }) rpc.auth(ssbKeys.signObj(keys, { ToS: 'be excellent to each other', public: keys.public, ts: Date.now(), }), function (err, res) { if(err || !res) { server.emit('rpc:unauthorized', err) rpc._emit('rpc:unauthorized', err) server.emit('log:warning', ['sbot', rpc._sessid, 'unauthed', err]) return } else { server.emit('rpc:authorized', rpc, res) rpc._emit('rpc:authorized', rpc, res) server.emit('log:info', ['sbot', rpc._sessid, 'authed', res]) } //TODO: put this stuff somewhere else...? //when the client connects (not a peer) we will be unable //to authorize with it. In this case, we shouldn't close //the connection... var n = 2 function done () { if(--n) return server.emit('log:info', ['sbot', rpc._sessid, 'done']) rpc.close() } rpc.once('done', function () { server.emit('log:info', ['sbot', rpc._sessid, 'remote-done']) done() }) rpc.task(function () { server.emit('log:info', ['sbot', rpc._sessid, 'local-done']) rpc.emit('done') done() }) if (cb) cb(err, res) }) return rpc }
tape('follow, isFollowing, followedUsers, unfollow', function (t) { var u = require('./util') var dbAlice = u.createDB('followtest-alice') var alice = dbAlice.feed var dbBob = u.createDB('followtest-bob') var bob = dbBob.feed var db = u.createDB('feed-test', {port: 1234, host: 'localhost'}) var feed = db.feed var server = db.use(require('../plugins/easy')) console.log(server.getManifest()) var client = sbot.createClient( db.config, server.getManifest() ) var signed = ssbkeys.signObj(feed.keys, { role: 'client', ts: Date.now(), public: feed.keys.public }) client.auth(signed, function(err) { if (err) throw err client.easy.follow(bob.id, function(err, msg) { if (err) throw err client.easy.isFollowing(bob.id, function(err, isFollowing) { if (err) throw err t.equal(isFollowing, true) client.easy.isFollowing(alice.id, function(err, isFollowing) { if (err) throw err t.equal(isFollowing, false) pull(client.easy.followedUsers(), pull.collect(function(err, users) { if (err) throw err t.equal(users.length, 1) t.equal(users[0].toString('base64'), bob.id.toString('base64')) // client.unfollow(bob.id, function(err) { // if (err) throw err // client.isFollowing(bob.id, function(err, isFollowing) { // if (err) throw err // t.equal(isFollowing, false) client.close(function() { t.end() server.close() }) // }) // }) })) }) }) }) }) })