From 4a4b625075562f9782ade160b6e98b97a0c6e6a7 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Fri, 22 Nov 2024 14:07:48 +0100 Subject: [PATCH] Implemented custom interface loading --- RNS/Reticulum.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/RNS/Reticulum.py b/RNS/Reticulum.py index 61ff121..4e0da75 100755 --- a/RNS/Reticulum.py +++ b/RNS/Reticulum.py @@ -148,6 +148,7 @@ class Reticulum: configpath = "" storagepath = "" cachepath = "" + interfacepath = "" __instance = None @@ -218,6 +219,7 @@ class Reticulum: Reticulum.cachepath = Reticulum.configdir+"/storage/cache" Reticulum.resourcepath = Reticulum.configdir+"/storage/resources" Reticulum.identitypath = Reticulum.configdir+"/storage/identities" + Reticulum.interfacepath = Reticulum.configdir+"/interfaces" Reticulum.__transport_enabled = False Reticulum.__remote_management_enabled = False @@ -263,6 +265,9 @@ class Reticulum: if not os.path.isdir(Reticulum.identitypath): os.makedirs(Reticulum.identitypath) + if not os.path.isdir(Reticulum.interfacepath): + os.makedirs(Reticulum.interfacepath) + if os.path.isfile(self.configpath): try: self.config = ConfigObj(self.configpath) @@ -671,6 +676,34 @@ class Reticulum: interface_post_init(interface) interface.start() + if interface == None: + # Interface was not handled by any internal interface types, + # attempt to load and initialise it from user-supplied modules + interface_type = c["type"] + interface_file = f"{interface_type}.py" + interface_path = os.path.join(self.interfacepath, interface_file) + if not os.path.isfile(interface_path): + RNS.log(f"Could not locate external interface module \"{interface_file}\" in \"{self.interfacepath}\"", RNS.LOG_ERROR) + + else: + try: + RNS.log(f"Loading external interface \"{interface_file}\" from \"{self.interfacepath}\"", RNS.LOG_NOTICE) + interface_globals = {} + interface_globals["Interface"] = Interface.Interface + interface_globals["RNS"] = RNS + with open(interface_path) as class_file: + interface_code = class_file.read() + exec(interface_code, interface_globals) + interface_class = interface_globals["interface_class"] + + if interface_class != None: + interface = interface_class(RNS.Transport, interface_config) + interface_post_init(interface) + + except Exception as e: + RNS.log(f"External interface initialisation failed for {interface_type} / {name}", RNS.LOG_ERROR) + RNS.trace_exception(e) + else: RNS.log("Skipping disabled interface \""+name+"\"", RNS.LOG_DEBUG)