From 84a1ab0ca39d4a4842bccf055071296a16f5509a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Schulthess?= Date: Mon, 29 Apr 2024 07:59:55 +0200 Subject: [PATCH] add option to load identity from file --- Examples/Echo.py | 18 +++++++++++++++--- Examples/Link.py | 18 +++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Examples/Echo.py b/Examples/Echo.py index e5b3930..abb79ff 100644 --- a/Examples/Echo.py +++ b/Examples/Echo.py @@ -5,6 +5,7 @@ # of the packet. # ########################################################## +import os import argparse import RNS @@ -27,8 +28,19 @@ def server(configpath): # We must first initialise Reticulum reticulum = RNS.Reticulum(configpath) - # Randomly create a new identity for our echo server - server_identity = RNS.Identity() + # Load identity from file if it exist or randomley create + if configpath: + ifilepath = "%s/storage/identitiesy/%s" % (configpath,APP_NAME) + else: + ifilepath = "%s/storage/identities/%s" % (RNS.Reticulum.configdir,APP_NAME) + if os.path.exists(ifilepath): + # Load identity from file + server_identity = RNS.Identity.from_file(ifilepath) + RNS.log("loaded identity from file: "+ifilepath, RNS.LOG_VERBOSE) + else: + # Randomly create a new identity for our link examples + server_identity = RNS.Identity() + RNS.log("created new identity", RNS.LOG_VERBOSE) # We create a destination that clients can query. We want # to be able to verify echo replies to our clients, so we @@ -328,4 +340,4 @@ if __name__ == "__main__": client(args.destination, configarg, timeout=timeoutarg) except KeyboardInterrupt: print("") - exit() \ No newline at end of file + exit() diff --git a/Examples/Link.py b/Examples/Link.py index 6b3210c..37de9dd 100644 --- a/Examples/Link.py +++ b/Examples/Link.py @@ -28,8 +28,20 @@ def server(configpath): # We must first initialise Reticulum reticulum = RNS.Reticulum(configpath) - # Randomly create a new identity for our link example - server_identity = RNS.Identity() + # Load identity from file if it exist or randomley create + if configpath: + ifilepath = "%s/storage/identitiesy/%s" % (configpath,APP_NAME) + else: + ifilepath = "%s/storage/identities/%s" % (RNS.Reticulum.configdir,APP_NAME) + RNS.log("ifilepath: %s" % ifilepath) + if os.path.exists(ifilepath): + # Load identity from file + server_identity = RNS.Identity.from_file(ifilepath) + RNS.log("loaded identity from file: "+ifilepath, RNS.LOG_VERBOSE) + else: + # Randomly create a new identity for our link examples + server_identity = RNS.Identity() + RNS.log("created new identity", RNS.LOG_VERBOSE) # We create a destination that clients can connect to. We # want clients to create links to this destination, so we @@ -288,4 +300,4 @@ if __name__ == "__main__": except KeyboardInterrupt: print("") - exit() \ No newline at end of file + exit()