From e41487f80c7bee8339e753014eca14743551bcbd Mon Sep 17 00:00:00 2001 From: 0Gitnick <36289298+0Gitnick@users.noreply.github.com> Date: Tue, 26 Nov 2019 18:19:28 -0600 Subject: [PATCH 1/4] Simplify replay_timestamp_validation --- src/onionrcrypto/cryptoutils/replayvalidation.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/onionrcrypto/cryptoutils/replayvalidation.py b/src/onionrcrypto/cryptoutils/replayvalidation.py index 59c338eb..fc8e8fd0 100644 --- a/src/onionrcrypto/cryptoutils/replayvalidation.py +++ b/src/onionrcrypto/cryptoutils/replayvalidation.py @@ -1,6 +1,3 @@ from onionrutils import epoch def replay_timestamp_validation(timestamp): - if epoch.get_epoch() - int(timestamp) > 2419200: - return False - else: - return True \ No newline at end of file + return epoch.get_epoch() - int(timestamp) <= 2419200 \ No newline at end of file From 6f8a843e49a64bff2a8f3e88ffafcfbfbbfb0038 Mon Sep 17 00:00:00 2001 From: 0Gitnick <36289298+0Gitnick@users.noreply.github.com> Date: Tue, 26 Nov 2019 18:34:10 -0600 Subject: [PATCH 2/4] Make peer cleanup timings smoother --- src/onionrpeers/peercleanup.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/onionrpeers/peercleanup.py b/src/onionrpeers/peercleanup.py index c332828b..6173a041 100644 --- a/src/onionrpeers/peercleanup.py +++ b/src/onionrpeers/peercleanup.py @@ -41,10 +41,8 @@ def peer_cleanup(): if peerprofiles.PeerProfiles(address).score < min_score: keydb.removekeys.remove_address(address) try: - if (int(epoch.get_epoch()) - int(keydb.transportinfo.get_address_info(address, 'lastConnect'))) >= 600: - expireTime = 600 - else: - expireTime = 86400 + lastConnect = int(keydb.transportinfo.get_address_info(address, 'lastConnect')) + expireTime = 86400 - int(epoch.get_epoch()) - lastConnect blacklist.addToDB(address, dataType=1, expire=expireTime) except sqlite3.IntegrityError: #TODO just make sure its not a unique constraint issue pass From e7c826d4c62b5870e6dd51bcbd9a0365c2bc8971 Mon Sep 17 00:00:00 2001 From: 0Gitnick <36289298+0Gitnick@users.noreply.github.com> Date: Tue, 26 Nov 2019 21:46:45 -0600 Subject: [PATCH 3/4] Clean up board.js syntax --- static-data/www/board/board.js | 79 ++++++++++++++++------------------ 1 file changed, 36 insertions(+), 43 deletions(-) diff --git a/static-data/www/board/board.js b/static-data/www/board/board.js index c3d7f556..12b2e89f 100755 --- a/static-data/www/board/board.js +++ b/static-data/www/board/board.js @@ -17,10 +17,6 @@ along with this program. If not, see . */ -requested = [] - -var windowHeight = window.innerHeight; -webpassword = webpass newPostForm = document.getElementById('addMsg') firstLoad = true lastLoadedBoard = 'global' @@ -30,14 +26,12 @@ loadingTimeout = 8000 let toggleLoadingMessage = function(){ switch (loadingMessage.style.display){ - case "block": - case "inline": case "inline-block": loadingMessage.style.display = "none" - break; + break; default: loadingMessage.style.display = "initial" - break; + break; } } @@ -45,37 +39,36 @@ fetch('/flow/version', { method: 'GET', headers: { "token": webpass - }}) -.then((resp) => resp.text()) -.then(function(data) { - document.getElementById('circlesVersion').innerText = data +}}) +.then((ver) => ver.text()) +.then(function(ver) { + document.getElementById('circlesVersion').innerText = ver }) -function appendMessages(msg, blockHash, beforeHash, channel){ - if (channel !== document.getElementById('feedIDInput').value){return} +function appendMessages(msg, blockHash, beforeHash, channel) { + if (channel !== document.getElementById('feedIDInput').value) return // ignore if channel name isn't matching + if (msg.length == 0) return // ignore empty messages + var humanDate = new Date(0) - if (msg.length == 0){ - return - } - //var msg = JSON.parse(msg) - var el = document.createElement('div') var msgDate = msg['meta']['time'] var feed = document.getElementById("feed") var beforeEl = null if (msgDate === undefined){ msgDate = 'unknown' - } - else{ + } else { humanDate.setUTCSeconds(msgDate) msgDate = humanDate.toLocaleTimeString() + ' ' + humanDate.toLocaleDateString() } + + var el = document.createElement('div') el.className = 'entry' el.innerText = msg['content'] - if (beforeHash !== null){ - for (x = 0; x < feed.children.length; x++){ - if (feed.children[x].getAttribute('data-bl') === beforeHash){ - beforeEl = feed.children[x] + + if (beforeHash !== null) { + for (i = 0; i < feed.children.length; i++) { + if (feed.children[i].getAttribute('data-bl') === beforeHash) { + beforeEl = feed.children[i] } } } @@ -133,12 +126,13 @@ function getBlocks(){ var feed = document.getElementById("feed") var ch = document.getElementById('feedIDInput').value if (lastLoadedBoard !== ch){ + requested = [] + toggleLoadingMessage() loadedAny = false - while (feed.firstChild) { - feed.removeChild(feed.firstChild); - } - requested = [] // reset requested list + + while (feed.firstChild) feed.removeChild(feed.firstChild); // remove all messages from feed + setTimeout(function(){ if (! loadedAny && ch == document.getElementById('feedIDInput').value){ PNotify.notice("There are no posts for " + ch + ". You can be the first!") @@ -156,13 +150,12 @@ function getBlocks(){ var blockList = feedText.split(',') for (i = 0; i < blockList.length; i++){ - while (blockList[i].length < 64) { blockList[i] = "0" + blockList[i] } + blockList[i] = "0".repeat(64 - blockList[i].length) + blockList[i] // pad hash with zeroes + if (! requested.includes(blockList[i])){ - if (blockList[i].length == 0){ - continue - } - requested.push(blockList[i]) - loadMessage(blockList[i], blockList, i, ch) + if (blockList[i].length == 0) continue + else requested.push(blockList[i]) + loadMessage(blockList[i], blockList, i, ch); } } } @@ -172,15 +165,14 @@ function loadMessage(blockHash, blockList, count, channel){ method: 'GET', headers: { "token": webpass - }}) + }}) .then((resp) => resp.json()) .then(function(data) { let before = blockList[count - 1] let delay = 2000 if (typeof before == "undefined"){ before = null - } - else{ + } else { let existing = document.getElementsByClassName('cMsgBox') for (x = 0; x < existing.length; x++){ if (existing[x].getAttribute('data-bl') === before){ @@ -190,10 +182,10 @@ function loadMessage(blockHash, blockList, count, channel){ } setTimeout(function(){appendMessages(data, blockHash, before, channel)}, delay) //appendMessages(data, blockHash, before) - }) + }) } -document.getElementById('refreshFeed').onclick = function(){ +document.getElementById('refreshFeed').onclick = function() { getBlocks() } @@ -211,7 +203,8 @@ newPostForm.onsubmit = function(){ headers: { "content-type": "application/json", "token": webpass - }}) + } + }) .then((resp) => resp.text()) // Transform the data into json .then(function(data) { newPostForm.style.display = 'block' @@ -223,8 +216,8 @@ newPostForm.onsubmit = function(){ } PNotify.success({ text: "Message queued for posting" - }) + }) setTimeout(function(){getBlocks()}, 500) - }) + }) return false } From d4ae6255d62be4a159532b98def979a07131ab2e Mon Sep 17 00:00:00 2001 From: 0Gitnick <36289298+0Gitnick@users.noreply.github.com> Date: Tue, 26 Nov 2019 22:11:08 -0600 Subject: [PATCH 4/4] Improve syntax --- src/apiservers/private/__init__.py | 5 +---- src/utils/reconstructhash.py | 12 +++--------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/apiservers/private/__init__.py b/src/apiservers/private/__init__.py index ca7587df..a7c07bd6 100644 --- a/src/apiservers/private/__init__.py +++ b/src/apiservers/private/__init__.py @@ -76,10 +76,7 @@ class PrivateAPI: logger.error("client password needs to be set") return False try: - if not hmac.compare_digest(self.clientToken, token): - return False - else: - return True + return hmac.compare_digest(self.clientToken, token) except TypeError: return False diff --git a/src/utils/reconstructhash.py b/src/utils/reconstructhash.py index ae50c0b2..c50840cb 100644 --- a/src/utils/reconstructhash.py +++ b/src/utils/reconstructhash.py @@ -21,7 +21,6 @@ def reconstruct_hash(hex_hash, length=64): return hex_hash.zfill(length) def deconstruct_hash(hex_hash): - new_hash = '' ret_bytes = False try: hex_hash = hex_hash.decode() @@ -29,15 +28,10 @@ def deconstruct_hash(hex_hash): except AttributeError: pass - c = 0 - for x in hex_hash: - if x == '0': - c += 1 - else: - break - new_hash = hex_hash[c:] + num_zeroes = hex_hash.count('0') + new_hash = hex_hash[num_zeroes:] if ret_bytes: - new_hash = new_hash.encode() + return new_hash \ No newline at end of file