aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEv Bogue <ev@evbogue.com>2019-09-10 13:54:46 -0500
committerEv Bogue <ev@evbogue.com>2019-09-10 13:54:46 -0500
commitf68aa0fc3403dc6a4f631fe5f8ff83954252290f (patch)
tree24641ebc330566f5a09c1c984d21898524e535f2
parent1819e6c0efc32c75abeefe1732ed6f5e9764529c (diff)
get publickeys from pub servers if they do not exist
-rw-r--r--gossip.js20
-rw-r--r--server.js184
-rw-r--r--views.js8
3 files changed, 113 insertions, 99 deletions
diff --git a/gossip.js b/gossip.js
index 73ea5ab..4f57c64 100644
--- a/gossip.js
+++ b/gossip.js
@@ -2,15 +2,23 @@ function sync (subs, keys) {
var wsServers
- localforage.getItem('pubs').then(function (servers) {
+ localforage.getItem('securepubs').then(function (servers) {
if (servers) {
- console.log(servers)
wsServers = servers
} else {
- servers = ['ws://localhost:8080/~@OXJ7Ma1eu8HOEakF+TW9yk1k09FbOqUSyMVneXWdLaM=']
- localforage.setItem('pubs', servers)
- console.log(servers)
- wsServers = servers
+ servers = ['ws://localhost:8080', 'ws://bogbook.com']
+ 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) {
diff --git a/server.js b/server.js
index fe378c8..6a56069 100644
--- a/server.js
+++ b/server.js
@@ -31,99 +31,105 @@ bog.keys().then(key => {
wserve.on('connection', function (ws) {
ws.on('message', function (message) {
var req = JSON.parse(message)
- bog.unbox(req.box, req.requester, key).then(unboxed => {
- var unboxedreq = JSON.parse(nacl.util.encodeUTF8(unboxed))
- 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
- fs.readFile(bogdir + 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: msg.seq})
- 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 = JSON.stringify(feed.slice(0, msg.seq - unboxedreq.seq))
- bog.box(diff, req.requester, key).then(boxed => {
- var obj = {
- requester: key.publicKey,
- box: boxed
- }
- ws.send(JSON.stringify(obj))
- })
- }
- })
- } 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(bogdir + msg.author, JSON.stringify(unboxedreq), 'UTF-8', function (err, success) {
- console.log('Saved full log of ' + msg.author + ' sent by ' + req.requester)
- })
- } if (msg.seq > unboxedreq.length) {
- fs.readFile(bogdir + msg.author, 'UTF-8', function (err, data) {
+ 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))
+ 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
+ fs.readFile(bogdir + 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(lastmsg => {
- if (unboxedreq.length + lastmsg.seq === msg.seq) {
- var newlog = unboxedreq.concat(feed)
- fs.writeFile(bogdir + msg.author, JSON.stringify(newlog), 'UTF-8', function (err, success) {
- console.log('combined existing feed of ' + msg.author + ' with diff and saved to server')
+ 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: msg.seq})
+ 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 = JSON.stringify(feed.slice(0, msg.seq - unboxedreq.seq))
+ bog.box(diff, req.requester, key).then(boxed => {
+ var obj = {
+ requester: key.publicKey,
+ box: boxed
+ }
+ ws.send(JSON.stringify(obj))
})
+ }
+ })
+ } 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(bogdir + msg.author, JSON.stringify(unboxedreq), 'UTF-8', function (err, success) {
+ console.log('Saved full log of ' + msg.author + ' sent by ' + req.requester)
+ })
+ } if (msg.seq > unboxedreq.length) {
+ fs.readFile(bogdir + msg.author, 'UTF-8', function (err, data) {
+ var feed = JSON.parse(data)
+ bog.open(feed[0]).then(lastmsg => {
+ if (unboxedreq.length + lastmsg.seq === msg.seq) {
+ var newlog = unboxedreq.concat(feed)
+ fs.writeFile(bogdir + msg.author, JSON.stringify(newlog), 'UTF-8', function (err, success) {
+ console.log('combined existing feed of ' + msg.author + ' with diff and saved to server')
+ })
+ }
+ })
})
- })
- }
- })
- }
- })
+ }
+ })
+ }
+ })
+ }
})
})
})
diff --git a/views.js b/views.js
index b7865e0..87a4c10 100644
--- a/views.js
+++ b/views.js
@@ -206,7 +206,7 @@ function pubs () {
var add = h('input', {placeholder: 'Add a pub'})
- localforage.getItem('pubs').then(function (servers) {
+ localforage.getItem('securepubs').then(function (servers) {
message.appendChild(h('div', [
add,
@@ -214,7 +214,7 @@ function pubs () {
onclick: function () {
if (add.value) {
servers.push(add.value)
- localforage.setItem('pubs', servers).then(function () { location.reload() })
+ localforage.setItem('securepubs', servers).then(function () { location.reload() })
}
}
}, ['Add a pub'])
@@ -226,7 +226,7 @@ function pubs () {
h('button', {
onclick: function () {
var newServers = servers.filter(item => item !== pub)
- localforage.setItem('pubs', newServers).then(function () { location.reload() })
+ localforage.setItem('securepubs', newServers).then(function () { location.reload() })
}
}, ['Remove'])
]))
@@ -235,7 +235,7 @@ function pubs () {
message.appendChild(h('button', {
onclick: function () {
- localforage.removeItem('pubs').then(function () {
+ localforage.removeItem('securepubs').then(function () {
location.hash = ''
location.reload()
})