* 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
This commit is contained in:
Kevin Froman 2018-10-30 17:22:06 -05:00
parent 8c63d6c205
commit 58aa8ce1cc
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
11 changed files with 39 additions and 128 deletions

View File

@ -83,7 +83,7 @@ class OnionrCommunicatorDaemon:
self._chat = onionrchat.OnionrChat(self) self._chat = onionrchat.OnionrChat(self)
if debug or developmentMode: if debug or developmentMode:
OnionrCommunicatorTimers(self, self.heartbeat, 10) OnionrCommunicatorTimers(self, self.heartbeat, 30)
# Set timers, function reference, seconds # Set timers, function reference, seconds
# requiresPeer True means the timer function won't fire if we have no connected peers # 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): def announce(self, peer):
'''Announce to peers our address''' '''Announce to peers our address'''
if self.daemonTools.announceNode(): if self.daemonTools.announceNode() == False:
logger.info('Successfully introduced node to ' + peer)
else:
logger.warn('Could not introduce node.') logger.warn('Could not introduce node.')
def detectAPICrash(self): def detectAPICrash(self):

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
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 from onionrblockapi import Block
import onionrutils, onionrcrypto, onionrproofs, onionrevents as events, onionrexceptions, onionrvalues import onionrutils, onionrcrypto, onionrproofs, onionrevents as events, onionrexceptions, onionrvalues
@ -276,14 +276,6 @@ class Core:
return data 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): def setData(self, data):
''' '''
Set the data assciated with a hash Set the data assciated with a hash
@ -294,7 +286,7 @@ class Core:
if not type(data) is bytes: if not type(data) is bytes:
data = data.encode() data = data.encode()
dataHash = self._getSha3Hash(data) dataHash = self._crypto.sha3Hash(data)
if type(dataHash) is bytes: if type(dataHash) is bytes:
dataHash = dataHash.decode() dataHash = dataHash.decode()
@ -319,42 +311,6 @@ class Core:
return dataHash 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): def daemonQueue(self):
''' '''
Gives commands to the communication proccess/daemon by reading an sqlite3 database Gives commands to the communication proccess/daemon by reading an sqlite3 database
@ -363,7 +319,7 @@ class Core:
''' '''
retData = False retData = False
if not os.path.exists(self.queueDB): if not os.path.exists(self.queueDB):
self.makeDaemonDB() self.dbCreate.createDaemonDB()
else: else:
conn = sqlite3.connect(self.queueDB, timeout=10) conn = sqlite3.connect(self.queueDB, timeout=10)
c = conn.cursor() c = conn.cursor()
@ -372,7 +328,7 @@ class Core:
retData = row retData = row
break break
except sqlite3.OperationalError: except sqlite3.OperationalError:
self.makeDaemonDB() self.dbCreate.createDaemonDB()
else: else:
if retData != False: if retData != False:
c.execute('DELETE FROM commands WHERE id=?;', (retData[3],)) c.execute('DELETE FROM commands WHERE id=?;', (retData[3],))
@ -383,16 +339,6 @@ class Core:
return retData 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=''): def daemonQueueAdd(self, command, data=''):
''' '''
Add a command to the daemon queue, used by the communication daemon (communicator.py) Add a command to the daemon queue, used by the communication daemon (communicator.py)

View File

@ -132,4 +132,16 @@ class DBCreator:
''') ''')
conn.commit() conn.commit()
conn.close() conn.close()
return 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()

View File

@ -103,31 +103,21 @@ class Onionr:
self.debug = False # Whole application debugging self.debug = False # Whole application debugging
if os.path.exists('data-encrypted.dat'): # If data folder does not exist
while True: if not data_exists:
print('Enter password to decrypt:') if not os.path.exists(self.dataDir + 'blocks/'):
password = getpass.getpass() os.mkdir(self.dataDir + 'blocks/')
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/')
# Copy default plugins into plugins folder # Copy default plugins into plugins folder
if not os.path.exists(plugins.get_plugins_folder()): if not os.path.exists(plugins.get_plugins_folder()):
if os.path.exists('static-data/default-plugins/'): 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)] 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()) shutil.copytree('static-data/default-plugins/', plugins.get_plugins_folder())
# Enable plugins # Enable plugins
for name in names: for name in names:
if not name in plugins.get_enabled_plugins(): if not name in plugins.get_enabled_plugins():
plugins.enable(name, self) plugins.enable(name, self)
for name in plugins.get_enabled_plugins(): for name in plugins.get_enabled_plugins():
if not os.path.exists(plugins.get_plugin_data_folder(name)): if not os.path.exists(plugins.get_plugin_data_folder(name)):
@ -265,11 +255,6 @@ class Onionr:
finally: finally:
self.execute(command) 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 return
''' '''

View File

@ -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: elif sys.version_info[0] == 3 and sys.version_info[1] >= 6:
import secrets import secrets
import config import config
config.reload()
class OnionrCrypto: class OnionrCrypto:
def __init__(self, coreInstance): def __init__(self, coreInstance):
config.reload()
self._core = coreInstance self._core = coreInstance
self._keyFile = self._core.dataDir + 'keys.txt' self._keyFile = self._core.dataDir + 'keys.txt'
self.keyPowFile = self._core.dataDir + 'keyPow.txt' self.keyPowFile = self._core.dataDir + 'keyPow.txt'

View File

@ -52,6 +52,7 @@ class DaemonTools:
logger.info('Announcing node to ' + url) logger.info('Announcing node to ' + url)
if self.daemon._core._utils.doPostRequest(url, data) == 'Success': if self.daemon._core._utils.doPostRequest(url, data) == 'Success':
logger.info('Successfully introduced node to ' + peer)
retData = True retData = True
self.daemon.decrementThreadCount('announceNode') self.daemon.decrementThreadCount('announceNode')
return retData return retData

View File

@ -20,7 +20,6 @@
import nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, logger, sys, base64, json import nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, logger, sys, base64, json
import core, config import core, config
config.reload()
def getHashDifficulty(h): 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 Return bool for a hash string to see if it meets pow difficulty defined in config
''' '''
config.reload()
hashDifficulty = getHashDifficulty(h) hashDifficulty = getHashDifficulty(h)
expected = int(config.get('minimum_block_pow')) expected = int(config.get('minimum_block_pow'))
if hashDifficulty >= expected: if hashDifficulty >= expected:

View File

@ -53,7 +53,7 @@ class OnionrCLIUI:
while showMenu: while showMenu:
if firstRun: 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) daemon = subprocess.Popen(["./onionr.py", "start"], stdin=subprocess.PIPE, stdout=subprocess.DEVNULL)
time.sleep(30) time.sleep(30)
firstRun = False firstRun = False

View File

@ -2,8 +2,8 @@
"general" : { "general" : {
"dev_mode": true, "dev_mode": true,
"display_header" : true, "display_header" : true,
"minimum_block_pow": 6, "minimum_block_pow": 5,
"minimum_send_pow": 6, "minimum_send_pow": 5,
"direct_connect" : { "direct_connect" : {
"respond" : true, "respond" : true,

View File

@ -14,7 +14,7 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import unittest, sys, os, base64, tarfile, shutil, simplecrypt, logger import unittest, sys, os, base64, tarfile, shutil, logger
class OnionrTests(unittest.TestCase): class OnionrTests(unittest.TestCase):
def testPython3(self): def testPython3(self):
@ -61,36 +61,6 @@ class OnionrTests(unittest.TestCase):
else: else:
self.assertTrue(False) 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): def testConfig(self):
logger.debug('-'*26 + '\n') logger.debug('-'*26 + '\n')
logger.info('Running simple configuration test...') logger.info('Running simple configuration test...')

View File

@ -4,7 +4,6 @@ PyNaCl==1.2.1
gevent==1.3.6 gevent==1.3.6
sha3==0.2.1 sha3==0.2.1
defusedxml==0.5.0 defusedxml==0.5.0
simple_crypt==4.1.7
Flask==1.0.2 Flask==1.0.2
PySocks==1.6.8 PySocks==1.6.8
stem==1.6.0 stem==1.6.0