ssl_decrypt.py now with output and check_call replace with subprocess.run

This commit is contained in:
2025-05-11 18:24:57 +02:00
parent a903666a26
commit 6604650adf
6 changed files with 68 additions and 39 deletions

View File

@ -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}")