From 122eb4ee5fc4d9bde1d91ad1bfd708b531935b06 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Tue, 25 Jun 2019 19:15:04 -0500 Subject: [PATCH] Finished removing onionrutils for the most part, except for some possible bugs remaining --- onionr/blockimporter.py | 4 ++-- onionr/communicatorutils/announcenode.py | 4 ++-- onionr/communicatorutils/downloadblocks.py | 4 ++-- onionr/communicatorutils/uploadblocks.py | 2 +- onionr/core.py | 7 +++++-- onionr/httpapi/miscpublicapi/announce.py | 4 ++-- onionr/logger.py | 2 +- onionr/onionr.py | 1 - onionr/onionrcommands/daemonlaunch.py | 2 +- onionr/onionrutils/blockmetadata.py | 1 + onionr/onionrutils/localcommand.py | 5 +++-- onionr/onionrutils/validatemetadata.py | 4 ++-- onionr/static-data/default-plugins/pms/main.py | 8 ++++---- 13 files changed, 26 insertions(+), 22 deletions(-) diff --git a/onionr/blockimporter.py b/onionr/blockimporter.py index d5e02e7d..067ec045 100755 --- a/onionr/blockimporter.py +++ b/onionr/blockimporter.py @@ -37,7 +37,7 @@ def importBlockFromData(content, coreInst): metas = blockmetadata.get_block_metadata_from_data(content) # returns tuple(metadata, meta), meta is also in metadata metadata = metas[0] - if validatemetadata(metadata, metas[2]): # check if metadata is valid + if validatemetadata.validate_metadata(coreInst, metadata, metas[2]): # check if metadata is valid if coreInst._crypto.verifyPow(content): # check if POW is enough/correct logger.info('Block passed proof, saving.', terminal=True) try: @@ -46,6 +46,6 @@ def importBlockFromData(content, coreInst): pass else: coreInst.addToBlockDB(blockHash, dataSaved=True) - blockmetadata.process_block_metadata(blockHash) # caches block metadata values to block database + blockmetadata.process_block_metadata(coreInst, blockHash) # caches block metadata values to block database retData = True return retData \ No newline at end of file diff --git a/onionr/communicatorutils/announcenode.py b/onionr/communicatorutils/announcenode.py index f72010f5..38453647 100755 --- a/onionr/communicatorutils/announcenode.py +++ b/onionr/communicatorutils/announcenode.py @@ -20,7 +20,7 @@ import base64 import onionrproofs, logger from etc import onionrvalues -from onionrutils import basicrequests +from onionrutils import basicrequests, bytesconverter def announce_node(daemon): '''Announce our node to our peers''' @@ -53,7 +53,7 @@ def announce_node(daemon): combinedNodes = ourID + peer if ourID != 1: #TODO: Extend existingRand for i2p - existingRand = daemon._core._utils.bytesToStr(daemon._core.getAddressInfo(peer, 'powValue')) + existingRand = bytesconverter.bytes_to_str(daemon._core.getAddressInfo(peer, 'powValue')) # Reset existingRand if it no longer meets the minimum POW if type(existingRand) is type(None) or not existingRand.endswith('0' * ov.announce_pow): existingRand = '' diff --git a/onionr/communicatorutils/downloadblocks.py b/onionr/communicatorutils/downloadblocks.py index ecd7b873..1a159c93 100755 --- a/onionr/communicatorutils/downloadblocks.py +++ b/onionr/communicatorutils/downloadblocks.py @@ -75,7 +75,7 @@ def download_blocks_from_communicator(comm_inst): content = content.decode() # decode here because sha3Hash needs bytes above metas = blockmetadata.get_block_metadata_from_data(content) # returns tuple(metadata, meta), meta is also in metadata metadata = metas[0] - if validatemetadata.validate_metadata(comm_inist._core, metadata, metas[2]): # check if metadata is valid, and verify nonce + if validatemetadata.validate_metadata(comm_inst._core, metadata, metas[2]): # check if metadata is valid, and verify nonce if comm_inst._core._crypto.verifyPow(content): # check if POW is enough/correct logger.info('Attempting to save block %s...' % blockHash[:12]) try: @@ -85,7 +85,7 @@ def download_blocks_from_communicator(comm_inst): removeFromQueue = False else: comm_inst._core.addToBlockDB(blockHash, dataSaved=True) - blockmetadata.process_block_metadata(blockHash) # caches block metadata values to block database + blockmetadata.process_block_metadata(comm_inst._core, blockHash) # caches block metadata values to block database else: logger.warn('POW failed for block %s.' % blockHash) else: diff --git a/onionr/communicatorutils/uploadblocks.py b/onionr/communicatorutils/uploadblocks.py index afe96525..0a3488a8 100755 --- a/onionr/communicatorutils/uploadblocks.py +++ b/onionr/communicatorutils/uploadblocks.py @@ -42,7 +42,7 @@ def upload_blocks_from_communicator(comm_inst): url = 'http://' + peer + '/upload' data = {'block': block.Block(bl).getRaw()} proxyType = proxypicker.pick_proxy(peer) - logger.info("Uploading block to " + peer) + logger.info("Uploading block to " + peer, terminal=True) if not basicrequests.do_post_request(core, url, data=data, proxyType=proxyType) == False: localcommand.local_command(core, 'waitforshare/' + bl, post=True) finishedUploads.append(bl) diff --git a/onionr/core.py b/onionr/core.py index 0d64877a..09d2cc4d 100755 --- a/onionr/core.py +++ b/onionr/core.py @@ -306,7 +306,7 @@ class Core: dateClaimed - timestamp claimed inside the block, only as trustworthy as the block author is expire - expire date for a block ''' - return coredb.blockmetadb.updateblockinfo + return coredb.blockmetadb.updateblockinfo.update_block_info(self, hash, key, data) def insertBlock(self, data, header='txt', sign=False, encryptType='', symKey='', asymPeer='', meta = {}, expire=None, disableForward=False): ''' @@ -437,8 +437,11 @@ class Core: else: # Tell the api server through localCommand to wait for the daemon to upload this block to make statistical analysis more difficult if localcommand.local_command(self, '/ping', maxWait=10) == 'pong!': - localcommand.local_command(self, '/waitforshare/' + retData, post=True, maxWait=5) + if self.config.get('general.security_level', 1) > 0: + localcommand.local_command(self, '/waitforshare/' + retData, post=True, maxWait=5) self.daemonQueueAdd('uploadBlock', retData) + else: + print('shite', localcommand.local_command(self, '/ping', maxWait=10)) self.addToBlockDB(retData, selfInsert=True, dataSaved=True) blockmetadata.process_block_metadata(self, retData) diff --git a/onionr/httpapi/miscpublicapi/announce.py b/onionr/httpapi/miscpublicapi/announce.py index f17c8d83..060bead4 100755 --- a/onionr/httpapi/miscpublicapi/announce.py +++ b/onionr/httpapi/miscpublicapi/announce.py @@ -21,7 +21,7 @@ import base64 from flask import Response import logger from etc import onionrvalues -from onionrutils import stringvalidators +from onionrutils import stringvalidators, bytesconverter def handle_announce(clientAPI, request): ''' @@ -53,7 +53,7 @@ def handle_announce(clientAPI, request): except AttributeError: pass if powHash.startswith('0' * onionrvalues.OnionrValues().announce_pow): - newNode = clientAPI._core._utils.bytesToStr(newNode) + newNode = bytesconverter.bytes_to_str(newNode) if stringvalidators.validate_transport(newNode) and not newNode in clientAPI._core.onionrInst.communicatorInst.newPeers: clientAPI._core.onionrInst.communicatorInst.newPeers.append(newNode) resp = 'Success' diff --git a/onionr/logger.py b/onionr/logger.py index 01ada8ce..1f65d0d4 100755 --- a/onionr/logger.py +++ b/onionr/logger.py @@ -131,7 +131,7 @@ def raw(data, fd = sys.stdout, terminal = False): Outputs raw data to console without formatting ''' - if terminal and (get_settings() & OUTPUT_TO_CONSOLE): + if (get_settings() & OUTPUT_TO_CONSOLE): try: ts = fd.write('%s\n' % data) except OSError: diff --git a/onionr/onionr.py b/onionr/onionr.py index 85a34020..cd16ddfe 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -43,7 +43,6 @@ try: except ImportError: raise Exception("You need the PySocks module (for use with socks5 proxy to use Tor)") - class Onionr: def __init__(self): ''' diff --git a/onionr/onionrcommands/daemonlaunch.py b/onionr/onionrcommands/daemonlaunch.py index 28202c10..c82d13de 100755 --- a/onionr/onionrcommands/daemonlaunch.py +++ b/onionr/onionrcommands/daemonlaunch.py @@ -60,7 +60,7 @@ def daemon(o_inst): logger.debug('Python version %s' % platform.python_version()) if o_inst._developmentMode: - logger.warn('DEVELOPMENT MODE ENABLED', timestamp = False, terminal=True) + logger.warn('Development mode enabled', timestamp = False, terminal=True) net = NetController(o_inst.onionrCore.config.get('client.public.port', 59497), apiServerIP=apiHost) logger.info('Tor is starting...', terminal=True) if not net.startTor(): diff --git a/onionr/onionrutils/blockmetadata.py b/onionr/onionrutils/blockmetadata.py index d892257f..72bdb48c 100644 --- a/onionr/onionrutils/blockmetadata.py +++ b/onionr/onionrutils/blockmetadata.py @@ -43,6 +43,7 @@ def process_block_metadata(core_inst, blockHash): myBlock.decrypt() if (myBlock.isEncrypted and myBlock.decrypted) or (not myBlock.isEncrypted): blockType = myBlock.getMetadata('type') # we would use myBlock.getType() here, but it is bugged with encrypted blocks + print('blockType', blockType) signer = bytesconverter.bytes_to_str(myBlock.signer) valid = myBlock.verifySig() if myBlock.getMetadata('newFSKey') is not None: diff --git a/onionr/onionrutils/localcommand.py b/onionr/onionrutils/localcommand.py index 90b0d7ac..6b5356ee 100644 --- a/onionr/onionrutils/localcommand.py +++ b/onionr/onionrutils/localcommand.py @@ -19,11 +19,12 @@ def local_command(core_inst, command, data='', silent = True, post=False, postDa if data != '': data = '&data=' + urllib.parse.quote_plus(data) payload = 'http://%s/%s%s' % (hostname, command, data) + print(payload) try: if post: - retData = requests.post(payload, data=postData, headers={'token': config.get('client.webpassword'), 'Connection':'close'}, timeout=(maxWait, maxWait)).text + retData = requests.post(payload, data=postData, headers={'token': core_inst.config.get('client.webpassword'), 'Connection':'close'}, timeout=(maxWait, maxWait)).text else: - retData = requests.get(payload, headers={'token': config.get('client.webpassword'), 'Connection':'close'}, timeout=(maxWait, maxWait)).text + retData = requests.get(payload, headers={'token': core_inst.config.get('client.webpassword'), 'Connection':'close'}, timeout=(maxWait, maxWait)).text except Exception as error: if not silent: logger.error('Failed to make local request (command: %s):%s' % (command, error), terminal=True) diff --git a/onionr/onionrutils/validatemetadata.py b/onionr/onionrutils/validatemetadata.py index 4e2e69f0..ec1f51da 100644 --- a/onionr/onionrutils/validatemetadata.py +++ b/onionr/onionrutils/validatemetadata.py @@ -1,7 +1,7 @@ import json import logger, onionrexceptions from etc import onionrvalues -from onionrutils import stringvalidators, epoch +from onionrutils import stringvalidators, epoch, bytesconverter def validate_metadata(core_inst, metadata, blockData): '''Validate metadata meets onionr spec (does not validate proof value computation), take in either dictionary or json string''' # TODO, make this check sane sizes @@ -59,7 +59,7 @@ def validate_metadata(core_inst, metadata, blockData): else: # if metadata loop gets no errors, it does not break, therefore metadata is valid # make sure we do not have another block with the same data content (prevent data duplication and replay attacks) - nonce = core_inst._utils.bytesToStr(core_inst._crypto.sha3Hash(blockData)) + nonce = bytesconverter.bytes_to_str(core_inst._crypto.sha3Hash(blockData)) try: with open(core_inst.dataNonceFile, 'r') as nonceFile: if nonce in nonceFile.read(): diff --git a/onionr/static-data/default-plugins/pms/main.py b/onionr/static-data/default-plugins/pms/main.py index dfacc915..5c532877 100755 --- a/onionr/static-data/default-plugins/pms/main.py +++ b/onionr/static-data/default-plugins/pms/main.py @@ -23,7 +23,7 @@ import logger, config, threading, time, datetime from onionrblockapi import Block import onionrexceptions from onionrusers import onionrusers -from onionrutils import stringvalidators, escapeansi +from onionrutils import stringvalidators, escapeansi, bytesconverter import locale, sys, os, json locale.setlocale(locale.LC_ALL, '') @@ -136,7 +136,7 @@ class OnionrMail: else: cancel = '' readBlock.verifySig() - senderDisplay = self.myCore._utils.bytesToStr(readBlock.signer) + senderDisplay = bytesconverter.bytes_to_str(readBlock.signer) if len(senderDisplay.strip()) == 0: senderDisplay = 'Anonymous' logger.info('Message received from %s' % (senderDisplay,), terminal=True) @@ -156,7 +156,7 @@ class OnionrMail: reply = logger.readline("Press enter to continue, or enter %s to reply" % ("-r",)) print('') if reply == "-r": - self.draft_message(self.myCore._utils.bytesToStr(readBlock.signer,)) + self.draft_message(bytesconverter.bytes_to_str(readBlock.signer,)) else: logger.readline("Press enter to continue") print('') @@ -200,7 +200,7 @@ class OnionrMail: self.sentMessages = {} for i in self.sentboxTools.listSent(): self.sentboxList.append(i['hash']) - self.sentMessages[i['hash']] = (self.myCore._utils.bytesToStr(i['message']), i['peer'], i['subject']) + self.sentMessages[i['hash']] = (bytesconverter.bytes_to_str(i['message']), i['peer'], i['subject']) if display: logger.info('%s. %s - %s - (%s) - %s' % (count, i['hash'], i['peer'][:12], i['subject'], i['date']), terminal=True) count += 1