From 58aa8ce1ccd135faea9b8481eb70da7f6236cb22 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Tue, 30 Oct 2018 17:22:06 -0500 Subject: [PATCH] * Increased heartbeat timer * Tried to fix bug where wrong node was being reported as being announced to * Refactored core somewhat - Removed data dir encryption (TODO: just encrypt pub/priv key pair) - Removed simplecrypt dependency --- onionr/communicator2.py | 6 +- onionr/core.py | 62 ++----------------- onionr/dbcreator.py | 14 ++++- onionr/onionr.py | 41 ++++-------- onionr/onionrcrypto.py | 2 +- onionr/onionrdaemontools.py | 1 + onionr/onionrproofs.py | 2 +- .../static-data/default-plugins/cliui/main.py | 2 +- onionr/static-data/default_config.json | 4 +- onionr/tests.py | 32 +--------- requirements.txt | 1 - 11 files changed, 39 insertions(+), 128 deletions(-) diff --git a/onionr/communicator2.py b/onionr/communicator2.py index fe4fb1ef..7f5f81fe 100755 --- a/onionr/communicator2.py +++ b/onionr/communicator2.py @@ -83,7 +83,7 @@ class OnionrCommunicatorDaemon: self._chat = onionrchat.OnionrChat(self) if debug or developmentMode: - OnionrCommunicatorTimers(self, self.heartbeat, 10) + OnionrCommunicatorTimers(self, self.heartbeat, 30) # Set timers, function reference, seconds # requiresPeer True means the timer function won't fire if we have no connected peers @@ -500,9 +500,7 @@ class OnionrCommunicatorDaemon: def announce(self, peer): '''Announce to peers our address''' - if self.daemonTools.announceNode(): - logger.info('Successfully introduced node to ' + peer) - else: + if self.daemonTools.announceNode() == False: logger.warn('Could not introduce node.') def detectAPICrash(self): diff --git a/onionr/core.py b/onionr/core.py index 296fec88..d15d30fb 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import sqlite3, os, sys, time, math, base64, tarfile, getpass, simplecrypt, hashlib, nacl, logger, json, netcontroller, math, config +import sqlite3, os, sys, time, math, base64, tarfile, nacl, logger, json, netcontroller, math, config from onionrblockapi import Block import onionrutils, onionrcrypto, onionrproofs, onionrevents as events, onionrexceptions, onionrvalues @@ -276,14 +276,6 @@ class Core: return data - def _getSha3Hash(self, data): - hasher = hashlib.sha3_256() - if not type(data) is bytes: - data = data.encode() - hasher.update(data) - dataHash = hasher.hexdigest() - return dataHash - def setData(self, data): ''' Set the data assciated with a hash @@ -294,7 +286,7 @@ class Core: if not type(data) is bytes: data = data.encode() - dataHash = self._getSha3Hash(data) + dataHash = self._crypto.sha3Hash(data) if type(dataHash) is bytes: dataHash = dataHash.decode() @@ -319,42 +311,6 @@ class Core: return dataHash - def dataDirEncrypt(self, password): - ''' - Encrypt the data directory on Onionr shutdown - ''' - if os.path.exists('data.tar'): - os.remove('data.tar') - tar = tarfile.open("data.tar", "w") - for name in ['data']: - tar.add(name) - tar.close() - tarData = open('data.tar', 'r', encoding = "ISO-8859-1").read() - encrypted = simplecrypt.encrypt(password, tarData) - open('data-encrypted.dat', 'wb').write(encrypted) - os.remove('data.tar') - - return - - def dataDirDecrypt(self, password): - ''' - Decrypt the data directory on startup - ''' - if not os.path.exists('data-encrypted.dat'): - return (False, 'encrypted archive does not exist') - data = open('data-encrypted.dat', 'rb').read() - try: - decrypted = simplecrypt.decrypt(password, data) - except simplecrypt.DecryptionException: - return (False, 'wrong password (or corrupted archive)') - else: - open('data.tar', 'wb').write(decrypted) - tar = tarfile.open('data.tar') - tar.extractall() - tar.close() - - return (True, '') - def daemonQueue(self): ''' Gives commands to the communication proccess/daemon by reading an sqlite3 database @@ -363,7 +319,7 @@ class Core: ''' retData = False if not os.path.exists(self.queueDB): - self.makeDaemonDB() + self.dbCreate.createDaemonDB() else: conn = sqlite3.connect(self.queueDB, timeout=10) c = conn.cursor() @@ -372,7 +328,7 @@ class Core: retData = row break except sqlite3.OperationalError: - self.makeDaemonDB() + self.dbCreate.createDaemonDB() else: if retData != False: c.execute('DELETE FROM commands WHERE id=?;', (retData[3],)) @@ -383,16 +339,6 @@ class Core: return retData - def makeDaemonDB(self): - '''generate the daemon queue db''' - conn = sqlite3.connect(self.queueDB, timeout=10) - c = conn.cursor() - # Create table - c.execute('''CREATE TABLE commands - (id integer primary key autoincrement, command text, data text, date text)''') - conn.commit() - conn.close() - def daemonQueueAdd(self, command, data=''): ''' Add a command to the daemon queue, used by the communication daemon (communicator.py) diff --git a/onionr/dbcreator.py b/onionr/dbcreator.py index 7f970310..82816f43 100644 --- a/onionr/dbcreator.py +++ b/onionr/dbcreator.py @@ -132,4 +132,16 @@ class DBCreator: ''') conn.commit() conn.close() - return \ No newline at end of file + return + + def createDaemonDB(self): + ''' + Create the daemon queue database + ''' + conn = sqlite3.connect(self.core.queueDB, timeout=10) + c = conn.cursor() + # Create table + c.execute('''CREATE TABLE commands + (id integer primary key autoincrement, command text, data text, date text)''') + conn.commit() + conn.close() \ No newline at end of file diff --git a/onionr/onionr.py b/onionr/onionr.py index eb56fb7c..24880258 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -103,31 +103,21 @@ class Onionr: self.debug = False # Whole application debugging - if os.path.exists('data-encrypted.dat'): - while True: - print('Enter password to decrypt:') - password = getpass.getpass() - result = self.onionrCore.dataDirDecrypt(password) - if os.path.exists(self.dataDir): - break - else: - logger.error('Failed to decrypt: ' + result[1], timestamp = False) - else: - # If data folder does not exist - if not data_exists: - if not os.path.exists(self.dataDir + 'blocks/'): - os.mkdir(self.dataDir + 'blocks/') + # If data folder does not exist + if not data_exists: + if not os.path.exists(self.dataDir + 'blocks/'): + os.mkdir(self.dataDir + 'blocks/') - # Copy default plugins into plugins folder - if not os.path.exists(plugins.get_plugins_folder()): - if os.path.exists('static-data/default-plugins/'): - names = [f for f in os.listdir("static-data/default-plugins/") if not os.path.isfile(f)] - shutil.copytree('static-data/default-plugins/', plugins.get_plugins_folder()) + # Copy default plugins into plugins folder + if not os.path.exists(plugins.get_plugins_folder()): + if os.path.exists('static-data/default-plugins/'): + names = [f for f in os.listdir("static-data/default-plugins/") if not os.path.isfile(f)] + shutil.copytree('static-data/default-plugins/', plugins.get_plugins_folder()) - # Enable plugins - for name in names: - if not name in plugins.get_enabled_plugins(): - plugins.enable(name, self) + # Enable plugins + for name in names: + if not name in plugins.get_enabled_plugins(): + plugins.enable(name, self) for name in plugins.get_enabled_plugins(): if not os.path.exists(plugins.get_plugin_data_folder(name)): @@ -265,11 +255,6 @@ class Onionr: finally: self.execute(command) - if not self._developmentMode: - encryptionPassword = self.onionrUtils.getPassword('Enter password to encrypt directory: ') - self.onionrCore.dataDirEncrypt(encryptionPassword) - shutil.rmtree(self.dataDir) - return ''' diff --git a/onionr/onionrcrypto.py b/onionr/onionrcrypto.py index 1105d844..8bc5c847 100644 --- a/onionr/onionrcrypto.py +++ b/onionr/onionrcrypto.py @@ -25,10 +25,10 @@ if sys.version_info[0] == 3 and sys.version_info[1] < 6: elif sys.version_info[0] == 3 and sys.version_info[1] >= 6: import secrets import config -config.reload() class OnionrCrypto: def __init__(self, coreInstance): + config.reload() self._core = coreInstance self._keyFile = self._core.dataDir + 'keys.txt' self.keyPowFile = self._core.dataDir + 'keyPow.txt' diff --git a/onionr/onionrdaemontools.py b/onionr/onionrdaemontools.py index 8192e0de..71a89410 100644 --- a/onionr/onionrdaemontools.py +++ b/onionr/onionrdaemontools.py @@ -52,6 +52,7 @@ class DaemonTools: logger.info('Announcing node to ' + url) if self.daemon._core._utils.doPostRequest(url, data) == 'Success': + logger.info('Successfully introduced node to ' + peer) retData = True self.daemon.decrementThreadCount('announceNode') return retData diff --git a/onionr/onionrproofs.py b/onionr/onionrproofs.py index 6c5045d0..40ae195a 100644 --- a/onionr/onionrproofs.py +++ b/onionr/onionrproofs.py @@ -20,7 +20,6 @@ import nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, logger, sys, base64, json import core, config -config.reload() def getHashDifficulty(h): ''' @@ -37,6 +36,7 @@ def hashMeetsDifficulty(h): ''' Return bool for a hash string to see if it meets pow difficulty defined in config ''' + config.reload() hashDifficulty = getHashDifficulty(h) expected = int(config.get('minimum_block_pow')) if hashDifficulty >= expected: diff --git a/onionr/static-data/default-plugins/cliui/main.py b/onionr/static-data/default-plugins/cliui/main.py index 5fc24385..646aa7c2 100644 --- a/onionr/static-data/default-plugins/cliui/main.py +++ b/onionr/static-data/default-plugins/cliui/main.py @@ -53,7 +53,7 @@ class OnionrCLIUI: while showMenu: if firstRun: - print("please wait while Onionr starts...") + logger.info("please wait while Onionr starts...") daemon = subprocess.Popen(["./onionr.py", "start"], stdin=subprocess.PIPE, stdout=subprocess.DEVNULL) time.sleep(30) firstRun = False diff --git a/onionr/static-data/default_config.json b/onionr/static-data/default_config.json index 768bb8ce..8ce16361 100644 --- a/onionr/static-data/default_config.json +++ b/onionr/static-data/default_config.json @@ -2,8 +2,8 @@ "general" : { "dev_mode": true, "display_header" : true, - "minimum_block_pow": 6, - "minimum_send_pow": 6, + "minimum_block_pow": 5, + "minimum_send_pow": 5, "direct_connect" : { "respond" : true, diff --git a/onionr/tests.py b/onionr/tests.py index 008d2bed..c23db1fa 100755 --- a/onionr/tests.py +++ b/onionr/tests.py @@ -14,7 +14,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import unittest, sys, os, base64, tarfile, shutil, simplecrypt, logger +import unittest, sys, os, base64, tarfile, shutil, logger class OnionrTests(unittest.TestCase): def testPython3(self): @@ -61,36 +61,6 @@ class OnionrTests(unittest.TestCase): else: self.assertTrue(False) - def testData_b_Encrypt(self): - self.assertTrue(True) - return - - logger.debug('-'*26 + '\n') - logger.info('Running data dir encrypt test...') - - import core - myCore = core.Core() - myCore.dataDirEncrypt('password') - if os.path.exists('data-encrypted.dat'): - self.assertTrue(True) - else: - self.assertTrue(False) - - def testData_a_Decrypt(self): - self.assertTrue(True) - return - - logger.debug('-'*26 + '\n') - logger.info('Running data dir decrypt test...') - - import core - myCore = core.Core() - myCore.dataDirDecrypt('password') - if os.path.exists('data/'): - self.assertTrue(True) - else: - self.assertTrue(False) - def testConfig(self): logger.debug('-'*26 + '\n') logger.info('Running simple configuration test...') diff --git a/requirements.txt b/requirements.txt index 05797aed..545d7cf9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,6 @@ PyNaCl==1.2.1 gevent==1.3.6 sha3==0.2.1 defusedxml==0.5.0 -simple_crypt==4.1.7 Flask==1.0.2 PySocks==1.6.8 stem==1.6.0