Rewrite duplicate connection handling in i2p.listener, correct except clause

This commit is contained in:
Lee Miller 2024-11-01 21:02:40 +02:00
parent efc4be0ec1
commit 21fe906ac3
Signed by: lee.miller
GPG Key ID: 4F97A5EA88F4AB63

View File

@ -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,18 +29,26 @@ 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())
except socket.timeout:
continue
hosts = set()
for c in self.state.connections.copy():
hosts.add(c.host)
if c.host == destination:
duplicate = True
break
else:
for d in self.state.i2p_dialers.copy():
hosts.add(d.destination)
if destination in hosts:
logging.debug('Rejecting duplicate I2P connection.')
if d.destination == destination:
duplicate = True
break
if duplicate:
logging.info('Rejecting duplicate I2P connection.')
self.s.close()
else:
c = self.state.connection(
@ -50,7 +56,5 @@ class I2PListener(I2PThread):
c.start()
self.state.connections.add(c)
c = None
self.new_socket()
except socket.timeout:
pass
logging.debug('Shutting down I2P Listener')