Wire-Py/ssl_decrypt.py

67 lines
2.0 KiB
Python
Executable File

#!/usr/bin/python3
""" This Script decrypt Wireguard files for Wirepy users """
import argparse
from pathlib import Path
import pwd
import shutil
from subprocess import CompletedProcess, run
from shared_libs.wp_app_config import AppConfig
from shared_libs.common_tools import LxTools
parser = argparse.ArgumentParser()
parser.add_argument("--user", required=True, help="Username of the target file system")
args = parser.parse_args()
try:
# Retrieve UID and GID
user_info = pwd.getpwnam(args.user)
uid = user_info.pw_uid # User ID (e.g., 1000)
gid = user_info.pw_gid # Group ID (e.g., 1000)
except KeyError:
LxTools.msg_window(
AppConfig.IMAGE_PATHS["icon_error"],
AppConfig.IMAGE_PATHS["icon_error"],
"Error decrypt",
f"User '{args.user}' not found.",
exc_info=True,
)
exit(1)
crypted_tunnel: Path = Path(f"/home/{args.user}/.config/wire_py")
if len([str(file) for file in crypted_tunnel.glob("*.dat")]) == 0:
pass
else:
crypted__tunnel = [str(file) for file in crypted_tunnel.glob("*.dat")]
for tunnel_path in crypted__tunnel:
base_name = Path(tunnel_path).stem
process: CompletedProcess[str] = 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", uid, gid)
# Output from Openssl Error
if process.stderr:
LxTools.msg_window(
AppConfig.IMAGE_PATHS["icon_error"],
AppConfig.IMAGE_PATHS["icon_error"],
"Error decrypt",
f"{process.stderr} Error by [{tunnel_path}] Code: {process.returncode}",
)