added topic support, apiGET timeout
This commit is contained in:
parent
7056475e17
commit
9c352e1917
20
hush-hush.js
20
hush-hush.js
@ -17,14 +17,15 @@
|
|||||||
*/
|
*/
|
||||||
var findMessageIntervalTime = 5000
|
var findMessageIntervalTime = 5000
|
||||||
var publicNodes = [
|
var publicNodes = [
|
||||||
"4wbarqtxh6zasoxxftakrellcjzztcib7bqth4u3igof5fskrrvrk4yd",
|
"ty3rq3kub6gvzpngnrvfsk5emhoh2ltkti62u4ophca6ijciajotqqid"
|
||||||
"zl67stwxpjkntaxcfdhj3dvayculoojju6eek2jhsrdz6uwf224o6oqd"
|
|
||||||
]
|
]
|
||||||
var messageHashes = []
|
var messageHashes = []
|
||||||
var blocks = []
|
var blocks = []
|
||||||
var basicTextEncoder = new TextEncoder()
|
var basicTextEncoder = new TextEncoder()
|
||||||
var difficulty = "0000"
|
var difficulty = "0000"
|
||||||
var maxBlockAge = 2678400
|
var maxBlockAge = 2678400
|
||||||
|
var postTopic = 'kic' // we use block types as the topic with 'kic' as the prefix
|
||||||
|
var lastLookup = Math.floor((Date.now() / 1000)) - maxBlockAge
|
||||||
|
|
||||||
|
|
||||||
function shuffleArray(array) {
|
function shuffleArray(array) {
|
||||||
@ -84,7 +85,7 @@ function addMessage(message, timestamp){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
message = DOMPurify.sanitize(marked(message),
|
message = DOMPurify.sanitize(message,
|
||||||
{FORBID_ATTR: ['style'],
|
{FORBID_ATTR: ['style'],
|
||||||
ALLOWED_TAGS: ['b', 'p', 'em', 'i', 'a', 'strong', 'sub', 'small', 'ul', 'li', 'ol', 'strike',
|
ALLOWED_TAGS: ['b', 'p', 'em', 'i', 'a', 'strong', 'sub', 'small', 'ul', 'li', 'ol', 'strike',
|
||||||
'tr', 'td', 'th', 'table', 'thead', 'tfoot', 'colgroup', 'col', 'caption', 'marquee',
|
'tr', 'td', 'th', 'table', 'thead', 'tfoot', 'colgroup', 'col', 'caption', 'marquee',
|
||||||
@ -107,13 +108,19 @@ function addMessage(message, timestamp){
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function apiGET(path, queryString, raw=false){
|
async function apiGET(path, queryString, raw=false){
|
||||||
let response = await fetch("http://" + getCurrentNode() + ".onion/" + encodeURIComponent(path) + queryString)
|
let nodeToUse = getCurrentNode()
|
||||||
|
let requestTimeout = setTimeout(function(){
|
||||||
|
console.debug(nodeToUse + " timed out")
|
||||||
|
publicNodes = publicNodes.filter(item => item !== nodeToUse)
|
||||||
|
}, 10000)
|
||||||
|
let response = await fetch("http://" + nodeToUse + ".onion/" + encodeURIComponent(path) + queryString)
|
||||||
|
|
||||||
if (response.ok) { // if HTTP-status is 200-299
|
if (response.ok) { // if HTTP-status is 200-299
|
||||||
// get the response body (the method explained below)
|
// get the response body (the method explained below)
|
||||||
if (raw){
|
if (raw){
|
||||||
return await response.blob()
|
return await response.blob()
|
||||||
}
|
}
|
||||||
|
clearTimeout(requestTimeout)
|
||||||
return await response.text()
|
return await response.text()
|
||||||
} else {
|
} else {
|
||||||
console.debug("HTTP-Error: " + response.status)
|
console.debug("HTTP-Error: " + response.status)
|
||||||
@ -125,7 +132,8 @@ async function findMessages(){
|
|||||||
setTimeout(function(){findMessages()}, 1000)
|
setTimeout(function(){findMessages()}, 1000)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let messages = (await apiGET("getblocklist", "?type=kic")).split('\n')
|
let messages = (await apiGET("getblocklist", "?type=" + postTopic + "&date=" + lastLookup)).split('\n')
|
||||||
|
lastLookup = Math.floor((Date.now() / 1000))
|
||||||
messages.forEach(block => {
|
messages.forEach(block => {
|
||||||
if (!block) { return}
|
if (!block) { return}
|
||||||
block = reconstructHash(block)
|
block = reconstructHash(block)
|
||||||
@ -141,7 +149,7 @@ async function findMessages(){
|
|||||||
try{
|
try{
|
||||||
var metadata = JSON.parse(d.split("\n")[0])
|
var metadata = JSON.parse(d.split("\n")[0])
|
||||||
// Make sure the block is an actual post so nodes can't send us stuff unrelated to dapp
|
// Make sure the block is an actual post so nodes can't send us stuff unrelated to dapp
|
||||||
if (JSON.parse(metadata['meta'])['type'] !== 'kic'){
|
if (JSON.parse(metadata['meta'])['type'] !== postTopic){
|
||||||
throw new Error("Not correct block type: " + block)
|
throw new Error("Not correct block type: " + block)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,11 @@
|
|||||||
<p>kiccan is a message board program that utilizes the <a href="https://onionr.net/">Onionr</a> network.</p>
|
<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>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>⚠️ the host of this web page has no control over posts. Browse and post at your own risk.</p>
|
||||||
|
<br>
|
||||||
|
<label class="checkbox">
|
||||||
|
<input type="checkbox" id="use-markdown" checked>
|
||||||
|
write using markdown/HTML
|
||||||
|
</label>
|
||||||
<p class="is-pulled-right">feed size: <span id="memUsage">0kb</span> | page size: 289kb</p>
|
<p class="is-pulled-right">feed size: <span id="memUsage">0kb</span> | page size: 289kb</p>
|
||||||
<p class="is-hidden has-text-warning noTor">You do not seem to be able to reach .onion services. Please use Tor Browser.</p>
|
<p class="is-hidden has-text-warning noTor">You do not seem to be able to reach .onion services. Please use Tor Browser.</p>
|
||||||
</div>
|
</div>
|
||||||
|
@ -19,13 +19,17 @@ document.getElementById("createMessageBtn").onclick = async function(){
|
|||||||
document.getElementById("createMessageBtn").setAttribute("disabled", true)
|
document.getElementById("createMessageBtn").setAttribute("disabled", true)
|
||||||
|
|
||||||
let field = document.getElementById("postMessageField")
|
let field = document.getElementById("postMessageField")
|
||||||
|
var postMessage = field.value
|
||||||
|
if (document.getElementById('use-markdown').checked){
|
||||||
|
postMessage = marked(postMessage)
|
||||||
|
}
|
||||||
|
|
||||||
let payload = {
|
let payload = {
|
||||||
"metadata": {
|
"metadata": {
|
||||||
'time': Math.floor((Date.now() / 1000)),
|
'time': Math.floor((Date.now() / 1000)),
|
||||||
'meta': JSON.stringify({'type': 'kic', 'ch': 'global'})
|
'meta': JSON.stringify({'type': postTopic})
|
||||||
},
|
},
|
||||||
"data": field.value,
|
"data": postMessage,
|
||||||
"difficulty": difficulty.length / 2
|
"difficulty": difficulty.length / 2
|
||||||
}
|
}
|
||||||
powWorker.postMessage(JSON.stringify(payload))
|
powWorker.postMessage(JSON.stringify(payload))
|
||||||
|
Loading…
Reference in New Issue
Block a user