diff options
-rw-r--r-- | gossip.js | 83 | ||||
-rw-r--r-- | server.js | 65 |
2 files changed, 1 insertions, 147 deletions
@@ -1,86 +1,3 @@ -function blobSync (blob, author, keys, needs) { - console.log(needs) - - var wsServers - - // duplicate code, we should abstract this - localforage.getItem('securepubs').then(function (servers) { - if (servers) { - wsServers = servers - } else { - servers = ['ws://bogbook.com', 'ws://localhost:8080'] - var pubs = [] - servers.forEach(server => { - var ws = new WebSocket(server) - ws.onopen = function () { - ws.send(JSON.stringify({requester: keys.publicKey, sendpub: true})) - } - ws.onmessage = function (message) { - pubs.push(server + '/~' + message.data) - localforage.setItem('securepubs', pubs) - } - }) - wsServers = pubs - } - }).then(function () { - wsServers.forEach(function (server, index) { - setTimeout(function () { - var split = server.split('~') - var serverurl = split[0] - var serverpub = split[1] - var ws = new WebSocket(serverurl) - - console.log('requesting ' + blob + ' from ' + server) - ws.onopen = function () { - var req = { - blob: blob, - needs: needs - } - - box(JSON.stringify(req), serverpub, keys).then(boxed => { - var obj = { - requester: keys.publicKey, - box: boxed - } - ws.send(JSON.stringify(obj)) - }) - } - ws.onmessage = function (message) { - var serverreq = JSON.parse(message.data) - unbox(serverreq.box, serverreq.requester, keys).then(unboxed => { - - var unboxedreq = JSON.parse(nacl.util.encodeUTF8(unboxed)) - //console.log(unboxedreq) - if (unboxedreq.blobFile) { - var openedimg = nacl.sign.open(nacl.util.decodeBase64(unboxedreq.blobFile), nacl.util.decodeBase64(author.substring(1))) - if (openedimg) { - localforage.setItem(unboxedreq.blob, unboxedreq.blobFile) - } - } else { - localforage.getItem(unboxedreq.blob).then(blob => { - if (blob) { - var nextreq = { - author: author, - blob: unboxedreq.blob, - blobFile: blob - } - box(JSON.stringify(nextreq), serverreq.requester, keys).then(boxed => { - var obj = { - requester: keys.publicKey, - box: boxed - } - ws.send(JSON.stringify(obj)) - }) - } - }) - } - }) - } - }, index * 10000) - }) - }) -} - function sync (subs, keys) { var wsServers @@ -36,72 +36,9 @@ bog.keys().then(key => { var req = JSON.parse(message) if (req.sendpub) { ws.send(key.publicKey) - } - else { + } else { bog.unbox(req.box, req.requester, key).then(unboxed => { var unboxedreq = JSON.parse(nacl.util.encodeUTF8(unboxed)) - //console.log(unboxedreq) - /*if (unboxedreq.blobFile) { - var openedimg = nacl.sign.open(nacl.util.decodeBase64(unboxedreq.blobFile), nacl.util.decodeBase64(unboxedreq.author.substring(1))) - if (openedimg) { - //console.log(openedimg) - fs.writeFileSync(blobdir + '/' + sanitize(unboxedreq.blob), unboxedreq.blobFile, 'UTF-8') - console.log('received blob ' + unboxedreq.blob + ' from ' + req.requester + ' and saved to blobs folder') - } - } - if (unboxedreq.blob) { - console.log(req.requester + ' has requested the blob ' + unboxedreq.blob) - var blobExists = fs.existsSync(blobdir + '/' + sanitize(unboxedreq.blob)) - if (unboxedreq.needs) { - console.log(req.requester + ' needs ' + unboxedreq.blob + ' do we have it?') - if (blobExists) { - console.log('We have it, so send it to the client') - var blobToSend = fs.readFileSync(blobdir + '/' + sanitize(unboxedreq.blob), 'UTF-8') - var sendblob = { - blob: unboxedreq.blob, - blobFile: blobToSend - } - console.log(sendblob) - bog.box(JSON.stringify(sendblob), req.requester, key).then(boxed => { - var obj = { - requester: key.publicKey, - box: boxed - } - ws.send(JSON.stringify(obj)) - }) - } - } else { - console.log(req.requester + ' has ' + unboxedreq.blob + ' do we need it?') - if (!blobExists) { - console.log('We need it, so request it from the client') - var blobreq = { blob: unboxedreq.blob, needs: true } - bog.box(JSON.stringify(blobreq), req.requester, key).then(boxed => { - var obj = { - requester: key.publicKey, - box: boxed - } - ws.send(JSON.stringify(obj)) - }) - } - } - }*/ - if (unboxedreq.seq === 0) { - console.log(req.requester + ' asked the full log of ' + unboxedreq.author) - fs.readFile(bogdir + 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 |