aboutsummaryrefslogtreecommitdiff
path: root/server.js
diff options
context:
space:
mode:
Diffstat (limited to 'server.js')
-rw-r--r--server.js256
1 files changed, 156 insertions, 100 deletions
diff --git a/server.js b/server.js
index 6cd0a38..a4f563f 100644
--- a/server.js
+++ b/server.js
@@ -1,34 +1,130 @@
+var fs = require('fs')
+var homedir = require('os').homedir()
+
+var path = homedir + '/.bogbook/'
+var bogdir = path + 'bogs/'
+var confpath = path + 'config.json'
+
+if (!fs.existsSync(homedir + '/.bogbook/')) {fs.mkdirSync(homedir + '/.bogbook/')}
+if (!fs.existsSync(bogdir)){fs.mkdirSync(bogdir)}
+
+if (fs.existsSync(confpath)) {
+ console.log('loading config from ' + confpath)
+ var config = require(confpath)
+} else {
+ var config = {
+ port: '8089',
+ wsport: '8080',
+ url: 'localhost',
+ author: '@Q++V5BbvWIg8B+TqtC9ZKFhetruuw+nOgxEqfjlOZI0='
+ }
+ config.fullurl = 'http://' + config.url + ':' + config.port + '/'
+ fs.writeFileSync(confpath, JSON.stringify(config), 'utf-8')
+}
+
+console.log(config)
+
+if (process.argv[2] === 'verbose') {
+ var VERBOSE = true
+} else {
+ var VERBOSE = false
+}
+
+console.log('Verbose output is ' + VERBOSE + ' run with `node server verbose` to see all output')
+
+// log messages
+
+function printAsk(req, unboxedreq) {
+ if (VERBOSE) {
+ console.log(req.requester + ' asked for feed ' + unboxedreq.author + ' after sequence ' + unboxedreq.seq)
+ }
+}
+
+function printNewFeed (msg, req) {
+ if (VERBOSE)
+ console.log('Saved full log of ' + msg.author + ' sent by ' + req.requester)
+ else
+ console.log('NEW FEED from ' + msg.author)
+}
+
+function printUpdateFeed (msg, req) {
+ if (VERBOSE)
+ console.log('combined existing feed of ' + msg.author + ' sent from ' + req.requester + ' with diff and saved to server')
+ else
+ console.log('NEW UPDATE from ' + msg.author)
+}
+
+function printNoFeed (msg, req) {
+ if (VERBOSE) {
+ console.log('We don\'t have the log on the server, requesting log from ' + req.requester )
+ }
+}
+
+function printClientLonger (msg, req) {
+ if (VERBOSE) {
+ console.log(req.requester + '\'s feed of ' + msg.author + ' is longer, requesting diff from ' + req.requester)
+ }
+}
+
+function printClientShorter (msg, req, baserange, endrange) {
+ if (VERBOSE) {
+ console.log(req.requester + ' feed of ' + msg.author + ' is shorter, sending from ' + baserange + ' to ' + endrange + ' to ' + req.requester)
+ }
+}
+
+function printFeedIdentical (msg, req) {
+ if (VERBOSE) {
+ console.log(msg.author + '\'s feed sent from ' + req.requester + ' is identical')
+ }
+}
+
// static server (8089)
-var http = require('http')
-var serve = require('ecstatic')
+var serve = require('koa-static-server')
+var koa = require('koa')
var open = require('open')
-http.createServer(
- serve({ root: __dirname})
-).listen(8089)
+var app = new koa()
+
+// namespace redirect -- add namespaces to ~/.bogbook/names.json
+app.use(async function (ctx, next) {
+ if (ctx.request.url[1] != '#') {
+ var name = ctx.request.url.substring(1)
+
+ if (!fs.existsSync(path + 'names.json')) {
+ var obj = {
+ ev: '@Q++V5BbvWIg8B+TqtC9ZKFhetruuw+nOgxEqfjlOZI0=',
+ mil3s: '@531mT2x1FnQdpYJxVrG8YD9wiE767xO88kKRhi5A3Yg=',
+ g: '@WVBPY53Bl4aUIngt2TXV8nW+IGKvCTqhv88EvktOX9s='
+ }
+ fs.writeFileSync(path + 'names.json', JSON.stringify(obj), 'UTF-8')
+ }
+
+ var obj = JSON.parse(fs.readFileSync(path + 'names.json', 'UTF-8'))
+
+ for (var property in obj) {
+ if ((name === property) || (name === property + '/')) {
+ ctx.redirect('/#' + obj[property])
+ }
+ }
+ }
+ return await next()
+})
+
+app.use(serve({rootDir: '.', notFoundFile: 'index.html'}))
+
+app.listen(config.port)
-open('http://localhost:8089')
+open(config.fullurl)
-// ws server (8080)
+console.log('Bogbook is running at: ' + config.fullurl)
var bog = require('./bog')
var WS = require('ws')
-var fs = require('fs')
var nacl = require('tweetnacl')
nacl.util = require('tweetnacl-util')
-var homedir = require('os').homedir()
-var sanitize = require('sanitize-filename')
-
-var bogdir = homedir + '/.bogbook/bogs/'
-var blobdir = homedir + '/.bogbook/blobs/'
-
-if (!fs.existsSync(homedir + '/.bogbook/')) {fs.mkdirSync(homedir + '/.bogbook/')}
-if (!fs.existsSync(bogdir)){fs.mkdirSync(bogdir)}
-if (!fs.existsSync(blobdir)){fs.mkdirSync(blobdir)}
-
-var wserve = new WS.Server({ port: 8080 })
+var wserve = new WS.Server({ port: config.wsport })
bog.keys().then(key => {
wserve.on('connection', function (ws) {
@@ -36,87 +132,23 @@ bog.keys().then(key => {
var req = JSON.parse(message)
if (req.sendpub) {
ws.send(key.publicKey)
- }
- else {
+ ws.close()
+ } 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.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)
+ if (unboxedreq.seq >= 0) {
+ printAsk(req, unboxedreq)
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')
- }
-
+ printFeedIdentical(msg, req)
+ ws.close()
+ }
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')
+ printClientLonger(msg, req)
var reqdiff = JSON.stringify({author: unboxedreq.author, seq: msg.seq})
bog.box(reqdiff, req.requester, key).then(boxed => {
var obj = {
@@ -126,22 +158,46 @@ bog.keys().then(key => {
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))
+ var endrange = feed.length - unboxedreq.seq - Math.floor(Math.random() * 50 + 1)
+ if (endrange < 0) {
+ endrange = 0
+ }
+ var baserange = feed.length - unboxedreq.seq
+ printClientShorter(msg, req, baserange, endrange)
+ if (baserange > 50) {
+ var latest = JSON.stringify({
+ latest: unboxedreq.author,
+ feed: feed.slice(0, 5)
+ })
+ bog.box(latest, req.requester, key).then(boxed => {
+ var obj = {
+ requester: key.publicKey,
+ box: boxed
+ }
+ //console.log('sending latest ' + unboxedreq.author)
+ ws.send(JSON.stringify(obj))
+ })
+ }
+
+ var diff = JSON.stringify(
+ feed.slice(
+ endrange,
+ baserange
+ )
+ )
bog.box(diff, req.requester, key).then(boxed => {
var obj = {
requester: key.publicKey,
box: boxed
}
ws.send(JSON.stringify(obj))
+ ws.close()
})
}
})
} 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 )
+ printNoFeed(unboxedreq, req)
var reqwhole = JSON.stringify({author: unboxedreq.author, seq: 0})
bog.box(reqwhole, req.requester, key).then(boxed => {
@@ -154,11 +210,10 @@ bog.keys().then(key => {
}
})
} 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)
+ printNewFeed(msg, req)
})
} if (msg.seq > unboxedreq.length) {
fs.readFile(bogdir + msg.author, 'UTF-8', function (err, data) {
@@ -167,9 +222,10 @@ bog.keys().then(key => {
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')
+ printUpdateFeed(msg, req)
})
}
+ ws.close()
})
})
}