2020-09-30 10:06:07 +00:00
|
|
|
/*
|
|
|
|
hush-hush: anonymous message board using the onionr network
|
|
|
|
Copyright (C) 2020 Kevin Froman
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-10-03 23:32:10 +00:00
|
|
|
var findMessageIntervalTime = 5000
|
2020-09-30 10:06:07 +00:00
|
|
|
var publicNodes = [
|
2020-10-07 22:46:13 +00:00
|
|
|
"4wbarqtxh6zasoxxftakrellcjzztcib7bqth4u3igof5fskrrvrk4yd",
|
|
|
|
"zl67stwxpjkntaxcfdhj3dvayculoojju6eek2jhsrdz6uwf224o6oqd"
|
2020-09-30 10:06:07 +00:00
|
|
|
]
|
|
|
|
var messageHashes = []
|
2020-10-05 04:09:49 +00:00
|
|
|
var blocks = []
|
|
|
|
var basicTextEncoder = new TextEncoder()
|
|
|
|
var difficulty = "0000"
|
|
|
|
var maxBlockAge = 2678400
|
|
|
|
|
|
|
|
|
|
|
|
function shuffleArray(array) {
|
|
|
|
if (document.hidden){return}
|
|
|
|
for (let i = array.length - 1; i > 0; i--) {
|
|
|
|
const j = Math.floor(Math.random() * (i + 1));
|
|
|
|
[array[i], array[j]] = [array[j], array[i]];
|
|
|
|
}
|
|
|
|
}
|
2020-10-05 08:42:20 +00:00
|
|
|
shuffleArray(publicNodes)
|
|
|
|
|
2020-10-05 04:09:49 +00:00
|
|
|
//https://stackoverflow.com/q/10420352
|
|
|
|
function getReadableFileSizeString(fileSizeInBytes) {
|
|
|
|
var i = -1;
|
|
|
|
var byteUnits = [' kB', ' MB', ' GB', ' TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
|
|
do {
|
|
|
|
fileSizeInBytes = fileSizeInBytes / 1024;
|
|
|
|
i++;
|
|
|
|
} while (fileSizeInBytes > 1024);
|
|
|
|
|
|
|
|
return Math.max(fileSizeInBytes, 0.1).toFixed(1) + byteUnits[i];
|
|
|
|
};
|
|
|
|
|
|
|
|
setInterval(function(){shuffleArray(publicNodes)}, 5000)
|
|
|
|
|
2020-09-30 10:06:07 +00:00
|
|
|
|
|
|
|
// Make Tor connect to each node to reduce future connection time
|
2020-10-07 22:46:13 +00:00
|
|
|
publicNodes.forEach(node => {
|
|
|
|
let doPing = async function(){
|
|
|
|
let res = await(await fetch("http://" + node + ".onion/ping")).text()
|
|
|
|
if (res !== "pong!"){
|
|
|
|
console.debug(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
doPing()
|
2020-09-30 10:06:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
function getCurrentNode(){
|
|
|
|
// Very basic round-robin use of nodes
|
|
|
|
let current = publicNodes.shift()
|
|
|
|
|
|
|
|
publicNodes.push(current)
|
|
|
|
return current
|
|
|
|
}
|
|
|
|
|
|
|
|
function addMessage(message, timestamp){
|
2020-10-07 22:46:13 +00:00
|
|
|
function sortEntries() {
|
|
|
|
var entries = document.getElementsByClassName('entry')
|
|
|
|
|
|
|
|
if (entries.length > 1) {
|
|
|
|
const sortBy = 'data-epoch'
|
|
|
|
const parent = entries[0].parentNode
|
|
|
|
|
|
|
|
const sorted = Array.from(entries).sort((a, b) => b.getAttribute(sortBy) - a.getAttribute(sortBy))
|
|
|
|
sorted.forEach(element => parent.appendChild(element))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 08:42:20 +00:00
|
|
|
|
|
|
|
message = DOMPurify.sanitize(marked(message),
|
|
|
|
{FORBID_ATTR: ['style'],
|
2020-10-07 22:46:13 +00:00
|
|
|
ALLOWED_TAGS: ['b', 'p', 'em', 'i', 'a', 'strong', 'sub', 'small', 'ul', 'li', 'ol', 'strike',
|
|
|
|
'tr', 'td', 'th', 'table', 'thead', 'tfoot', 'colgroup', 'col', 'caption', 'marquee',
|
2020-10-05 08:42:20 +00:00
|
|
|
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'pre', 'center', 'br', 'hr']})
|
|
|
|
|
|
|
|
let childEl = document.createElement('div')
|
|
|
|
childEl.classList.add('content')
|
|
|
|
childEl.innerHTML = message
|
2020-09-30 10:06:07 +00:00
|
|
|
var tmpl = document.getElementById("cMsgTemplate")
|
2020-10-07 22:46:13 +00:00
|
|
|
timestamp = timestamp.toString()
|
2020-09-30 10:06:07 +00:00
|
|
|
|
|
|
|
let newEl = tmpl.content.cloneNode(true)
|
2020-10-07 22:46:13 +00:00
|
|
|
newEl.children[0].setAttribute('data-epoch', timestamp)
|
|
|
|
newEl.children[0].classList.add("entry")
|
2020-10-05 08:42:20 +00:00
|
|
|
newEl.children[0].children[0].children[0].innerText = ""
|
|
|
|
newEl.children[0].children[0].children[0].append(childEl)
|
2020-10-07 22:46:13 +00:00
|
|
|
newEl.children[0].children[0].children[2].innerText = new Date(timestamp * 1000).toString().split('GMT')[0]
|
2020-10-05 08:42:20 +00:00
|
|
|
document.getElementsByClassName("messageFeed")[0].prepend(newEl)
|
2020-10-07 22:46:13 +00:00
|
|
|
sortEntries()
|
2020-09-30 10:06:07 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 23:32:10 +00:00
|
|
|
async function apiGET(path, queryString, raw=false){
|
2020-09-30 10:06:07 +00:00
|
|
|
let response = await fetch("http://" + getCurrentNode() + ".onion/" + encodeURIComponent(path) + queryString)
|
|
|
|
|
|
|
|
if (response.ok) { // if HTTP-status is 200-299
|
|
|
|
// get the response body (the method explained below)
|
2020-10-03 23:32:10 +00:00
|
|
|
if (raw){
|
|
|
|
return await response.blob()
|
|
|
|
}
|
2020-09-30 10:06:07 +00:00
|
|
|
return await response.text()
|
|
|
|
} else {
|
|
|
|
console.debug("HTTP-Error: " + response.status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function findMessages(){
|
2020-10-05 04:09:49 +00:00
|
|
|
if (document.hidden){
|
|
|
|
setTimeout(function(){findMessages()}, 1000)
|
|
|
|
return
|
|
|
|
}
|
2020-10-07 22:46:13 +00:00
|
|
|
let messages = (await apiGET("getblocklist", "?type=kic")).split('\n')
|
2020-09-30 10:06:07 +00:00
|
|
|
messages.forEach(block => {
|
2020-10-05 08:42:20 +00:00
|
|
|
if (!block) { return}
|
2020-10-05 04:09:49 +00:00
|
|
|
block = reconstructHash(block)
|
|
|
|
if (!block.startsWith(difficulty)){console.debug("not difficulty reached:" + block); return}
|
|
|
|
|
|
|
|
if (blocks.includes(block)){return}
|
2020-10-03 23:32:10 +00:00
|
|
|
apiGET("getdata", "/" + block, raw=false).then(function(d){
|
2020-10-05 04:09:49 +00:00
|
|
|
let updateMemoryUsage = function(data, block){
|
|
|
|
let current = parseInt(document.getElementById('memUsage').innerText)
|
|
|
|
// Size is size of data (not metadata) and block hash
|
|
|
|
document.getElementById('memUsage').innerText = getReadableFileSizeString(current + ((basicTextEncoder.encode(data)).length + block.length))
|
|
|
|
}
|
2020-10-07 22:46:45 +00:00
|
|
|
try{
|
|
|
|
let metadata = JSON.parse(d.split("\n")[0])
|
|
|
|
}
|
|
|
|
catch(e){
|
|
|
|
console.debug(e)
|
|
|
|
return
|
|
|
|
}
|
2020-10-06 02:07:48 +00:00
|
|
|
|
2020-10-05 08:42:20 +00:00
|
|
|
let data = d.substring(d.indexOf('\n') + 1);
|
2020-10-05 04:09:49 +00:00
|
|
|
try{
|
|
|
|
verifyBlock(d, block)
|
2020-10-05 08:42:20 +00:00
|
|
|
verifyTime(metadata['time'])
|
2020-10-05 04:09:49 +00:00
|
|
|
}
|
|
|
|
catch(e){
|
|
|
|
console.debug(block + ":" + e)
|
2020-10-05 08:42:20 +00:00
|
|
|
return
|
2020-10-05 04:09:49 +00:00
|
|
|
}
|
|
|
|
blocks.push(block)
|
2020-10-07 22:46:13 +00:00
|
|
|
addMessage(data, metadata['time'])
|
2020-10-05 04:09:49 +00:00
|
|
|
updateMemoryUsage(data, block)
|
2020-09-30 10:06:07 +00:00
|
|
|
})
|
|
|
|
})
|
2020-10-05 04:09:49 +00:00
|
|
|
setTimeout(function(){findMessages()}, findMessageIntervalTime)
|
2020-09-30 10:06:07 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 04:09:49 +00:00
|
|
|
setTimeout(function(){findMessages()}, findMessageIntervalTime)
|