from core.utils.singleton_state import singleton, StatePersistenceMixin, wipe_model from core.Constants import Constants from core.errors.logger import logger from dataclasses import dataclass import os from functools import partial @singleton @dataclass class ConnectionRecovery(StatePersistenceMixin): PAYLOAD_FIELDS = {"prompted_for_port", "asked_to_change_wg_key", "mtu_initially_tested", "location_tested", "location_up"} KEEP_FIELDS = ["parent_folder", "instance_id", "folder_path", "_data_file"] # metadata on it's own instance: parent_folder = None instance_id: int = None folder_path: str = None _data_file: str = None # actual payload data: # LOCATION location_tested: bool = False location_up: bool = False # PORT prompted_for_port: bool = False # MTU mtu_initially_tested: bool = False # WG KEY asked_to_change_wg_key: bool = False # Overall state attempts: int = 0 bypass_attempts: bool = False reached_end_of_loop: bool = False def __post_init__(self): # Localized mode: each ID gets its own folder self.parent_folder = Constants.HV_PROFILE_CONFIG_HOME self.folder_path = os.path.join(self.parent_folder, str(self.instance_id)) os.makedirs(self.folder_path, exist_ok=True) self._data_file = os.path.join(self.folder_path, "recovery.json") logger.info(f"full path of self._data_file {self._data_file}") self.load() def wipe(self): """Wipe all data and start over.""" wipe_model(model=self, KEEP_FIELDS=self.KEEP_FIELDS) wiped = self.wipe_data_on_disk() self.prompted_for_port = False self.asked_to_change_wg_key = False self.location_tested = False self.mtu_initially_tested = False logger.info(f"Wiped: {wiped}")