fixes and plaintext support aware
This commit is contained in:
parent
14c8aac76b
commit
760f99d864
@ -17,13 +17,15 @@
|
|||||||
*/
|
*/
|
||||||
var findMessageIntervalTime = 5000
|
var findMessageIntervalTime = 5000
|
||||||
var publicNodes = [
|
var publicNodes = [
|
||||||
"ty3rq3kub6gvzpngnrvfsk5emhoh2ltkti62u4ophca6ijciajotqqid",
|
"ueawiiskhaxdhkqjvgz6drrlf7srvaifrewnb6rxf6tro3welajvlgyd",
|
||||||
"aekulizc7lsl4bh3ainlnnbfqtboavitfxuvanqq3eyposdifzzeddqd"
|
"csb2thc5yzv2gbhoozbqrzv747irs5z2lbpd7eiyh6eivvltok76qrqd"
|
||||||
]
|
]
|
||||||
|
var initialNodes = JSON.parse(JSON.stringify(publicNodes))
|
||||||
|
publicNodes = []
|
||||||
var messageHashes = []
|
var messageHashes = []
|
||||||
var blocks = []
|
var blocks = []
|
||||||
var basicTextEncoder = new TextEncoder()
|
var basicTextEncoder = new TextEncoder()
|
||||||
var difficulty = "0000"
|
var difficulty = "00000"
|
||||||
var maxBlockAge = 2678400
|
var maxBlockAge = 2678400
|
||||||
var postTopic = 'kic' // we use block types as the topic with 'kic' as the prefix
|
var postTopic = 'kic' // we use block types as the topic with 'kic' as the prefix
|
||||||
var lastLookup = Math.floor((Date.now() / 1000)) - maxBlockAge
|
var lastLookup = Math.floor((Date.now() / 1000)) - maxBlockAge
|
||||||
@ -36,7 +38,7 @@ function shuffleArray(array) {
|
|||||||
[array[i], array[j]] = [array[j], array[i]];
|
[array[i], array[j]] = [array[j], array[i]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
shuffleArray(publicNodes)
|
shuffleArray(initialNodes)
|
||||||
|
|
||||||
//https://stackoverflow.com/q/10420352
|
//https://stackoverflow.com/q/10420352
|
||||||
function getReadableFileSizeString(fileSizeInBytes) {
|
function getReadableFileSizeString(fileSizeInBytes) {
|
||||||
@ -50,20 +52,9 @@ function getReadableFileSizeString(fileSizeInBytes) {
|
|||||||
return Math.max(fileSizeInBytes, 0.1).toFixed(1) + byteUnits[i];
|
return Math.max(fileSizeInBytes, 0.1).toFixed(1) + byteUnits[i];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
setInterval(function(){shuffleArray(publicNodes)}, 5000)
|
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(){
|
function getCurrentNode(){
|
||||||
// Very basic round-robin use of nodes
|
// Very basic round-robin use of nodes
|
||||||
let current = publicNodes.shift()
|
let current = publicNodes.shift()
|
||||||
@ -132,6 +123,7 @@ async function apiGET(path, queryString, raw=false){
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function findMessages(){
|
async function findMessages(){
|
||||||
|
|
||||||
findMessageIntervalTime = 5000
|
findMessageIntervalTime = 5000
|
||||||
if (document.hidden){
|
if (document.hidden){
|
||||||
findMessageIntervalTime = 10000
|
findMessageIntervalTime = 10000
|
||||||
|
@ -30,7 +30,7 @@ document.getElementById("createMessageBtn").onclick = async function(){
|
|||||||
'meta': JSON.stringify({'type': postTopic})
|
'meta': JSON.stringify({'type': postTopic})
|
||||||
},
|
},
|
||||||
"data": postMessage,
|
"data": postMessage,
|
||||||
"difficulty": difficulty.length / 2
|
"difficulty": 5
|
||||||
}
|
}
|
||||||
powWorker.postMessage(JSON.stringify(payload))
|
powWorker.postMessage(JSON.stringify(payload))
|
||||||
|
|
||||||
|
@ -15,12 +15,12 @@
|
|||||||
You should have received a copy of the GNU Affero General Public License
|
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/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
importScripts("jspow/index.js")
|
importScripts("/jspow/index.js")
|
||||||
importScripts("sha3.js")
|
importScripts("/js/sha3.js")
|
||||||
|
|
||||||
self.addEventListener('message', function(e) {
|
self.addEventListener('message', function(e) {
|
||||||
var data = JSON.parse(e.data)
|
var data = JSON.parse(e.data)
|
||||||
console.debug(data.metadata)
|
|
||||||
self.postMessage(
|
self.postMessage(
|
||||||
doPow(
|
doPow(
|
||||||
data.metadata,
|
data.metadata,
|
||||||
|
@ -15,19 +15,31 @@
|
|||||||
You should have received a copy of the GNU Affero General Public License
|
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/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
var lookupWorker = new Worker('peer-lookup.js');
|
var lookupWorker = new Worker('/js/peer-lookup.js')
|
||||||
var powWorker = new Worker("powworker.js")
|
var powWorker = new Worker("/js/powworker.js")
|
||||||
|
|
||||||
lookupWorker.addEventListener('message', function(e) {
|
lookupWorker.addEventListener('message', function(e) {
|
||||||
if (publicNodes.includes(e.data)){
|
let addPeer = async function(p){
|
||||||
return
|
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);
|
}, false);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
setInterval(function(){
|
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)
|
}, 60000)
|
||||||
lookupWorker.postMessage(JSON.stringify({"node": getCurrentNode()}))
|
lookupWorker.postMessage(JSON.stringify({"node": initialNodes[0]}))
|
||||||
|
@ -46,19 +46,20 @@ function doPow(metadata, data, difficulty){
|
|||||||
data = encoder.encode(data)
|
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){
|
while (true){
|
||||||
var difficultyCounter = 0
|
difficultyCounter = 0
|
||||||
let metadataString = encoder.encode(JSON.stringify(metadata) + "\n")
|
let metadataString = encoder.encode(JSON.stringify(metadata) + "\n")
|
||||||
let arr = new Uint8Array(metadataString.length + data.length)
|
let arr = new Uint8Array(metadataString.length + data.length)
|
||||||
arr.set(metadataString)
|
arr.set(metadataString)
|
||||||
arr.set(data, metadataString.length)
|
arr.set(data, metadataString.length)
|
||||||
hash = doHash(arr)
|
hash = doHashHex(arr)
|
||||||
|
|
||||||
for (var i = 0; i < hash.length; i++){
|
for (var i = 0; i < difficulty; i++){
|
||||||
if (hash[i] == 0){
|
if (hash[i] == '0'){
|
||||||
difficultyCounter += 1
|
difficultyCounter += 1
|
||||||
if (difficultyCounter === difficulty){
|
if (difficultyCounter === difficulty){
|
||||||
return arr
|
return arr
|
||||||
@ -69,6 +70,9 @@ function doPow(metadata, data, difficulty){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
metadata['c'] += 1
|
metadata['c'] += 1
|
||||||
|
if (metadata['c'] % 10000 == 0){
|
||||||
|
console.debug(metadata['c'])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user