diff --git a/core/infrastructure/global_state.py b/core/infrastructure/global_state.py index 62d0dec..b2f8789 100644 --- a/core/infrastructure/global_state.py +++ b/core/infrastructure/global_state.py @@ -67,8 +67,12 @@ class ProfileStateRepository: active_list = [] for each_profile_id in current_list: - if self.global_state.session_profiles[each_profile_id][ProcessType.ACTIVE] is True: - active_list.append(each_profile_id) + try: + if self.global_state.session_profiles[each_profile_id][ProcessType.ACTIVE] is True: + active_list.append(each_profile_id) + except KeyError: + print(f"active doesn't exist yet for profile id {each_profile_id}") + continue return active_list diff --git a/core/infrastructure/global_state_coordinator.py b/core/infrastructure/global_state_coordinator.py index 08ad47d..5447e0e 100644 --- a/core/infrastructure/global_state_coordinator.py +++ b/core/infrastructure/global_state_coordinator.py @@ -1,10 +1,10 @@ from core.services.networking.general_connection_tools.connection_enable import establish_connection from core.controllers.ConfigurationController import ConfigurationController -from core.infrastructure.global_state import ProfileStateRepository +# from core.infrastructure.global_state import ProfileStateRepository from core.controllers.ProfileController import ProfileController from core.controllers.SystemStateController import SystemStateController from core.controllers.SessionStateController import SessionStateController -from core.models.system.SystemState import SystemState +# from core.models.system.SystemState import SystemState from core.models.enum_models import ProtocolChoice from core.models.Result import Result, ResultError from core.services.networking.systemwide import killswitch @@ -21,67 +21,69 @@ from core.errors.logger import logger # generic from functools import partial import time +# import atexit + SINGBOX_PROTOCOLS = (ProtocolChoice.HYSTERIA2, ProtocolChoice.VLESS) -def is_systemwide_on() -> bool: - """Returns a boolean if systemwide is connected. True is Yes, False is No""" - state_repo = ProfileStateRepository() - if not state_repo.global_state.verified_working: - return False +# def is_systemwide_on() -> bool: +# """Returns a boolean if systemwide is connected. True is Yes, False is No""" +# state_repo = ProfileStateRepository() +# if not state_repo.global_state.verified_working: +# return False - return state_repo.global_state.systemwide_active +# return state_repo.global_state.systemwide_active -def verified_system_profile_id() -> int|None: - state_repo = ProfileStateRepository() - if not state_repo.global_state.verified_working: - return False - else: - return state_repo.global_state.systemwide_profile_id +# def verified_system_profile_id() -> int|None: +# state_repo = ProfileStateRepository() +# if not state_repo.global_state.verified_working: +# return False +# else: +# return state_repo.global_state.systemwide_profile_id -def connected_system_profile_id() -> int|None: - """Returns a single number of the connected system profile""" - state_repo = ProfileStateRepository() - print(f"state_repo is {state_repo.global_state}") - return state_repo.global_state.systemwide_profile_id +# def connected_system_profile_id() -> int|None: +# """Returns a single number of the connected system profile""" +# state_repo = ProfileStateRepository() +# print(f"state_repo is {state_repo.global_state}") +# return state_repo.global_state.systemwide_profile_id -def connected_session_profile_ids() -> list|None: - """Returns a list of the currently connected session profile IDs""" - state_repo = ProfileStateRepository() - return state_repo.active_session_ids +# def connected_session_profile_ids() -> list|None: +# """Returns a list of the currently connected session profile IDs""" +# state_repo = ProfileStateRepository() +# return state_repo.active_session_ids -def get_all_connected() -> list|None: - profile_ids = connected_session_profile_ids() - system = connected_system_profile_id() - if system: - profile_ids.append(system) - return profile_ids +# def get_all_connected() -> list|None: +# profile_ids = connected_session_profile_ids() +# system = connected_system_profile_id() +# if system: +# profile_ids.append(system) +# return profile_ids -def quantity_connected() -> list|None: - connected = get_all_connected() - if connected: - return len(connected) - else: - return None +# def quantity_connected() -> list|None: +# connected = get_all_connected() +# if connected: +# return len(connected) +# else: +# return None -def is_connected(profile_id: int) -> bool: - """Returns a boolean if a given profile is connected""" +# def is_connected(profile_id: int) -> bool: +# """Returns a boolean if a given profile is connected""" - # system-wide: - systemwide_profile_id = connected_system_profile_id() - if profile_id == systemwide_profile_id: - return True +# # system-wide: +# systemwide_profile_id = connected_system_profile_id() +# if profile_id == systemwide_profile_id: +# return True - # session: - state_repo = ProfileStateRepository() - is_session_connected = state_repo.is_profile_active(profile_id) - print(f"Is this session profile connected? {profile_id}") - return is_session_connected +# # session: +# state_repo = ProfileStateRepository() +# is_session_connected = state_repo.is_profile_active(profile_id) +# print(f"Is this session profile connected? {profile_id}") +# return is_session_connected def coordinate_evaluation(connection_observer: ConnectionObserver) -> Result: @@ -229,3 +231,10 @@ def evaluate_state_on_startup() -> Result: time.sleep(4) return Result(valid=True) + + +# def cleanup_all_processes(): +# connected_system_profile_id() + + +# atexit.register(cleanup_all_processes) \ No newline at end of file diff --git a/core/infrastructure/state_management/global_state_controller.py b/core/infrastructure/state_management/global_state_controller.py new file mode 100644 index 0000000..2ecb28f --- /dev/null +++ b/core/infrastructure/state_management/global_state_controller.py @@ -0,0 +1,60 @@ +from core.infrastructure.global_state import ProfileStateRepository + +def is_systemwide_on() -> bool: + """Returns a boolean if systemwide is connected. True is Yes, False is No""" + state_repo = ProfileStateRepository() + if not state_repo.global_state.verified_working: + return False + + return state_repo.global_state.systemwide_active + + +def verified_system_profile_id() -> int|None: + state_repo = ProfileStateRepository() + if not state_repo.global_state.verified_working: + return False + else: + return state_repo.global_state.systemwide_profile_id + + +def connected_system_profile_id() -> int|None: + """Returns a single number of the connected system profile""" + state_repo = ProfileStateRepository() + print(f"state_repo is {state_repo.global_state}") + return state_repo.global_state.systemwide_profile_id + +def connected_session_profile_ids() -> list|None: + """Returns a list of the currently connected session profile IDs""" + state_repo = ProfileStateRepository() + return state_repo.active_session_ids + + +def get_all_connected() -> list|None: + profile_ids = connected_session_profile_ids() + system = connected_system_profile_id() + if system: + profile_ids.append(system) + return profile_ids + +def quantity_connected() -> list|None: + connected = get_all_connected() + if connected: + return len(connected) + else: + return None + + +def is_connected(profile_id: int) -> bool: + """Returns a boolean if a given profile is connected""" + + # system-wide: + systemwide_profile_id = connected_system_profile_id() + if profile_id == systemwide_profile_id: + return True + + # session: + state_repo = ProfileStateRepository() + is_session_connected = state_repo.is_profile_active(profile_id) + print(f"Is this session profile connected? {profile_id}") + return is_session_connected + diff --git a/core/services/networking/general_connection_tools/disconnect.py b/core/services/networking/general_connection_tools/disconnect.py new file mode 100644 index 0000000..dd75096 --- /dev/null +++ b/core/services/networking/general_connection_tools/disconnect.py @@ -0,0 +1,16 @@ +from core.controllers.SessionStateController import SessionStateController +from core.models.Result import Result, ResultError +from core.errors.logger import logger + + +def kill_profile_from_grave(profile_id: int) -> Result: + """Uses the JSON state to kill all related process ids. Good for recovered bootups""" + killed = SessionStateController.scortched_earth_kill_profile(profile_id) + + if killed: + logger.info("psutil worked to kill it.") + return Result(valid=True) + else: + error_msg = "Even psutil scortched earth failed to kill the profile." + logger.error(error_msg) + return Result(valid=False, error_type=ResultError.STATE, message=error_msg) \ No newline at end of file diff --git a/core/services/profiles/profile_connections.py b/core/services/profiles/profile_connections.py index 8028d9b..faa118d 100644 --- a/core/services/profiles/profile_connections.py +++ b/core/services/profiles/profile_connections.py @@ -1,3 +1,4 @@ +from core.infrastructure.state_management.kill_processes import kill_all_associated_processes from core.models.enum_models import ProtocolChoice from core.services.apps.launch_app import orchestrate_app_launch from core.services.networking.session.session_networking import start_session_networking