From cb3015652a0a896bd0c914cd66c6c45c4b8fa7b5 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Fri, 16 Mar 2018 10:35:37 -0500 Subject: [PATCH] peer/key exchange work (mostly done) --- onionr/communicator.py | 34 +++++++++++++++++++++++++++++++--- onionr/core.py | 4 ++-- onionr/onionrutils.py | 18 ++++++++++++++++++ onionr/tests.py | 6 +++--- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/onionr/communicator.py b/onionr/communicator.py index 413ee3ee..45bb8bae 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -19,7 +19,7 @@ and code to operate as a daemon, getting commands from the command queue databas You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import sqlite3, requests, hmac, hashlib, time, sys, os, math, logger, urllib.parse +import sqlite3, requests, hmac, hashlib, time, sys, os, math, logger, urllib.parse, random import core, onionrutils, onionrcrypto, onionrproofs, btc, config, onionrplugins as plugins class OnionrCommunicate: @@ -49,7 +49,7 @@ class OnionrCommunicate: blockProcessAmount = 5 heartBeatTimer = 0 heartBeatRate = 5 - pexTimer = 900 # How often we should check for new peers + pexTimer = 5 # How often we should check for new peers pexCount = 0 logger.debug('Communicator debugging enabled.') torID = open('data/hs/hostname').read() @@ -89,14 +89,42 @@ class OnionrCommunicate: ''' Get new peers ''' + peersCheck = 5 # Amount of peers to ask for new peers + keys + peersChecked = 0 + peerList = list(self._core.listAdders()) # random ordered list of peers + logger.warn(len(peerList)) + newKeys = [] + newAdders = [] + if len(peerList) > peersCheck: + peersCheck = len(peerList) + + while peersCheck > peersChecked: + i = random.randint(0, len(peerList)) + logger.info('Using ' + peerList[i] + ' to find new peers') + try: + newAdders = self.performGet('pex', peerList[i]) + self._utils.mergeAdders(newAdders) + except requests.exceptions.ConnectionError: + logger.info(peerList[i] + ' connection failed') + continue + else: + try: + logger.info('Using ' + peerList[i] + ' to find new keys') + newKeys = self.performGet('kex', peerList[i]) + # 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') + continue + else: + peersChecked += 1 return def lookupBlocks(self): ''' Lookup blocks and merge new ones ''' - peerList = self._core.listAdders() blocks = '' for i in peerList: diff --git a/onionr/core.py b/onionr/core.py index 54ac195e..ef91549c 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -57,7 +57,7 @@ class Core: def addPeer(self, peerID, name=''): ''' - Add a peer by their ID, with an optional name, to the peer database + Adds a public key to the key database (misleading function name) DOES NO SAFETY CHECKS if the ID is valid, but prepares the insertion ''' @@ -346,7 +346,7 @@ class Core: def listPeers(self, randomOrder=True): ''' - Return a list of peers + Return a list of public keys (misleading function name) randomOrder determines if the list should be in a random order ''' diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index 50c973fb..051ad193 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -41,6 +41,24 @@ class OnionrUtils: '''High level function to encrypt a message to a peer and insert it as a block''' return + def mergeKeys(self, newKeyList): + '''Merge ed25519 key list to our database''' + retVal = False + for key in newKeyList: + if not key in self._core.listPeers(randomOrder=False): + if self._core.addPeer(key): + retVal = True + return retVal + + def mergeAdders(self, newAdderList): + '''Merge peer adders list to our database''' + retVal = False + for adder in newAdderList: + if not adder in self._core.listAdders(randomOrder=False): + if self._core.addAddress(adder): + retVal = True + return retVal + def localCommand(self, command): ''' Send a command to the local http API server, securely. Intended for local clients, DO NOT USE for remote peers. diff --git a/onionr/tests.py b/onionr/tests.py index 7fd95ec0..18e6187d 100755 --- a/onionr/tests.py +++ b/onionr/tests.py @@ -120,10 +120,10 @@ class OnionrTests(unittest.TestCase): def testBitcoinNode(self): # temporarily disabled- this takes a lot of time the CI doesn't have self.assertTrue(True) - logger.debug('-'*26 + '\n') - logger.info('Running bitcoin node test...') + #logger.debug('-'*26 + '\n') + #logger.info('Running bitcoin node test...') - sbitcoin = btc.OnionrBTC() + #sbitcoin = btc.OnionrBTC() def testPluginReload(self): logger.debug('-'*26 + '\n')