From b5fce6461994fa95eb1fc0e7bca33a8fd24ff73f Mon Sep 17 00:00:00 2001 From: TheKysek Date: Sat, 14 Jan 2017 13:42:15 +0100 Subject: [PATCH] Added trusted_peer and listen_for_connections options --- src/main.py | 58 ++++++++++++++++++++++++++------------------------ src/manager.py | 22 ++++++++++--------- src/shared.py | 3 +++ 3 files changed, 45 insertions(+), 38 deletions(-) diff --git a/src/main.py b/src/main.py index 13b561e..3d3316c 100644 --- a/src/main.py +++ b/src/main.py @@ -47,16 +47,17 @@ def main(): shared.core_nodes = {tuple(row) for row in reader} shared.node_pool.update(shared.core_nodes) - try: - for item in socket.getaddrinfo('bootstrap8080.bitmessage.org', 80): - shared.unchecked_node_pool.add((item[4][0], 8080)) - logging.debug('Adding ' + item[4][0] + ' to unchecked_node_pool based on DNS bootstrap method') - for item in socket.getaddrinfo('bootstrap8444.bitmessage.org', 80): - shared.unchecked_node_pool.add((item[4][0], 8444)) - logging.debug('Adding ' + item[4][0] + ' to unchecked_node_pool based on DNS bootstrap method') - except Exception as e: - logging.error('Error during DNS bootstrap') - logging.error(e) + if not shared.trusted_peer: + try: + for item in socket.getaddrinfo('bootstrap8080.bitmessage.org', 80): + shared.unchecked_node_pool.add((item[4][0], 8080)) + logging.debug('Adding ' + item[4][0] + ' to unchecked_node_pool based on DNS bootstrap method') + for item in socket.getaddrinfo('bootstrap8444.bitmessage.org', 80): + shared.unchecked_node_pool.add((item[4][0], 8444)) + logging.debug('Adding ' + item[4][0] + ' to unchecked_node_pool based on DNS bootstrap method') + except Exception as e: + logging.error('Error during DNS bootstrap') + logging.error(e) manager = Manager() manager.clean_objects() @@ -69,25 +70,26 @@ def main(): listener_ipv4 = None listener_ipv6 = None - if socket.has_ipv6: - try: - listener_ipv6 = Listener('', shared.listening_port, family=socket.AF_INET6) - listener_ipv6.start() - except Exception as e: - logging.warning('Error while starting IPv6 listener') - logging.warning(e) + if not shared.listen_for_connections: + if socket.has_ipv6: + try: + listener_ipv6 = Listener('', shared.listening_port, family=socket.AF_INET6) + listener_ipv6.start() + except Exception as e: + logging.warning('Error while starting IPv6 listener') + logging.warning(e) - try: - listener_ipv4 = Listener('', shared.listening_port) - listener_ipv4.start() - except Exception as e: - if listener_ipv6: - logging.warning('Error while starting IPv4 listener. ' - 'However the IPv6 one seems to be working and will probably accept IPv4 connections.') - else: - logging.error('Error while starting IPv4 listener.' - 'You will not receive incoming connections. Please check your port configuration') - logging.error(e) + try: + listener_ipv4 = Listener('', shared.listening_port) + listener_ipv4.start() + except Exception as e: + if listener_ipv6: + logging.warning('Error while starting IPv4 listener. ' + 'However the IPv6 one seems to be working and will probably accept IPv4 connections.') + else: + logging.error('Error while starting IPv4 listener.' + 'You will not receive incoming connections. Please check your port configuration') + logging.error(e) if __name__ == '__main__': main() diff --git a/src/manager.py b/src/manager.py index b6e3b92..fd4c24a 100644 --- a/src/manager.py +++ b/src/manager.py @@ -63,8 +63,10 @@ class Manager(threading.Thread): hosts.add(c.host) if not c.server: outgoing_connections += 1 - if outgoing_connections < shared.outgoing_connections and shared.send_outgoing_connections: - to_connect = set() + to_connect = set() + if shared.trusted_peer: + to_connect.add(shared.trusted_peer) + if outgoing_connections < shared.outgoing_connections and shared.send_outgoing_connections and not shared.trusted_peer: if len(shared.unchecked_node_pool) > 16: to_connect.update(random.sample(shared.unchecked_node_pool, 16)) else: @@ -74,14 +76,14 @@ class Manager(threading.Thread): to_connect.update(random.sample(shared.node_pool, 8)) else: to_connect.update(shared.node_pool) - for addr in to_connect: - if addr[0] in hosts: - continue - c = Connection(addr[0], addr[1]) - c.start() - hosts.add(c.host) - with shared.connections_lock: - shared.connections.add(c) + for addr in to_connect: + if addr[0] in hosts: + continue + c = Connection(addr[0], addr[1]) + c.start() + hosts.add(c.host) + with shared.connections_lock: + shared.connections.add(c) shared.hosts = hosts @staticmethod diff --git a/src/shared.py b/src/shared.py index ed96b1a..a0aadf2 100644 --- a/src/shared.py +++ b/src/shared.py @@ -6,8 +6,11 @@ import threading listening_port = 8444 send_outgoing_connections = True +listen_for_connections = True data_directory = 'minode_data/' source_directory = os.path.dirname(os.path.realpath(__file__)) +trusted_peer = None +# trusted_peer = ('127.0.0.1', 8444) log_level = logging.DEBUG