~ev/mailbox

e0d897aa82d12a66c587093776263805509ef679 — Ev Bogue 2 years ago dc90da2 master
ask the server for pubkey on first contact
3 files changed, 79 insertions(+), 43 deletions(-)

M client.js
M css/style.css
M server.js
M client.js => client.js +66 -37
@@ 1,11 1,37 @@
var pub = 'ws://localhost:7770/~@4hRsBtxqwvGMZSEBarv4RRSanmDaHm3Jm4LngYzgZJQ='
async function getPub (keys) {
  var pub = await localforage.getItem('pub')
  console.log(pub)
  if (pub != null) {
    return pub
  } else {
    var serverurl = 'ws://' + location.hostname + ':7770'

    var ws = new WebSocket(serverurl)
    ws.onopen = function () {
      ws.send(keys.publicKey + '~' + 'pubkey')
    }

    ws.onmessage = function (message) {
      var split = message.data.split('~')
      console.log(split)
      unbox(split[1], split[0], keys).then(unboxed => {
        localforage.setItem('pub', unboxed).then(success => {
          console.log('saved pub')
      
          return pub
        }).then(restart => {
          location.reload()
        })
      })
    }
  }
}
//var pub = 'ws://localhost:7770/~@4hRsBtxqwvGMZSEBarv4RRSanmDaHm3Jm4LngYzgZJQ='

var screen = h('div', {id: 'screen'})
document.body.appendChild(screen)

function render (msg, keys) {
  console.log(msg)

  var getRaw = h('button', {
    onclick: function () {
      if (msg.to === keys.publicKey) {


@@ 58,7 84,6 @@ function render (msg, keys) {
      }, [msg.to.substring(0, 10) + '...'])
    ]))
  }

}

function existingMail (keys) {


@@ 78,48 103,51 @@ function existingMail (keys) {
    collected.forEach(function (msg) {
      render(msg, keys)
    })
    send('checkmailbox', pub, keys)
    send('checkmailbox', keys)
  }).catch(function(err) {
    console.log(err)
  })
}

function send (req, pub, keys) {
function send (req, keys) {
  getPub(keys).then(pub => {

  var split = pub.split('~')
  var serverurl = split[0]
  var serverpubkey = split[1]
  var ws = new WebSocket(serverurl)
    console.log(pub)
    var split = pub.split('~')
    console.log(split)
    var serverurl = split[0]
    var serverpubkey = split[1]
    var ws = new WebSocket(serverurl)

  ws.onopen = function () {
    box(req, serverpubkey, keys).then(boxed => {
      ws.send(keys.publicKey + '~' + boxed)
    })
  }
  ws.onmessage = function (message) {
    var split = message.data.split('~')
    unbox(split[1], split[0], keys).then(unboxed => {
      var hash = nacl.hash(nacl.util.decodeUTF8(unboxed))
      var base = nacl.util.encodeBase64(hash)
      localforage.getItem(base).then(function (data) {
        if (data === null) {
          localforage.setItem(base, unboxed).then(function () {console.log('We should have saved it')})
          var split = unboxed.split('~')
          unbox(split[1], split[0], keys).then(unboxed => {
            msg = JSON.parse(unboxed)
            msg.boxed = message.data
            msg.author = split[0]
            render(msg, keys)
          })
        } else {
        }
    ws.onopen = function () {
      box(req, serverpubkey, keys).then(boxed => {
        ws.send(keys.publicKey + '~' + boxed)
      })
    })
  }
    }
    ws.onmessage = function (message) {
      var split = message.data.split('~')
      unbox(split[1], split[0], keys).then(unboxed => {
        var hash = nacl.hash(nacl.util.decodeUTF8(unboxed))
        var base = nacl.util.encodeBase64(hash)
        localforage.getItem(base).then(function (data) {
          if (data === null) {
            localforage.setItem(base, unboxed).then(function () {console.log('We should have saved it')})
            var split = unboxed.split('~')
            unbox(split[1], split[0], keys).then(unboxed => {
              msg = JSON.parse(unboxed)
              msg.boxed = message.data
              msg.author = split[0]
              render(msg, keys)
            })
          } else {
          }
        })
      })
    }
  })
}

function mailbox (keys) {

  existingMail(keys)

  var to = h('input', {id: 'to'})


@@ 138,12 166,12 @@ function mailbox (keys) {
      if (keys.publicKey != to.value) {
        box(JSON.stringify(msg), keys.publicKey, keys).then(boxed => {
          var sendmail = keys.publicKey + '~' + boxed
          send(sendmail, pub,keys)
          send(sendmail,keys)
        })
      }
      box(JSON.stringify(msg), to.value, keys).then(boxed => {
        var sendmail = keys.publicKey + '~' + boxed
        send(sendmail, pub, keys)
        send(sendmail, keys)
        setTimeout(function () {
          location.reload()
        }, 1000)


@@ 203,6 231,7 @@ function route (keys) {
  var scroller = h('div', {id: 'scroller'})
  var screen = document.getElementById('screen')
  screen.appendChild(scroller)
  getPub(keys)
  mailbox(keys)
}


M css/style.css => css/style.css +1 -1
@@ 8,7 8,7 @@ body { background: #f5f5f5; }

a {color: #2196f3;}

.hint {color: #999;}
.hint {color: #999; font-size: .8em;}

pre, code {color: #dd1144;}


M server.js => server.js +12 -5
@@ 4,15 4,15 @@ var open = require('open')

var PORT = 7777
var WSPORT = 7770
var URL = 'ws://localhost'

http.createServer(
  serve({ root: __dirname})
).listen(PORT)

console.log('Now serving Mailbox at http://localhost:' + PORT + '/')
console.log('Now serving Mailbox at '+ URL + ':' + PORT + '/')
open('http://localhost:' + PORT)


var bog = require('./bog')
var WS = require('ws')
var fs = require('fs')


@@ 31,13 31,20 @@ if (!fs.existsSync(homedir + '/.mailbox/')) {
var wserve = new WS.Server({ port: WSPORT })

bog.keys().then(keys => {
  console.log('Now accepting connections at http://localhost:' + WSPORT + '/~' + keys.publicKey)
  console.log('Now accepting connections at ' + URL + ':' + WSPORT + '/~' + keys.publicKey)
  wserve.on('connection', function (ws) {
    ws.on('message', function (message) {
      console.log(message)
      var split = message.split('~')
      console.log(split)
      if (split.length === 2) {
      if (split[1] === 'pubkey') {
        console.log(split[0] + ' requests pubkey')
        var pub = URL + ':' + WSPORT + '/~' + keys.publicKey
        bog.box(pub, split[0], keys).then(boxed => {
          ws.send(keys.publicKey + '~' + boxed)
        })
      }
      else if (split.length === 2) {
        bog.unbox(split[1], split[0], keys).then(unboxed => {
          console.log(unboxed)
          checkforpm = unboxed.split('~')


@@ 61,7 68,7 @@ bog.keys().then(keys => {
            })
          }
        })
      } 
      }
    })
  })
})