diff --git a/core/Constants.py b/core/Constants.py index 5e8e9f2..b230486 100644 --- a/core/Constants.py +++ b/core/Constants.py @@ -6,7 +6,7 @@ import os @dataclass(frozen=True) class Constants: - DB_VERSION_THIS_APP_WANTS = 1 + DB_VERSION_THIS_APP_WANTS = 2 # ticketing group: TICKET_API_BASE_URL: Final[str] = os.environ.get( diff --git a/core/controllers/ConnectionController.py b/core/controllers/ConnectionController.py index 1f4ace0..28a4202 100644 --- a/core/controllers/ConnectionController.py +++ b/core/controllers/ConnectionController.py @@ -1,3 +1,6 @@ +from core.errors.exceptions import * +from core.errors.logger import logger + from collections.abc import Callable from core.Constants import Constants from core.Errors import InvalidSubscriptionError, MissingSubscriptionError, ConnectionUnprotectedError, ConnectionTerminationError, CommandNotFoundError @@ -232,9 +235,13 @@ class ConnectionController: @staticmethod def establish_tor_session_connection(port_number: int, connection_observer: Optional[ConnectionObserver] = None): - - tor_module = TorModule(Constants.HV_TOR_STATE_HOME) - tor_module.create_session(port_number, connection_observer) + try: + tor_module = TorModule(Constants.HV_TOR_STATE_HOME) + tor_module.create_session(port_number, connection_observer) + except TorServiceInitializationError as e: + logger.error(f"TorServiceInitializationError. Tor Can't Start: {e}") + except Exception as e: + logger.error(f"Tor Can't Start: {e}") @staticmethod def terminate_tor_session_connection(port_number: int): diff --git a/core/errors/exceptions.py b/core/errors/exceptions.py index 021d33f..cd81195 100644 --- a/core/errors/exceptions.py +++ b/core/errors/exceptions.py @@ -1,3 +1,7 @@ + +class TorServiceInitializationError(Exception): + pass + class ApplicationError(Exception): pass diff --git a/core/services/networking/use_tor.py b/core/services/networking/use_tor.py index 7479a7c..18b8ddd 100644 --- a/core/services/networking/use_tor.py +++ b/core/services/networking/use_tor.py @@ -16,54 +16,98 @@ from core.errors.logger import logger import json, os from typing import Any from core.Constants import Constants - +import time def tor_get_request(custom_url: str, connection_observer: ConnectionObserver) -> dict: import requests + # ============================================================================ + # INITIALIZE TOR + # ============================================================================ port_number = ConnectionService.get_random_available_port_number() - logger.debug(f"Using Tor on port number {port_number}") + logger.debug(f"[USE TOR SERVICE] Using Tor on port number {port_number}") try: tor_module = TorModule(Constants.HV_TOR_STATE_HOME) - tor_module.create_session(port_number, connection_observer) + logger.debug(f"[USE TOR SERVICE] Tor Started a Session") - proxies = { - "http": f"socks5h://127.0.0.1:{port_number}", - "https": f"socks5h://127.0.0.1:{port_number}", - } + except TorServiceInitializationError as e: + error_msg = f"[USE TOR SERVICE] Tor failed to start: {e}" + tor_module.destroy_session(port_number) + logger.error(error_msg, exc_info=True) + problem_results = evaluate_tor_networking_problem( + custom_url, connection_observer + ) + return problem_results + except Exception as e: + logger.error(f"[USE TOR SERVICE] Unexpected Tor error: {e}") + tor_module.destroy_session(port_number) + problem_results = evaluate_tor_networking_problem( + custom_url, connection_observer + ) + return problem_results - logger.debug(f"Doing Request through Tor via port {port_number}") + proxies = { + "http": f"socks5h://127.0.0.1:{port_number}", + "https": f"socks5h://127.0.0.1:{port_number}", + } + + # ============================================================================ + # USE TOR AS PROXY + # ============================================================================ + try: + logger.debug(f"[TOR GET REQUEST] Using Tor on port number {port_number}") response = requests.get(custom_url, proxies=proxies, timeout=5) - logger.debug("Request through Tor successful.") + logger.debug("[TOR GET REQUEST] Request through Tor Successful!") - # if it crashes from the above error check, then it won't destroy the proxy, + # if it crashes from an error in trying to get a JSON out of it, then we'd skip destroying the Tor session, + # so we'll destroy it now first, + logger.debug("[TOR GET REQUEST] DESTROY SESSION!") tor_module.destroy_session(port_number) - # return response - + # Now that it's safe, get a JSON out of it, and return the response dictonary_of_response = response.json() + logger.debug(f"[TOR GET REQUEST] Got Data out of the reply. {dictonary_of_response}") final_reply = {"valid": True, "data": dictonary_of_response} return final_reply except requests.exceptions.ConnectionError: + tor_module.destroy_session(port_number) problem_results = evaluate_tor_networking_problem( - custom_url, connection_observer, port_number, proxies + custom_url, connection_observer ) return problem_results except requests.exceptions.Timeout: logger.debug("Connection timed out") + tor_module.destroy_session(port_number) problem_results = evaluate_tor_networking_problem( - custom_url, connection_observer, port_number + custom_url, connection_observer ) return problem_results except requests.exceptions.HTTPError: logger.debug(f"HTTP error: {response.status_code}") + tor_module.destroy_session(port_number) problem_results = evaluate_tor_networking_problem( - custom_url, connection_observer, port_number + custom_url, connection_observer + ) + return problem_results + except requests.exceptions.ProxyError: + error_msg = "[USE TOR SERVICE] Requests Proxy Error" + tor_module.destroy_session(port_number) + logger.error(error_msg, exc_info=True) + problem_results = evaluate_tor_networking_problem( + custom_url, connection_observer + ) + return problem_results + + except Exception as e: + logger.error(f"[USE TOR SERVICE] Unexpected Tor error: {e}") + tor_module.destroy_session(port_number) + problem_results = evaluate_tor_networking_problem( + custom_url, connection_observer ) return problem_results @@ -80,41 +124,63 @@ def tor_post_request( "status": "unable_to_get" } # incase this goes to the except block, it's "defined" + # ============================================================================ + # INITIALIZE TOR + # ============================================================================ port_number = ConnectionService.get_random_available_port_number() logger.debug(f"Using Tor on port number {port_number}") try: tor_module = TorModule(Constants.HV_TOR_STATE_HOME) - tor_module.create_session(port_number, connection_observer) + except TorServiceInitializationError as e: + error_msg = f"[USE TOR SERVICE] Tor failed to start: {e}" + tor_module.destroy_session(port_number) + logger.error(error_msg, exc_info=True) + problem_results = evaluate_tor_networking_problem( + custom_url, connection_observer + ) + return problem_results + except Exception as e: + logger.error(f"[USE TOR SERVICE] Unexpected Tor error: {e}") + tor_module.destroy_session(port_number) + problem_results = evaluate_tor_networking_problem( + custom_url, connection_observer + ) + return problem_results - proxies = { - "http": f"socks5h://127.0.0.1:{port_number}", - "https": f"socks5h://127.0.0.1:{port_number}", - } - - logger.debug(f"Sending data through Tor via port {port_number}") + proxies = { + "http": f"socks5h://127.0.0.1:{port_number}", + "https": f"socks5h://127.0.0.1:{port_number}", + } + logger.debug(f"Sending data through Tor via port {port_number}") + # ============================================================================ + # USE TOR AS PROXY + # ============================================================================ + try: response = requests.post(custom_url, json=payload, proxies=proxies) - logger.debug("Sending data through Tor successful.") + logger.debug("[USE TOR SERVICE] Sent data through Tor successfully. Now deleting the session..") tor_module.destroy_session(port_number) return response except requests.exceptions.ConnectionError: - error_msg = "There was a Connection Error with the Tor Module." + error_msg = "[USE TOR SERVICE] There was a Connection Error with the Use Tor Module." logger.error(error_msg, exc_info=True) logger.debug(error_msg) + tor_module.destroy_session(port_number) problem_results = evaluate_tor_networking_problem( custom_url, connection_observer ) return problem_results except requests.exceptions.Timeout: - error_msg = "Connection timed out" + error_msg = "[USE TOR SERVICE] Connection timed out" logger.error(error_msg, exc_info=True) logger.debug(error_msg) + tor_module.destroy_session(port_number) problem_results = evaluate_tor_networking_problem( custom_url, connection_observer ) @@ -122,10 +188,28 @@ def tor_post_request( except requests.exceptions.HTTPError: if isinstance(response, requests.Response): - error_msg = f"HTTP error: {response.status_code}" + error_msg = f"[USE TOR SERVICE] HTTP error: {response.status_code}" logger.error(error_msg, exc_info=True) logger.debug(error_msg) + tor_module.destroy_session(port_number) + problem_results = evaluate_tor_networking_problem( + custom_url, connection_observer + ) + return problem_results + + except requests.exceptions.ProxyError: + error_msg = "[USE TOR SERVICE] Requests Proxy Error" + logger.error(error_msg, exc_info=True) + tor_module.destroy_session(port_number) + problem_results = evaluate_tor_networking_problem( + custom_url, connection_observer + ) + return problem_results + + except Exception as e: + logger.error(f"[USE TOR SERVICE] Unexpected Tor error: {e}") + tor_module.destroy_session(port_number) problem_results = evaluate_tor_networking_problem( custom_url, connection_observer ) diff --git a/pyproject.toml b/pyproject.toml index b0894f8..95e2fea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "sp-hydra-veil-core" -version = "2.3.6" +version = "2.3.7" authors = [ { name = "Simplified Privacy" }, ] @@ -18,7 +18,7 @@ dependencies = [ "pysocks ~= 1.7.1", "python-dateutil ~= 2.9.0.post0", "requests ~= 2.32.5", - "sp-essentials ~= 1.1.0", + "sp-essentials ~= 1.2.0", "annotated-types==0.7.0", "certifi==2026.4.22", "charset-normalizer==3.4.7",