Compare commits

..

No commits in common. "1695e0314e179c3a095bb117de7506ceae7db443" and "951802fe5caf3e9be145d8e225484d741a9fea84" have entirely different histories.

7 changed files with 170 additions and 353 deletions

View file

@ -1,10 +1,5 @@
# Major Change Log:
# Reduce Redundancy
### Aug 13, 2026
Isolated download file modules to their own module, then had both the install application versions (browsers), and install dependencies both use that same module.
<br/>
# Singbox Setup
### Aug 12, 2026
Prepared Singbox setup modules, which includes installation, download, move to sudo folder, and sudo setup scripts. Added a `Dependency` model, endpoint, and the ability to sync that model. (Related note: Server-side prepared the endpoint, and stocked with real data.) And also the sync service modules were adjusted to handle new data types more smoothly, before they had errors. As part of that sync flow change, the CachedSync metadata model was transitioned to ints instead of strings, with default 0 values. This should in theory migrate all clients without further changes needed.

View file

@ -1,9 +1,6 @@
from core.services.networking.httpx import httpx_client
from core.services.networking.httpx import connect
from core.services.networking.api_requests.ApiResponseModel import ApiResponse, ErrorType
from core.models.Result import Result, ResultError
from core.services.helpers.download_file import download_file_and_verify
from core.controllers.ConfigurationController import ConfigurationController
from core.models.Configuration import Configuration, ConnectionChoice
@ -21,7 +18,7 @@ from core.observers.ConnectionObserver import ConnectionObserver
from core.services.WebServiceApiService import WebServiceApiService
from core.errors.logger import logger
# import httpx
import httpx
from io import BytesIO
from typing import Optional
import hashlib
@ -90,27 +87,54 @@ class ApplicationVersionController:
def __install(application_version: ApplicationVersion, application_version_observer: Optional[ApplicationVersionObserver] = None, connection_observer: Optional[ConnectionObserver] = None):
target_app_name = application_version.application_code.capitalize()
target_app_version = application_version.version_number
download_path = application_version.download_path
target_file_hash = application_version.file_hash
if application_version_observer is not None:
application_version_observer.notify('downloading', f"Downloading {target_app_name} {target_app_version}. Connecting..")
download_result = download_file_and_verify(
target_app_name=target_app_name,
download_path=download_path,
target_file_hash=target_file_hash,
application_version_observer=application_version_observer,
target_app_version=target_app_version,
connection_observer=connection_observer
)
# legacy:
# application_version_observer.notify('downloading', application_version)
################################################
# IT WORKED - SAVE IT
# SETUP HTTP CLIENT
################################################
if download_result.valid:
response_buffer = download_result.data
file_hash = download_result.message
client = httpx_client.get_http_session()
logger.info(f"client type is {type(client)}")
if client is None:
client = _get_httpx_client(target_app_name=target_app_name, connection_observer=connection_observer)
################################################
# GET THE DATA
################################################
download_path = application_version.download_path
logger.info(f"download_path is {download_path}")
with client.stream('GET', download_path) as response:
logger.info("doing the stream...")
if response.status_code == 200:
response_size = int(response.headers.get('Content-Length', 0))
response_buffer = BytesIO()
block_size = 1024
bytes_written = 0
for data in response.iter_bytes(block_size):
bytes_written += len(data)
response_buffer.write(data)
progress = (bytes_written / response_size) * 100 if response_size > 0 else 0
if application_version_observer is not None:
application_version_observer.notify('download_progressing', f"Downloading {target_app_name} {progress:.2f}% v: {target_app_version}")
else:
raise ConnectionError('The application version could not be downloaded.')
application_version_observer.notify('downloaded', f"Downloaded {target_app_name} {target_app_version}")
response_buffer.seek(0)
################################################
# VERIFY THE HASH
################################################
file_hash = ApplicationVersionController.__calculate_file_hash(response_buffer)
if file_hash != application_version.file_hash:
raise FileIntegrityError('Application version file integrity could not be verified.')
with tarfile.open(fileobj=response_buffer, mode = 'r:gz') as tar_file:
tar_file.extractall(application_version.get_installation_path())
@ -118,93 +142,43 @@ class ApplicationVersionController:
with open(f'{application_version.get_installation_path()}/.sha3-512', 'w') as hash_file:
hash_file.write(f'{file_hash}\n')
################################################
# FAILED
################################################
else:
if download_result.error_type == ResultError.CONNECTION:
@staticmethod
def __calculate_file_hash(file):
hasher = hashlib.sha3_512()
buffer = file.read(65536)
while len(buffer) > 0:
hasher.update(buffer)
buffer = file.read(65536)
file.seek(0)
return hasher.hexdigest()
# This function is a temporary transition for the connect module to get better public APIs
def _get_httpx_client(target_app_name: str, connection_observer: Optional[ConnectionObserver]) -> httpx.Client:
connection_type = ConfigurationController.get_connection_enum()
client = connect.make_client(connection_type, connection_observer)
logger.info(f"client type is {type(client)}")
if isinstance(client, ApiResponse):
if not client.valid:
raise ConnectionError(f'Could not connect, to download {target_app_name}.')
if isinstance(client, bool):
if not client:
raise ConnectionError(f'Could not connect, to download {target_app_name}.')
elif download_result.error_type == ResultError.INVALID_INPUT:
raise FileIntegrityError('Application version file integrity could not be verified.')
else:
return None
logger.info("It's a boolean, getting the client now.")
client = httpx_client.get_http_session()
logger.info(f"client type is {type(client)}")
return client
# legacy:
# @staticmethod
# def __calculate_file_hash(file):
# hasher = hashlib.sha3_512()
# buffer = file.read(65536)
# while len(buffer) > 0:
# hasher.update(buffer)
# buffer = file.read(65536)
# file.seek(0)
# return hasher.hexdigest()
# 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
# 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)}")
# return client
# @staticmethod
# def get_all(application: Optional[Application] = None):
# return ApplicationVersion.all(application)
# legacy:
# application_version_observer.notify('downloading', application_version)
################################################
# SETUP HTTP CLIENT
################################################
# client = httpx_client.get_http_session()
# logger.info(f"client type is {type(client)}")
# if client is None:
# client = _get_httpx_client(target_app_name=target_app_name, connection_observer=connection_observer)
# ################################################
# # GET THE DATA
# ################################################
# download_path = application_version.download_path
# logger.info(f"download_path is {download_path}")
# with client.stream('GET', download_path) as response:
# logger.info("doing the stream...")
# if response.status_code == 200:
# response_size = int(response.headers.get('Content-Length', 0))
# response_buffer = BytesIO()
# block_size = 1024
# bytes_written = 0
# for data in response.iter_bytes(block_size):
# bytes_written += len(data)
# response_buffer.write(data)
# progress = (bytes_written / response_size) * 100 if response_size > 0 else 0
# if application_version_observer is not None:
# application_version_observer.notify('download_progressing', f"Downloading {target_app_name} {progress:.2f}% v: {target_app_version}")
# else:
# raise ConnectionError('The application version could not be downloaded.')
# application_version_observer.notify('downloaded', f"Downloaded {target_app_name} {target_app_version}")
# response_buffer.seek(0)
################################################
# VERIFY THE HASH
################################################
# file_hash = ApplicationVersionController.__calculate_file_hash(response_buffer)
# if file_hash != application_version.file_hash:
# raise FileIntegrityError('Application version file integrity could not be verified.')

View file

@ -1,107 +0,0 @@
from core.services.networking.httpx import httpx_client
from core.services.networking.httpx import connect
from core.services.networking.api_requests.ApiResponseModel import ApiResponse, ErrorType
from core.models.Result import Result, ResultError
from core.errors.logger import logger
from core.controllers.ConfigurationController import ConfigurationController
from core.models.Configuration import Configuration, ConnectionChoice
from core.observers.ApplicationVersionObserver import ApplicationVersionObserver
from core.observers.ConnectionObserver import ConnectionObserver
import httpx
from io import BytesIO
from typing import Optional
import hashlib
def download_file_and_verify(
target_app_name: str,
download_path: str,
target_file_hash: str,
application_version_observer: Optional[ApplicationVersionObserver] = None,
target_app_version: Optional[str] = None,
connection_observer: Optional[ConnectionObserver] = None
) -> Result:
"""
Download/stream a file, and return the BytesIO buffer
"""
################################################
# SETUP HTTP CLIENT
################################################
client = httpx_client.get_http_session()
if client is None:
client = _get_httpx_client(target_app_name=target_app_name, connection_observer=connection_observer)
################################################
# GET THE DATA
################################################
with client.stream('GET', download_path) as response:
if response.status_code == 200:
response_size = int(response.headers.get('Content-Length', 0))
response_buffer = BytesIO()
block_size = 1024
bytes_written = 0
for data in response.iter_bytes(block_size):
bytes_written += len(data)
response_buffer.write(data)
progress = (bytes_written / response_size) * 100 if response_size > 0 else 0
if application_version_observer is not None:
if target_app_version:
application_version_observer.notify('download_progressing', f"Downloading {target_app_name} {progress:.2f}% v: {target_app_version}")
else:
application_version_observer.notify('download_progressing', f"Downloading {target_app_name} {progress:.2f}%")
else:
error_msg = f"Could not download {target_app_name} because of a Connection Error."
logger.error(error_msg)
return Result(valid=False, error_type=ResultError.CONNECTION, message=error_msg)
if application_version_observer is not None:
application_version_observer.notify('downloaded', f"Downloaded {target_app_name}")
response_buffer.seek(0)
################################################
# VERIFY THE HASH
################################################
real_file_hash = __calculate_file_hash(response_buffer)
if real_file_hash != target_file_hash:
error_msg = f'Application version file integrity could not be verified. We are targeting {target_file_hash}, but got {real_file_hash}'
logger.error(error_msg)
return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=error_msg)
else:
return Result(valid=True, data=response_buffer, message=real_file_hash)
def _get_httpx_client(target_app_name: str, connection_observer: Optional[ConnectionObserver]) -> httpx.Client:
connection_type = ConfigurationController.get_connection_enum()
made_client = connect.make_client(connection_type, connection_observer) # always gets boolean
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):
hasher = hashlib.sha3_512()
buffer = file.read(65536)
while len(buffer) > 0:
hasher.update(buffer)
buffer = file.read(65536)
file.seek(0)
return hasher.hexdigest()

View file

@ -1,5 +1,4 @@
from core.services.networking.httpx.httpx_client import get_http_session, init_session
from core.services.helpers.download_file import download_file_and_verify
from core.utils.basic_operations.folder_tools import validate_folder_structure
from core.utils.basic_operations.does_file_exist import does_file_exist
from core.models.Result import Result, ResultError
@ -11,7 +10,6 @@ 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
@ -22,10 +20,7 @@ import os
SUDO_SINGBOX_LOCATION = f"{Constants.SUDO_TARGET_FOLDER}/sing-box"
def setup_singbox_binary(
application_version_observer: Optional[ApplicationVersionObserver],
connection_observer: Optional[ConnectionObserver]
) -> Result:
def setup_singbox_binary(application_version_observer: Optional[ApplicationVersionObserver]) -> Result:
"""
Rank:
Module's Main Orchestrator
@ -98,8 +93,7 @@ def setup_singbox_binary(
target_file_hash=target_file_hash,
target_app_name="sing-box",
target_version=target_version,
application_version_observer=application_version_observer,
connection_observer=connection_observer
application_version_observer=application_version_observer
)
if not file_result.valid:
@ -255,7 +249,6 @@ def download_and_verify(
target_app_name: str,
target_version: str,
application_version_observer: Optional[ApplicationVersionObserver] = None,
connection_observer: Optional[ConnectionObserver] = None
) -> Result:
"""
Purpose:
@ -269,24 +262,55 @@ def download_and_verify(
application_version_observer.notify('downloading', "singbox")
################################################
# GET & VERIFY
# SETUP HTTP CLIENT
################################################
download_result = download_file_and_verify(
target_app_name=target_app_name,
download_path=download_path,
target_file_hash=target_file_hash,
application_version_observer=application_version_observer,
target_app_version=target_version,
connection_observer=connection_observer
)
init_session()
client = get_http_session()
if client is None:
init_session
client = get_http_session()
if not download_result.valid:
return download_result
################################################
# GET THE DATA
################################################
with client.stream('GET', download_path) as response:
if response.status_code == 200:
response_size = int(response.headers.get('Content-Length', 0))
response_buffer = BytesIO()
block_size = 1024
bytes_written = 0
for data in response.iter_bytes(block_size):
bytes_written += len(data)
response_buffer.write(data)
progress = (bytes_written / response_size) * 100 if response_size > 0 else 0
if application_version_observer is not None:
application_version_observer.notify('download_progressing', f"Downloading {target_app_name} {progress:.2f}%")
else:
error_msg = f"Could not download {target_app_name} because of a Connection Error."
logger.error(error_msg)
return Result(valid=False, error_type=ResultError.CONNECTION, message=error_msg)
if application_version_observer is not None:
application_version_observer.notify('downloaded', f"Downloaded {target_app_name}")
response_buffer.seek(0)
################################################
# VERIFY THE HASH
################################################
real_file_hash = __calculate_file_hash(response_buffer)
if real_file_hash != target_file_hash:
error_msg = f'Application version file integrity could not be verified. We are targeting {target_file_hash}, but got {real_file_hash}'
logger.error(error_msg)
return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=error_msg)
################################################
# SAVE IT IN CORRECT STRUCTURE
################################################
response_buffer = download_result.data
temp_dir = f"{target_folder}/temp_dir"
final_target_folder = f"{target_folder}/{target_version}"
@ -310,71 +334,18 @@ def download_and_verify(
return Result(valid=False, error_type=ResultError.MISSING_FILE)
# Legacy:
# Repeat function outside ApplicationController,
# because we are moving away from fake static object structure.
def __calculate_file_hash(file):
# def __calculate_file_hash(file):
hasher = hashlib.sha3_512()
buffer = file.read(65536)
# hasher = hashlib.sha3_512()
# buffer = file.read(65536)
while len(buffer) > 0:
# while len(buffer) > 0:
hasher.update(buffer)
buffer = file.read(65536)
# hasher.update(buffer)
# buffer = file.read(65536)
# file.seek(0)
# return hasher.hexdigest()
# ################################################
# # SETUP HTTP CLIENT
# ################################################
# init_session()
# client = get_http_session()
# if client is None:
# init_session
# client = get_http_session()
################################################
# GET THE DATA
################################################
# with client.stream('GET', download_path) as response:
# if response.status_code == 200:
# response_size = int(response.headers.get('Content-Length', 0))
# response_buffer = BytesIO()
# block_size = 1024
# bytes_written = 0
# for data in response.iter_bytes(block_size):
# bytes_written += len(data)
# response_buffer.write(data)
# progress = (bytes_written / response_size) * 100 if response_size > 0 else 0
# if application_version_observer is not None:
# application_version_observer.notify('download_progressing', f"Downloading {target_app_name} {progress:.2f}%")
# else:
# error_msg = f"Could not download {target_app_name} because of a Connection Error."
# logger.error(error_msg)
# return Result(valid=False, error_type=ResultError.CONNECTION, message=error_msg)
# if application_version_observer is not None:
# application_version_observer.notify('downloaded', f"Downloaded {target_app_name}")
# response_buffer.seek(0)
# ################################################
# # VERIFY THE HASH
# ################################################
# real_file_hash = __calculate_file_hash(response_buffer)
# if real_file_hash != target_file_hash:
# error_msg = f'Application version file integrity could not be verified. We are targeting {target_file_hash}, but got {real_file_hash}'
# logger.error(error_msg)
# return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=error_msg)
file.seek(0)
return hasher.hexdigest()

View file

@ -5,7 +5,6 @@ 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
@ -45,7 +44,7 @@ def single_endpoint(method: str, url: str, observer: ConnectionObserver, payload
########################################################
if client is None:
observer.notify('message', "Testing Connection..")
made_client = make_client(connection_type, observer) # makes boolean
made_client = make_client(connection_type, observer)
if not made_client and connection_type == ConnectionChoice.TOR:
observer.notify('message', "Tor Bootstrap..")
return bootstrap_and_try_again(
@ -115,16 +114,13 @@ def single_endpoint(method: str, url: str, observer: ConnectionObserver, payload
return initial_result
def make_client(connection_type: str, observer: Optional[ConnectionObserver] = None) -> bool:
def make_client(connection_type: str, observer: Optional[ConnectionObserver] = None) -> httpx.Client | ApiResponse:
"""
Rank:
Coordinator
Purpose:
Create an HTTPx Client for either kind of transport
Returns:
Boolean of result
"""
global _port_used
@ -132,21 +128,20 @@ def make_client(connection_type: str, observer: Optional[ConnectionObserver] = N
if connection_type == ConnectionChoice.SYSTEM:
return httpx_client.init_session() # this is not the client, its a boolean
# Tor:
if _port_used is None:
_port_used = Constants.DEFAULT_TOR_PORT
logger.info(f"Set to default port of {Constants.DEFAULT_TOR_PORT}")
# Tor:
# check if port is even listening:
listening = ports.is_port_in_use(_port_used)
if listening:
boolean_if_worked = httpx_client.init_tor_session(_port_used)
if boolean_if_worked:
return True
client = httpx_client.init_tor_session(_port_used)
if client:
return client
else:
logger.error(f"Could NOT create a Tor HTTPx client on port {_port_used}.. Bootstrapping..")
return False
return False
@ -165,24 +160,6 @@ 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:
@ -197,13 +174,22 @@ def coordinate_bootstrap(observer: ConnectionObserver) -> ApiResponse:
# BOOTSTRAP SUCCESS
_port_used = bootstrap_results.port
if observer:
observer.notify('message', "Testing Tor..")
made_client = httpx_client.init_tor_session(_port_used)
if not made_client:
return bootstrap_results
else:
return ApiResponse(valid=True)
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 bulk_async(wanted_list: list, observer: ConnectionObserver, client_observer: ClientObserver) -> ApiResponse:

View file

@ -33,4 +33,4 @@ def version_update_required(new_version: str, version_installed: str) -> bool:
return False
# step 4) if equal, go into the last digit
return new_patch >= installed_patch
return new_patch > installed_patch

View file

@ -50,8 +50,6 @@ def validate_folder_structure(temp_dir: str, target_folder: str, target_file: st
if found_file:
if found_file.parent != target_folder:
shutil.move(str(found_file), str(target_file_path))
# finally, remove temp directory,
shutil.rmtree(temp_dir, ignore_errors=True)
return True
else:
return False