From 7369b636141d066511c299172d19e5fecea29c6c Mon Sep 17 00:00:00 2001 From: Arinerron Date: Wed, 18 Apr 2018 18:47:35 -0700 Subject: [PATCH 1/3] Update logging --- onionr/api.py | 2 +- onionr/btc.py | 2 +- onionr/communicator.py | 18 +++++++++--------- onionr/cryptotests.py | 10 +++++----- onionr/logger.py | 24 ++++++++++++------------ onionr/netcontroller.py | 2 +- onionr/onionr.py | 20 ++++++++++++-------- onionr/onionrproofs.py | 10 +++++----- onionr/onionrutils.py | 38 ++++++++++++++++++++++---------------- 9 files changed, 68 insertions(+), 58 deletions(-) diff --git a/onionr/api.py b/onionr/api.py index 59ff34f4..b43908e1 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -215,7 +215,7 @@ class API: return resp if not os.environ.get("WERKZEUG_RUN_MAIN") == "true": - logger.info('Starting client on ' + self.host + ':' + str(bindPort) + '...') + logger.info('Starting client on ' + self.host + ':' + str(bindPort) + '...', timestamp=True) try: app.run(host=self.host, port=bindPort, debug=True, threaded=True) diff --git a/onionr/btc.py b/onionr/btc.py index bf4549f2..f09ec622 100644 --- a/onionr/btc.py +++ b/onionr/btc.py @@ -41,4 +41,4 @@ if __name__ == "__main__": bitcoin = OnionrBTC(torPort) while True: print(bitcoin.node.getBlockHash(bitcoin.node.getLastBlockHeight())) # Using print on purpose, do not change to logger - time.sleep(5) \ No newline at end of file + time.sleep(5) diff --git a/onionr/communicator.py b/onionr/communicator.py index a25df609..700a4235 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -36,12 +36,12 @@ class OnionrCommunicate: self.highFailureAmount = 7 ''' - logger.info('Starting Bitcoin Node... with Tor socks port:' + str(sys.argv[2])) + logger.info('Starting Bitcoin Node... with Tor socks port:' + str(sys.argv[2]), timestamp=True) try: self.bitcoin = btc.OnionrBTC(torP=int(sys.argv[2])) except _gdbm.error: pass - logger.info('Bitcoin Node started, on block: ' + self.bitcoin.node.getBlockHash(self.bitcoin.node.getLastBlockHeight())) + logger.info('Bitcoin Node started, on block: ' + self.bitcoin.node.getBlockHash(self.bitcoin.node.getLastBlockHeight()), timestamp=True) ''' #except: #logger.fatal('Failed to start Bitcoin Node, exiting...') @@ -89,16 +89,16 @@ class OnionrCommunicate: blockProcessTimer = 0 if command != False: if command[0] == 'shutdown': - logger.info('Daemon recieved exit command.') + logger.info('Daemon recieved exit command.', timestamp=True) break elif command[0] == 'anounceNode': announceAmount = 1 announceVal = False for i in command[1]: - logger.info('Announcing our node to ' + command[1][i]) + logger.info('Announcing our node to ' + command[1][i], timestamp=True) while not announceVal: announceVal = self.performGet('announce', command[1][i], data=self._core.hsAdder, skipHighFailureAddress=True) - + time.sleep(1) return @@ -123,14 +123,14 @@ class OnionrCommunicate: while peersCheck > peersChecked: i = random.randint(0, maxN) - logger.info('Using ' + peerList[i] + ' to find new peers') + logger.info('Using ' + peerList[i] + ' to find new peers', timestamp=True) try: newAdders = self.performGet('pex', peerList[i], skipHighFailureAddress=True) logger.debug('Attempting to merge address: ') logger.debug(newAdders) self._utils.mergeAdders(newAdders) except requests.exceptions.ConnectionError: - logger.info(peerList[i] + ' connection failed') + logger.info(peerList[i] + ' connection failed', timestamp=True) continue else: try: @@ -141,7 +141,7 @@ class OnionrCommunicate: # TODO: Require keys to come with POW token (very large amount of POW) self._utils.mergeKeys(newKeys) except requests.exceptions.ConnectionError: - logger.info(peerList[i] + ' connection failed') + logger.info(peerList[i] + ' connection failed', timestamp=True) continue else: peersChecked += 1 @@ -223,7 +223,7 @@ class OnionrCommunicate: self._core.setData(data) if data.startswith('-txt-'): self._core.setBlockType(hash, 'txt') - logger.info('Successfully obtained data for ' + hash) + logger.info('Successfully obtained data for ' + hash, timestamp=True) if len(data) < 120: logger.debug('Block text:\n' + data) else: diff --git a/onionr/cryptotests.py b/onionr/cryptotests.py index 036d5fb0..f7927fb8 100755 --- a/onionr/cryptotests.py +++ b/onionr/cryptotests.py @@ -24,12 +24,12 @@ class OnionrCryptoTests(unittest.TestCase): crypto = onionrcrypto.OnionrCrypto(myCore) key = key = b"tttttttttttttttttttttttttttttttt" - logger.info("Encrypting: " + dataString) + logger.info("Encrypting: " + dataString, timestamp=True) encrypted = crypto.symmetricEncrypt(dataString, key, returnEncoded=True) - logger.info(encrypted) - logger.info('Decrypting encrypted string:') + logger.info(encrypted, timestamp=True) + logger.info('Decrypting encrypted string:', timestamp=True) decrypted = crypto.symmetricDecrypt(encrypted, key, encodedMessage=True) - logger.info(decrypted) + logger.info(decrypted, timestamp=True) self.assertTrue(True) if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main() diff --git a/onionr/logger.py b/onionr/logger.py index 389d868c..ca138559 100644 --- a/onionr/logger.py +++ b/onionr/logger.py @@ -141,10 +141,10 @@ def log(prefix, data, color = '', timestamp=True): data : The actual data to output color : The color to output before the data ''' + curTime = '' if timestamp: curTime = time.strftime("%m-%d %H:%M:%S") - else: - curTime = '' + output = colors.reset + str(color) + '[' + colors.bold + str(prefix) + colors.reset + str(color) + '] ' + curTime + ' ' + str(data) + colors.reset if not get_settings() & USE_ANSI: output = colors.filter(output) @@ -201,26 +201,26 @@ def confirm(default = 'y', message = 'Are you sure %s? '): return default == 'y' # debug: when there is info that could be useful for debugging purposes only -def debug(data): +def debug(data, timestamp=True): if get_level() <= LEVEL_DEBUG: - log('/', data) + log('/', data, timestamp=timestamp) # info: when there is something to notify the user of, such as the success of a process -def info(data): +def info(data, timestamp=False): if get_level() <= LEVEL_INFO: - log('+', data, colors.fg.green) + log('+', data, colors.fg.green, timestamp=timestamp) # warn: when there is a potential for something bad to happen -def warn(data): +def warn(data, timestamp=True): if get_level() <= LEVEL_WARN: - log('!', data, colors.fg.orange) + log('!', data, colors.fg.orange, timestamp=timestamp) # error: when only one function, module, or process of the program encountered a problem and must stop -def error(data): +def error(data, timestamp=True): if get_level() <= LEVEL_ERROR: - log('-', data, colors.fg.red) + log('-', data, colors.fg.red, timestamp=timestamp) # fatal: when the something so bad has happened that the prorgam must stop -def fatal(data): +def fatal(data, timestamp=True): if get_level() <= LEVEL_FATAL: - log('#', data, colors.bg.red + colors.fg.green + colors.bold) + log('#', data, colors.bg.red + colors.fg.green + colors.bold, timestamp=timestamp) diff --git a/onionr/netcontroller.py b/onionr/netcontroller.py index 76248beb..af20f335 100644 --- a/onionr/netcontroller.py +++ b/onionr/netcontroller.py @@ -97,7 +97,7 @@ HiddenServicePort 80 127.0.0.1:''' + str(self.hsPort) + ''' logger.fatal('Failed to start Tor. Try killing any other Tor processes owned by this user.') return False - logger.info('Finished starting Tor') + logger.info('Finished starting Tor', timestamp=True) self.readyState = True myID = open('data/hs/hostname', 'r') diff --git a/onionr/onionr.py b/onionr/onionr.py index a65d905e..699af257 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -158,6 +158,8 @@ class Onionr: 'addpeer': self.addPeer, 'add-peer': self.addPeer, 'add-address': self.addAddress, + 'add-addr': self.addAddress, + 'addaddr': self.addAddress, 'addaddress': self.addAddress, 'connect': self.addAddress @@ -173,12 +175,12 @@ class Onionr: 'enable-plugin': 'Enables and starts a plugin', 'disable-plugin': 'Disables and stops a plugin', 'reload-plugin': 'Reloads a plugin', - 'list-peers': 'Displays a list of peers', 'add-peer': 'Adds a peer (?)', + 'list-peers': 'Displays a list of peers', 'add-msg': 'Broadcasts a message to the Onionr network', 'pm': 'Adds a private message to block', - 'gui': 'Opens a graphical interface for Onionr', - 'getpms': 'Shows private messages sent to you' + 'get-pms': 'Shows private messages sent to you', + 'gui': 'Opens a graphical interface for Onionr' } command = '' @@ -300,7 +302,7 @@ class Onionr: ''' Adds a peer (?) ''' - + try: newPeer = sys.argv[2] except: @@ -312,7 +314,9 @@ class Onionr: return def addAddress(self): - '''Adds a Onionr node address''' + ''' + Adds a Onionr node address + ''' try: newAddress = sys.argv[2] except: @@ -320,9 +324,9 @@ class Onionr: else: logger.info("Adding address: " + logger.colors.underline + newAddress) if self.onionrCore.addAddress(newAddress): - logger.info("Successfully added address") + logger.info("Successfully added address.") else: - logger.warn("Unable to add address") + logger.warn("Unable to add address.") return @@ -342,7 +346,7 @@ class Onionr: self.onionrCore.setBlockType(addedHash, 'txt') return - + def getPMs(self): ''' display PMs sent to us diff --git a/onionr/onionrproofs.py b/onionr/onionrproofs.py index 546e4449..d190705a 100644 --- a/onionr/onionrproofs.py +++ b/onionr/onionrproofs.py @@ -47,10 +47,10 @@ class POW: if iFound: endTime = math.floor(time.time()) if self.reporting: - logger.info('Found token ' + token) - logger.info('took ' + str(endTime - startTime)) + logger.info('Found token ' + token, timestamp=True) + logger.info('took ' + str(endTime - startTime), timestamp=True) self.result = token - + def __init__(self, difficulty, bitcoinNode): self.foundHash = False self.difficulty = difficulty @@ -72,7 +72,7 @@ class POW: def shutdown(self): self.hashing = False self.puzzle = '' - + def changeDifficulty(self, newDiff): self.difficulty = newDiff @@ -83,4 +83,4 @@ class POW: except AttributeError: retVal = False self.result = False - return retVal \ No newline at end of file + return retVal diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index 565b706a..bdc768ff 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -39,19 +39,17 @@ class OnionrUtils: self.timingToken = '' return - + def getTimeBypassToken(self): if os.path.exists('data/time-bypass.txt'): with open('data/time-bypass.txt', 'r') as bypass: self.timingToken = bypass.read() def sendPM(self, pubkey, message): - '''High level function to encrypt a message to a peer and insert it as a block''' + ''' + High level function to encrypt a message to a peer and insert it as a block + ''' - #forwardKey = self._core.getPeerInfo(pubkey, 'forwardKey') - - #if self._core.getPeerInfo(pubkey, 'pubkeyExchanged') == 1: - # pass encrypted = self._core._crypto.pubKeyEncrypt(message, pubkey, anonymous=True, encodedData=True).decode() block = self._core.insertBlock(encrypted, header='pm') @@ -61,21 +59,27 @@ class OnionrUtils: logger.info('Sent PM, hash: ' + block) return - + def incrementAddressSuccess(self, address): - '''Increase the recorded sucesses for an address''' + ''' + Increase the recorded sucesses for an address + ''' increment = self._core.getAddressInfo(address, 'success') + 1 self._core.setAddressInfo(address, 'success', increment) return - + def decrementAddressSuccess(self, address): - '''Decrease the recorded sucesses for an address''' + ''' + Decrease the recorded sucesses for an address + ''' increment = self._core.getAddressInfo(address, 'success') - 1 self._core.setAddressInfo(address, 'success', increment) return def mergeKeys(self, newKeyList): - '''Merge ed25519 key list to our database''' + ''' + Merge ed25519 key list to our database + ''' retVal = False if newKeyList != False: for key in newKeyList.split(','): @@ -84,15 +88,17 @@ class OnionrUtils: retVal = True return retVal - + def mergeAdders(self, newAdderList): - '''Merge peer adders list to our database''' + ''' + Merge peer adders list to our database + ''' retVal = False if newAdderList != False: for adder in newAdderList.split(','): if not adder in self._core.listAdders(randomOrder=False) and adder.strip() != self.getMyAddress(): if self._core.addAddress(adder): - logger.info('added ' + adder + ' to db') + logger.info('Added ' + adder + ' to db.', timestamp=True) input() retVal = True else: @@ -116,7 +122,7 @@ class OnionrUtils: try: retData = requests.get('http://' + open('data/host.txt', 'r').read() + ':' + str(config.get('client')['port']) + '/client/?action=' + command + '&token=' + str(config.get('client')['client_hmac']) + '&timingToken=' + self.timingToken).text except requests.ConnectionError: - retData = False + retData = False return retData @@ -296,4 +302,4 @@ class OnionrUtils: pass else: logger.info('Recieved message: ' + message.decode()) - return \ No newline at end of file + return From 86a2c0d6d0989bcbbf1945a3260b4e26d92bc947 Mon Sep 17 00:00:00 2001 From: Arinerron Date: Wed, 18 Apr 2018 18:50:53 -0700 Subject: [PATCH 2/3] Remove extra padding --- onionr/logger.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/onionr/logger.py b/onionr/logger.py index ca138559..3d25c9db 100644 --- a/onionr/logger.py +++ b/onionr/logger.py @@ -143,9 +143,9 @@ def log(prefix, data, color = '', timestamp=True): ''' curTime = '' if timestamp: - curTime = time.strftime("%m-%d %H:%M:%S") + curTime = time.strftime("%m-%d %H:%M:%S") + ' ' - output = colors.reset + str(color) + '[' + colors.bold + str(prefix) + colors.reset + str(color) + '] ' + curTime + ' ' + str(data) + colors.reset + output = colors.reset + str(color) + '[' + colors.bold + str(prefix) + colors.reset + str(color) + '] ' + curTime + str(data) + colors.reset if not get_settings() & USE_ANSI: output = colors.filter(output) From ded179bcad81267d907302c1e90d9e07fb2eb17a Mon Sep 17 00:00:00 2001 From: Arinerron Date: Wed, 18 Apr 2018 18:57:37 -0700 Subject: [PATCH 3/3] Add error logging --- onionr/logger.py | 4 +++- onionr/onionrutils.py | 25 ++++++++++++++----------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/onionr/logger.py b/onionr/logger.py index 3d25c9db..23ede687 100644 --- a/onionr/logger.py +++ b/onionr/logger.py @@ -216,9 +216,11 @@ def warn(data, timestamp=True): log('!', data, colors.fg.orange, timestamp=timestamp) # error: when only one function, module, or process of the program encountered a problem and must stop -def error(data, timestamp=True): +def error(data, error=None, timestamp=True): if get_level() <= LEVEL_ERROR: log('-', data, colors.fg.red, timestamp=timestamp) + if not error is None: + debug('Error details: ' + str(error)) # fatal: when the something so bad has happened that the prorgam must stop def fatal(data, timestamp=True): diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index bdc768ff..8dcdb89b 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -291,15 +291,18 @@ class OnionrUtils: for i in blocks: if len (i) == 0: continue - with open('data/blocks/' + i + '.dat', 'r') as potentialMessage: - message = potentialMessage.read() - if message.startswith('-pm-'): - try: - message = self._core._crypto.pubKeyDecrypt(message.replace('-pm-', ''), encodedData=True, anonymous=True) - except nacl.exceptions.CryptoError as e: - #logger.debug('Unable to decrypt ' + i) - #logger.debug(str(e)) - pass - else: - logger.info('Recieved message: ' + message.decode()) + try: + with open('data/blocks/' + i + '.dat', 'r') as potentialMessage: + message = potentialMessage.read() + if message.startswith('-pm-'): + try: + message = self._core._crypto.pubKeyDecrypt(message.replace('-pm-', ''), encodedData=True, anonymous=True) + except nacl.exceptions.CryptoError as e: + #logger.debug('Unable to decrypt ' + i) + #logger.debug(str(e)) + pass + else: + logger.info('Recieved message: ' + message.decode()) + except Exception as error: + logger.error('Failed to open block ' + str(i) + '.', error=error) return