From 75e32af1c5652901f27bd15a6021906ca47b88c8 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Tue, 13 Sep 2022 20:17:25 +0200 Subject: [PATCH] Added periodic data persistence for shared and standalone instances --- RNS/Identity.py | 9 +++++++-- RNS/Reticulum.py | 46 +++++++++++++++++++++++++++++----------------- RNS/Transport.py | 10 +++++++--- 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/RNS/Identity.py b/RNS/Identity.py index 075c818..343fe48 100644 --- a/RNS/Identity.py +++ b/RNS/Identity.py @@ -139,7 +139,7 @@ class Identity: if not destination_hash in Identity.known_destinations: Identity.known_destinations[destination_hash] = storage_known_destinations[destination_hash] - RNS.log("Saving known destinations to storage...", RNS.LOG_VERBOSE) + RNS.log("Saving "+str(len(Identity.known_destinations))+" known destinations to storage...", RNS.LOG_VERBOSE) file = open(RNS.Reticulum.storagepath+"/known_destinations","wb") umsgpack.dump(Identity.known_destinations, file) file.close() @@ -246,9 +246,14 @@ class Identity: RNS.log("Error occurred while validating announce. The contained exception was: "+str(e), RNS.LOG_ERROR) return False + @staticmethod + def persist_data(): + if not RNS.Transport.owner.is_connected_to_shared_instance: + Identity.save_known_destinations() + @staticmethod def exit_handler(): - Identity.save_known_destinations() + Identity.persist_data() @staticmethod diff --git a/RNS/Reticulum.py b/RNS/Reticulum.py index db0e6d5..2e307ee 100755 --- a/RNS/Reticulum.py +++ b/RNS/Reticulum.py @@ -119,25 +119,27 @@ class Reticulum: # Length of truncated hashes in bits. TRUNCATED_HASHLENGTH = 128 - HEADER_MINSIZE = 2+1+(TRUNCATED_HASHLENGTH//8)*1 - HEADER_MAXSIZE = 2+1+(TRUNCATED_HASHLENGTH//8)*2 - IFAC_MIN_SIZE = 1 - IFAC_SALT = bytes.fromhex("adf54d882c9a9b80771eb4995d702d4a3e733391b2a0f53f416d9f907e55cff8") + HEADER_MINSIZE = 2+1+(TRUNCATED_HASHLENGTH//8)*1 + HEADER_MAXSIZE = 2+1+(TRUNCATED_HASHLENGTH//8)*2 + IFAC_MIN_SIZE = 1 + IFAC_SALT = bytes.fromhex("adf54d882c9a9b80771eb4995d702d4a3e733391b2a0f53f416d9f907e55cff8") - MDU = MTU - HEADER_MAXSIZE - IFAC_MIN_SIZE + MDU = MTU - HEADER_MAXSIZE - IFAC_MIN_SIZE - RESOURCE_CACHE = 24*60*60 - JOB_INTERVAL = 15*60 + RESOURCE_CACHE = 24*60*60 + JOB_INTERVAL = 5*60 + CLEAN_INTERVAL = 15*60 + PERSIST_INTERVAL = 60*60*12 - router = None - config = None + router = None + config = None # The default configuration path will be expanded to a directory # named ".reticulum" inside the current users home directory - configdir = os.path.expanduser("~")+"/.reticulum" - configpath = "" - storagepath = "" - cachepath = "" + configdir = os.path.expanduser("~")+"/.reticulum" + configpath = "" + storagepath = "" + cachepath = "" @staticmethod def exit_handler(): @@ -206,6 +208,8 @@ class Reticulum: self.is_connected_to_shared_instance = False self.is_standalone_instance = False self.jobs_thread = None + self.last_data_persist = time.time() + self.last_cache_clean = 0 if not os.path.isdir(Reticulum.storagepath): os.makedirs(Reticulum.storagepath) @@ -230,7 +234,6 @@ class Reticulum: RNS.log("Could not load config file, creating default configuration file...") self.__create_default_config() RNS.log("Default config file created. Make any necessary changes in "+Reticulum.configdir+"/config and restart Reticulum if needed.") - import time time.sleep(1.5) self.__apply_config() @@ -261,8 +264,15 @@ class Reticulum: def __jobs(self): while True: - # Clean caches - self.__clean_caches() + now = time.time() + + if now > self.last_cache_clean+Reticulum.CLEAN_INTERVAL: + self.__clean_caches() + self.last_cache_clean = time.time() + + if now > self.last_data_persist+Reticulum.PERSIST_INTERVAL: + self.__persist_data() + self.last_data_persist = time.time() time.sleep(Reticulum.JOB_INTERVAL) @@ -928,7 +938,9 @@ class Reticulum: RNS.Transport.interfaces.append(interface) - + def __persist_data(self): + RNS.Transport.persist_data() + RNS.Identity.persist_data() def __clean_caches(self): RNS.log("Cleaning resource and packet caches...", RNS.LOG_EXTREME) diff --git a/RNS/Transport.py b/RNS/Transport.py index 56cb35f..0e3ae6d 100755 --- a/RNS/Transport.py +++ b/RNS/Transport.py @@ -2240,9 +2240,13 @@ class Transport: Transport.saving_tunnel_table = False + @staticmethod + def persist_data(): + Transport.save_packet_hashlist() + Transport.save_path_table() + Transport.save_tunnel_table() @staticmethod def exit_handler(): - Transport.save_packet_hashlist() - Transport.save_path_table() - Transport.save_tunnel_table() \ No newline at end of file + if not Transport.owner.is_connected_to_shared_instance: + Transport.persist_data() \ No newline at end of file