improved some debug info and REMOVED STRAY INPUT()

This commit is contained in:
Kevin Froman 2018-04-18 21:19:38 -05:00
commit d8d29b7c28
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
2 changed files with 129 additions and 99 deletions

View File

@ -36,6 +36,7 @@ class Core:
'''
Initialize Core Onionr library
'''
try:
self.queueDB = 'data/queue.db'
self.peerDB = 'data/peers.db'
self.blockDB = 'data/blocks.db'
@ -57,7 +58,10 @@ class Core:
self._utils = onionrutils.OnionrUtils(self)
# Initialize the crypto object
self._crypto = onionrcrypto.OnionrCrypto(self)
except Exception as error:
logger.error('Failed to initialize core Onionr library.', error=error)
logger.fatal('Cannot recover from error.')
exit(1)
return
def addPeer(self, peerID, name=''):

View File

@ -41,15 +41,19 @@ class OnionrUtils:
return
def getTimeBypassToken(self):
try:
if os.path.exists('data/time-bypass.txt'):
with open('data/time-bypass.txt', 'r') as bypass:
self.timingToken = bypass.read()
except Exception as error:
logger.error('Failed to fetch time bypass token.', error=error)
def sendPM(self, pubkey, message):
'''
High level function to encrypt a message to a peer and insert it as a block
'''
try:
encrypted = self._core._crypto.pubKeyEncrypt(message, pubkey, anonymous=True, encodedData=True).decode()
block = self._core.insertBlock(encrypted, header='pm')
@ -57,6 +61,8 @@ class OnionrUtils:
logger.error('Could not send PM')
else:
logger.info('Sent PM, hash: ' + block)
except Exception as error:
logger.error('Failed to send PM.', error=error)
return
@ -80,6 +86,7 @@ class OnionrUtils:
'''
Merge ed25519 key list to our database
'''
try:
retVal = False
if newKeyList != False:
for key in newKeyList.split(','):
@ -87,16 +94,19 @@ class OnionrUtils:
if self._core.addPeer(key):
retVal = True
return retVal
except Exception as error:
logger.error('Failed to merge keys.', error=error)
return False
def mergeAdders(self, newAdderList):
'''
Merge peer adders list to our database
'''
try:
retVal = False
if newAdderList != False:
for adder in newAdderList.split(','):
logger.debug('Seeing if we should add ' + adder)
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.', timestamp=True)
@ -104,12 +114,20 @@ class OnionrUtils:
else:
logger.debug(adder + " is either our address or already in our DB")
return retVal
except Exception as error:
logger.error('Failed to merge adders.', error=error)
return False
def getMyAddress(self):
try:
myAddressFile = open("data/hs/hostname", 'r')
myAddress = myAddressFile.read()
myAddressFile.close()
return myAddress.strip()
except Exception as error:
logger.error('Failed to read my address.', error=error)
return ''
def localCommand(self, command):
'''
@ -121,7 +139,8 @@ class OnionrUtils:
# TODO: URL encode parameters, just as an extra measure. May not be needed, but should be added regardless.
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:
except Exception as error:
logger.error('Failed to make local request (command: ' + str(command) + ').', error=error)
retData = False
return retData
@ -179,6 +198,7 @@ class OnionrUtils:
'''
Return a sha3_256 hash of the blocks DB
'''
try:
with open(self._core.blockDB, 'rb') as data:
data = data.read()
hasher = hashlib.sha3_256()
@ -186,6 +206,8 @@ class OnionrUtils:
dataHash = hasher.hexdigest()
return dataHash
except Exception as error:
logger.error('Failed to get block DB hash.', error=error)
def hasBlock(self, hash):
'''
@ -224,7 +246,9 @@ class OnionrUtils:
return retVal
def validatePubKey(self, key):
'''Validate if a string is a valid base32 encoded Ed25519 key'''
'''
Validate if a string is a valid base32 encoded Ed25519 key
'''
retVal = False
try:
nacl.signing.SigningKey(seed=key, encoder=nacl.encoding.Base32Encoder)
@ -241,6 +265,7 @@ class OnionrUtils:
'''
Validate if an address is a valid tor or i2p hidden service
'''
try:
idLength = len(id)
retVal = True
idNoDomain = ''
@ -280,6 +305,8 @@ class OnionrUtils:
retVal = False
return retVal
except:
return False
def loadPMs(self):
'''
@ -298,8 +325,7 @@ class OnionrUtils:
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))
logger.debug('Unable to decrypt ' + i, error=e)
pass
else:
logger.info('Recieved message: ' + message.decode())