From 007d7ad9fb72ee3ba8ff9cfbc35616bc33467504 Mon Sep 17 00:00:00 2001 From: Arinerron Date: Sat, 19 May 2018 15:11:51 -0700 Subject: [PATCH] Update to new Block API --- onionr/api.py | 5 ++- onionr/communicator.py | 5 ++- onionr/core.py | 5 +-- onionr/netcontroller.py | 1 + onionr/onionrutils.py | 59 +++++++++++++--------------- onionr/static-data/default_plugin.py | 1 + 6 files changed, 37 insertions(+), 39 deletions(-) diff --git a/onionr/api.py b/onionr/api.py index 27b7cc1b..1416a1ea 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -22,9 +22,10 @@ from flask import request, Response, abort from multiprocessing import Process from gevent.wsgi import WSGIServer import sys, random, threading, hmac, hashlib, base64, time, math, os, logger, config - from core import Core +from onionrblockapi import Block import onionrutils, onionrcrypto + class API: ''' Main HTTP API (Flask) @@ -50,7 +51,7 @@ class API: ''' config.reload() - + if config.get('devmode', True): self._developmentMode = True logger.set_level(logger.LEVEL_DEBUG) diff --git a/onionr/communicator.py b/onionr/communicator.py index defe4c0a..3661b6bb 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -21,6 +21,7 @@ ''' import sqlite3, requests, hmac, hashlib, time, sys, os, math, logger, urllib.parse, base64, binascii, random, json, threading import core, onionrutils, onionrcrypto, netcontroller, onionrproofs, config, onionrplugins as plugins +from onionrblockapi import Block class OnionrCommunicate: def __init__(self, debug, developmentMode): @@ -76,7 +77,7 @@ class OnionrCommunicate: while True: command = self._core.daemonQueue() # Process blocks based on a timer - self.timerTick() + self.timerTick() # TODO: migrate below if statements to be own functions which are called in the above timerTick() function if self.communicatorTimers['highFailure'] == self.communicatorTimerCounts['highFailure']: self.communicatorTimerCounts['highFailure'] = 0 @@ -191,7 +192,7 @@ class OnionrCommunicate: self.communicatorTimers[timerName] = rate self.communicatorTimerCounts[timerName] = 0 self.communicatorTimerFuncs[timerName] = timerFunc - + def timerTick(self): '''Increments timers "ticks" and calls funcs if applicable''' tName = '' diff --git a/onionr/core.py b/onionr/core.py index 9b78ee1a..ee5d6170 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -18,8 +18,7 @@ along with this program. If not, see . ''' import sqlite3, os, sys, time, math, base64, tarfile, getpass, simplecrypt, hashlib, nacl, logger, json, netcontroller, math, config -#from Crypto.Cipher import AES -#from Crypto import Random +from onionrblockapi import Block import onionrutils, onionrcrypto, onionrproofs, onionrevents as events @@ -111,7 +110,7 @@ class Core: ''' Add an address to the address database (only tor currently) ''' - if address == config.get('i2p')['ownAddr']: + if (not (config.is_set('i2p') and 'ownAddr' in config.get('i2p'))) or address == config.get('i2p')['ownAddr']: return False if self._utils.validateID(address): conn = sqlite3.connect(self.addressDB) diff --git a/onionr/netcontroller.py b/onionr/netcontroller.py index 4c25a7fc..a40dab86 100644 --- a/onionr/netcontroller.py +++ b/onionr/netcontroller.py @@ -19,6 +19,7 @@ ''' import subprocess, os, random, sys, logger, time, signal +from onionrblockapi import Block class NetController: ''' diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index e96cabee..72c52c90 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -20,6 +20,7 @@ # Misc functions that do not fit in the main api, but are useful import getpass, sys, requests, os, socket, hashlib, logger, sqlite3, config, binascii, time, base64, json, glob, shutil, math import nacl.signing, nacl.encoding +from onionrblockapi import Block if sys.version_info < (3, 6): try: @@ -347,47 +348,41 @@ class OnionrUtils: ''' Find, decrypt, and return array of PMs (array of dictionary, {from, text}) ''' - #blocks = self._core.getBlockList() - blocks = self._core.getBlocksByType('pm') + blocks = Block.getBlocks(type = 'pm', core = self._core) message = '' sender = '' for i in blocks: - if len (i) == 0: - continue try: - with open('data/blocks/' + i + '.dat', 'r') as potentialMessage: - potentialMessage = potentialMessage.read() - blockMetadata = json.loads(potentialMessage[:potentialMessage.find('\n')]) - blockContent = potentialMessage[potentialMessage.find('\n') + 1:] + blockContent = i.getContent() + + try: + message = self._core._crypto.pubKeyDecrypt(blockContent, encodedData=True, anonymous=True) + except nacl.exceptions.CryptoError as e: + pass + else: + try: + message = message.decode() + except AttributeError: + pass try: - message = self._core._crypto.pubKeyDecrypt(blockContent, encodedData=True, anonymous=True) - except nacl.exceptions.CryptoError as e: + message = json.loads(message) + except json.decoder.JSONDecodeError: pass else: - try: - message = message.decode() - except AttributeError: - pass + logger.debug('Decrypted %s:' % i.getHash()) + logger.info(message["msg"]) - try: - message = json.loads(message) - except json.decoder.JSONDecodeError: - pass - else: - logger.info('Decrypted %s:' % i) - logger.info(message["msg"]) + signer = message["id"] + sig = message["sig"] - signer = message["id"] - sig = message["sig"] - - if self.validatePubKey(signer): - if self._core._crypto.edVerify(message["msg"], signer, sig, encodedData=True): - logger.info("Good signature by %s" % signer) - else: - logger.warn("Bad signature by %s" % signer) + if self.validatePubKey(signer): + if self._core._crypto.edVerify(message["msg"], signer, sig, encodedData=True): + logger.info("Good signature by %s" % signer) else: - logger.warn('Bad sender id: %s' % signer) + logger.warn("Bad signature by %s" % signer) + else: + logger.warn('Bad sender id: %s' % signer) except FileNotFoundError: pass @@ -475,7 +470,7 @@ class OnionrUtils: sys.stdout.write("\r┣{0}┫ {1}%".format(arrow + spaces, int(round(percent * 100)))) sys.stdout.flush() - + def getEpoch(self): '''returns epoch''' return math.floor(time.time()) @@ -504,4 +499,4 @@ def humanSize(num, suffix='B'): if abs(num) < 1024.0: return "%.1f %s%s" % (num, unit, suffix) num /= 1024.0 - return "%.1f %s%s" % (num, 'Yi', suffix) \ No newline at end of file + return "%.1f %s%s" % (num, 'Yi', suffix) diff --git a/onionr/static-data/default_plugin.py b/onionr/static-data/default_plugin.py index cc6d1d20..7704d32b 100644 --- a/onionr/static-data/default_plugin.py +++ b/onionr/static-data/default_plugin.py @@ -5,6 +5,7 @@ # Imports some useful libraries import logger, config +from onionrblockapi import Block plugin_name = '$name'