diff --git a/core/controllers/ProfileController.py b/core/controllers/ProfileController.py index 5fe5217..abbf277 100644 --- a/core/controllers/ProfileController.py +++ b/core/controllers/ProfileController.py @@ -1,8 +1,6 @@ -from core.services.profiles.create_objects import create_profile_object +from core.services.profiles import model_manipulator +from core.services.profiles.create_objects import create_new_profile_object from core.services.profiles.ProfileRepository import ProfileRepository -# from core.models.orm_calls.application_version_calls import get_application_by_id -# from core.models.orm_calls.location_calls import get_profile_location_data -# from core.services.subscriptions import subscriptions from core.models.Result import Result, ResultError from core.errors.logger import logger from core.models.system.SystemConnection import SystemConnection @@ -41,9 +39,10 @@ class ProfileController: @staticmethod def _save_and_sync(profile, event_type: str, observer: ProfileObserver = None): """Internal helper to handle the actual persistence and cache synchronization.""" + # 1. Persist to disk logger.info("Saving to disk.") - profile.save() + model_manipulator.save_to_disk(profile) # 2. update cache to prevent drift ProfileController._repo.update_cache(profile) @@ -65,7 +64,7 @@ class ProfileController: logger.info(f"New profile id is {profile_id}") # Create the object: - created_object = create_profile_object(profile_id=profile_id, potential_data=potential_data) + created_object = create_new_profile_object(profile_id=profile_id, potential_data=potential_data) if not created_object.valid: return created_object.wrap("Failed to make profile object.") diff --git a/core/controllers/profile_state/update_profile.py b/core/controllers/profile_state/update_profile.py index 0e507bf..99da85f 100644 --- a/core/controllers/profile_state/update_profile.py +++ b/core/controllers/profile_state/update_profile.py @@ -7,16 +7,9 @@ from core.controllers.ProfileController import ProfileController from core.models.BaseProfile import BaseProfile as Profile from core.models.BaseProfile import ProfileType -# ORM Calls from core.models.orm_calls.location_calls import get_profile_location_data -from core.models.BaseProfile import get_application_version - -# previously used: -# from core.models.Subscription import Subscription -# from core.models.session.SessionProfile import SessionProfile -# from core.models.system.SystemProfile import SystemProfile +from core.models.orm_calls.application_version_calls import get_application_version from core.models.orm_models.Location import Location -# from core.models.orm_models.Operator import Operator SYSTEMWIDE_CHOICES = ['wireguard', 'hysteria2', 'vless'] SESSION_CHOICES = ['wireguard', 'tor', 'proxy'] @@ -48,7 +41,7 @@ def update_profile( if not key or not new_value: return Result(valid=False, data=f"Invalid Inputs of {key} and {new_value}", error_type=ResultError.INVALID_INPUT) - profile = Profile.find_by_id(profile_id) + profile = ProfileController.get(profile_id) if not profile: error_msg = f"Invalid profile id of {profile_id}" return Result(valid=False, message=error_msg, error_type=ResultError.INVALID_INPUT) @@ -154,6 +147,7 @@ def change_session_connection(profile: Profile, new_value: str) -> Result: return Result(valid=False, message=error_msg, error_type=ResultError.NOT_SUPPORTED) profile.connection.code = new_value + profile.protocol = new_value if new_value == 'wireguard': profile.connection.masked = False @@ -178,9 +172,5 @@ def change_systemwide_connection(profile: Profile, new_value: str) -> Result: # UPDATE PROFILE logger.info("Literally updating the value..") profile.connection.code = new_value - # final_data = "edit_session" + profile.protocol = new_value return Result(valid=True, data=profile) - - -# def save_billing_code(profile: Profile, billing_code: str) -> bool: - \ No newline at end of file diff --git a/core/models/BaseProfile.py b/core/models/BaseProfile.py index 61dd020..e20d23f 100644 --- a/core/models/BaseProfile.py +++ b/core/models/BaseProfile.py @@ -1,68 +1,18 @@ -from core.models.manage.session_management import get_session -from core.errors.logger import logger - -from sqlalchemy import select -from sqlalchemy.orm import joinedload - from abc import ABC, abstractmethod from core.Constants import Constants -from core.Helpers import write_atomically - -from core.models.manage.wrapper import safe_db_operation, WrapperRollback -from core.models.DatabaseOperation import DatabaseOperation, DBErrorType - -# If switching to enums for Connection: -# from core.models.system.SystemConnection import SystemConnection, SystemConnectionTypes -# from core.models.session.SessionConnection import SessionConnection, SessionConnectionTypes - -from core.controllers.LocationController import LocationController from core.models.orm_models.Location import Location -from core.models.orm_models.Operator import Operator - from core.models.Subscription import Subscription -# from core.models.session.ApplicationVersion import ApplicationVersion -from core.models.orm_models.ApplicationVersion import ApplicationVersion -from core.models.orm_calls.application_version_calls import get_application_version +from core.models.enum_models import ProtocolChoice -from dataclasses import dataclass, field, asdict +from dataclasses import dataclass, field from dataclasses_json import config, Exclude, dataclass_json - from pathlib import Path -from typing import Optional, Self -import json +from typing import Optional import os import re import shutil import tempfile -from sqlalchemy.orm import Session from enum import Enum -import inspect - -from core.models.manage.session_management import init_session, get_session - - -@safe_db_operation -def execute_location_sql(country_code: str, city_code: str, session: Session) -> DatabaseOperation: - location_object = session.execute( - select(Location) - .where((Location.country_code == country_code) & (Location.code == city_code)) - .options(joinedload(Location.operator)) - ).scalar_one_or_none() - return location_object - - - -def get_profile_location_data(country_code: str, city_code: str) -> Location: - # with get_session() as session: - api_reply_object = execute_location_sql(country_code, city_code) - if api_reply_object.valid: - data = api_reply_object.data - location_obj = api_reply_object.data - return location_obj - else: - logger.error(f"[get_profile_location_data] Got invalid SQL Query") - return None - class ProfileType(str, Enum): SESSION = "session" @@ -78,9 +28,7 @@ class BaseProfile(ABC): subscription: Optional[Subscription] type: ProfileType location: Optional[Location] = field(metadata=config(exclude=Exclude.ALWAYS)) # SQLAlchemy object - - # legacy version included it to be serialized, but now it's an SQLAlchemy object. - # location: Optional[Location] + protocol: ProtocolChoice @abstractmethod def get_wireguard_configuration_path(self): @@ -109,74 +57,6 @@ class BaseProfile(ABC): return type(self).__name__ == 'SystemProfile' - def save(self, app_version_dict: dict = None): - """ - First warn via the logs, if the caller is bypassing the Cache and accessing it directly. - Then persist changes in the profile/model to disk. - """ - #-------------------------- - # Cache Drift Protection - #-------------------------- - # 1. Get the call stack - # stack[0] is this current 'save' method - # stack[1] is the immediate caller - - # stack = inspect.stack() - # caller_frame = stack[1] - - # caller_function_name = caller_frame.function - # caller_module_name = caller_frame.filename # Full path to the file - - # # 2. Only the Repo or Controller is allowed to save - # allowed_callers = ['create', 'update_cache'] - # is_allowed_file = "core/controllers/ProfileController.py" in caller_module_name or "core/services/profiles/ProfileRepository.py" in caller_module_name - - # if caller_function_name not in allowed_callers and not is_allowed_file: - # logger.warning( - # f"ARCHITECTURE VIOLATION: .save() called directly from " - # f"function '{caller_function_name}' in {caller_module_name}. " - # f"Please route this through ProfileController to ensure cache integrity." - # ) - - #-------------------------- - # Actual Save - #-------------------------- - - # === SERIALIZATION === - - print(f"PARENT: self is {type(self)}") - print(f"PARENT: self.id is {type(self.id)}") - print("I am here") - config_dict = self.to_dict() - - # === LOCATION === - location_dict = self.location.convert_to_dict() # this is from SQLAlchemy, and not JSON-models, that's why it's separate. - if self.location: - config_dict["location"] = location_dict - - # === APPLICATION === - if app_version_dict: - config_dict["application_version"] = app_version_dict - - # === FILE I/O === - config_file_contents = json.dumps(config_dict, indent=4) + '\n' - - # legacy version: - # config_file_contents = f'{self.to_json(indent=4)}\n' - - os.makedirs(self.get_config_path(), exist_ok=True) - os.makedirs(self.get_data_path(), exist_ok=True) - - config_file_path = f'{self.get_config_path()}/config.json' - write_atomically(config_file_path, config_file_contents) - - def delete_data(self): - shutil.rmtree(self.get_data_path(), ignore_errors=True) - - def delete(self): - shutil.rmtree(self.get_config_path(), ignore_errors=True) - self.delete_data() - def get_wireguard_configuration_metadata(self, key): configuration = self.get_wireguard_configuration() @@ -246,125 +126,6 @@ class BaseProfile(ABC): if not any(incident_path_contents): incident_path.rmdir() - def _get_dirty_keys(self: Self): - - reference = BaseProfile.find_by_id(self.id) - - if type(reference) != type(self): - return list(self.__dataclass_fields__.keys()) - - return list([key for key, value in asdict(self).items() if value != asdict(reference).get(key)]) - - - @staticmethod - def find_by_id(id: int): - - try: - config_file_contents = open(f'{BaseProfile.__get_config_path(id)}/config.json', 'r').read() - except FileNotFoundError: - return None - - try: - profile = json.loads(config_file_contents) - except ValueError: - return None - - profile['id'] = id - - profiles_location = profile['location'] - - if profile['location'] is not None: - # =========== GET COUNTRY & LOCATION =========== - if isinstance(profiles_location, dict): - try: - country_code = profile['location']['country_code'] - city_code = profile['location']['code'] - except: - logger.error(f"CRITICAL ERROR! Can't find country code or city for profile id {profile['id']} inside BaseProfile") - return - else: - # potentially coming from SQLAlchemy ALREADY: - country_code = profile.location.country_code - city_code = profile.location.code - - # =========== GET DATA USING THAT COUNTRY & LOCATION =========== - location_dict = get_profile_location_data(country_code, city_code) - - if location_dict: - profile['location'] = location_dict - - # this needs error handling if there's no location or malconformed config. - - - # =========== SESSION =========== - if 'application_version' in profile: - profile['type'] = ProfileType.SESSION - - - if isinstance(profile['application_version'], ApplicationVersion): - print("It's already an app version") - - if profile['application_version'] is not None: - application_version = get_application_version( - profile['application_version']['application_code'] or None, - profile['application_version']['version_number'] or None - ) - - if application_version is not None: - profile['application_version'] = application_version - - if application_version is None: - profile['application_version'] = None - - # legacy - # if profile['application_version'] is not None: - # application_version = ApplicationVersion.find(profile['application_version']['application_code'] or None, profile['application_version']['version_number'] or None) - - # if application_version is not None: - # profile['application_version'] = application_version - - - from core.models.session.SessionProfile import SessionProfile - # noinspection PyUnresolvedReferences - profile = SessionProfile.from_dict(profile) - - - # =========== SYSTEM =========== - else: - profile['type'] = ProfileType.SYSTEM - - from core.models.system.SystemProfile import SystemProfile - # noinspection PyUnresolvedReferences - profile = SystemProfile.from_dict(profile) - - return profile - - - @staticmethod - def exists(id: int): - return re.match(r'^\d{1,2}$', str(id)) and os.path.isfile(f'{BaseProfile.__get_config_path(id)}/config.json') - - @staticmethod - def all(): - - profiles = {} - - for directory_entry in os.listdir(Constants.HV_PROFILE_CONFIG_HOME): - - try: - id = int(directory_entry) - except ValueError: - continue - - if BaseProfile.exists(id): - - profile = BaseProfile.find_by_id(id) - - if profile is not None: - profiles[id] = profile - - return dict(sorted(profiles.items())) - @staticmethod def __get_config_path(id: int): return f'{Constants.HV_PROFILE_CONFIG_HOME}/{str(id)}' @@ -372,15 +133,3 @@ class BaseProfile(ABC): @staticmethod def __get_data_path(id: int): return f'{Constants.HV_PROFILE_DATA_HOME}/{str(id)}' - - -# legacy phased out since SQLAlchemy handles the time_zone. -# if location is not None: -# if profile['location'].get('time_zone') is not None: -# location.time_zone = profile['location']['time_zone'] -# profile['location'] = location - - - - - diff --git a/core/models/enum_models.py b/core/models/enum_models.py index afa6d6e..9ce2f5a 100644 --- a/core/models/enum_models.py +++ b/core/models/enum_models.py @@ -1,7 +1,7 @@ -from enum import Enum +from enum import Enum, StrEnum from typing import Type -class ProtocolChoice(Enum): +class ProtocolChoice(StrEnum): WIREGUARD = "wireguard" HYSTERIA2 = "hysteria2" VLESS = "vless" diff --git a/core/models/orm_calls/location_calls.py b/core/models/orm_calls/location_calls.py index e61ec58..ca5425b 100644 --- a/core/models/orm_calls/location_calls.py +++ b/core/models/orm_calls/location_calls.py @@ -23,15 +23,12 @@ def execute_location_sql(country_code: str, city_code: str, session: Session) -> def get_profile_location_data(country_code: str, city_code: str) -> Location: - print(f"country_code is {country_code}") - location_object = execute_location_sql(country_code, city_code) if location_object.valid: return location_object.data else: critical_error = f"Got invalid SQL Query which could not be solved by the wrapper, with error message {location_object.message} and type {location_object.error_type}" logger.error(critical_error) - print(critical_error) return None @@ -80,4 +77,42 @@ def get_profile_location_data_by_id(location_id: int) -> Location: critical_error = f"Got invalid SQL Query which could not be solved by the wrapper, with error message {location_object.message} and type {location_object.error_type}" logger.error(critical_error) print(critical_error) - return None \ No newline at end of file + return None + + +def get_all_locations() -> dict: + location_object = execute_all_locations() + if location_object.valid: + return location_object.data + else: + return {} + +class LocationCache: + _instance = None + + def __new__(cls): + if cls._instance is None: + cls._instance = super(LocationCache, cls).__new__(cls) + cls._instance._initialize() + return cls._instance + + def _initialize(self): + logger.info("Initializing LocationCache: fetching all locations from DB") + all_locations = get_all_locations() + + if all_locations == {} or all_locations is None: + logger.error("Could not initiate the LocationCache") + + # Create a map using the combination of country and city codes as the key + self._cache = { + (loc.country_code, loc.code): loc + for loc in all_locations + } + + def get_location(self, country_code: str, city_code: str): + return self._cache.get((country_code, city_code)) + + def clear(self): + logger.info("Clearing LocationCache to prevent data drift") + self._cache = {} + LocationCache._instance = None diff --git a/core/models/session/SessionProfile.py b/core/models/session/SessionProfile.py index 62a7433..b0ec904 100644 --- a/core/models/session/SessionProfile.py +++ b/core/models/session/SessionProfile.py @@ -1,11 +1,7 @@ from core.Constants import Constants from core.Errors import UnknownTimeZoneError from core.models.BaseProfile import BaseProfile -# from core.models.session.ApplicationVersion import ApplicationVersion - from core.models.orm_models.ApplicationVersion import ApplicationVersion - - from core.models.session.ProxyConfiguration import ProxyConfiguration from core.models.session.SessionConnection import SessionConnection from dataclasses import dataclass @@ -27,35 +23,6 @@ class SessionProfile(BaseProfile): def has_connection(self): return self.connection is not None - def save(self): - print("We are able to trigger save on the child session") - - # if 'application_version' in self._get_dirty_keys(): - - # persistent_state_path = f'{self.get_data_path()}/persistent-state' - - # if os.path.isdir(persistent_state_path): - # shutil.rmtree(persistent_state_path, ignore_errors=True) - - # if 'location' in self._get_dirty_keys(): - - # self.__delete_proxy_configuration() - # self.delete_wireguard_configuration() - - # === APPLICATION === - print(f"self.application_version is {self.application_version}") - - if isinstance(self.application_version, ApplicationVersion): - app_version_dict = self.application_version.convert_to_dict() - print(f"session child got {app_version_dict} as dict..") - else: - print("Unable to convert the dict.") - print(f"self.application_version type is {type(self.application_version)}") - return False - - print(f"CHILD: self is {type(self)}") - super().save(app_version_dict=app_version_dict) - def attach_proxy_configuration(self, proxy_configuration): proxy_configuration_file_contents = f'{proxy_configuration.to_json(indent=4)}\n' @@ -130,7 +97,7 @@ class SessionProfile(BaseProfile): return time_zone - def __delete_proxy_configuration(self): + def delete_proxy_configuration(self): Path(self.get_proxy_configuration_path()).unlink(missing_ok=True) def delete_wireguard_configuration(self): diff --git a/core/models/system/SystemProfile.py b/core/models/system/SystemProfile.py index f38ccd1..dd750d3 100644 --- a/core/models/system/SystemProfile.py +++ b/core/models/system/SystemProfile.py @@ -20,12 +20,6 @@ class SystemProfile(BaseProfile): the_id = self.id return filepath - def save(self): - - if 'location' in self._get_dirty_keys(): - self.__delete_wireguard_configuration() - - super().save() def attach_wireguard_configuration(self, wireguard_configuration): @@ -65,11 +59,11 @@ class SystemProfile(BaseProfile): def address_security_incident(self): super().address_security_incident() - self.__delete_wireguard_configuration() + self.delete_wireguard_configuration() def delete(self): try: - self.__delete_wireguard_configuration() + self.delete_wireguard_configuration() except ProfileModificationError: raise ProfileDeletionError('The WireGuard configuration could not be deleted.') @@ -86,7 +80,7 @@ class SystemProfile(BaseProfile): super().delete() - def __delete_wireguard_configuration(self): + def delete_wireguard_configuration(self): if self.has_wireguard_configuration(): diff --git a/core/services/profiles/ProfileRepository.py b/core/services/profiles/ProfileRepository.py index 7a412be..ab23430 100644 --- a/core/services/profiles/ProfileRepository.py +++ b/core/services/profiles/ProfileRepository.py @@ -1,7 +1,9 @@ -from core.models.BaseProfile import BaseProfile +from core.services.profiles import model_manipulator +from core.errors.logger import logger + # generic -from typing import Dict, List, Union, Optional +from typing import Dict from threading import Lock class ProfileRepository: @@ -20,31 +22,6 @@ class ProfileRepository: cls._instance._cache: Dict[int, any] = {} return cls._instance - # def get(self, profile_id: int): - # # 1. Check cache first - # if profile_id in self._cache: - # return self._cache[profile_id] - - # # 2. Fallback to disk via the Model - # profile = BaseProfile.find_by_id(profile_id) - - # # 3. Update cache for next time - # if profile: - # self._cache[profile_id] = profile - - # return profile - - # def get_all(self): - # # First, get the current list of IDs from the model, to avoid drift, from new files added to disk - # all_profiles = BaseProfile.all() - - # # Update our internal cache with everything currently on disk - # for p in all_profiles: - # self._cache[id] = p - # # self._cache[p.id] = p - - # return all_profiles - def update_cache(self, profile): """Explicitly updates the cache to prevent drift after a save.""" self._cache[profile.id] = profile @@ -53,31 +30,13 @@ class ProfileRepository: """Removes an item from cache if it's deleted from disk.""" self._cache.pop(profile_id, None) - # def get_next_id(self) -> int: - # # Step 1: Check against the current cache for the highest known ID - # next_id = max(self._cache.keys(), default=0) + 1 - - # # Step 2 & 3: Verify the ID doesn't exist on disk - # # We loop in case the cache is behind and the next sequential IDs are already taken - # while True: - # try: - # profile = BaseProfile.find_by_id(next_id) - # if profile is None: - # # Confirmation fails (it doesn't exist), so this ID is safe to use - # return next_id - # except Exception: - # # If the disk lookup itself fails, we treat it as the ID being available - # return next_id - - # next_id += 1 - def get_next_id(self) -> int: # Use the current cache to find a starting point for the next ID next_id = max(self._cache.keys(), default=0) + 1 while True: try: - profile = BaseProfile.find_by_id(next_id) + profile = model_manipulator.create_profile(next_id) if profile is None: return next_id except Exception: @@ -89,19 +48,26 @@ class ProfileRepository: if profile_id in self._cache: return self._cache[profile_id] - profile = BaseProfile.find_by_id(profile_id) + logger.info("Doing fresh SQL to load it AGAIN...") + profile = model_manipulator.create_profile(profile_id) + logger.info(f"SQL done AGAIN for {profile_id}.") if profile: + logger.info(f"Setting cache for the profile {profile_id}") self._cache[profile_id] = profile return profile def get_all(self) -> Dict[int, any]: - # BaseProfile.all() returns a dict { id: profile } - all_profiles_dict = BaseProfile.all() + """On the first run, return all profiles from disk. On future runs, return it from cache.""" + + # Only run the slow SQL/file operation if the cache is empty + if not self._cache: + + # model_manipulator returns a dict { id: profile } + all_profiles_dict = model_manipulator.get_all_profiles() + + # Sync the cache with whatever is currently on disk + self._cache.update(all_profiles_dict) - # Sync the cache with whatever is currently on disk - self._cache.update(all_profiles_dict) - - # Return the dict so that .keys() and other dict methods still work across the app - return all_profiles_dict + return self._cache diff --git a/core/services/profiles/create_objects.py b/core/services/profiles/create_objects.py index 70b5c22..44fc618 100644 --- a/core/services/profiles/create_objects.py +++ b/core/services/profiles/create_objects.py @@ -1,3 +1,5 @@ +from core.models.orm_models.ApplicationVersion import ApplicationVersion +from core.models.Subscription import Subscription from core.models.orm_models.Location import Location from core.models.enum_models import ProtocolChoice from core.models.orm_calls.application_version_calls import get_application_by_id @@ -11,44 +13,61 @@ from core.models.Result import Result, ResultError from core.errors.logger import logger from core.utils.type_checker import type_checker +from typing import Optional + +def create_app_object_by_id(potential_data: dict) -> Result: + """With a given app id, get an ApplicationVersion object""" + app_id = potential_data.get('Browser', None) + + if not app_id: + error_msg = "Missing the app by id" + logger.error(error_msg) + return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=error_msg) + + # Lookup App: + application_version = get_application_by_id(app_id) + + if not application_version: + error_msg = "Failed to find the app by id" + logger.error(error_msg) + return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=error_msg) + + return Result(valid=True, data=application_version) + @type_checker def create_session_object( profile_id: int, potential_data: dict, protocol: ProtocolChoice, location: Location, - name: str + name: str, + application: Optional[ApplicationVersion] = None, + subscription: Optional[Subscription] = None ) -> Result: """Return a SessionProfile object""" # SESSION ONLY PRE-REQS resolution = potential_data.get('Resolution', None) - app_id = potential_data.get('Browser', None) - if resolution and app_id: - logger.info(f"Creating a session profile with {resolution} and {app_id}") - else: - error_msg = "Missing the resolution or app version" - logger.error(error_msg) - return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=error_msg) + if not resolution: + resolution = potential_data.get('resolution', None) - # Lookup App: - application_version = get_application_by_id(app_id) - if not application_version: - error_msg = "Failed to find the app by id" - logger.error(error_msg) - return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=error_msg) + if not application: + get_application = create_app_object_by_id(potential_data) + if not get_application.valid: + return get_application.wrap("Failed to get app object") + application = get_application.data - # logger.info(f"application_version is {application_version}") connection = SessionConnection(protocol.value) profile = SessionProfile( id=profile_id, name=name, - subscription=None, + subscription=subscription, type=ProfileType.SESSION, location=location, + protocol=protocol, resolution=resolution, - application_version=application_version, + application_version=application, connection=connection ) return Result(valid=True, data=profile) @@ -60,7 +79,8 @@ def create_system_object( potential_data: dict, protocol: ProtocolChoice, location: Location, - name: str + name: str, + subscription: Optional[Subscription] = None ) -> Result: """Return a SessionProfile object""" connection = SystemConnection(protocol.value) @@ -68,7 +88,8 @@ def create_system_object( id=profile_id, name=name, type=ProfileType.SYSTEM, - subscription=None, + protocol=protocol, + subscription=subscription, location=location, connection=connection ) @@ -76,8 +97,8 @@ def create_system_object( -def create_profile_object(profile_id: int, potential_data: dict) -> Result: - """Creates and returns a literal profile object. (but not save to disk.)""" +def create_new_profile_object(profile_id: int, potential_data: dict) -> Result: + """Creates and returns a brand new, literal profile object. (but not save to disk.)""" #--------------------------- # EXTRACT DATA diff --git a/core/services/profiles/model_manipulator.py b/core/services/profiles/model_manipulator.py new file mode 100644 index 0000000..7a2f6ec --- /dev/null +++ b/core/services/profiles/model_manipulator.py @@ -0,0 +1,294 @@ +from core.models.session.SessionProfile import SessionProfile +from core.models.orm_calls.location_calls import LocationCache +from core.models.BaseProfile import BaseProfile as Profile +from core.models.BaseProfile import ProfileType +from core.services.profiles.create_objects import create_session_object, create_system_object +from core.utils.basic_operations.write_or_read_from_json import read_entire_json +from core.models.orm_calls.application_version_calls import get_application_version +from core.models.Subscription import Subscription +from core.models.enum_models import ProtocolChoice +from core.models.Result import Result, ResultError +from core.errors.logger import logger +from core.Constants import Constants +from core.Helpers import write_atomically + +from datetime import datetime +from dataclasses import asdict +import json +import os +import re +import shutil + + +def get_config_path(id: int): + return f'{Constants.HV_PROFILE_CONFIG_HOME}/{str(id)}' + + +def get_data_path(id: int): + return f'{Constants.HV_PROFILE_DATA_HOME}/{str(id)}' + + +def profile_exists(profile_id: int): + return re.match(r'^\d{1,2}$', str(profile_id)) and os.path.isfile(f'{get_config_path(profile_id)}/config.json') + + +def get_wireguard_configuration_path(profile_id: int): + return f'{get_config_path(profile_id)}/wg.conf' + + +def delete_from_disk(profile: Profile) -> bool: + """Delete the folders and their files for a given profile""" + shutil.rmtree(get_config_path(profile.id), ignore_errors=True) + shutil.rmtree(get_data_path(profile.id), ignore_errors=True) + return True + +def read_profile_from_disk(profile_id: int) -> dict: + """Gets the contents of a profile config""" + base_config_folder = get_config_path(profile_id) + config_file = f'{base_config_folder}/config.json' + return read_entire_json(config_file) + + +def get_legacy_profile_type(config_contents: dict) -> ProfileType: + """For legacy profile configs, prior to adding an explicit type, they inferred it from the config contents.""" + if 'application_version' in config_contents: + return ProfileType.SESSION + else: + return ProfileType.SYSTEM + +def get_application_object_from_config_dict(config_contents: dict) -> Result: + """Given a raw config, get an application version object from it.""" + try: + application_code = config_contents['application_version']['application_code'] + version_number = config_contents['application_version']['version_number'] + except Exception as e: + logger.error(f"CRITICAL ERROR! Can't find : {str(e)}") + return None + + if not application_code or not version_number: + error_msg = "Can't make a location object because the profile config is missing application_code or version_number" + logger.error(error_msg) + return Result(valid=False, error_type=ResultError.MISSING_DATA, message=error_msg) + + application = get_application_version(application_code=application_code, version_number=version_number) + if application: + return Result(valid=True, data=application) + else: + error_msg = "Can't make a application object from get_application_version" + logger.error(error_msg) + return Result(valid=False, error_type=ResultError.MISSING_DATA, message=error_msg) + + +def get_location_object_from_config_dict(config_contents: dict) -> Result: + """Given a raw config, get a location object from it.""" + try: + country_code = config_contents['location']['country_code'] + city_code = config_contents['location']['code'] + except Exception as e: + logger.error(f"CRITICAL ERROR! Can't find country code or city: {str(e)}") + return None + + if not country_code or not city_code: + error_msg = "Can't make a location object because the profile config is missing country_code or city_code" + logger.error(error_msg) + return Result(valid=False, error_type=ResultError.MISSING_DATA, message=error_msg) + + # Use the singleton cache to reduce SQL calls + location = LocationCache().get_location(country_code, city_code) + + if location: + return Result(valid=True, data=location) + else: + error_msg = "Can't find location in cache for the provided codes" + logger.error(error_msg) + return Result(valid=False, error_type=ResultError.MISSING_DATA, message=error_msg) + + +def make_subscription_from_config(config_contents: dict) -> Subscription: + """Given a raw config, get a Subscription object from it.""" + if config_contents is None: + return None + + if "subscription" not in config_contents: + return None + + try: + sub_data = config_contents["subscription"] + if not sub_data: + return None + raw_expires_at = sub_data.get("expires_at") + + # Convert string to datetime object if it exists, otherwise keep as None + expires_at = None + if isinstance(raw_expires_at, str): + expires_at = datetime.fromisoformat(raw_expires_at) + elif isinstance(raw_expires_at, datetime): + expires_at = raw_expires_at + + return Subscription( + billing_code=sub_data["billing_code"], + expires_at=expires_at + ) + except (TypeError, KeyError, ValueError): + return None + +def create_profile_object_from_disk(profile_id: int) -> Result: + config_contents = read_profile_from_disk(profile_id) + if not config_contents: + return Result(valid=False, error_type=ResultError.MISSING_DATA, message="Missing the config") + + #======================================================== + # LOCATION + #======================================================== + location_result = get_location_object_from_config_dict(config_contents) + if not location_result.valid: + return location_result.wrap("Cant make profile due to location.") + location = location_result.data + + #======================================================== + # PROTOCOL & NAME + #======================================================== + name = config_contents["name"] + protocol_as_string = config_contents["connection"]["code"] + protocol = ProtocolChoice(protocol_as_string) + + #======================================================== + # SUBSCRIPTION + #======================================================== + subscription = make_subscription_from_config(config_contents) + + #======================================================== + # PROFILE TYPE + #======================================================== + type_as_string = config_contents.get('type', None) + if type_as_string is None: + profile_type = get_legacy_profile_type(config_contents) + else: + profile_type = ProfileType(type_as_string) + + #======================================================== + # SYSTEM + #======================================================== + if profile_type == ProfileType.SYSTEM: + return create_system_object( + profile_id=profile_id, + potential_data=config_contents, + protocol=protocol, + location=location, + name=name, + subscription=subscription + ) + + #======================================================== + # APPLICATION + #======================================================== + app_result = get_application_object_from_config_dict(config_contents) + if not app_result.valid: + return app_result.wrap("Cant make profile due to application.") + application = app_result.data + + return create_session_object( + profile_id=profile_id, + potential_data=config_contents, + protocol=protocol, + location=location, + name=name, + application=application, + subscription=subscription + ) + + +def create_profile(profile_id) -> Profile|None: + """Wrapper for the giant function, to just return the raw profile object or nothing. (strips Result wrapper)""" + result = create_profile_object_from_disk(profile_id) + if not result.valid: + logger.error(f"Failed to create profile id {profile_id}") + return None + + return result.data + + + +def get_all_profiles() -> dict: + """Find and Create all Profiles from what's available in the folders.""" + all_profiles = {} + skipped_profiles = 0 + + for directory_entry in os.listdir(Constants.HV_PROFILE_CONFIG_HOME): + try: + profile_id = int(directory_entry) + except ValueError: + continue + + if profile_exists(profile_id): + + profile_result = create_profile_object_from_disk(profile_id) + if not profile_result.valid: + logger.error(f"Failed to create a profile object for {profile_id}") + logger.error(profile_result.message) + skipped_profiles = skipped_profiles + 1 + continue + + profile = profile_result.data + if profile is not None: + all_profiles[profile_id] = profile + + full_final_dict = dict(sorted(all_profiles.items())) + if skipped_profiles > 0: + logger.error(f"Skipped {skipped_profiles} profiles") + return full_final_dict + + +def get_dirty_keys(profile: Profile) -> list: + reference = create_profile(profile.id) + + if type(reference) != type(profile): + return list(profile.__dataclass_fields__.keys()) + + return list([key for key, value in asdict(profile).items() if value != asdict(reference).get(key)]) + + +def handle_outdated_session_data(dirty_keys: dict, profile: SessionProfile): + if 'application_version' in dirty_keys: + persistent_state_path = f'{get_data_path(profile.id)}/persistent-state' + if os.path.isdir(persistent_state_path): + shutil.rmtree(persistent_state_path, ignore_errors=True) + + if 'location' in dirty_keys: + profile.delete_proxy_configuration() + + +def save_to_disk(profile: Profile): + """ + Persist a Profile model to disk. + For SQL based attributes, we manually override the dict prior to serialization. + """ + profile_id = profile.id + profile_config_path = get_config_path(profile_id) + profile_data_path = get_data_path(profile_id) + config_dict = profile.to_dict() + + dirty_keys = get_dirty_keys(profile) + if 'location' in dirty_keys: + profile.delete_wireguard_configuration() # for all polymorphic types. + + if profile.type == ProfileType.SESSION: + handle_outdated_session_data(dirty_keys=dirty_keys, profile=profile) + # Application is an SQL model, so we override the dict, + app_version_dict = profile.application_version.convert_to_dict() + config_dict["application_version"] = app_version_dict + + # === LOCATION === + # Location is an SQL model, so we override the dict, + location_dict = profile.location.convert_to_dict() # this is from SQLAlchemy, and not JSON-models, that's why it's separate. + config_dict["location"] = location_dict + + # === FILE I/O === + config_file_contents = json.dumps(config_dict, indent=4) + '\n' + + os.makedirs(profile_config_path, exist_ok=True) + os.makedirs(profile_data_path, exist_ok=True) + + config_file_path = f'{profile_config_path}/config.json' + write_atomically(config_file_path, config_file_contents) +