Define a static method network_group() in NetAddrNoPrefix

and use it in manager.
This commit is contained in:
Lee Miller 2023-08-24 00:23:54 +03:00
parent ba897c8d40
commit 78f170451b
Signed by untrusted user: lee.miller
GPG Key ID: 4F97A5EA88F4AB63
2 changed files with 24 additions and 8 deletions

View File

@ -77,7 +77,7 @@ class Manager(threading.Thread):
with shared.connections_lock:
shared.connections.remove(c)
else:
hosts.add(c.host)
hosts.add(structure.NetAddrNoPrefix.network_group(c.host))
if not c.server:
outgoing_connections += 1
@ -119,15 +119,16 @@ class Manager(threading.Thread):
else:
to_connect.update(shared.i2p_node_pool)
for addr in to_connect:
if addr[0] in hosts:
for host, port in to_connect:
group = structure.NetAddrNoPrefix.network_group(host)
if group in hosts:
continue
if addr[1] == 'i2p' and shared.i2p_enabled:
if shared.i2p_session_nick and addr[0] != shared.i2p_dest_pub:
if port == 'i2p' and shared.i2p_enabled:
if shared.i2p_session_nick and host != shared.i2p_dest_pub:
try:
d = I2PDialer(
shared,
addr[0], shared.i2p_session_nick,
host, shared.i2p_session_nick,
shared.i2p_sam_host, shared.i2p_sam_port)
d.start()
hosts.add(d.destination)
@ -139,9 +140,9 @@ class Manager(threading.Thread):
else:
continue
else:
c = Connection(addr[0], addr[1])
c = Connection(host, port)
c.start()
hosts.add(c.host)
hosts.add(group)
with shared.connections_lock:
shared.connections.add(c)
shared.hosts = hosts

View File

@ -173,6 +173,21 @@ class NetAddrNoPrefix():
b += struct.pack('>H', int(self.port))
return b
@staticmethod
def network_group(host):
"""A simplified network group identifier from pybitmessage protocol"""
try:
host = socket.inet_pton(socket.AF_INET, host)
return host[:2]
except socket.error:
try:
host = socket.inet_pton(socket.AF_INET6, host)
return host[:12]
except OSError:
return host
except TypeError:
return host
@classmethod
def from_bytes(cls, b):
services, host, port = struct.unpack('>Q16sH', b)