195 lines
No EOL
6.5 KiB
Python
195 lines
No EOL
6.5 KiB
Python
from core.services.networking.session.tor_tools import terminate_tor_session_connection
|
|
from core.models.enum_models import ProtocolChoice
|
|
from core.infrastructure.state_management.kill_processes import kill_all_associated_processes
|
|
from core.models.session.SessionState import SessionState
|
|
# from core.controllers.ProfileController import ProfileController
|
|
from core.infrastructure.global_state import ProfileStateRepository, ProcessType, LIST_OF_PROCESSES
|
|
from core.observers.Notifications import Notifications, EventType
|
|
from core.models.Result import Result, ResultError
|
|
from core.errors.logger import logger
|
|
|
|
import subprocess
|
|
|
|
|
|
class SessionStateController:
|
|
@staticmethod
|
|
def get_model(profile_id: int):
|
|
return SessionState.find_by_id(profile_id)
|
|
|
|
@staticmethod
|
|
def get_or_new(profile_id: int):
|
|
session_state = SessionStateController.get_model(profile_id)
|
|
|
|
if session_state is None:
|
|
# Update the Singleton Repo
|
|
repo = ProfileStateRepository()
|
|
repo.setup_session_profile(profile_id)
|
|
|
|
# Create and save the session
|
|
session_state = SessionState(profile_id)
|
|
session_state.save()
|
|
return session_state
|
|
|
|
return session_state
|
|
|
|
|
|
@staticmethod
|
|
def kill_session_profile(profile_id: int, protocol: ProtocolChoice) -> Result:
|
|
|
|
# get process objects from repo:
|
|
repo = ProfileStateRepository()
|
|
|
|
for each_process_type in LIST_OF_PROCESSES:
|
|
each_process = repo.get_session_process(profile_id=profile_id, process_type=each_process_type)
|
|
|
|
try:
|
|
pid = each_process.pid
|
|
print(f"Killing process type {each_process_type} with a pid of {pid}")
|
|
each_process.kill()
|
|
|
|
# Wait for the process to actually exit and clean up
|
|
exit_code = each_process.wait()
|
|
print(f"Process {each_process_type} terminated with code {exit_code}")
|
|
continue
|
|
|
|
except ProcessLookupError:
|
|
print("Process was already dead or terminated prior. But we continue")
|
|
continue
|
|
|
|
except AttributeError:
|
|
print("Process variable was never initialized. But we continue")
|
|
continue
|
|
|
|
except OSError as e:
|
|
pid_label = pid if 'pid' in locals() else "unknown"
|
|
# Catch-all for other OS-level failures (e.g., permission issues)
|
|
error_msg = f"Failed to kill process {each_process_type} with pid: {pid_label} and error: {e}"
|
|
logger.error(error_msg)
|
|
return Result(valid=False, error_type=ResultError.STATE, message=error_msg)
|
|
|
|
|
|
# Tor isn't kept as a subprocess to kill, it has its own method:
|
|
if protocol == ProtocolChoice.TOR:
|
|
SessionStateController.shut_down_tor_from_state(profile_id)
|
|
|
|
# Clear the repo:
|
|
print("Clearing the repo")
|
|
repo.remove_session_profile(profile_id)
|
|
|
|
# Delete model:
|
|
SessionState.remove_file(id=profile_id)
|
|
return Result(valid=True)
|
|
|
|
@staticmethod
|
|
def shut_down_tor_from_state(profile_id: int):
|
|
session_state = SessionStateController.get_model(profile_id)
|
|
if session_state:
|
|
tor_port = session_state.network_port_numbers.tor
|
|
if tor_port:
|
|
if isinstance(tor_port, list):
|
|
tor_port = tor_port[0]
|
|
print(f"Closing Tor on port {tor_port}")
|
|
terminate_tor_session_connection(tor_port)
|
|
|
|
@staticmethod
|
|
def new(profile_id: int):
|
|
# model
|
|
session_state = SessionState(profile_id)
|
|
session_state.save()
|
|
|
|
# repo
|
|
repo = ProfileStateRepository()
|
|
repo.setup_session_profile(profile_id)
|
|
|
|
@staticmethod
|
|
def add_process(profile_id: int, process_type: ProcessType, process: subprocess.Popen):
|
|
repo = ProfileStateRepository()
|
|
repo.add_session_process(profile_id=profile_id, process_type=process_type, process=process)
|
|
|
|
@staticmethod
|
|
def remove_process(profile_id: int, process_type: ProcessType):
|
|
repo = ProfileStateRepository()
|
|
repo.add_session_process(profile_id=profile_id, process_type=process_type, process=None)
|
|
|
|
|
|
@staticmethod
|
|
def activate(profile_id: int) -> bool:
|
|
repo = ProfileStateRepository()
|
|
activated = repo.flip_session_profile_on(profile_id=profile_id)
|
|
|
|
if not activated:
|
|
logger.error("Called activate, when the processes weren't ready to do so.")
|
|
return False
|
|
|
|
# notify UI:
|
|
logger.info(f"Activated profile {profile_id}")
|
|
Notifications.emit(EventType.SESSION_PROFILE_ENABLE, profile_id)
|
|
notification = f"Profile {profile_id} enabled."
|
|
Notifications.emit(EventType.STATUS_UPDATE, notification)
|
|
return True
|
|
|
|
|
|
@staticmethod
|
|
def get_repo(id: int):
|
|
repo = ProfileStateRepository()
|
|
return repo.get_entire_session_process_group(int)
|
|
|
|
@staticmethod
|
|
def exists(id: int):
|
|
return SessionState.exists(id)
|
|
|
|
@staticmethod
|
|
def all():
|
|
return SessionState.all()
|
|
|
|
@staticmethod
|
|
def update_or_create(session_state):
|
|
# save to model:
|
|
session_state.save()
|
|
|
|
# get profile id:
|
|
profile_id = session_state.id
|
|
|
|
# Ensure repo knows this session exists
|
|
ProfileStateRepository().setup_session_profile(profile_id)
|
|
|
|
# # notify UI:
|
|
# Notifications.emit(EventType.SESSION_PROFILE_ENABLE, profile_id)
|
|
|
|
@staticmethod
|
|
def remove(profile_id: int):
|
|
# save to model:
|
|
print("dissolving model")
|
|
SessionState.dissolve(profile_id)
|
|
|
|
# cache:
|
|
print("removing from cache repo")
|
|
repo = ProfileStateRepository()
|
|
repo.remove_session_profile(profile_id)
|
|
|
|
# notify UI:
|
|
print("sending")
|
|
Notifications.emit(EventType.SESSION_PROFILE_DISABLE, profile_id)
|
|
|
|
|
|
@staticmethod
|
|
def scortched_earth_kill_profile(profile_id: int) -> bool:
|
|
session_state = SessionStateController.get_model(profile_id)
|
|
|
|
if session_state:
|
|
killed = kill_all_associated_processes(session_state)
|
|
if not killed:
|
|
return False
|
|
|
|
# delete model:
|
|
SessionState.remove_file(profile_id)
|
|
|
|
# cache:
|
|
print("removing from cache repo")
|
|
repo = ProfileStateRepository()
|
|
repo.remove_session_profile(profile_id)
|
|
|
|
# notify UI:
|
|
print("sending")
|
|
Notifications.emit(EventType.SESSION_PROFILE_DISABLE, profile_id)
|
|
return True |