diff --git a/core/models/manage/pydantic_manager.py b/core/models/manage/pydantic_manager.py index 8e375f2..951c6d9 100644 --- a/core/models/manage/pydantic_manager.py +++ b/core/models/manage/pydantic_manager.py @@ -14,13 +14,12 @@ def save_to_sudo_folder(model: BaseModel, filepath: str) -> bool: Save model to a sudo-protected file using pkexec. """ try: - Path(filepath).parent.mkdir(parents=True, exist_ok=True) - json_data = model.model_dump_json(indent=2) + parent_dir = str(Path(filepath).parent) - # Use pkexec + tee to write with elevated privileges + # Single pkexec call: mkdir + write process = subprocess.Popen( - ('pkexec', 'tee', filepath), + ('pkexec', 'bash', '-c', f'mkdir -p "{parent_dir}" && tee "{filepath}"'), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, diff --git a/core/services/networking/systemwide/encrypted_proxy/configure_singbox.py b/core/services/networking/systemwide/encrypted_proxy/configure_singbox.py index ae2f952..9070b83 100644 --- a/core/services/networking/systemwide/encrypted_proxy/configure_singbox.py +++ b/core/services/networking/systemwide/encrypted_proxy/configure_singbox.py @@ -98,12 +98,16 @@ def configure_singbox( # parse the link: vless = parse_vless_link(validated_data.links) + logger.info("parsed vlink link!") + # use the parsed link: real_config = build_vless_config( vless=vless, socks5_port=random_port, server_ip=validated_data.server_ip ) + if real_config: + logger.info("Got the real vless config") # UNKNOWN else: @@ -114,6 +118,7 @@ def configure_singbox( # SAVE REAL CONFIG ################################### # goes in a sudo protected folder & prompts for password: + logger.info("Saving to sudo folder.") saved_it = attach_config_to_sudo_folder( config_data=real_config, sudo_filepath=profile_sudo_filepath, @@ -169,18 +174,27 @@ def attach_config_to_sudo_folder( if shutil.which('pkexec') is None: raise CommandNotFoundError('pkexec') + logger.info("About to save to backup config path") backup_path = f'{backup_folder}/backup.conf.bak' - with open(backup_path, "w") as configuration_file: - json.dump(config_data, configuration_file, indent=4) - + try: + with open(backup_path, "w") as configuration_file: + json.dump(config_data, configuration_file, indent=4) + logger.info("Saved to backup config path!") + except Exception as e: + logger.error(e) + return False + configuration_is_attached = False failed_attempt_count = 0 while not configuration_is_attached and failed_attempt_count < 3: - - process = subprocess.Popen(('pkexec', 'install', '-D', backup_path, sudo_filepath, '-o', 'root', '-m', '744')) - configuration_is_attached = not bool(os.waitpid(process.pid, 0)[1] >> 8) + logger.info(f"Trying attempt {failed_attempt_count} to write to the sudo folder..") + try: + process = subprocess.Popen(('pkexec', 'install', '-D', backup_path, sudo_filepath, '-o', 'root', '-m', '744')) + configuration_is_attached = not bool(os.waitpid(process.pid, 0)[1] >> 8) + except Exception as e: + logger.error(e) if not configuration_is_attached: failed_attempt_count += 1 diff --git a/core/services/networking/systemwide/encrypted_proxy/singbox_runner.py b/core/services/networking/systemwide/encrypted_proxy/singbox_runner.py index b4afb54..0726671 100644 --- a/core/services/networking/systemwide/encrypted_proxy/singbox_runner.py +++ b/core/services/networking/systemwide/encrypted_proxy/singbox_runner.py @@ -61,8 +61,8 @@ def launch_singbox_binary(profile_id: int) -> Result: process_id = int(activation_result.data) - logger.info(f"Waiting 2 seconds to see if the process id {process_id} is still alive..") - time.sleep(2) + logger.info(f"Waiting 6 seconds to see if the process id {process_id} is still alive..") + time.sleep(6) # Evaluate if running by that exact pid: active = pid_tools.is_running(pid=process_id, process_name="sing-box") diff --git a/core/services/networking/systemwide/encrypted_proxy/vless_config.py b/core/services/networking/systemwide/encrypted_proxy/vless_config.py index 9f6ea21..144027a 100644 --- a/core/services/networking/systemwide/encrypted_proxy/vless_config.py +++ b/core/services/networking/systemwide/encrypted_proxy/vless_config.py @@ -57,7 +57,7 @@ def build_vless_config(vless: dict, socks5_port: int, server_ip: str) -> dict: { "type": "vless", "tag": "proxy", - "server": server_ip, + "server": str(server_ip), "server_port": vless["port"], "uuid": vless["uuid"], "tls": { diff --git a/core/utils/basic_operations/write_or_read_from_json.py b/core/utils/basic_operations/write_or_read_from_json.py index e5524ff..8723cd0 100644 --- a/core/utils/basic_operations/write_or_read_from_json.py +++ b/core/utils/basic_operations/write_or_read_from_json.py @@ -107,6 +107,11 @@ def get_value_from_json_file(filepath: str, key: str, category: str | None = Non data = read_entire_json(filepath) if category: + if isinstance(category, bool): + error_msg = f"Error in get_value_from_json_file, category should not be a boolean: {category}" + logger.error(error_msg) + raise ValueError(error_msg) + if category not in data: raise KeyError(f"Category '{category}' not found in JSON file") subcategory = data[category]