basic peer and block sync finished, torcheck added

This commit is contained in:
Kevin Froman 2020-10-05 04:09:49 +00:00
parent 329d296df3
commit c185b0b02a
8 changed files with 132 additions and 28 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 399 B

View File

@ -17,10 +17,36 @@
*/
var findMessageIntervalTime = 5000
var publicNodes = [
"4gh2dwbmlrombeoyco55un7kbej7trsuebxfzvo53h6uj5adseceduyd"
"yre3tmbu25lcogl42xlh73wfchgbx3unz2zz3ttyiylj6gaq5mzhevid"
]
var messageHashes = []
var blocks = {}
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]];
}
}
//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)
// Make Tor connect to each node to reduce future connection time
publicNodes.forEach(element => {
@ -40,7 +66,7 @@ function addMessage(message, timestamp){
let newEl = tmpl.content.cloneNode(true)
newEl.children[0].children[0].children[0].innerText = message
newEl.children[0].children[0].children[1].innerText = timestamp
newEl.children[0].children[0].children[2].innerText = timestamp
document.getElementsByClassName("messageFeed")[0].append(newEl)
}
@ -59,16 +85,40 @@ async function apiGET(path, queryString, raw=false){
}
async function findMessages(){
if (document.hidden){
setTimeout(function(){findMessages()}, 1000)
return
}
let messages = (await apiGET("getblocklist", "?type=brd")).split('\n')
messages.forEach(block => {
if (! block){return}
if (block in blocks){return}
block = reconstructHash(block)
if (!block.startsWith(difficulty)){console.debug("not difficulty reached:" + block); return}
if (blocks.includes(block)){return}
apiGET("getdata", "/" + block, raw=false).then(function(d){
let metadata = d.split("\n")[0]
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))
}
try{
verifyBlock(d, block)
verifyTime()
}
catch(e){
console.debug(block + ":" + e)
}
let metadata = JSON.parse(d.split("\n")[0])
console.debug(metadata)
let data = d.split('\n')[1]
blocks[block] = data
blocks.push(block)
addMessage(data, new Date(metadata['time']))
updateMemoryUsage(data, block)
})
})
setTimeout(function(){findMessages()}, findMessageIntervalTime)
}
setInterval(function(){findMessages()}, findMessageIntervalTime)
setTimeout(function(){findMessages()}, findMessageIntervalTime)

View File

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<title>hush-hush</title>
<title>kiccan</title>
<meta name="description" content="Generate Onionr block">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
@ -11,32 +11,52 @@
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>👁️</text></svg>">
<link rel="stylesheet" href="css/bulma-dark.min.css">
<link rel="stylesheet" href="style.css">
<script src="tordetect.js" async></script>
<script src="marked.min.js" defer></script>
<script src="sha3.js" defer></script>
<script src="onionr-jspow/index.js" defer></script>
<script src="hush-hush.js" defer></script>
<script src="onionr-blocks.js" defer></script>
<script src="hush-hush.js" defer></script>
<script src="worker-handler.js" defer></script>
</head>
<body>
<section class="section">
<div class="container">
<h1 class="title">
hush-hush
kiccan
</h1>
<p class="subtitle">
Anonymous decentralized message board 🤫
anonymous decentralized message board 🌐
<img src="vanilla.png" alt="this website uses vanilla JS" aria-hidden="true" loading="lazy" class="is-pulled-right">
<img src="copyleft.png" alt="GNU" aria-hidden="true" loading="lazy" class="is-pulled-right">
</p>
<noscript><p>JavaScript is required for this app.</p></noscript>
<p> hush-hush is a message board program that utilizes the Onionr network.</p>
<p>⚠️ Posts are rate-limited using a partial hash collision proof of work function.</p>
<p>⏲️ The host of this web page has no control over posts. Browse and post at your own risk.</p>
<img src="vanilla.png" alt="this website uses vanilla JS" aria-hidden="true" loading="lazy">
<img src="hacker.png" alt="hacker" aria-hidden="true" loading="lazy">
<img src="copyleft.png" alt="GNU" aria-hidden="true" loading="lazy">
<noscript><p class="has-text-warning">
JavaScript is required for this app. The server has no knowledge of posts, so content must be served by P2P nodes.
</p></noscript>
<p>kiccan is a message board program that utilizes the <a href="https://onionr.net/">Onionr</a> network.</p>
<p>posts are rate-limited using a partial hash collision proof of work function.</p>
<p>⚠️ the host of this web page has no control over posts. Browse and post at your own risk.</p>
<p class="is-pulled-right">post memory usage: <span id="memUsage">0kb</span></p>
<p class="is-hidden has-text-warning noTor">You do not seem to be able to reach .onion services. Please use Tor Browser or Brave Browser's private tabs.</p>
</div>
</section>
<div class="container messageCreator">
<div class="columns">
<div class="column is-two-fifths">
<div class="postForm">
<div class="control">
<textarea class="textarea" placeholder="Normal textarea"></textarea>
</div>
</div>
</div>
<div class="column">
</div>
</div>
<div class="control">
<a class="button is-primary">Create Post</a>
</div>
</div>
<div class="container messageFeed">
</div>
<template id="cMsgTemplate">
@ -46,7 +66,6 @@
Message
</div>
<div class="column cAuthor is-narrow"></div>
<!--<img class="identicon image is-48x48" alt="user icon" src="/shared/images/anon.svg">-->
<div class="column is-narrow cMsgDate">
Date
</div>

6
marked.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -17,14 +17,22 @@
*/
self.addEventListener('message', function(e) {
self.addEventListener('message', async function(e) {
let lookupPeer = async function(peer){
let newList = await fetch('http://')
let newList = await (await fetch('http://' + peer + '.onion/pex')).text()
newList = newList.replace('.onion', '')
return newList.split(',')
}
var data = JSON.parse(e.data)
let peers = data['nodeList']
lookupPeer(peers)
let peer = data['node']
let peerList = await lookupPeer(peer)
peerList.forEach(node => {
if (node){
postMessage(node)
}
})
}
)

7
style.css Normal file
View File

@ -0,0 +1,7 @@
.subtitle img{
padding-left: 5px;
}
.messageCreator{
margin-bottom: 1em;
}

8
tordetect.js Normal file
View File

@ -0,0 +1,8 @@
let torDetect = function(){
let el = document.createElement('img')
el.onerror = function(){
document.getElementsByClassName('noTor')[0].classList.remove('is-hidden')
}
el.src = "https://3g2upl4pq6kufc4m.onion/favicon.ico"
}
setTimeout(torDetect, 1000)

View File

@ -2,6 +2,12 @@ var lookupWorker = new Worker('peer-lookup.js');
lookupWorker.addEventListener('message', function(e) {
console.log('Worker said: ', e.data);
publicNodes.push(e.data)
}, false);
lookupWorker.postMessage(JSON.stringify({"nodeList": publicNodes}))
setInterval(function(){
lookupWorker.postMessage(JSON.stringify({"node": getCurrentNode()}))
}, 60000)
lookupWorker.postMessage(JSON.stringify({"node": getCurrentNode()}))