Compare commits

..

3 commits

9 changed files with 192 additions and 29 deletions

View file

@ -1,19 +1,26 @@
from core.models.session.SessionState import SessionState from core.models.session.SessionState import SessionState
from core.controllers.ProfileController import ProfileController
from core.infrastructure.global_state import ProfileStateRepository
class SessionStateController: class SessionStateController:
@staticmethod @staticmethod
def get(id: int): def get(id: int):
return SessionState.find_by_id(id) return SessionState.find_by_id(id)
@staticmethod @staticmethod
def get_or_new(id: int): def get_or_new(id: int):
session_state = SessionStateController.get(id) session_state = SessionStateController.get(id)
if session_state is None: if session_state is None:
return SessionState(id) # Update the Singleton Repo
repo = ProfileStateRepository()
repo.sync_session_added(id)
# Create and save the session
session_state = SessionState(id)
session_state.save()
return session_state
return session_state return session_state
@ -28,3 +35,10 @@ class SessionStateController:
@staticmethod @staticmethod
def update_or_create(session_state): def update_or_create(session_state):
session_state.save() session_state.save()
# Ensure repo knows this session exists
ProfileStateRepository().sync_session_added(session_state.id)
@staticmethod
def remove(profile_id: int):
SessionState.dissolve(profile_id)
ProfileStateRepository().sync_session_removed(profile_id)

View file

@ -1,8 +1,10 @@
from core.models.system.SystemState import SystemState from core.models.system.SystemState import SystemState
from core.models.enum_models import ProtocolChoice from core.models.enum_models import ProtocolChoice
# from core.controllers.ProfileController import ProfileController
from core.infrastructure.global_state import ProfileStateRepository
class SystemStateController: class SystemStateController:
@staticmethod @staticmethod
def get(): def get():
return SystemState.get() return SystemState.get()
@ -13,17 +15,23 @@ class SystemStateController:
@staticmethod @staticmethod
def create(profile_id: int, firewalled: bool, dns_set: bool, protocol: ProtocolChoice, process_id: int = None) -> SystemState: def create(profile_id: int, firewalled: bool, dns_set: bool, protocol: ProtocolChoice, process_id: int = None) -> SystemState:
if process_id:
current_state = SystemState(profile_id, firewalled, dns_set, protocol, process_id) current_state = SystemState(profile_id, firewalled, dns_set, protocol, process_id)
else:
current_state = SystemState(profile_id, firewalled, dns_set, protocol)
current_state.save() current_state.save()
# Sync to Repo
ProfileStateRepository().sync_system_state(current_state)
return current_state return current_state
@staticmethod @staticmethod
def update_or_create(system_state): def update_or_create(system_state):
system_state.save() system_state.save()
# Sync to Repo
ProfileStateRepository().sync_system_state(system_state)
@staticmethod @staticmethod
def dissolve(): def dissolve():
return SystemState.dissolve() res = SystemState.dissolve()
# Sync to Repo: clear system ID
ProfileStateRepository().global_state.systemwide_profile_id = None
return res

View file

@ -0,0 +1,59 @@
from core.models.system.SystemState import SystemState
from core.models.session.SessionState import SessionState
from dataclasses import dataclass, field
from typing import Optional
@dataclass
class GlobalState:
systemwide_active: bool = False
firewalled: bool = False
dns_set: bool = False
systemwide_profile_id: Optional[int] = None
session_profile_ids: list[int] = field(default_factory=list)
verified: bool = False
class ProfileStateRepository:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(ProfileStateRepository, cls).__new__(cls)
cls._instance._initialize()
return cls._instance
def _initialize(self):
self.global_state = GlobalState()
# Initial load: Sync memory with whatever is currently on disk
sys = SystemState.get()
if sys:
self.global_state.systemwide_profile_id = sys.profile_id
self.global_state.firewalled = sys.firewalled
self.global_state.dns_set = sys.dns_set
# Load existing session IDs from disk into the list
self.global_state.session_profile_ids = [s.id for s in SessionState.all()]
def sync_system_state(self, state: SystemState):
"""Called by SystemStateController to update memory cache."""
self.global_state.systemwide_profile_id = state.profile_id
self.global_state.firewalled = state.firewalled
self.global_state.dns_set = state.dns_set
def sync_session_added(self, profile_id: int):
"""Called by SessionStateController to update memory cache."""
if profile_id not in self.global_state.session_profile_ids:
self.global_state.session_profile_ids.append(profile_id)
def sync_session_removed(self, profile_id: int):
"""Called by SessionStateController to update memory cache."""
if profile_id in self.global_state.session_profile_ids:
self.global_state.session_profile_ids.remove(profile_id)
def is_profile_active(self, profile_id: int) -> bool:
# Check system profile
if self.global_state.systemwide_active and self.global_state.systemwide_profile_id == profile_id:
return True
# Check session profiles
return profile_id in self.global_state.session_profile_ids

View file

@ -0,0 +1,27 @@
from core.controllers.SystemStateController import SystemStateController
from core.controllers.SessionStateController import SessionStateController
from core.models.Result import Result, ResultError
from core.errors.logger import logger
def evaluate_state_on_startup() -> Result:
"""When the app first starts, evaluate pre-existing state"""
#-----------------------
# SYSTEM-WIDE
#-----------------------
systemwide_active = SystemStateController.get()
if systemwide_active:
error_msg = "A pre-existing systemwide state is active."
logger.error(error_msg)
return Result(valid=False, error_type=ResultError.STATE_CONFLICT, message=error_msg)
#-----------------------
# SESSION
#-----------------------
active_session_profiles = SessionStateController.all()
if active_session_profiles:
for each_profile in active_session_profiles:
logger.info(f"Removing this profile {active_session_profiles} from session since it's a fresh restart.")
SessionStateController.remove(each_profile.id)
return Result(valid=True)

View file

@ -20,6 +20,10 @@ class ResultError(Enum):
PERMISSION = "permission" PERMISSION = "permission"
SUBSCRIPTION = "subscription" SUBSCRIPTION = "subscription"
EXPIRED = "expired" # meant for invoice EXPIRED = "expired" # meant for invoice
UNPAID = "unpaid"
EXPIRED_SUB = "expired_sub"
ACTIVE_BILLING = "active_billing"
STATE_CONFLICT = "state_conflict"
TICKET = "ticket" TICKET = "ticket"
NO_TICKET = "no_ticket" NO_TICKET = "no_ticket"
ALREADY_EXISTS = "already_exists" ALREADY_EXISTS = "already_exists"
@ -40,10 +44,7 @@ class ResultError(Enum):
NEED_SYNC = "need_sync" NEED_SYNC = "need_sync"
WRONG_LOCATION = "wrong_location" WRONG_LOCATION = "wrong_location"
SERVER_SIDE = "server_side" SERVER_SIDE = "server_side"
UNPAID = "unpaid"
MTU = "mtu" MTU = "mtu"
ACTIVE_BILLING = "active_billing"
STATE_CONFLICT = "state_conflict"
TOR_NEEDED = "tor_needed" TOR_NEEDED = "tor_needed"
NAVIGATE_TO = "navigate_to" NAVIGATE_TO = "navigate_to"
UNKNOWN = "unknown" UNKNOWN = "unknown"

View file

@ -1,4 +1,3 @@
from core.models.enum_models import ProtocolChoice, valid_enum
from core.controllers.ConnectionController import ConnectionController from core.controllers.ConnectionController import ConnectionController
from core.controllers.ProfileController import ProfileController from core.controllers.ProfileController import ProfileController
from core.services.networking.systemwide.systemwide_wireguard import terminate_system_connection from core.services.networking.systemwide.systemwide_wireguard import terminate_system_connection
@ -156,6 +155,7 @@ def enable_profile(
if connection_result.valid: if connection_result.valid:
if profile_observer is not None: if profile_observer is not None:
profile_observer.notify('enabled', profile) profile_observer.notify('enabled', profile)
return Result(valid=True, data=profile.type) return Result(valid=True, data=profile.type)
else: else:
logger.error(f"Couldn't enable the profile: {connection_result.error_type}") logger.error(f"Couldn't enable the profile: {connection_result.error_type}")
@ -206,7 +206,8 @@ def disable_profile(
for port_number in session_state.network_port_numbers.tor: for port_number in session_state.network_port_numbers.tor:
ConnectionController.terminate_tor_session_connection(port_number) ConnectionController.terminate_tor_session_connection(port_number)
session_state.dissolve(session_state.id) # session_state.dissolve(session_state.id)
SessionStateController.remove(profile.id)
if profile_observer is not None: if profile_observer is not None:
@ -250,11 +251,20 @@ def disable_profile(
else: else:
raise ProfileDeactivationError('Unsupported protocol.') raise ProfileDeactivationError('Unsupported protocol.')
# ================= UPDATE UI ================ # ================= UPDATE UI ================
# if it made it this far, it worked in theory. # if it made it this far, it worked (in theory).
# wipe state:
SystemStateController.dissolve()
# update ui:
if profile_observer is not None: if profile_observer is not None:
profile_observer.notify('disabled', profile, dict( profile_observer.notify('disabled', profile, dict(
explicitly=explicitly, explicitly=explicitly,
)) ))
# return profile type for UI:
return Result(valid=True, data=profile.type)
except ConnectionTerminationError: except ConnectionTerminationError:
raise ProfileDeactivationError('The profile could not be disabled.') raise ProfileDeactivationError('The profile could not be disabled.')
except ValueError: except ValueError:
@ -313,16 +323,3 @@ def destroy_profile(
profile_observer.notify('destroyed', profile) profile_observer.notify('destroyed', profile)
return Result(valid=True, message="Deleted Profile") return Result(valid=True, message="Deleted Profile")
def get_protocol_enum(profile_id: int) -> ProtocolChoice|bool:
"""Return the Enum for a given Profile's Protocol Choice"""
profile = ProfileController.get(profile_id)
protocol_string = profile.connection.code
if not valid_enum(protocol_string, ProtocolChoice):
return False
else:
return ProtocolChoice(protocol_string)

View file

@ -447,6 +447,21 @@ def get_profile_subscription(profile_id: int, save_pending: Optional[bool] = Fal
return profile.subscription return profile.subscription
def wipe_subscription(profile_id: int) -> Result:
"""Wipe a given profile's subscription."""
profile = ProfileController.get(profile_id)
if not profile:
error_msg = f"Invalid profile id of {profile_id}"
logger.error(error_msg)
return Result(valid=False, message=error_msg, error_type=ResultError.INVALID_INPUT)
profile.subscription = None
ProfileController.update(profile)
return Result(valid=True)
def mark_as_paid() -> Result: def mark_as_paid() -> Result:
"""Conclude the payment phase""" """Conclude the payment phase"""

View file

@ -0,0 +1,17 @@
from core.errors.logger import logger
from datetime import datetime
def check_if_time_lapsed(insert_date: datetime|str) -> bool:
logger.info(f"We are evaluating if this date is expired: {insert_date}")
# Remove timezone awareness if present
if hasattr(insert_date, 'tzinfo') and insert_date.tzinfo is not None:
insert_date = insert_date.replace(tzinfo=None)
# Parse string to datetime if needed
if isinstance(insert_date, str):
insert_date = datetime.fromisoformat(insert_date)
# Compare with current time
return datetime.now() > insert_date

25
core/utils/singleton.py Normal file
View file

@ -0,0 +1,25 @@
from functools import wraps
def singleton(cls):
instances = {}
@wraps(cls)
def get_instance(*args, **kwargs):
instance_id = kwargs.get('instance_id')
# If instance_id provided, use localized mode (one instance per ID)
# Otherwise, use global mode (single instance)
key = (cls, instance_id) if instance_id else cls
if key not in instances:
print(f"Creating new instance with key: {key}")
instances[key] = cls(*args, **kwargs)
else:
print(f"Reusing existing instance with key: {key}")
# either way:
return instances[key]
return get_instance