Files
Wire-Py/ssl_decrypt.py

66 lines
1.9 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
from shared_libs.message import MessageDialog
parser = argparse.ArgumentParser()
parser.add_argument("--user", required=True, help="Username of the target file system")
args = parser.parse_args()
_ = AppConfig.setup_translations()
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:
MessageDialog("error", _(f"User '{args.user}' not found."), title="Error decrypt")
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:
MessageDialog(
"erro",
_(
f"{process.stderr} Error by [{tunnel_path}] Code: {process.returncode}"
),
title="Error decrypt",
)