aboutsummaryrefslogtreecommitdiff
path: root/server.js
diff options
context:
space:
mode:
authorEv Bogue <ev@evbogue.com>2019-04-19 09:38:52 -0500
committerEv Bogue <ev@evbogue.com>2019-04-19 09:38:52 -0500
commit0d07708fe1ae9164ec1b361f60c5186af7ac7aed (patch)
tree7bad9765de7d78b91369b2b1dd623217bbd3e4e4 /server.js
parent951d2b36fe8bf2cd7c88eb7c22215708bde36c14 (diff)
major refactor -- only storing author, hash, and signatures on messages and opening signed messages with nacl.sign.open
Diffstat (limited to 'server.js')
-rw-r--r--server.js47
1 files changed, 6 insertions, 41 deletions
diff --git a/server.js b/server.js
index f6756ca..377aea3 100644
--- a/server.js
+++ b/server.js
@@ -8,55 +8,20 @@ http.createServer(
serve({ root: __dirname})
).listen(8089)
-
-//opn('http://localhost:8089')
+opn('http://localhost:8089')
// websocket server (8080)
-
var WebSocket = require('ws')
var fs = require('fs')
+var nacl = require('tweetnacl')
+ nacl.util = require('tweetnacl-util')
-var wss = new WebSocket.Server({ port: 8080 })
+var wserver = new WebSocket.Server({ port: 8080 })
-wss.on('connection', function (ws) {
+wserver.on('connection', function (ws) {
ws.on('message', function (message) {
- var receivedLog = JSON.parse(message)
- if (receivedLog.publicKey) {
- var publicKey = receivedLog.publicKey
-
- var clientLog = receivedLog.log
- // check to see if log is on server
- if (fs.existsSync(__dirname + '/bogs/' + publicKey)) {
- var serverLog = JSON.parse(fs.readFileSync(__dirname + '/bogs/' + receivedLog.publicKey))
- // if the server log has more entries than the log in the client, send the server log to the client
- if (serverLog.length > clientLog.length) {
- sendingLog = {
- publicKey: publicKey,
- log: serverLog
- }
- ws.send(JSON.stringify(sendingLog))
- console.log('SENT ' + publicKey)
- }
- // if server log has less entries than the log sent by the client, write it to the server
- if (serverLog.length < clientLog.length) {
- fs.writeFile(__dirname + '/bogs/' + publicKey, JSON.stringify(clientLog), function (err) {
- if (err) throw err
- console.log('SAVED ' + publicKey)
- })
- }
- // if logs are identical, do nothing
- if (serverLog.length == clientLog.length) {
- console.log('SAME ' + publicKey)
- }
- // if log doesn't already exist, write it
- } else {
- fs.writeFile(__dirname + '/bogs/' + publicKey, JSON.stringify(clientLog), function (err) {
- if (err) throw err
- console.log('SAVED ' + publicKey)
- })
- }
- }
+ console.log(message)
})
})