From 21fe906ac30ed04e888587e32c4197af05aed388 Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Fri, 1 Nov 2024 21:02:40 +0200 Subject: [PATCH] Rewrite duplicate connection handling in i2p.listener, correct except clause --- minode/i2p/listener.py | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/minode/i2p/listener.py b/minode/i2p/listener.py index f63de06..86388d7 100644 --- a/minode/i2p/listener.py +++ b/minode/i2p/listener.py @@ -15,8 +15,6 @@ class I2PListener(I2PThread): self.version_reply = [] - self.new_socket() - def new_socket(self): self.s = socket.create_connection((self.host, self.port)) self._send(b'HELLO VERSION MIN=3.0 MAX=3.3\n') @@ -31,26 +29,32 @@ class I2PListener(I2PThread): def run(self): while not self.state.shutting_down: + self.new_socket() + duplicate = False try: destination = self._receive_line().split()[0] logging.info( 'Incoming I2P connection from: %s', destination.decode()) - - hosts = set() - for c in self.state.connections.copy(): - hosts.add(c.host) - for d in self.state.i2p_dialers.copy(): - hosts.add(d.destination) - if destination in hosts: - logging.debug('Rejecting duplicate I2P connection.') - self.s.close() - else: - c = self.state.connection( - destination, 'i2p', self.s, 'i2p', True, destination) - c.start() - self.state.connections.add(c) - c = None - self.new_socket() except socket.timeout: - pass + continue + + for c in self.state.connections.copy(): + if c.host == destination: + duplicate = True + break + else: + for d in self.state.i2p_dialers.copy(): + if d.destination == destination: + duplicate = True + break + if duplicate: + logging.info('Rejecting duplicate I2P connection.') + self.s.close() + else: + c = self.state.connection( + destination, 'i2p', self.s, 'i2p', True, destination) + c.start() + self.state.connections.add(c) + c = None + logging.debug('Shutting down I2P Listener')