Compare commits

...

2 Commits

Author SHA1 Message Date
Lee Miller 44aaccb7a4
Make a session-wide set of junk vectors to not request repeatedly 2023-08-17 02:32:12 +03:00
Lee Miller 46ea5e0744
Define an additional object validation method is_junk(),
checking curve number and key length in encrypted payload.
2023-08-17 02:32:06 +03:00
3 changed files with 31 additions and 0 deletions

View File

@ -384,6 +384,7 @@ class Connection(threading.Thread):
logging.debug('%s:%s -> %s', self.host_print, self.port, inv)
to_get = inv.vectors.copy()
to_get.difference_update(shared.objects.keys())
to_get.difference_update(shared.junk_vectors)
self.vectors_to_get.update(to_get)
# Do not send objects they already have.
self.vectors_to_send.difference_update(inv.vectors)
@ -395,6 +396,8 @@ class Connection(threading.Thread):
self.vectors_to_get.discard(obj.vector)
if obj.is_valid() and obj.vector not in shared.objects:
with shared.objects_lock:
if obj.is_junk():
return shared.junk_vectors.add(obj.vector)
shared.objects[obj.vector] = obj
if (
obj.object_type == shared.i2p_dest_obj_type

View File

@ -16,6 +16,8 @@ ip_enabled = True
log_level = logging.INFO
curve = 714 # secp256k1
key_length = 32
magic_bytes = b'\xe9\xbe\xb4\xd9'
protocol_version = 3
services = 3 # NODE_NETWORK, NODE_SSL
@ -63,4 +65,5 @@ outgoing_connections = 8
connection_limit = 250
objects = {}
junk_vectors = set()
objects_lock = threading.Lock()

View File

@ -132,6 +132,31 @@ class Object():
return False
return True
def is_junk(self):
"""
Returns True if an object with encrypted payload has
curve number or key length different from those defined in shared.
"""
if self.object_type not in (1, 2, 3):
return False
if self.object_type == 2:
sp = 0
elif self.object_type == 1 and self.version != 4:
return False
else:
sp = 32
sp += 16
curve = struct.unpack('!H', self.object_payload[sp:sp + 2])[0]
if curve != shared.curve:
return True
length = struct.unpack('!H', self.object_payload[sp + 2:sp + 4])[0]
if length > shared.key_length:
return True
length = struct.unpack('!H', self.object_payload[sp + 36:sp + 38])[0]
if length > shared.key_length:
return True
return False
def pow_target(self):
"""Compute PoW target"""
data = self.to_bytes()[8:]