From fc5d702706b1a3fc2a67716692d3da96866a3dbb Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Tue, 27 Feb 2018 18:00:37 -0600 Subject: [PATCH] bug fixes --- onionr/communicator.py | 26 +++++++++---------- onionr/core.py | 59 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/onionr/communicator.py b/onionr/communicator.py index 36f1108a..54e9c512 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -33,14 +33,14 @@ class OnionrCommunicate: self._utils = onionrutils.OnionrUtils(self._core) self._crypto = onionrcrypto.OnionrCrypto(self._core) logger.info('Starting Bitcoin Node... with Tor socks port:' + str(sys.argv[2])) - while True: - try: - self.bitcoin = btc.OnionrBTC(torP=int(sys.argv[2])) - except: + #while True: + #try: + self.bitcoin = btc.OnionrBTC(torP=int(sys.argv[2])) + #except: # ugly but needed - pass - else: - break + # pass + #else: + # break logger.info('Bitcoin Node started, on block: ' + self.bitcoin.node.getBlockHash(self.bitcoin.node.getLastBlockHeight())) blockProcessTimer = 0 blockProcessAmount = 5 @@ -81,11 +81,11 @@ class OnionrCommunicate: peerList = self._core.listAdders() blocks = '' for i in peerList: - lastDB = self._core.getPeerInfo(i, 'blockDBHash') + lastDB = self._core.getAddressInfo(i, 'DBHash') if lastDB == None: logger.debug('Fetching hash from ' + i + ' No previous known.') else: - logger.debug('Fetching hash from ' + i + ', ' + lastDB + ' last known') + logger.debug('Fetching hash from ' + str(i) + ', ' + lastDB + ' last known') currentDB = self.performGet('getDBHash', i) if currentDB != False: logger.debug(i + " hash db (from request): " + currentDB) @@ -96,7 +96,7 @@ class OnionrCommunicate: logger.debug('Fetching hash from ' + i + ' - ' + currentDB + ' current hash.') blocks += self.performGet('getBlockHashes', i) if self._utils.validateHash(currentDB): - self._core.setPeerInfo(i, "blockDBHash", currentDB) + self._core.setAddressInfo(i, "DBHash", currentDB) if len(blocks.strip()) != 0: logger.debug('BLOCKS:' + blocks) blockList = blocks.split('\n') @@ -162,7 +162,7 @@ class OnionrCommunicate: ''' return urllib.parse.quote_plus(data) - def performGet(self, action, peer, data=None, type='tor'): + def performGet(self, action, peer, data=None, peerType='tor'): ''' Performs a request to a peer through Tor or i2p (currently only Tor) ''' @@ -172,10 +172,10 @@ class OnionrCommunicate: # Store peer in peerData dictionary (non permanent) if not peer in self.peerData: self.peerData[peer] = {'connectCount': 0, 'failCount': 0, 'lastConnectTime': math.floor(time.time())} - socksPort = sys.argv[2] + logger.debug('Contacting ' + peer + ' on port ' + socksPort) '''We use socks5h to use tor as DNS''' - proxies = {'http': 'socks5h://127.0.0.1:' + str(socksPort), 'https': 'socks5h://127.0.0.1:' + str(socksPort)} + proxies = {'http': 'socks5://127.0.0.1:' + str(socksPort), 'https': 'socks5://127.0.0.1:' + str(socksPort)} headers = {'user-agent': 'PyOnionr'} url = 'http://' + peer + '/public/?action=' + self.urlencode(action) if data != None: diff --git a/onionr/core.py b/onionr/core.py index cb52653c..54ac195e 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -84,7 +84,7 @@ class Core: return True else: return False - + def removeAddress(self, address): '''Remove an address from the address database''' if self._utils.validateID(address): @@ -115,6 +115,7 @@ class Core: knownPeer text, speed int, success int, + DBHash text, failure int ); ''') @@ -339,7 +340,7 @@ class Core: addresses = c.execute('SELECT * FROM adders;') addressList = [] for i in addresses: - addressList.append(i[2]) + addressList.append(i[0]) conn.close() return addressList @@ -369,16 +370,15 @@ class Core: id text 0 name text, 1 adders text, 2 - blockDBHash text, 3 - forwardKey text, 4 - dateSeen not null, 5 - bytesStored int, 6 - trust int 7 + forwardKey text, 3 + dateSeen not null, 4 + bytesStored int, 5 + trust int 6 ''' conn = sqlite3.connect(self.peerDB) c = conn.cursor() command = (peer,) - infoNumbers = {'id': 0, 'name': 1, 'adders': 2, 'blockDBHash': 3, 'forwardKey': 4, 'dateSeen': 5, 'bytesStored': 6, 'trust': 7} + infoNumbers = {'id': 0, 'name': 1, 'adders': 2, 'forwardKey': 3, 'dateSeen': 4, 'bytesStored': 5, 'trust': 6} info = infoNumbers[info] iterCount = 0 retVal = '' @@ -406,7 +406,50 @@ class Core: c.execute('UPDATE peers SET ' + key + ' = ? WHERE id=?', command) conn.commit() conn.close() + return + def getAddressInfo(self, address, info): + ''' + Get info about an address from its database entry + + address text, 0 + type int, 1 + knownPeer text, 2 + speed int, 3 + success int, 4 + DBHash text, 5 + failure int 6 + ''' + conn = sqlite3.connect(self.addressDB) + c = conn.cursor() + command = (address,) + infoNumbers = {'address': 0, 'type': 1, 'knownPeer': 2, 'speed': 3, 'success': 4, 'DBHash': 5, 'failure': 6} + info = infoNumbers[info] + iterCount = 0 + retVal = '' + for row in c.execute('SELECT * from adders where address=?;', command): + for i in row: + if iterCount == info: + retVal = i + break + else: + iterCount += 1 + conn.close() + return retVal + + def setAddressInfo(self, address, key, data): + ''' + Update an address for a key + ''' + conn = sqlite3.connect(self.addressDB) + c = conn.cursor() + command = (data, address) + # TODO: validate key on whitelist + if key not in ('address', 'type', 'knownPeer', 'speed', 'success', 'DBHash', 'failure'): + raise Exception("Got invalid database key when setting address info") + c.execute('UPDATE adders SET ' + key + ' = ? WHERE address=?', command) + conn.commit() + conn.close() return def getBlockList(self, unsaved=False):