diff options
author | Ev Bogue <ev@evbogue.com> | 2019-09-05 14:19:46 -0500 |
---|---|---|
committer | Ev Bogue <ev@evbogue.com> | 2019-09-05 14:19:46 -0500 |
commit | 07c88bb0a329cff80d9d6acfaa292d2cc40a79cc (patch) | |
tree | 17a5d8ed49486f27627348777b1772a0a809bed8 | |
parent | e73ba7b9c63e5a48cee9c319d0f693deb211da1a (diff) |
half-way towards having secure bog transfer completed, just need diffs
-rw-r--r-- | gossip.js | 66 | ||||
-rw-r--r-- | server.js | 78 |
2 files changed, 140 insertions, 4 deletions
@@ -37,13 +37,75 @@ function sync (subs, keys) { }) } ws.onmessage = function (message) { + var req = JSON.parse(message.data) + console.log(req) + unbox(req.box, req.requester, keys).then(unboxed => { + var nextreq = JSON.parse(nacl.util.encodeUTF8(unboxed)) + console.log(nextreq) + if (nextreq.seq === 0) { + var feed = JSON.stringify(srclog) + box(feed, serverpub, keys).then(boxed => { + var obj = { + requester: keys.publicKey, + box: boxed + } + console.log('Sending entire log of ' + msg.author + ' to ' + serverpub) + console.log(obj) + ws.send(JSON.stringify(obj)) + }) + } + }) } }) - } else if (srclog === null) { + } else { + console.log('NO LOG IN CLIENT') ws.onopen = function () { - console.log('NO LOG IN CLIENT') + var reqwhole = JSON.stringify({author: sub, seq: 0}) + console.log(reqwhole) + box(reqwhole, serverpub, keys).then(boxed => { + var obj = { + requester: keys.publicKey, + box: boxed + } + console.log(boxed) + ws.send(JSON.stringify(obj)) + }) } ws.onmessage = function (message) { + var req = JSON.parse(message.data) + console.log('received message from ' + req.requester) + unbox(req.box, req.requester, keys).then(unboxed => { + var unboxedreq = JSON.parse(nacl.util.encodeUTF8(unboxed)) + console.log(unboxedreq) + if (Array.isArray(unboxedreq)) { + open(unboxedreq[0]).then(msg => { + console.log(msg.seq) + localforage.getItem(msg.author).then(feed => { + if (!feed) { + localforage.setItem(msg.author, unboxedreq).then(success => { + console.log('saved log of ' + msg.author + ' to localforage') + }) + localforage.getItem('log').then(log => { + for (var i = unboxedreq.length -1; i >= 0; --i) { + console.log(i) + open(unboxedreq[i]).then(opened => { + log.unshift(opened) + var scroller = document.getElementById('scroller') + scroller.insertBefore(render(opened, keys), scroller.childNodes[2]) + console.log(opened) + if (opened.seq === unboxedreq.length) { + localforage.setItem('log', log).then(success => { + console.log('saved log with ' + opened.author + ' prepended to localForage') + }) + } + }) + } + }) + } + }) + }) + } + }) } } }) @@ -24,9 +24,83 @@ bog.keys().then(key => { wserve.on('connection', function (ws) { ws.on('message', function (message) { var req = JSON.parse(message) - console.log(message) bog.unbox(req.box, req.requester, key).then(unboxed => { - console.log(nacl.util.encodeUTF8(unboxed)) + var unboxedreq = JSON.parse(nacl.util.encodeUTF8(unboxed)) + console.log(unboxedreq) + if (unboxedreq.seq === 0) { + console.log(req.requester + ' asked the full log of ' + unboxedreq.author) + fs.readFile(__dirname + '/bogs/' + unboxedreq.author, 'UTF-8', function (err, data) { + if (data) { + //var feed = JSON.stringify(data) + var feed = data + bog.box(feed, req.requester, key).then(boxed => { + var obj = { + requester: key.publicKey, + box: boxed + } + ws.send(JSON.stringify(obj)) + console.log('Sent full log of ' + unboxedreq.author + ' to ' + req.requester) + }) + } + }) + } + if (unboxedreq.seq) { + console.log(req.requester + ' asked for feed ' + unboxedreq.author + ' after sequence ' + unboxedreq.seq) + // check to see if we have the feed on disk + fs.readFile(__dirname + '/bogs/' + unboxedreq.author, 'UTF-8', function (err, data) { + if (data) { + // TODO open the latest message, and check the sequence number + var feed = JSON.parse(data) + bog.open(feed[0]).then(msg => { + if (unboxedreq.seq === msg.seq) { + console.log(unboxedreq.author + '\'s feed is identical, sending nothing to client') + } + + if (unboxedreq.seq > msg.seq) { + // right now the client is still sending the entire log, which works just fine but isn't optimal + console.log('client feed is longer, requesting diff from client') + var reqdiff = JSON.stringify({author: unboxedreq.author, seq: 0}) + bog.box(reqdiff, req.requester, key).then(boxed => { + var obj = { + requester: key.publicKey, + box: boxed + } + ws.send(JSON.stringify(obj)) + }) + } + + if (unboxedreq.seq < msg.seq) { + console.log('client feed is shorter, sending diff to client') + var diff = feed.slice(0, unboxedreq.seq) + console.log(diff) + } + }) + } else { + // if we don't have the feed, request the feed from the client and save + console.log('We don\'t have the log on the server, requesting log from ' + req.requester ) + var reqwhole = JSON.stringify({author: unboxedreq.author, seq: 0}) + + bog.box(reqwhole, req.requester, key).then(boxed => { + var obj = { + requester: key.publicKey, + box: boxed + } + ws.send(JSON.stringify(obj)) + }) + } + }) + } else if (Array.isArray(unboxedreq)) { + // first check to make sure that we have an entire log + bog.open(unboxedreq[0]).then(msg => { + if (msg.seq === unboxedreq.length) { + fs.writeFile(__dirname + '/bogs/' + msg.author, JSON.stringify(unboxedreq), 'UTF-8', function (err, success) { + // TODO this should check to make sure the sequence number is equal to the length of the array, otherwise we might have bunk feed + console.log('Saved full log of ' + msg.author + ' sent by ' + req.requester) + }) + } + }) + // TODO if we have a partial log then load the log we have, concat, and then save the log + } }) }) }) |