diff options
Diffstat (limited to 'gossip.js')
-rw-r--r-- | gossip.js | 83 |
1 files changed, 83 insertions, 0 deletions
@@ -1,3 +1,86 @@ +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 |