diff --git a/core/services/helpers/download_file.py b/core/services/helpers/download_file.py index 8d62a9d..17dc07d 100644 --- a/core/services/helpers/download_file.py +++ b/core/services/helpers/download_file.py @@ -77,13 +77,19 @@ def download_file_and_verify( def _get_httpx_client(target_app_name: str, connection_observer: Optional[ConnectionObserver]) -> httpx.Client: connection_type = ConfigurationController.get_connection_enum() - did_it_work = connect.make_client(connection_type, connection_observer) # always gets boolean + made_client = connect.make_client(connection_type, connection_observer) # always gets boolean - if not did_it_work: - raise ConnectionError(f'Could not connect, to download {target_app_name}.') - else: - client = httpx_client.get_http_session() - logger.info(f"client type is {type(client)}") + if not made_client: + if connection_type == ConnectionChoice.SYSTEM: + raise ConnectionError(f'Could not connect, to download {target_app_name}.') + else: # Tor: + if connection_observer: + connection_observer.notify('message', "Tor Bootstrap..") + bootstrap_results = connect.coordinate_bootstrap(connection_observer) + if not bootstrap_results.valid: + raise ConnectionError(f'Could not connect, to download {target_app_name}.') + + client = httpx_client.get_http_session() return client def __calculate_file_hash(file): diff --git a/core/services/helpers/install_dependencies.py b/core/services/helpers/install_dependencies.py index 989fe21..87928b4 100644 --- a/core/services/helpers/install_dependencies.py +++ b/core/services/helpers/install_dependencies.py @@ -11,6 +11,7 @@ from core.models.orm_models.Dependency import Dependency from core.models.orm_calls.dependency_calls import get_dependency_version from core.controllers.ConfigurationController import ConfigurationController from core.utils.basic_operations.compare_versions import version_update_required +from core.observers.ConnectionObserver import ConnectionObserver import httpx from io import BytesIO @@ -21,7 +22,10 @@ import os SUDO_SINGBOX_LOCATION = f"{Constants.SUDO_TARGET_FOLDER}/sing-box" -def setup_singbox_binary(application_version_observer: Optional[ApplicationVersionObserver]) -> Result: +def setup_singbox_binary( + application_version_observer: Optional[ApplicationVersionObserver], + connection_observer: Optional[ConnectionObserver] +) -> Result: """ Rank: Module's Main Orchestrator diff --git a/core/services/networking/httpx/connect.py b/core/services/networking/httpx/connect.py index 804f014..a78527b 100644 --- a/core/services/networking/httpx/connect.py +++ b/core/services/networking/httpx/connect.py @@ -5,6 +5,7 @@ from core.services.networking.tor_tools.tor_orchestrator import establish_tor_co from core.services.networking.tor_tools.tor_dns import setup_SINGLE_use_resolver from core.services.networking.api_requests.ApiResponseModel import ApiResponse, ErrorType +from core.models.Result import Result, ResultError from core.services.networking.httpx.async_batch_requests import async_parallel from core.services.networking.tor_tools.pre_bootstrap import get_bootstrap_port from core.services.networking.api_requests.subtools.get_connection_type import get_connection_type @@ -164,6 +165,24 @@ def bootstrap_and_try_again(method: str, url: str, observer: ConnectionObserver, Called by: single_endpoint """ + bootstrap_results = coordinate_bootstrap(observer) + if not bootstrap_results.valid: + return bootstrap_results + + observer.notify('message', "Tor Confirmed") + + client = httpx_client.get_http_session() + observer.notify('message', "Making Request..") + return make_request( + method=method, + url=url, + client=client, + payload=payload, + billing_code=billing_code + ) + + +def coordinate_bootstrap(observer: ConnectionObserver) -> ApiResponse: global _port_used if _port_used is None: @@ -178,22 +197,13 @@ def bootstrap_and_try_again(method: str, url: str, observer: ConnectionObserver, # BOOTSTRAP SUCCESS _port_used = bootstrap_results.port - observer.notify('message', "Testing Tor..") + if observer: + observer.notify('message', "Testing Tor..") made_client = httpx_client.init_tor_session(_port_used) if not made_client: return bootstrap_results - observer.notify('message', "Tor Confirmed") - - client = httpx_client.get_http_session() - observer.notify('message', "Making Request..") - return make_request( - method=method, - url=url, - client=client, - payload=payload, - billing_code=billing_code - ) - + else: + return ApiResponse(valid=True) def bulk_async(wanted_list: list, observer: ConnectionObserver, client_observer: ClientObserver) -> ApiResponse: