From d0593ef30041ce638379736b9604453437b18fdb Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Tue, 27 Feb 2018 15:23:49 -0600 Subject: [PATCH] added address add command and test --- onionr/api.py | 5 +++++ onionr/bitpeer | 2 +- onionr/core.py | 28 +++++++++++++++++++++++++++- onionr/onionr.py | 18 +++++++++++++++++- onionr/tests.py | 14 +++++++++++++- 5 files changed, 63 insertions(+), 4 deletions(-) diff --git a/onionr/api.py b/onionr/api.py index 4175ecdf..7550f1c7 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -149,6 +149,11 @@ class API: abort(404) resp = "" resp = Response(resp) + elif action == 'pex': + response = ','.join(self._core.listAdders()) + if len(response) == 0: + response = 'none' + resp = Response(response) else: resp = Response("") diff --git a/onionr/bitpeer b/onionr/bitpeer index 90d5edad..a74e826e 160000 --- a/onionr/bitpeer +++ b/onionr/bitpeer @@ -1 +1 @@ -Subproject commit 90d5edad0c017cec1a3b190d9b0cd08321840ed8 +Subproject commit a74e826e9c69e643ead7950f9f76a05ab8664ddc diff --git a/onionr/core.py b/onionr/core.py index e070218a..cb52653c 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -71,7 +71,33 @@ class Core: conn.commit() conn.close() return True - + + def addAddress(self, address): + '''Add an address to the address database (only tor currently)''' + if self._utils.validateID(address): + conn = sqlite3.connect(self.addressDB) + c = conn.cursor() + t = (address, 1) + c.execute('INSERT INTO adders (address, type) VALUES(?, ?);', t) + conn.commit() + conn.close() + return True + else: + return False + + def removeAddress(self, address): + '''Remove an address from the address database''' + if self._utils.validateID(address): + conn = sqlite3.connect(self.addressDB) + c = conn.cursor() + t = (address,) + c.execute('Delete from adders where address=?;', t) + conn.commit() + conn.close() + return True + else: + return False + def createAddressDB(self): ''' Generate the address database diff --git a/onionr/onionr.py b/onionr/onionr.py index 8591322b..ee18f02c 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -146,7 +146,10 @@ class Onionr: 'pm': self.sendEncrypt, 'gui': self.openGUI, 'addpeer': self.addPeer, - 'add-peer': self.addPeer + 'add-peer': self.addPeer, + 'add-address': self.addAddress, + 'connect': self.addAddress, + 'addaddress': self.addAddress } def getHelp(self): @@ -251,6 +254,19 @@ class Onionr: else: logger.info("Adding peer: " + logger.colors.underline + newPeer) self.onionrCore.addPeer(newPeer) + + def addAddress(self): + '''Adds a Onionr node address''' + try: + newAddress = sys.argv[2] + except: + pass + else: + logger.info("Adding address: " + logger.colors.underline + newAddress) + if self.onionrCore.addAddress(newAddress): + logger.info("Successfully added address") + else: + logger.warn("Unable to add address") def addMessage(self): ''' diff --git a/onionr/tests.py b/onionr/tests.py index 82472333..6a70186c 100755 --- a/onionr/tests.py +++ b/onionr/tests.py @@ -115,5 +115,17 @@ class OnionrTests(unittest.TestCase): self.assertTrue(True) else: self.assertTrue(False) - + + def testAddAdder(self): + logger.debug('--------------------------') + logger.info('Running address add+remove test') + import core + myCore = core.Core() + if myCore.addAddress('facebookcorewwwi.onion') and not myCore.removeAddress('invalid'): + if myCore.removeAddress('facebookcorewwwi.onion'): + self.assertTrue(True) + else: + self.assertTrue(False) + else: + self.assertTrue(False) unittest.main()