From f99ff2700676e5184e6763ea831abb45ae174bba Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Tue, 10 Sep 2019 01:05:59 -0500 Subject: [PATCH] onionr can now run from relative dirs and lots of bug fixes --- onionr.sh | 2 ++ onionr/communicator/__init__.py | 2 +- onionr/etc/onionrvalues.py | 1 + onionr/httpapi/miscclientapi/endpoints.py | 2 ++ onionr/onionrblockapi.py | 3 +-- onionr/onionrblocks/insert.py | 5 +++-- onionr/onionrcommands/filecommands.py | 21 +++++++++++++------ onionr/onionrcommands/softreset.py | 1 + onionr/onionrcrypto/signing/__init__.py | 1 + onionr/static-data/www/mail/index.html | 2 +- onionr/static-data/www/mail/mail.js | 12 ++++++++++- .../static-data/www/mail/sethumanreadable.js | 15 +++++++++++-- 12 files changed, 52 insertions(+), 15 deletions(-) diff --git a/onionr.sh b/onionr.sh index 9360e48b..d7114740 100755 --- a/onionr.sh +++ b/onionr.sh @@ -1,4 +1,6 @@ #!/bin/sh +ORIG_ONIONR_RUN_DIR=`pwd` +export ORIG_ONIONR_RUN_DIR cd "$(dirname "$0")" cd onionr/ ./__init__.py "$@" diff --git a/onionr/communicator/__init__.py b/onionr/communicator/__init__.py index fb650205..db169f5c 100755 --- a/onionr/communicator/__init__.py +++ b/onionr/communicator/__init__.py @@ -108,7 +108,7 @@ class OnionrCommunicatorDaemon: # Timers to periodically lookup new blocks and download them lookup_blocks_timer = OnionrCommunicatorTimers(self, lookupblocks.lookup_blocks_from_communicator, config.get('timers.lookupBlocks', 25), myArgs=[self], requiresPeer=True, maxThreads=1) # The block download timer is accessed by the block lookup function to trigger faster download starts - self.download_blocks_timer = OnionrCommunicatorTimers(self, self.getBlocks, config.get('timers.getBlocks', 30), requiresPeer=True, maxThreads=5) + self.download_blocks_timer = OnionrCommunicatorTimers(self, self.getBlocks, config.get('timers.getBlocks', 10), requiresPeer=True, maxThreads=5) # Timer to reset the longest offline peer so contact can be attempted again OnionrCommunicatorTimers(self, onlinepeers.clear_offline_peer, 58, myArgs=[self]) diff --git a/onionr/etc/onionrvalues.py b/onionr/etc/onionrvalues.py index f2bcbc17..6fb225bc 100755 --- a/onionr/etc/onionrvalues.py +++ b/onionr/etc/onionrvalues.py @@ -29,6 +29,7 @@ DEVELOPMENT_MODE = True MAX_BLOCK_TYPE_LENGTH = 15 MAX_BLOCK_CLOCK_SKEW = 120 MAIN_PUBLIC_KEY_SIZE = 32 +ORIG_RUN_DIR_ENV_VAR = 'ORIG_ONIONR_RUN_DIR' # Begin OnionrValues migrated values ANNOUNCE_POW = 5 diff --git a/onionr/httpapi/miscclientapi/endpoints.py b/onionr/httpapi/miscclientapi/endpoints.py index a87310a4..b5a0e7eb 100644 --- a/onionr/httpapi/miscclientapi/endpoints.py +++ b/onionr/httpapi/miscclientapi/endpoints.py @@ -18,6 +18,7 @@ along with this program. If not, see . ''' from flask import Response, Blueprint, request, send_from_directory, abort +import unpaddedbase32 from httpapi import apiutils import onionrcrypto, config @@ -111,6 +112,7 @@ class PrivateEndpoints: @private_endpoints_bp.route('/getHumanReadable/') def getHumanReadable(name): + name = unpaddedbase32.repad(bytesconverter.str_to_bytes(name)) return Response(mnemonickeys.get_human_readable_ID(name)) @private_endpoints_bp.route('/getBase32FromHumanReadable/') diff --git a/onionr/onionrblockapi.py b/onionr/onionrblockapi.py index 4e989099..2d50d686 100755 --- a/onionr/onionrblockapi.py +++ b/onionr/onionrblockapi.py @@ -124,8 +124,7 @@ class Block: ''' Verify if a block's signature is signed by its claimed signer ''' - - if signing.ed_verify(data=self.signedData, key=self.signer, sig=self.signature, encodedData=True): + if self.signer is None or signing.ed_verify(data=self.signedData, key=self.signer, sig=self.signature, encodedData=True): self.validSig = True else: self.validSig = False diff --git a/onionr/onionrblocks/insert.py b/onionr/onionrblocks/insert.py index bc9ab158..6297d317 100644 --- a/onionr/onionrblocks/insert.py +++ b/onionr/onionrblocks/insert.py @@ -114,8 +114,9 @@ def insert_block(data: Union[str, bytes], header: str ='txt', # compile metadata metadata['meta'] = jsonMeta - metadata['sig'] = signature - metadata['signer'] = signer + if len(signature) > 0: # I don't like not pattern + metadata['sig'] = signature + metadata['signer'] = signer metadata['time'] = createTime # ensure expire is integer and of sane length diff --git a/onionr/onionrcommands/filecommands.py b/onionr/onionrcommands/filecommands.py index 953abb2e..6ac0383a 100755 --- a/onionr/onionrcommands/filecommands.py +++ b/onionr/onionrcommands/filecommands.py @@ -21,8 +21,15 @@ import base64, sys, os import logger from onionrblockapi import Block +import onionrexceptions from onionrutils import stringvalidators +from etc import onionrvalues from onionrblocks import insert +_ORIG_DIR = onionrvalues.ORIG_RUN_DIR_ENV_VAR + +def _get_dir(path: str)->str: + if not os.getenv(_ORIG_DIR) is None: return os.getenv(_ORIG_DIR) + '/' + path + else: return path def add_html(singleBlock=True, blockType='html'): add_file(singleBlock, blockType) @@ -35,13 +42,12 @@ def add_file(singleBlock=False, blockType='bin'): if len(sys.argv) >= 3: filename = sys.argv[2] contents = None - - if not os.path.exists(filename): + if not os.path.exists(_get_dir(filename)): logger.error('That file does not exist. Improper path (specify full path)?', terminal=True) return logger.info('Adding file... this might take a long time.', terminal=True) try: - with open(filename, 'rb') as singleFile: + with open(_get_dir(filename), 'rb') as singleFile: blockhash = insert(base64.b64encode(singleFile.read()), header=blockType) if len(blockhash) > 0: logger.info('File %s saved in block %s' % (filename, blockhash), terminal=True) @@ -55,7 +61,7 @@ def get_file(): Get a file from onionr blocks ''' try: - fileName = sys.argv[2] + fileName = _get_dir(sys.argv[2]) bHash = sys.argv[3] except IndexError: logger.error("Syntax %s %s" % (sys.argv[0], '/path/to/filename '), terminal=True) @@ -70,6 +76,9 @@ def get_file(): logger.error('Block hash is invalid', terminal=True) return - with open(fileName, 'wb') as myFile: - myFile.write(base64.b64decode(Block(bHash).bcontent)) + try: + with open(fileName, 'wb') as myFile: + myFile.write(base64.b64decode(Block(bHash).bcontent)) + except onionrexceptions.NoDataAvailable: + logger.error('That block is not available. Trying again later may work.', terminal=True) return \ No newline at end of file diff --git a/onionr/onionrcommands/softreset.py b/onionr/onionrcommands/softreset.py index 4dfcb273..68f5f96e 100644 --- a/onionr/onionrcommands/softreset.py +++ b/onionr/onionrcommands/softreset.py @@ -31,6 +31,7 @@ def soft_reset(): path = filepaths.block_data_location shutil.rmtree(path) os.remove(dbfiles.block_meta_db) + os.remove(filepaths.upload_list) logger.info("Soft reset Onionr", terminal=True) soft_reset.onionr_help = "Deletes Onionr blocks and their associated metadata, except for any exported block files." \ No newline at end of file diff --git a/onionr/onionrcrypto/signing/__init__.py b/onionr/onionrcrypto/signing/__init__.py index a62f5ba9..3562efe0 100644 --- a/onionr/onionrcrypto/signing/__init__.py +++ b/onionr/onionrcrypto/signing/__init__.py @@ -4,6 +4,7 @@ import unpaddedbase32 import nacl.encoding, nacl.signing, nacl.exceptions from onionrutils import bytesconverter +from onionrutils import mnemonickeys import logger def ed_sign(data, key, encodeResult=False): '''Ed25519 sign data''' diff --git a/onionr/static-data/www/mail/index.html b/onionr/static-data/www/mail/index.html index cf04e60d..c1802a77 100755 --- a/onionr/static-data/www/mail/index.html +++ b/onionr/static-data/www/mail/index.html @@ -59,7 +59,7 @@ Mail

- Send email style messages + Private and safe messages

diff --git a/onionr/static-data/www/mail/mail.js b/onionr/static-data/www/mail/mail.js index 29870051..9d1ac19b 100755 --- a/onionr/static-data/www/mail/mail.js +++ b/onionr/static-data/www/mail/mail.js @@ -26,6 +26,7 @@ threadContent = {} replyBtn = document.getElementById('replyBtn') addUnknownContact = document.getElementById('addUnknownContact') noInbox = document.getElementById('noInbox') +humanReadableCache = {} function addContact(pubkey, friendName){ fetch('/friends/add/' + pubkey, { @@ -61,7 +62,14 @@ function openReply(bHash, quote, subject){ for (var x = 0; x < splitQuotes.length; x++){ splitQuotes[x] = '> ' + splitQuotes[x] } - quote = '\n' + key.substring(0, 12) + ' wrote:' + '\n' + splitQuotes.join('\n') + + if (typeof humanReadableCache[key] != 'undefined'){ + document.getElementById('draftID').value = humanReadableCache[key] + quote = '\n' + humanReadableCache[key].split(' ').slice(0,3).join(' ') + ' wrote:\n' + splitQuotes.join('\n') + } + else{ + quote = '\n' + key.substring(0, 12) + ' wrote:' + '\n' + splitQuotes.join('\n') + } document.getElementById('draftText').value = quote setActiveTab('compose') } @@ -184,6 +192,7 @@ function loadInboxEntries(bHash){ entry.setAttribute('data-nameSet', false) } else{ + loadHumanReadableToCache(resp['meta']['signer']) senderInput.value = name entry.setAttribute('data-nameSet', true) } @@ -295,6 +304,7 @@ function getSentbox(){ sentDate.innerText = humanDate.substring(0, humanDate.indexOf('(')) if (resp[i]['name'] == null || resp[i]['name'].toLowerCase() == 'anonymous'){ toEl.value = resp[i]['peer'] + setHumanReadableValue(toEl, resp[i]['peer']) } else{ toEl.value = resp[i]['name'] diff --git a/onionr/static-data/www/mail/sethumanreadable.js b/onionr/static-data/www/mail/sethumanreadable.js index 83279acd..24e72428 100644 --- a/onionr/static-data/www/mail/sethumanreadable.js +++ b/onionr/static-data/www/mail/sethumanreadable.js @@ -1,10 +1,21 @@ -function setHumanReadableValue(el, key){ +function loadHumanReadableToCache(key){ fetch('/getHumanReadable/' + key, { headers: { "token": webpass }}) .then((resp) => resp.text()) .then(function(resp) { - el.value = resp + humanReadableCache[key] = resp }) +} + +function setHumanReadableValue(el, key){ + if (typeof humanReadableCache[key] != 'undefined'){ + el.value = humanReadableCache[key] + return + } + else{ + setTimeout(function(){setHumanReadableValue(el, key)}) + return + } } \ No newline at end of file