#!/usr/bin/python3 """ This Script decrypt Wireguard files for Wirepy users """ from pathlib import Path import shutil from subprocess import CompletedProcess import subprocess from wp_app_config import AppConfig log_name = AppConfig.USER_FILE.read_text().strip() 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(): process: CompletedProcess[str] = subprocess.run( [ "openssl", "rsa", "-in", AppConfig.SYSTEM_PATHS["pkey_path"], "-out", keyfile, "-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) 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", "-decrypt", "-inkey", AppConfig.SYSTEM_PATHS["pkey_path"], "-in", tunnel_path, # full path to the file "-out", f"{AppConfig.TEMP_DIR}/{base_name}.conf", ], capture_output=True, text=True, check=False, ) shutil.chown(f"{AppConfig.TEMP_DIR}/{base_name}.conf", 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}")