Work on tunnels

This commit is contained in:
Mark Qvist 2021-09-18 19:08:45 +02:00
parent c1e280d896
commit 80455c9614

View File

@ -781,10 +781,18 @@ class Transport:
new_announce.hops = packet.hops new_announce.hops = packet.hops
new_announce.send() new_announce.send()
destination_table_entry = [now, received_from, announce_hops, expires, random_blobs, packet.receiving_interface, packet]
Transport.destination_table[packet.destination_hash] = [now, received_from, announce_hops, expires, random_blobs, packet.receiving_interface, packet] Transport.destination_table[packet.destination_hash] = destination_table_entry
RNS.log("Path to "+RNS.prettyhexrep(packet.destination_hash)+" is now "+str(announce_hops)+" hops away via "+RNS.prettyhexrep(received_from)+" on "+str(packet.receiving_interface), RNS.LOG_VERBOSE) RNS.log("Path to "+RNS.prettyhexrep(packet.destination_hash)+" is now "+str(announce_hops)+" hops away via "+RNS.prettyhexrep(received_from)+" on "+str(packet.receiving_interface), RNS.LOG_VERBOSE)
# If the receiving interface is a tunnel, we add the
# announce to the tunnels table
if hasattr(packet.receiving_interface, "tunnel_id") and packet.receiving_interface.tunnel_id != None:
tunnel_entry = Transport.tunnels[packet.receiving_interface.tunnel_id]
paths = tunnel_entry[2]
paths[packet.destination_hash] = destination_table_entry
RNS.log("Path to "+RNS.prettyhexrep(packet.destination_hash)+" associated with tunnel "+RNS.prettyhexrep(packet.receiving_interface.tunnel_id), RNS.LOG_VERBOSE)
# Call externally registered callbacks from apps # Call externally registered callbacks from apps
# wanting to know when an announce arrives # wanting to know when an announce arrives
for handler in Transport.announce_handlers: for handler in Transport.announce_handlers:
@ -928,13 +936,13 @@ class Transport:
tnl_snth_dst = RNS.Destination(None, RNS.Destination.OUT, RNS.Destination.PLAIN, Transport.APP_NAME, "tunnel", "synthesize") tnl_snth_dst = RNS.Destination(None, RNS.Destination.OUT, RNS.Destination.PLAIN, Transport.APP_NAME, "tunnel", "synthesize")
# TODO: Remove # TODO: Remove
RNS.log("Tunnel synth for "+str(interface)) # RNS.log("Tunnel synth for "+str(interface))
RNS.log("Transport ID : "+str(Transport.identity)) # RNS.log("Transport ID : "+str(Transport.identity))
RNS.log("Tunnel ID : "+RNS.hexrep(tunnel_id)) # RNS.log("Tunnel ID : "+RNS.hexrep(tunnel_id))
RNS.log("IF hash : "+RNS.hexrep(interface_hash)) # RNS.log("IF hash : "+RNS.hexrep(interface_hash))
RNS.log("Rnd hash : "+RNS.hexrep(random_hash)) # RNS.log("Rnd hash : "+RNS.hexrep(random_hash))
RNS.log("Public key : "+RNS.hexrep(public_key)) # RNS.log("Public key : "+RNS.hexrep(public_key))
RNS.log("Signature : "+RNS.hexrep(signature)) # RNS.log("Signature : "+RNS.hexrep(signature))
packet = RNS.Packet(tnl_snth_dst, data, packet_type = RNS.Packet.DATA, transport_type = RNS.Transport.BROADCAST, header_type = RNS.Packet.HEADER_1, attached_interface = interface) packet = RNS.Packet(tnl_snth_dst, data, packet_type = RNS.Packet.DATA, transport_type = RNS.Transport.BROADCAST, header_type = RNS.Packet.HEADER_1, attached_interface = interface)
packet.send() packet.send()
@ -943,44 +951,45 @@ class Transport:
@staticmethod @staticmethod
def tunnel_synthesize_handler(data, packet): def tunnel_synthesize_handler(data, packet):
# TODO: Remove try:
RNS.log("Received tunnel synthesize packet ("+str(len(data))+"):\n"+RNS.hexrep(data)) expected_length = RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8+RNS.Reticulum.TRUNCATED_HASHLENGTH//8+RNS.Identity.SIGLENGTH//8
if len(data) == expected_length:
public_key = data[:RNS.Identity.KEYSIZE//8]
interface_hash = data[RNS.Identity.KEYSIZE//8:RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8]
tunnel_id_data = public_key+interface_hash
tunnel_id = RNS.Identity.full_hash(tunnel_id_data)
random_hash = data[RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8:RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8+RNS.Reticulum.TRUNCATED_HASHLENGTH//8]
signature = data[RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8+RNS.Reticulum.TRUNCATED_HASHLENGTH//8:expected_length]
signed_data = tunnel_id_data+random_hash
expected_length = RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8+RNS.Reticulum.TRUNCATED_HASHLENGTH//8+RNS.Identity.SIGLENGTH//8 remote_transport_identity = RNS.Identity(create_keys=False)
if len(data) == expected_length: remote_transport_identity.load_public_key(public_key)
public_key = data[:RNS.Identity.KEYSIZE//8]
interface_hash = data[RNS.Identity.KEYSIZE//8:RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8]
tunnel_id_data = public_key+interface_hash
tunnel_id = RNS.Identity.full_hash(tunnel_id_data)
random_hash = data[RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8:RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8+RNS.Reticulum.TRUNCATED_HASHLENGTH//8]
signature = data[RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8+RNS.Reticulum.TRUNCATED_HASHLENGTH//8:expected_length]
signed_data = tunnel_id_data+random_hash
remote_transport_identity = RNS.Identity(create_keys=False)
remote_transport_identity.load_public_key(public_key)
# TODO: Remove
RNS.log("Transport ID : "+str(remote_transport_identity))
RNS.log("Tunnel ID : "+RNS.hexrep(tunnel_id))
RNS.log("IF hash : "+RNS.hexrep(interface_hash))
RNS.log("Rnd hash : "+RNS.hexrep(random_hash))
RNS.log("Public key : "+RNS.hexrep(public_key))
RNS.log("Signature : "+RNS.hexrep(signature))
if remote_transport_identity.validate(signature, signed_data):
RNS.log("Signature is valid")
Transport.handle_tunnel(tunnel_id, packet.receiving_interface)
else:
# TODO: Remove # TODO: Remove
RNS.log("Signature is invalid") # RNS.log("Transport ID : "+str(remote_transport_identity))
# RNS.log("Tunnel ID : "+RNS.hexrep(tunnel_id))
# RNS.log("IF hash : "+RNS.hexrep(interface_hash))
# RNS.log("Rnd hash : "+RNS.hexrep(random_hash))
# RNS.log("Public key : "+RNS.hexrep(public_key))
# RNS.log("Signature : "+RNS.hexrep(signature))
if remote_transport_identity.validate(signature, signed_data):
# RNS.log("Signature is valid")
Transport.handle_tunnel(tunnel_id, packet.receiving_interface)
# else:
# TODO: Remove
# RNS.log("Signature is invalid")
except Exception as e:
RNS.log("An error occurred while validating tunnel establishment packet.", RNS.LOG_DEBUG)
RNS.log("The contained exception was: "+str(e), RNS.LOG_DEBUG)
@staticmethod @staticmethod
def handle_tunnel(tunnel_id, interface): def handle_tunnel(tunnel_id, interface):
if not tunnel_id in Transport.tunnels: if not tunnel_id in Transport.tunnels:
RNS.log("Tunnel endpoint "+RNS.prettyhexrep(tunnel_id)+" established.", RNS.LOG_DEBUG) RNS.log("Tunnel endpoint "+RNS.prettyhexrep(tunnel_id)+" established.", RNS.LOG_DEBUG)
announces = [] paths = []
tunnel_entry = [tunnel_id, interface, announces] tunnel_entry = [tunnel_id, interface, paths]
interface.tunnel_id = tunnel_id interface.tunnel_id = tunnel_id
Transport.tunnels[tunnel_id] = tunnel_entry Transport.tunnels[tunnel_id] = tunnel_entry
else: else:
@ -988,9 +997,9 @@ class Transport:
tunnel_entry = Transport.tunnels[tunnel_id] tunnel_entry = Transport.tunnels[tunnel_id]
tunnel_entry[1] = interface tunnel_entry[1] = interface
interface.tunnel_id = tunnel_id interface.tunnel_id = tunnel_id
announces = tunnel_entry[2] paths = tunnel_entry[2]
for announce in announces: for path_entry in paths:
# Reassign paths # Reassign paths
pass pass