82 lines
2.2 KiB
Python
Executable File
82 lines
2.2 KiB
Python
Executable File
#!/usr/bin/python3
|
|
""" This Script encrypt Wireguardfiles for Wirepy users for more Security """
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
from subprocess import CompletedProcess
|
|
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")
|
|
|
|
target: 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,
|
|
)
|
|
|
|
if process.stdout:
|
|
print(process.stdout)
|
|
|
|
# Output from Openssl Error
|
|
if process.stderr:
|
|
print("(Error):", process.stderr)
|
|
|
|
if process.returncode == 0:
|
|
print("Public key generated successfully.")
|
|
else:
|
|
print(f"Error generate Publickey: Code: {process.returncode}")
|
|
|
|
shutil.chown(keyfile, 1000, 1000)
|
|
|
|
# any() get True when directory is not empty
|
|
if AppConfig.TEMP_DIR.exists() and any(AppConfig.TEMP_DIR.iterdir()):
|
|
clear_files = [str(file) for file in AppConfig.TEMP_DIR.glob("*.conf")]
|
|
|
|
for config_file in clear_files:
|
|
base_name = Path(config_file).stem
|
|
process: CompletedProcess[str] = subprocess.run(
|
|
[
|
|
"openssl",
|
|
"pkeyutl",
|
|
"-encrypt",
|
|
"-inkey",
|
|
keyfile,
|
|
"-pubin",
|
|
"-in",
|
|
config_file,
|
|
"-out",
|
|
f"{target}/{base_name}.dat",
|
|
],
|
|
capture_output=True,
|
|
text=True,
|
|
check=False,
|
|
)
|
|
|
|
print(f"Processing of the file: {config_file}")
|
|
|
|
# Output from Openssl Error
|
|
if process.stderr:
|
|
print("(Error):", process.stderr)
|
|
|
|
if process.returncode == 0:
|
|
print(f"File {base_name}.dat successfully encrypted.")
|
|
else:
|
|
print(f"Error by {config_file}: Code: {process.returncode}")
|