Moved global state coordinator functions to a dedicated controller, so that the coordinator isn't circular imported when it doesn't need to be. The underlying functions are not even used in the coordinator, so the original reasons for having them there are no longer relevant
This commit is contained in:
parent
da7d912a0c
commit
039c162976
5 changed files with 138 additions and 48 deletions
|
|
@ -67,8 +67,12 @@ class ProfileStateRepository:
|
|||
active_list = []
|
||||
|
||||
for each_profile_id in current_list:
|
||||
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
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
@ -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
|
||||
|
||||
|
|
@ -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)
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue