From 6604650adffad8750898fa325f98017ee0dcb523 Mon Sep 17 00:00:00 2001 From: punix Date: Sun, 11 May 2025 18:24:57 +0200 Subject: [PATCH] ssl_decrypt.py now with output and check_call replace with subprocess.run --- .gitignore | 1 + common_tools.py | 23 +++++++++-------- ssl_decrypt.py | 64 ++++++++++++++++++++++++++++++++---------------- ssl_encrypt.py | 2 +- wirepy.py | 2 +- wp_app_config.py | 15 +++++++----- 6 files changed, 68 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index aaaf151..e0b46ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ debug.log .venv +.venv.bak .idea .vscode __pycache__ diff --git a/common_tools.py b/common_tools.py index 5ffc99f..331384f 100755 --- a/common_tools.py +++ b/common_tools.py @@ -90,20 +90,23 @@ class Create: """ process: CompletedProcess[str] = subprocess.run( ["pkexec", "/usr/local/bin/ssl_decrypt.py"], - stdout=subprocess.PIPE, + capture_output=True, text=True, - check=True, + check=False, ) - path: Path = Path.home() / ".config/wire_py/" - file_in_path: list[Path] = list(path.rglob("*.dat")) - if file_in_path: - if process.returncode == 0: - print("File successfully decrypted...") - else: - print(f"Error with the following code... {process.returncode}") + # Output from Openssl + if process.stdout: + print(process.stdout) + + # Output from Openssl Error + if process.stderr: + print(process.stderr) + + if process.returncode == 0: + print("Datei entschlüsseln wurde erfolgreich entschlossen.") else: - print(_("Ready for import")) + print(f"Fehler bei der Verarbeitung von Dateien: Code {process.returncode}") @staticmethod def encrypt() -> str: diff --git a/ssl_decrypt.py b/ssl_decrypt.py index 617f805..6d65207 100755 --- a/ssl_decrypt.py +++ b/ssl_decrypt.py @@ -1,19 +1,19 @@ #!/usr/bin/python3 """ This Script decrypt Wireguard files for Wirepy users """ -import os -import shutil from pathlib import Path -from subprocess import check_call +import shutil +from subprocess import CompletedProcess +import subprocess from wp_app_config import AppConfig log_name = AppConfig.USER_FILE.read_text() keyfile: Path = Path(f"/home/{log_name}/.config/wire_py/pbwgk.pem") +path_of_crypted_tunnel: Path = Path(f"/home/{log_name}/.config/wire_py") if not keyfile.is_file(): - - check_call( + process: CompletedProcess[str] = subprocess.run( [ "openssl", "rsa", @@ -24,21 +24,27 @@ if not keyfile.is_file(): "-outform", "PEM", "-pubout", - ] + ], + capture_output=True, + text=True, + check=False, ) + print(process.stdout) + if process.returncode == 0: + print("Public key generated successfully.") + else: + print(f"Error with the following code... {process.returncode}") shutil.chown(keyfile, 1000, 1000) -AppConfig.TEMP_DIR2 = f"/home/{log_name}/.config/wire_py/" -detl: list[str] = os.listdir(AppConfig.TEMP_DIR2) -os.chdir(AppConfig.TEMP_DIR2) -detl.remove("keys") -detl.remove("settings") -if os.path.exists(f"{AppConfig.TEMP_DIR2}pbwgk.pem"): - detl.remove("pbwgk.pem") - for detunnels in detl: - tlname2 = f"{detunnels[:-4]}.conf" - extpath = f"{AppConfig.TEMP_DIR}/{tlname2}" - check_call( +if AppConfig.PUBLICKEY.exists: + + crypted__tunnel = [str(file) for file in path_of_crypted_tunnel.glob("*.dat")] + + for tunnel_path in crypted__tunnel: + + base_name = Path(tunnel_path).stem + + process: CompletedProcess[str] = subprocess.run( [ "openssl", "pkeyutl", @@ -46,9 +52,25 @@ if os.path.exists(f"{AppConfig.TEMP_DIR2}pbwgk.pem"): "-inkey", AppConfig.SYSTEM_PATHS["pkey_path"], "-in", - detunnels, + tunnel_path, # full path to the file "-out", - extpath, - ] + f"{AppConfig.TEMP_DIR}/{base_name}.conf", + ], + capture_output=True, + text=True, + check=False, ) - shutil.chown(extpath, 1000, 1000) + + print(f"Processing of the file: {tunnel_path}") + + if process.stdout: + print(process.stdout) + + # Output from Openssl Error + if process.stderr: + print("(Error):", process.stderr) + + if process.returncode == 0: + print(f"File {base_name}.dat successfully decrypted.") + else: + print(f"Error by {tunnel_path}: Code: {process.returncode}") diff --git a/ssl_encrypt.py b/ssl_encrypt.py index 0f07f6a..306928d 100755 --- a/ssl_encrypt.py +++ b/ssl_encrypt.py @@ -2,8 +2,8 @@ """ This Script encrypt Wireguardfiles for Wirepy users for more Security """ import os -import shutil from pathlib import Path +import shutil from subprocess import check_call from wp_app_config import AppConfig from common_tools import LxTools diff --git a/wirepy.py b/wirepy.py index 281e5dc..457b2df 100755 --- a/wirepy.py +++ b/wirepy.py @@ -874,7 +874,7 @@ class FrameWidgets(ttk.Frame): """ checkbox for enable autostart Tunnel """ - Create.files_for_autostart() + AppConfig.get_autostart_content() if self.l_box.size() != 0: self.wg_autostart.configure(state="normal") self.lb_rename.config(state="normal") diff --git a/wp_app_config.py b/wp_app_config.py index 1a0c28b..8f34591 100644 --- a/wp_app_config.py +++ b/wp_app_config.py @@ -122,18 +122,21 @@ class AppConfig: if not cls.SYSTEMD_USER_FOLDER.exists(): cls.SYSTEMD_USER_FOLDER.mkdir(parents=True, exist_ok=True) - for line in SYSTEMD_FILE: - cls.AUTOSTART_SERVICE.write_text(line) + from subprocess import CompletedProcess - process = subprocess.run( + if not cls.AUTOSTART_SERVICE.is_file(): + + content = "\n".join([line for line in SYSTEMD_FILE]) + cls.AUTOSTART_SERVICE.write_text(content) + + process: CompletedProcess[str] = subprocess.run( ["systemctl", "--user", "enable", "wg_start.service"], - stdout=subprocess.PIPE, + capture_output=True, text=True, - check=True, + check=False, ) print(process.stdout) if process.returncode == 0: - print("File for autostart created successfully") print(process.stdout) else: print(f"Error with the following code... {process.returncode}")