sp-hydra-veil-core/core/controllers/SystemStateController.py

79 lines
2.5 KiB
Python

from core.models.system.SystemState import SystemState
from core.models.enum_models import ProtocolChoice
# from core.controllers.ProfileController import ProfileController
from core.infrastructure.global_state import ProfileStateRepository
from core.observers.Notifications import Notifications, EventType
from core.errors.logger import logger
class SystemStateController:
@staticmethod
def get():
return SystemState.get()
@staticmethod
def exists():
return SystemState.exists()
@staticmethod
def create(profile_id: int, firewalled: bool, dns_set: bool, protocol: ProtocolChoice, process_id: int = None) -> SystemState:
# model on disk
current_state = SystemState(profile_id, firewalled, dns_set, protocol, process_id)
current_state.save()
# cache (Sync to Repo)
ProfileStateRepository().sync_system_state(current_state)
return current_state
@staticmethod
def update_or_create(system_state):
"""Update the model & cache for a systemwide connection"""
# model
system_state.save()
# cache (Sync to Repo)
ProfileStateRepository().sync_system_state(system_state)
# notify is done separately because this function can be used to modify state in the event of firewall failure.
@staticmethod
def dissolve():
# get singleton:
state_repo = ProfileStateRepository()
# extract current active id:
profile_id = state_repo.global_state.systemwide_profile_id
# model wipes data on disk:
SystemState.dissolve()
# Wipe the Repo's Cache:
state_repo.turn_off_system_connection()
# notify UI with that past id:
Notifications.emit(EventType.SYSTEM_DISABLE, profile_id)
# old version:
# ProfileStateRepository().global_state.systemwide_profile_id = None
# @staticmethod
# def notify_on():
# """Notify the notifications that systemwide state shows on"""
# # get singleton:
# state_repo = ProfileStateRepository()
# # extract current data:
# profile_id = state_repo.global_state.systemwide_profile_id
# systemwide_active = state_repo.global_state.systemwide_active
# # double check, confirm:
# if systemwide_active and systemwide_active:
# # notify UI:
# Notifications.emit(EventType.SYSTEM_ENABLE, profile_id)
# else:
# logger.error("Critical Error, notify_on was called out of place.")
# Notifications.emit(EventType.SYSTEM_DISABLE, profile_id)