Browse Source

initial stab at private beacons

master
Ev Bogue 3 years ago
parent
commit
3fe02c9117
  1. 3
      app.js
  2. 89
      beacons.js
  3. 4
      css/style.css
  4. 3
      gossip.js
  5. 1
      index.html
  6. 73
      render.js
  7. 59
      server.js
  8. 49
      settings.js

3
app.js

@ -10,6 +10,8 @@ function route (keys) {
if (src === 'settings') {
settingsPage(keys)
} else if (src === 'beacons') {
beaconsPage(keys)
} else if (src[0] === '@') {
profilePage(src, keys)
} else if (src[0] === '?') {
@ -28,6 +30,7 @@ keys().then(key => {
h('div', {classList: 'internal'}, [
h('li', [h('a', {href: '#'}, ['Home'])]),
h('li', [h('a', {href: '#' + key.publicKey}, [getName(key.publicKey, keys)])]),
h('li', [h('a', {href: '#beacons'}, ['Beacons'])]),
h('li', {classList: 'right'}, [h('a', {href: '#settings'}, ['Settings'])]),
h('form', { classList: 'search',
onsubmit: function (e) {

89
beacons.js

@ -0,0 +1,89 @@
function beaconsPage (keys) {
var pubslist = h('select')
localforage.getItem('securepubs').then(function (servers) {
servers.forEach(function (pub) {
pubslist.appendChild(
h('option', {value: pub}, [pub])
)
})
})
var ads = h('div', {classList: 'message'})
ads.appendChild(h('span', {innerHTML: marked('Sometimes when you\'re lost on the Internet you might want to send out a beacon so that people can see you. \n\n Beacons from guests will run for 100 views before they are deleted by the pub. \n\nSelect a pub:')}))
ads.appendChild(pubslist)
var recp = h('input', {placeholder: 'Ex: @Q++V5BbvWIg8B+TqtC9ZKFhetruuw+nOgxEqfjlOZI0='})
var adstext = h('textarea', {placeholder: 'Hello World!'})
ads.appendChild(h('span', [
h('br'),
h('p', [" Send a beacon (leave the 'To:' field blank for a public beacon): "]),
h('p', ['To: ',
recp
]),
adstext,
h('br'),
h('button', {
onclick: function () {
var split = pubslist.value.split('~')
console.log(split)
var serverurl = split[0]
var serverpub = split[1]
var ws = new WebSocket(serverurl)
if ((recp.value) && (adstext.value)) {
var tobox = {
author: keys.publicKey,
timestamp: Date.now(),
content: adstext.value
}
box(JSON.stringify(tobox), recp.value, keys).then(boxedmsg => {
var msg = {
type: 'beacon',
author: keys.publicKey,
box: boxedmsg
}
ws.onopen = function () {
box(JSON.stringify(msg), serverpub, keys).then(boxed => {
var obj = {
requester: keys.publicKey,
box: boxed
}
ws.send(JSON.stringify(obj))
})
adstext.value = ''
recp.value = ''
}
})
}
if ((!recp.value) && (adstext.value)) {
var msg = {
type: 'beacon',
author: keys.publicKey
}
msg.signature = nacl.util.encodeBase64(nacl.sign(nacl.util.decodeUTF8(JSON.stringify(adstext.value)), nacl.util.decodeBase64(keys.privateKey)))
ws.onopen = function () {
box(JSON.stringify(msg), serverpub, keys).then(boxed => {
var obj = {
requester: keys.publicKey,
box: boxed
}
ws.send(JSON.stringify(obj))
})
adstext.value = ''
}
}
}
}, ['Publish'])
]))
scroller.appendChild(ads)
}

4
css/style.css

@ -70,7 +70,7 @@ hr {
animation: fadein .5s;
}
#ad {
#ad, #pm {
background: white;
font-size: .8em;
border: 1px solid #ddd;
@ -82,7 +82,7 @@ hr {
width: 250px;
}
#ad button {
#ad button, #pm button {
font-size: .8em;
margin: 0;
padding: 0;

3
gossip.js

@ -53,6 +53,9 @@ function sync (subs, keys) {
unboxedreq.signature = unboxedreq.content
renderAd(unboxedreq)
}
if (unboxedreq.box) {
renderAd(unboxedreq, keys)
}
if (unboxedreq.seq === 0) {
var stringedfeed = JSON.stringify(srclog)
box(stringedfeed, serverpub, keys).then(boxed => {

1
index.html

@ -17,6 +17,7 @@
<script src="bog.js"></script>
<script src="composer.js"></script>
<script src="settings.js"></script>
<script src="beacons.js"></script>
<script src="identify.js"></script>
<script src="render.js"></script>
<script src="gossip.js"></script>

73
render.js

@ -1,4 +1,6 @@
function renderAd (ad) {
function renderAd (ad, keys) {
console.log(ad)
var screen = document.getElementById('screen')
var adspot = document.getElementById('ad')
@ -11,30 +13,57 @@ function renderAd (ad) {
if (ad.views) {
adspace.appendChild(h('span', {classList: 'right'}, [h('pre', [ad.views + ' views'])]))
} else {
adspace.appendChild(h('span', {classList: 'right'}, [h('pre', ['ad'])]))
adspace.appendChild(h('span', {classList: 'right'}, [h('pre', ['beacon'])]))
}
open(ad).then(opened => {
quickName(ad.author).then(gotName => {
newAd = h('div', {id: 'ad'}, [
adspace,
h('p', {innerHTML: marked(opened)}),
h('button', {classList: 'right',
onclick: function () {
adspot = document.getElementById('ad')
adspot.parentNode.removeChild(adspot)
}
}, ['Heard']),
h('span', [
'—',
h('a', {href: '#' + ad.author}, [gotName]),
' from ',
h('a', {href: ad.name}, [ad.name])
if (ad.box) {
unbox(ad.box, ad.author, keys).then(unboxed => {
var msg = JSON.parse(nacl.util.encodeUTF8(unboxed))
quickName(ad.author).then(gotName => {
beacon = h('div', {id: 'pm'}, [
h('span', {classList: 'right'}, [h('pre', [human(new Date(msg.timestamp))])]),
h('p', {innerHTML: marked(msg.content)}),
h('button', {classList: 'right',
onclick: function () {
pmspot = document.getElementById('pm')
pmspot.parentNode.removeChild(pmspot)
}
}, ['Heard']),
h('span', [
'—',
h('a', {href: '#' + ad.author}, [gotName]),
' from ',
h('a', {href: ad.name}, [ad.name])
])
])
])
screen.appendChild(newAd)
})
})
screen.append(beacon)
})
})
}
if (ad.signature) {
open(ad).then(opened => {
quickName(ad.author).then(gotName => {
newAd = h('div', {id: 'ad'}, [
adspace,
h('p', {innerHTML: marked(opened)}),
h('button', {classList: 'right',
onclick: function () {
adspot = document.getElementById('ad')
adspot.parentNode.removeChild(adspot)
}
}, ['Heard']),
h('span', [
'—',
h('a', {href: '#' + ad.author}, [gotName]),
' from ',
h('a', {href: ad.name}, [ad.name])
])
])
screen.appendChild(newAd)
})
})
}
}
function getHeader (post, keys, mini) {

59
server.js

@ -119,15 +119,27 @@ bog.keys().then(key => {
} else {
bog.unbox(req.box, req.requester, key).then(unboxed => {
var unboxedreq = JSON.parse(nacl.util.encodeUTF8(unboxed))
if (unboxedreq.type == 'ad') {
var hex = Buffer.from(nacl.hash(nacl.util.decodeUTF8(unboxedreq.signature))).toString('hex')
var obj = {
hash: hex,
author: unboxedreq.author,
signature: unboxedreq.signature,
views: 0
//console.log(unboxedreq)
if (unboxedreq.type == 'beacon') {
if (unboxedreq.box) {
var hex = Buffer.from(nacl.hash(nacl.util.decodeUTF8(unboxedreq.box))).toString('hex')
var obj = {
hash: hex,
author: unboxedreq.author,
box: unboxedreq.box,
views: 0
}
}
if (unboxedreq.signature) {
var hex = Buffer.from(nacl.hash(nacl.util.decodeUTF8(unboxedreq.signature))).toString('hex')
var obj = {
hash: hex,
author: unboxedreq.author,
signature: unboxedreq.signature,
views: 0
}
}
fs.writeFile(addir + hex, JSON.stringify(obj), 'UTF-8', function () {
@ -150,13 +162,25 @@ bog.keys().then(key => {
var num = Math.floor(Math.random() * (adfiles.length))
fs.readFile(addir + adfiles[num], 'UTF-8', function (err, adFile) {
var obj = JSON.parse(adFile)
var ad = {
author: obj.author,
name: config.fullurl,
content: obj.signature,
timestamp: Date.now(),
views: obj.views
if (obj.signature) {
var ad = {
author: obj.author,
name: config.fullurl,
content: obj.signature,
views: obj.views
}
}
if (obj.box) {
var ad = {
author: obj.author,
name: config.fullurl,
box: obj.box,
views: obj.views
}
}
if ((obj.views > 100) && (obj.author != config.author)) {
fs.unlinkSync(addir + obj.hash)
//console.log('REMOVING AD')
@ -165,6 +189,7 @@ bog.keys().then(key => {
fs.writeFileSync(addir + obj.hash, JSON.stringify(obj), 'UTF-8')
}
printSendAd(ad, req)
console.log(ad)
//console.log('SENDING AD')
bog.box(JSON.stringify(ad), req.requester, key).then(boxed => {
sendobj = {
@ -175,9 +200,7 @@ bog.keys().then(key => {
ws.close()
})
})
} else {
ads.make('Hello World.')
}
}
})
}
}

49
settings.js

@ -56,11 +56,7 @@ function settingsPage (keys) {
var add = h('input', {placeholder: 'Add a pub'})
var pubslist = h('select')
localforage.getItem('securepubs').then(function (servers) {
pubs.appendChild(h('div', [
add,
h('button', {
@ -74,9 +70,6 @@ function settingsPage (keys) {
]))
servers.forEach(function (pub) {
pubslist.appendChild(
h('option', {value: pub}, [pub])
)
pubs.appendChild(h('p', [
pub,
h('button', {
@ -98,52 +91,10 @@ function settingsPage (keys) {
}
}, ['Reset pubs']))
var ads = h('div', {classList: 'message'})
ads.appendChild(h('span', {innerHTML: marked('Submit an advertisement. Right now advertisements are free for anyone to post to this pub. Ads from guests will run for 100 views before they are deleted by the pub. \n\nSelect a pub:')}))
ads.appendChild(pubslist)
var adstext = h('input', {placeholder: 'Hello World!'})
ads.appendChild(h('span', [
h('br'),
' Write an ad: ',
h('br'),
adstext,
h('button', {
onclick: function () {
var split = pubslist.value.split('~')
console.log(split)
var serverurl = split[0]
var serverpub = split[1]
var ws = new WebSocket(serverurl)
var msg = {
type: 'ad',
author: keys.publicKey
}
msg.signature = nacl.util.encodeBase64(nacl.sign(nacl.util.decodeUTF8(JSON.stringify(adstext.value)), nacl.util.decodeBase64(keys.privateKey)))
ws.onopen = function () {
box(JSON.stringify(msg), serverpub, keys).then(boxed => {
var obj = {
requester: keys.publicKey,
box: boxed
}
ws.send(JSON.stringify(obj))
})
}
adstext.value = ''
}
}, ['Publish'])
]))
scroller.appendChild(welcome)
scroller.appendChild(keyDiv)
scroller.appendChild(pubs)
scroller.appendChild(everything)
//scroller.appendChild(regenerate)
scroller.appendChild(ads)
}

Loading…
Cancel
Save