From e6ecaa5e7d7bedfe9f7271fa390f3d3c6691d51c Mon Sep 17 00:00:00 2001 From: 813492291816 Date: Thu, 3 Nov 2022 17:10:42 -0400 Subject: [PATCH] Add API endpoint to return inbound and outbound connections --- src/api.py | 28 ++++++++++++++++++++++++++++ src/tests/test_api.py | 6 ++++++ 2 files changed, 34 insertions(+) diff --git a/src/api.py b/src/api.py index 8e4ddda4..c31457b0 100644 --- a/src/api.py +++ b/src/api.py @@ -91,6 +91,7 @@ from bmconfigparser import config from debug import logger from helper_sql import SqlBulkExecute, sqlExecute, sqlQuery, sqlStoredProcedure, sql_ready from inventory import Inventory +from network import BMConnectionPool from network.threads import StoppableThread from six.moves import queue from version import softwareVersion @@ -1441,6 +1442,33 @@ class BMRPCDispatcher(object): 'softwareVersion': softwareVersion } + @command('listConnections') + def HandleListConnections(self): + """ + Returns bitmessage connection information as dict with keys *inbound, + *outbound. + """ + inboundConnections = [] + outboundConnections = [] + for i in BMConnectionPool().inboundConnections.values(): + inboundConnections.append({ + 'host': i.destination.host, + 'port': i.destination.port, + 'fullyEstablished': i.fullyEstablished, + 'userAgent': str(i.userAgent) + }) + for i in BMConnectionPool().outboundConnections.values(): + outboundConnections.append({ + 'host': i.destination.host, + 'port': i.destination.port, + 'fullyEstablished': i.fullyEstablished, + 'userAgent': str(i.userAgent) + }) + return { + 'inbound': inboundConnections, + 'outbound': outboundConnections + } + @command('helloWorld') def HandleHelloWorld(self, a, b): """Test two string params""" diff --git a/src/tests/test_api.py b/src/tests/test_api.py index fc0f4db2..95448947 100644 --- a/src/tests/test_api.py +++ b/src/tests/test_api.py @@ -157,6 +157,12 @@ class TestAPI(TestAPIProto): else: self.assertGreater(status["networkConnections"], 0) + def test_listconnections_consistency(self): + """Checking the return of API command 'listConnections'""" + result = json.loads(self.api.listConnections()) + self.assertGreaterEqual(len(result["inbound"]), 0) + self.assertGreaterEqual(len(result["outbound"]), 0) + def test_list_addresses(self): """Checking the return of API command 'listAddresses'""" self.assertEqual(