66 lines
2 KiB
Python
66 lines
2 KiB
Python
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 = {"port_changed", "port_changed_to", "port_change_worked", "mtu_tested", "mtu_changed", "wg_key_changed", "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
|
|
asked_to_change_port: bool = False
|
|
port_changed: bool = False
|
|
port_changed_to: int|None = None
|
|
port_change_worked: bool = False
|
|
|
|
# MTU
|
|
mtu_tested: bool = False
|
|
mtu_changed: bool = False
|
|
asked_to_change_mtu: bool = False
|
|
|
|
# WG KEY
|
|
asked_to_change_wg_key: bool = False
|
|
wg_key_confirmed: bool = False
|
|
wg_key_changed: bool = False
|
|
|
|
# Overall state
|
|
attempts: int = 0
|
|
bypass_attempts: bool = False
|
|
reached_end_of_loop: bool = False
|
|
|
|
stashed_partial: partial = None
|
|
|
|
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()
|
|
logger.info(f"Wiped: {wiped}")
|
|
|