diff options
author | Ev Bogue <ev@evbogue.com> | 2019-12-31 15:58:18 -0600 |
---|---|---|
committer | Ev Bogue <ev@evbogue.com> | 2019-12-31 15:58:18 -0600 |
commit | 9983e7b768f2fc04376e3dfad7335a8a7941dbaa (patch) | |
tree | 52f28e253e79a1ff622af6e27b468a75739504fd | |
parent | 7b410ab4c605928ce72d9c07f4784954999d9dda (diff) |
refactor gossip v 1.8.0
-rw-r--r-- | beacons.js | 24 | ||||
-rw-r--r-- | bog.js | 2 | ||||
-rw-r--r-- | gossip.js | 352 | ||||
-rw-r--r-- | package-lock.json | 2 | ||||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | render.js | 15 | ||||
-rw-r--r-- | server.js | 5 | ||||
-rw-r--r-- | settings.js | 6 | ||||
-rw-r--r-- | views.js | 8 |
9 files changed, 216 insertions, 200 deletions
@@ -84,5 +84,29 @@ function beaconsPage (keys) { ])) scroller.appendChild(ads) + + localforage.getItem('beacons').then(beacons => { + beacons.forEach(beacon => { + var message = h('div', {classList: 'message'}) + + if (beacon.signature) { + open(beacon).then(opened => { + quickName(beacon.author).then(gotName => { + message.appendChild(h('p', {innerHTML: marked(opened)})) + message.appendChild(h('span', [ + '—', + h('a', {href: '#' + beacon.author}, [gotName]), + ' from ', + h('a', {href: beacon.name}, [beacon.name]) + ]) + ) + }) + }) + } + + scroller.appendChild(message) + console.log(beacon) + }) + }) } @@ -76,7 +76,7 @@ async function unbox (boxed, sender, keys) { var nonceMsg = nacl.util.decodeBase64(boxed) var nonce = nonceMsg.slice(0, nacl.box.nonceLength) var msg = nonceMsg.slice(nacl.box.nonceLength, nonceMsg.length) - var message = nacl.box.open(msg, nonce, ed2curve.convertPublicKey(nacl.util.decodeBase64(sender.substring(1))), ed2curve.convertSecretKey(nacl.util.decodeBase64(keys.privateKey))) + var message = nacl.util.encodeUTF8(nacl.box.open(msg, nonce, ed2curve.convertPublicKey(nacl.util.decodeBase64(sender.substring(1))), ed2curve.convertSecretKey(nacl.util.decodeBase64(keys.privateKey)))) return message } @@ -1,198 +1,180 @@ -function sync (subs, keys) { +function processreq (req, pubkey, connection, keys) { + console.log(req) + if (req.box || req.signature) { + renderAd(req, keys) + } - var wsServers - - 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 () { - subs.forEach(function (sub, index) { - setTimeout(function () { - wsServers.forEach(function (server, index) { - setTimeout(function () { - //console.log('SYNCING WITH: ' + server + ' to fetch ' + sub) - var split = server.split('~') - var serverurl = split[0] - var serverpub = split[1] - var ws = new WebSocket(serverurl) + if (req.seq === 0 || req.seq) { + console.log('feed sync') + bog(req.author).then(feed => { + if (feed) { + open(feed[0]).then(msg => { + if (req.seq > msg.seq) { + var reqdiff = JSON.stringify({author: req.author, seq: msg.seq}) + box(reqdiff, pubkey, keys).then(boxed => { + connection.send(JSON.stringify({ + requester: keys.publicKey, + box: boxed + })) + }) + } - bog(sub).then(srclog => { - if (srclog) { - open(srclog[0]).then(msg => { - ws.onopen = function () { - var message = JSON.stringify(msg) - // if we have a log then send the latest log and see if we get anything back - box(message, serverpub, keys).then(boxed => { - var obj = { - requester: keys.publicKey, - box: boxed - } - ws.send(JSON.stringify(obj)) - }) - } - ws.onmessage = function (message) { - var req = JSON.parse(message.data) - unbox(req.box, req.requester, keys).then(unboxed => { - var unboxedreq = JSON.parse(nacl.util.encodeUTF8(unboxed)) - if (unboxedreq.content) { - unboxedreq.signature = unboxedreq.content - renderAd(unboxedreq, keys) - } - if (unboxedreq.box) { - renderAd(unboxedreq, keys) - } - if (unboxedreq.seq === 0) { - var stringedfeed = JSON.stringify(srclog) - box(stringedfeed, serverpub, keys).then(boxed => { - var obj = { - requester: keys.publicKey, - box: boxed - } - //console.log('Sending entire log of ' + msg.author + ' to ' + serverpub) - ws.send(JSON.stringify(obj)) - }) - } - - if (unboxedreq.seq > msg.seq) { - //console.log('server feed is longer, requesting diff from server') - var reqdiff = JSON.stringify({author: unboxedreq.author, seq: msg.seq}) - box(reqdiff, serverpub, keys).then(boxed => { - var obj = { - requester: keys.publicKey, - box: boxed - } - ws.send(JSON.stringify(obj)) - }) - } + if (req.seq < msg.seq) { + var endrange = feed.length - req.seq - 25 + if (endrange < 0) { + endrange = feed.length - req.seq - 1 + } + var baserange = feed.length - req.seq + var diff = JSON.stringify( + feed.slice( + endrange, + baserange) + ) + box(diff, pubkey, keys).then(boxed => { + connection.send(JSON.stringify({ + requester: keys.publicKey, + box: boxed + })) + }) + } + }) + } else { console.log('we dont have it')} + }) + } - if (unboxedreq.seq < msg.seq) { - //console.log('server feed is shorter, sending diff to server') - var diff = JSON.stringify(srclog.slice(0, msg.seq - unboxedreq.seq)) - box(diff, serverpub, keys).then(boxed => { - var obj = { - requester: keys.publicKey, - box: boxed - } - ws.send(JSON.stringify(obj)) - }) - } + if (Array.isArray(req)) { + open(req[0]).then(msg => { + localforage.getItem(msg.author).then(feed => { + if (!feed) { + localforage.setItem(msg.author, req) + localforage.getItem('log').then(log => { + if (!log) { + var log = [] + } + for (var i = req.length -1; i >= 0; --i) { + open(req[i]).then(opened => { + log.unshift(opened) + var src = window.location.hash.substring(1) + if ((src === msg.author) || (src === '')) { + var scroller = document.getElementById('scroller') + scroller.insertBefore(render(opened, keys), scroller.childNodes[1]) + } + if (opened.seq === req.length) { + log.sort((a, b) => a.timestamp - b.timestamp) + var reversed = log.reverse() + localforage.setItem('log', reversed) + } + }) + } + }) + } if (feed) { + open(feed[0]).then(lastmsg => { + if (req.length + lastmsg.seq === msg.seq) { + var newlog = req.concat(feed) + localforage.setItem(msg.author, newlog) + localforage.getItem('log').then(log => { + if (!log) { + var log = [] + } + for (var i = req.length -1; i >= 0; --i) { + open(req[i]).then(opened => { + log.unshift(opened) + var scroller = document.getElementById('scroller') - if (Array.isArray(unboxedreq)) { - //console.log('received diff from server') - open(unboxedreq[0]).then(msg => { - localforage.getItem(msg.author).then(feed => { - open(feed[0]).then(lastmsg => { - if (unboxedreq.length + lastmsg.seq === msg.seq) { - var newlog = unboxedreq.concat(feed) - localforage.setItem(msg.author, newlog).then(success => { - //console.log('combined existing feed of ' + msg.author + ' with diff and saved to client') - }) - localforage.getItem('log').then(log => { - if (!log) { - var log = [] - } - for (var i = unboxedreq.length -1; i >= 0; --i) { - open(unboxedreq[i]).then(opened => { - log.unshift(opened) - var scroller = document.getElementById('scroller') - - var src = window.location.hash.substring(1) - if ((src === sub) || (src === '')) { - var scroller = document.getElementById('scroller') - scroller.insertBefore(render(opened, keys), scroller.childNodes[1]) - } - if (unboxedreq.length + lastmsg.seq === opened.seq) { - log.sort((a, b) => a.timestamp - b.timestamp) - var reversed = log.reverse() - localforage.setItem('log', reversed).then(success => { - //console.log('saved log with ' + opened.author + ' prepended to localForage') - }) - } - }) - } - }) - } - }) - }) - }) - - } - - }) - } - }) - } else { - //console.log('NO LOG IN CLIENT') - ws.onopen = function () { - var reqwhole = JSON.stringify({author: sub, seq: 0}) - box(reqwhole, serverpub, keys).then(boxed => { - var obj = { - requester: keys.publicKey, - box: boxed + var src = window.location.hash.substring(1) + if ((src === msg.author) || (src === '')) { + var scroller = document.getElementById('scroller') + scroller.insertBefore(render(opened, keys), scroller.childNodes[1]) } - 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)) - if (Array.isArray(unboxedreq)) { - open(unboxedreq[0]).then(msg => { - 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 => { - if (!log) { - var log = [] - } - for (var i = unboxedreq.length -1; i >= 0; --i) { - open(unboxedreq[i]).then(opened => { - log.unshift(opened) - var src = window.location.hash.substring(1) - if ((src === sub) || (src === '')) { - var scroller = document.getElementById('scroller') - scroller.insertBefore(render(opened, keys), scroller.childNodes[1]) - } - if (opened.seq === unboxedreq.length) { - log.sort((a, b) => a.timestamp - b.timestamp) - var reversed = log.reverse() - localforage.setItem('log', reversed).then(success => { - //console.log('saved log with ' + opened.author + ' prepended to localForage') - }) - } - }) - } - }) - } - }) - }) + if (req.length + lastmsg.seq === opened.seq) { + log.sort((a, b) => a.timestamp - b.timestamp) + var reversed = log.reverse() + localforage.setItem('log', reversed) } }) } - } + }) + } + }) + } + }) + }) + } +} + +function getpubkey (connection, keys) { + console.log('asking for pubkey') + connection.onopen = () => { + connection.send(JSON.stringify({ + requester: keys.publicKey, sendpub: true + })) + } + + connection.onmessage = (m) => { + console.log(m) + localforage.setItem(m.origin, m.data) + } +} + +function getfeed (feed, pubkey, connection, keys) { + bog(feed).then(log => { + var logseq = 0 + connection.onopen = () => { + if (log) { + open(log[0]).then(msg => { + box(JSON.stringify(msg), pubkey, keys).then(boxed => { + connection.send(JSON.stringify({ + requester: keys.publicKey, + box: boxed + })) + }) + logseq = msg.seq + }) + } else { + var msg = { + author: feed, + seq: logseq + } + box(JSON.stringify(msg), pubkey, keys).then(boxed => { + connection.send(JSON.stringify({ + requester: keys.publicKey, + box: boxed + })) + }) + } + } + connection.onmessage = (m) => { + var req = JSON.parse(m.data) + unbox(req.box, req.requester, keys).then(unboxed => { + var unboxedreq = JSON.parse(unboxed) + processreq(unboxedreq, pubkey, connection, keys) + }) + } + }) +} + +function sync (feeds, keys) { + var pubs + localforage.getItem('pubs').then(pubs => { + if (!pubs) { + pubs = ['ws://' + location.hostname + ':8080'] + localforage.setItem('pubs', pubs) + } + pubs.forEach(function (pub, index) { + setTimeout(function () { + console.log(pub) + var connection = new WebSocket(pub) + localforage.getItem(pub).then(pubkey => { + if (!pubkey) { + getpubkey(connection, keys) + } + if (pubkey) { + feeds.forEach(feed => { + getfeed(feed, pubkey, connection, keys) }) - }, index * 50000) + } }) - }, index * 20000) + }, index * 5000) }) }) } diff --git a/package-lock.json b/package-lock.json index 655685a..ad97945 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "bogbook", - "version": "1.7.0", + "version": "1.8.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index ac7e569..a174152 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bogbook", - "version": "1.7.0", + "version": "1.8.0", "description": "secure blockchain logging (blogging, without the l) -- bogging", "main": "server.js", "scripts": { @@ -21,7 +21,7 @@ function renderAd (ad, keys) { if (ad.box) { unbox(ad.box, ad.author, keys).then(unboxed => { if (unboxed) { - var msg = JSON.parse(nacl.util.encodeUTF8(unboxed)) + var msg = JSON.parse(unboxed) if (pmspot) { pmspot.parentNode.removeChild(pmspot) } @@ -65,6 +65,12 @@ function renderAd (ad, keys) { ws.send(JSON.stringify(obj)) localforage.setItem(ad.hash, true).then(success => { //console.log('heard: ' + ad.hash) + localforage.getItem('beacons').then(beacons => { + if (!beacons) { beacons = [] } + beacons.unshift(ad) + localforage.setItem('beacons', beacons) + }) + }) }) } @@ -100,8 +106,6 @@ function renderAd (ad, keys) { var serverpub = split[1] var ws = new WebSocket(serverurl) - - var tobox = { author: keys.publicKey, timestamp: Date.now(), @@ -122,6 +126,11 @@ function renderAd (ad, keys) { } ws.send(JSON.stringify(obj)) localforage.setItem(ad.hash, true).then(success => {console.log('heard: ' + ad.hash)}) + localforage.getItem('beacons').then(beacons => { + if (!beacons) { beacons = [] } + beacons.unshift(ad) + localforage.setItem('beacons', beacons) + }) }) } }) @@ -114,11 +114,12 @@ bog.keys().then(key => { wserve.on('connection', function (ws) { ws.on('message', function (message) { var req = JSON.parse(message) + console.log(req) if (req.sendpub) { ws.send(key.publicKey) } else { bog.unbox(req.box, req.requester, key).then(unboxed => { - var unboxedreq = JSON.parse(nacl.util.encodeUTF8(unboxed)) + var unboxedreq = JSON.parse(unboxed) //console.log(unboxedreq) if (unboxedreq.type == 'beacon') { if (unboxedreq.box) { @@ -169,7 +170,7 @@ bog.keys().then(key => { hash: obj.hash, name: config.fullurl, pub: 'ws://' + config.url + ':' + config.wsport + '/~' + key.publicKey, - content: obj.signature, + signature: obj.signature, views: obj.views } } diff --git a/settings.js b/settings.js index c88ea07..f5b341c 100644 --- a/settings.js +++ b/settings.js @@ -52,18 +52,18 @@ function settingsPage (keys) { var pubs = h('div', {classList: 'message'}) - pubs.appendChild(h('p', {innerHTML: marked('These are your bogbook pubs. Bogbook will gossip with these pubs to publish your messages and check for new messages from your subscriptions. You should have at least one Bogbook pub in order to gossip your messages. If you don\'t see a bogbook pub below, try clicking "Reset Pubs" or add \n```\nws://bogbook.com/~@h4e3bHDJeDWiCAkzp83HINPR4y7BLR7tI3fOVqwLQqw=\n```\n to your pubs list.')})) + pubs.appendChild(h('p', {innerHTML: marked('These are your bogbook pubs. Bogbook will gossip with these pubs to publish your messages and check for new messages from your subscriptions. You should have at least one Bogbook pub in order to gossip your messages. If you don\'t see a bogbook pub below, try clicking "Reset Pubs" or add \n```\nws://bogbook.com\n```\n to your pubs list.')})) var add = h('input', {placeholder: 'Add a pub'}) - localforage.getItem('securepubs').then(function (servers) { + localforage.getItem('pubs').then(function (servers) { pubs.appendChild(h('div', [ add, h('button', { onclick: function () { if (add.value) { servers.push(add.value) - localforage.setItem('securepubs', servers).then(function () { location.reload() }) + localforage.setItem('pubs', servers).then(function () { location.reload() }) } } }, ['Add a pub']) @@ -15,10 +15,10 @@ function profilePage (src, keys) { var subs = [src] - var interval = 500 + var interval = 50 timer = function() { if (src === window.location.hash.substring(1)) { - if (interval < 10000) { interval = interval + 100 } + if (interval < 10000) { interval = interval + 50 } sync(subs, keys) setTimeout(timer, interval) } @@ -121,6 +121,7 @@ function searchPage (src, keys) { } function publicPage (keys) { + localforage.getItem('subscriptions').then(function (subs) { var interval = 1000 timer = function() { @@ -133,9 +134,8 @@ function publicPage (keys) { if (subs) { timer() } else { - var subs = [] + var subs = [keys.publickey] localforage.setItem('subscriptions', subs) - subs.push(keys.publicKey) timer() } }) |