Compare commits

..

1 Commits

Author SHA1 Message Date
jeremybox
2e79b71381
Merge b4ac3df2d0 into 6a392fdb0f 2024-09-05 12:47:41 -06:00
4 changed files with 38 additions and 51 deletions

View File

@ -249,7 +249,7 @@ class Identity:
@staticmethod @staticmethod
def _remember_ratchet(destination_hash, ratchet): def _remember_ratchet(destination_hash, ratchet):
# TODO: Remove at some point, and only log new ratchets # TODO: Remove at some point
RNS.log(f"Remembering ratchet {RNS.prettyhexrep(Identity.truncated_hash(ratchet))} for {RNS.prettyhexrep(destination_hash)}", RNS.LOG_EXTREME) RNS.log(f"Remembering ratchet {RNS.prettyhexrep(Identity.truncated_hash(ratchet))} for {RNS.prettyhexrep(destination_hash)}", RNS.LOG_EXTREME)
try: try:
Identity.known_ratchets[destination_hash] = ratchet Identity.known_ratchets[destination_hash] = ratchet
@ -286,21 +286,20 @@ class Identity:
try: try:
now = time.time() now = time.time()
ratchetdir = RNS.Reticulum.storagepath+"/ratchets" ratchetdir = RNS.Reticulum.storagepath+"/ratchets"
if os.path.isdir(ratchetdir): for filename in os.listdir(ratchetdir):
for filename in os.listdir(ratchetdir): try:
try: expired = False
expired = False with open(f"{ratchetdir}/{filename}", "rb") as rf:
with open(f"{ratchetdir}/{filename}", "rb") as rf: ratchet_data = umsgpack.unpackb(rf.read())
ratchet_data = umsgpack.unpackb(rf.read()) if now > ratchet_data["received"]+Identity.RATCHET_EXPIRY:
if now > ratchet_data["received"]+Identity.RATCHET_EXPIRY: expired = True
expired = True
if expired: if expired:
os.unlink(f"{ratchetdir}/{filename}") os.unlink(f"{ratchetdir}/{filename}")
except Exception as e: except Exception as e:
RNS.log(f"An error occurred while cleaning ratchets, in the processing of {ratchetdir}/{filename}.", RNS.LOG_ERROR) RNS.log(f"An error occurred while cleaning ratchets, in the processing of {ratchetdir}/{filename}.", RNS.LOG_ERROR)
RNS.log(f"The contained exception was: {e}", RNS.LOG_ERROR) RNS.log(f"The contained exception was: {e}", RNS.LOG_ERROR)
except Exception as e: except Exception as e:
RNS.log(f"An error occurred while cleaning ratchets. The contained exception was: {e}", RNS.LOG_ERROR) RNS.log(f"An error occurred while cleaning ratchets. The contained exception was: {e}", RNS.LOG_ERROR)

View File

@ -1677,38 +1677,30 @@ class Transport:
# 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: if packet.context != RNS.Packet.PATH_RESPONSE:
try: for handler in Transport.announce_handlers:
# Check that the announced destination matches try:
# the handlers aspect filter # Check that the announced destination matches
execute_callback = False # the handlers aspect filter
announce_identity = RNS.Identity.recall(packet.destination_hash) execute_callback = False
if handler.aspect_filter == None: announce_identity = RNS.Identity.recall(packet.destination_hash)
# If the handlers aspect filter is set to if handler.aspect_filter == None:
# None, we execute the callback in all cases # If the handlers aspect filter is set to
execute_callback = True # None, we execute the callback in all cases
else:
handler_expected_hash = RNS.Destination.hash_from_name_and_identity(handler.aspect_filter, announce_identity)
if packet.destination_hash == handler_expected_hash:
execute_callback = True execute_callback = True
# If this is a path response, check whether the
# handler wants to receive it.
if packet.context == RNS.Packet.PATH_RESPONSE:
if hasattr(handler, "receive_path_responses") and handler.receive_path_responses == True:
pass
else: else:
execute_callback = False handler_expected_hash = RNS.Destination.hash_from_name_and_identity(handler.aspect_filter, announce_identity)
if packet.destination_hash == handler_expected_hash:
if execute_callback: execute_callback = True
handler.received_announce( if execute_callback:
destination_hash=packet.destination_hash, handler.received_announce(
announced_identity=announce_identity, destination_hash=packet.destination_hash,
app_data=RNS.Identity.recall_app_data(packet.destination_hash) announced_identity=announce_identity,
) app_data=RNS.Identity.recall_app_data(packet.destination_hash)
except Exception as e: )
RNS.log("Error while processing external announce callback.", RNS.LOG_ERROR) except Exception as e:
RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR) RNS.log("Error while processing external announce callback.", RNS.LOG_ERROR)
RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR)
# Handling for link requests to local destinations # Handling for link requests to local destinations
elif packet.packet_type == RNS.Packet.LINKREQUEST: elif packet.packet_type == RNS.Packet.LINKREQUEST:
@ -1987,9 +1979,7 @@ class Transport:
""" """
Registers an announce handler. Registers an announce handler.
:param handler: Must be an object with an *aspect_filter* attribute and a *received_announce(destination_hash, announced_identity, app_data)* :param handler: Must be an object with an *aspect_filter* attribute and a *received_announce(destination_hash, announced_identity, app_data)* callable. See the :ref:`Announce Example<example-announce>` for more info.
callable. Can optionally have a *receive_path_responses* attribute set to ``True``, to also receive all path responses, in addition to live
announces. See the :ref:`Announce Example<example-announce>` for more info.
""" """
if hasattr(handler, "received_announce") and callable(handler.received_announce): if hasattr(handler, "received_announce") and callable(handler.received_announce):
if hasattr(handler, "aspect_filter"): if hasattr(handler, "aspect_filter"):

View File

@ -1898,9 +1898,7 @@ Transport system of Reticulum.</p>
<dd><p>Registers an announce handler.</p> <dd><p>Registers an announce handler.</p>
<dl class="field-list simple"> <dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt> <dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><p><strong>handler</strong> Must be an object with an <em>aspect_filter</em> attribute and a <em>received_announce(destination_hash, announced_identity, app_data)</em> <dd class="field-odd"><p><strong>handler</strong> Must be an object with an <em>aspect_filter</em> attribute and a <em>received_announce(destination_hash, announced_identity, app_data)</em> callable. See the <a class="reference internal" href="examples.html#example-announce"><span class="std std-ref">Announce Example</span></a> for more info.</p>
callable. Can optionally have a <em>receive_path_responses</em> attribute set to <code class="docutils literal notranslate"><span class="pre">True</span></code>, to also receive all path responses, in addition to live
announces. See the <a class="reference internal" href="examples.html#example-announce"><span class="std std-ref">Announce Example</span></a> for more info.</p>
</dd> </dd>
</dl> </dl>
</dd></dl> </dd></dl>

File diff suppressed because one or more lines are too long