diff --git a/js/hush-hush.js b/js/hush-hush.js index f7722a0..afef0d3 100644 --- a/js/hush-hush.js +++ b/js/hush-hush.js @@ -17,13 +17,15 @@ */ var findMessageIntervalTime = 5000 var publicNodes = [ - "ty3rq3kub6gvzpngnrvfsk5emhoh2ltkti62u4ophca6ijciajotqqid", - "aekulizc7lsl4bh3ainlnnbfqtboavitfxuvanqq3eyposdifzzeddqd" + "ueawiiskhaxdhkqjvgz6drrlf7srvaifrewnb6rxf6tro3welajvlgyd", + "csb2thc5yzv2gbhoozbqrzv747irs5z2lbpd7eiyh6eivvltok76qrqd" ] +var initialNodes = JSON.parse(JSON.stringify(publicNodes)) +publicNodes = [] var messageHashes = [] var blocks = [] var basicTextEncoder = new TextEncoder() -var difficulty = "0000" +var difficulty = "00000" 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 @@ -36,7 +38,7 @@ function shuffleArray(array) { [array[i], array[j]] = [array[j], array[i]]; } } -shuffleArray(publicNodes) +shuffleArray(initialNodes) //https://stackoverflow.com/q/10420352 function getReadableFileSizeString(fileSizeInBytes) { @@ -50,20 +52,9 @@ function getReadableFileSizeString(fileSizeInBytes) { 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(node => { - let doPing = async function(){ - let res = await(await fetch("http://" + node + ".onion/ping")).text() - if (res !== "pong!"){ - console.debug(node) - } - } - doPing() -}) - function getCurrentNode(){ // Very basic round-robin use of nodes let current = publicNodes.shift() @@ -132,6 +123,7 @@ async function apiGET(path, queryString, raw=false){ } async function findMessages(){ + findMessageIntervalTime = 5000 if (document.hidden){ findMessageIntervalTime = 10000 diff --git a/js/message-creator.js b/js/message-creator.js index 7b4c041..2b209fc 100644 --- a/js/message-creator.js +++ b/js/message-creator.js @@ -30,7 +30,7 @@ document.getElementById("createMessageBtn").onclick = async function(){ 'meta': JSON.stringify({'type': postTopic}) }, "data": postMessage, - "difficulty": difficulty.length / 2 + "difficulty": 5 } powWorker.postMessage(JSON.stringify(payload)) diff --git a/js/powworker.js b/js/powworker.js index d040792..061e848 100644 --- a/js/powworker.js +++ b/js/powworker.js @@ -15,12 +15,12 @@ You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ -importScripts("jspow/index.js") -importScripts("sha3.js") +importScripts("/jspow/index.js") +importScripts("/js/sha3.js") self.addEventListener('message', function(e) { var data = JSON.parse(e.data) - console.debug(data.metadata) + self.postMessage( doPow( data.metadata, diff --git a/js/worker-handler.js b/js/worker-handler.js index 4c05347..f3fec04 100644 --- a/js/worker-handler.js +++ b/js/worker-handler.js @@ -15,19 +15,31 @@ You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ -var lookupWorker = new Worker('peer-lookup.js'); -var powWorker = new Worker("powworker.js") +var lookupWorker = new Worker('/js/peer-lookup.js') +var powWorker = new Worker("/js/powworker.js") lookupWorker.addEventListener('message', function(e) { - if (publicNodes.includes(e.data)){ - return + let addPeer = async function(p){ + if (await (await fetch("http://" + p + ".onion/plaintext")).text() != "true") + { + console.debug("cannot add " + p) + return + } + if (publicNodes.includes(p) || initialNodes.includes(p)){ + return + } + publicNodes.push(p) } - publicNodes.push(e.data) + addPeer(e.data) }, false); setInterval(function(){ - lookupWorker.postMessage(JSON.stringify({"node": getCurrentNode()})) + let n = getCurrentNode() + if (typeof n == "undefined"){ + n = initialNodes[0] + } + lookupWorker.postMessage(JSON.stringify({"node": n})) }, 60000) -lookupWorker.postMessage(JSON.stringify({"node": getCurrentNode()})) +lookupWorker.postMessage(JSON.stringify({"node": initialNodes[0]})) diff --git a/jspow/index.js b/jspow/index.js index 6dc790e..b7512d6 100644 --- a/jspow/index.js +++ b/jspow/index.js @@ -46,19 +46,20 @@ function doPow(metadata, data, difficulty){ data = encoder.encode(data) } - metadata['c'] = 0 - metadata['n'] = Math.floor(Math.random() * Math.floor(10000)) + Math.floor(Math.random() * Math.floor(10000)) + metadata['c'] = -9999999 + metadata['n'] = Math.floor(Math.random() * Math.floor(10000)) + Math.floor(Math.random() * Math.floor(10000)) + var difficultyCounter = 0 while (true){ - var difficultyCounter = 0 + difficultyCounter = 0 let metadataString = encoder.encode(JSON.stringify(metadata) + "\n") let arr = new Uint8Array(metadataString.length + data.length) arr.set(metadataString) arr.set(data, metadataString.length) - hash = doHash(arr) + hash = doHashHex(arr) - for (var i = 0; i < hash.length; i++){ - if (hash[i] == 0){ + for (var i = 0; i < difficulty; i++){ + if (hash[i] == '0'){ difficultyCounter += 1 if (difficultyCounter === difficulty){ return arr @@ -69,6 +70,9 @@ function doPow(metadata, data, difficulty){ } } metadata['c'] += 1 + if (metadata['c'] % 10000 == 0){ + console.debug(metadata['c']) + } } }