AppConfig and common_utils further developed for Zenrale configuration

This commit is contained in:
Désiré Werner Menrath 2025-05-10 01:55:30 +02:00
parent 481362b2e6
commit d0adaa76e4
5 changed files with 56 additions and 40 deletions

View File

@ -84,7 +84,7 @@ class Create:
AppConfig.TEMP_DIR.mkdir() AppConfig.TEMP_DIR.mkdir()
@staticmethod @staticmethod
def decrypt() -> None: def decrypt() -> str:
""" """
Starts SSL dencrypt Starts SSL dencrypt
""" """
@ -106,7 +106,7 @@ class Create:
print(_("Ready for import")) print(_("Ready for import"))
@staticmethod @staticmethod
def encrypt() -> None: def encrypt() -> str:
""" """
Starts SSL encryption Starts SSL encryption
""" """
@ -258,10 +258,9 @@ class LxTools(tk.Tk):
check=True, check=True,
) )
if result.returncode != 0: if result.returncode != 0:
exit(1) pass
else:
print(result.stdout.strip()) return result.stdout.strip()
return result.stdout.strip()
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
pass pass

View File

@ -1,8 +1,9 @@
[UPDATES] # Configuration
on on
[THEME] # Theme
light dark
[TOOLTIP] # Tooltips
True True
[AUTOSTART ON] # Autostart
off off

View File

@ -6,15 +6,8 @@ import shutil
from pathlib import Path from pathlib import Path
from subprocess import check_call from subprocess import check_call
from wp_app_config import AppConfig from wp_app_config import AppConfig
import getpass
log_name: str = getpass.getuser() log_name = AppConfig.USER_FILE.read_text()
if log_name == "root":
from common_tools import LxTools
log_name: str = LxTools.get_username()
print("replacement method applied")
keyfile: Path = Path(f"/home/{log_name}/.config/wire_py/pbwgk.pem") keyfile: Path = Path(f"/home/{log_name}/.config/wire_py/pbwgk.pem")

View File

@ -2,6 +2,8 @@
""" """
this script is a simple GUI for managing Wireguard Tunnels this script is a simple GUI for managing Wireguard Tunnels
""" """
import getpass
import os import os
import shutil import shutil
import subprocess import subprocess
@ -23,8 +25,9 @@ from common_tools import (
) )
from wp_app_config import AppConfig, Msg from wp_app_config import AppConfig, Msg
Create.dir_and_files() AppConfig.USER_FILE.write_text(getpass.getuser())
Create.make_dir() AppConfig.ensure_directories()
AppConfig.create_default_settings()
Create.decrypt() Create.decrypt()

View File

@ -4,6 +4,7 @@
import gettext import gettext
import locale import locale
from pathlib import Path from pathlib import Path
import subprocess
from typing import Dict, Any from typing import Dict, Any
@ -24,7 +25,14 @@ class AppConfig:
# Configuration files # Configuration files
SETTINGS_FILE: Path = CONFIG_DIR / "settings" SETTINGS_FILE: Path = CONFIG_DIR / "settings"
KEYS_FILE: Path = CONFIG_DIR / "keys" KEYS_FILE: Path = CONFIG_DIR / "keys"
SYSTEMD_USER_FOLDER: Path = Path.home() / ".config/systemd/user"
AUTOSTART_SERVICE: Path = Path.home() / ".config/systemd/user/wg_start.service" AUTOSTART_SERVICE: Path = Path.home() / ".config/systemd/user/wg_start.service"
DEFAULT_SETTINGS: Dict[str, str] = {
"# Configuration": "on",
"# Theme": "dark",
"# Tooltips": True,
"# Autostart": "off",
}
# Updates # Updates
# 1 = 1. Year, 09 = Month of the Year, 2924 = Day and Year of the Year # 1 = 1. Year, 09 = Month of the Year, 2924 = Day and Year of the Year
@ -79,7 +87,8 @@ class AppConfig:
@classmethod @classmethod
def ensure_directories(cls) -> None: def ensure_directories(cls) -> None:
"""Ensures that all required directories exist""" """Ensures that all required directories exist"""
cls.CONFIG_DIR.mkdir(parents=True, exist_ok=True) if not cls.CONFIG_DIR.exists():
cls.CONFIG_DIR.mkdir(parents=True, exist_ok=True)
cls.TEMP_DIR.mkdir(parents=True, exist_ok=True) cls.TEMP_DIR.mkdir(parents=True, exist_ok=True)
@classmethod @classmethod
@ -91,29 +100,40 @@ class AppConfig:
) )
cls.SETTINGS_FILE.write_text(content) cls.SETTINGS_FILE.write_text(content)
@classmethod
def get_image_paths(cls) -> Dict[str, Path]:
"""Returns paths to UI images"""
return {
"main_icon": cls.SYSTEM_PATHS["image_path"] / "48/wg_vpn.png",
"warning": cls.CONFIG_DIR / "images/warning.png",
"success": cls.CONFIG_DIR / "images/success.png",
"error": cls.CONFIG_DIR / "images/error.png",
}
@classmethod @classmethod
def get_autostart_content(cls) -> str: def get_autostart_content(cls) -> str:
"""Returns the content for the autostart service file""" """Returns the content for the autostart service file"""
SYSTEMD_FILE: list[str] = [
"[Unit]",
"Description=Automatic Tunnel Start",
"After=network-online.target",
"",
"[Service]",
"Type=oneshot",
"ExecStartPre=/bin/sleep 5",
"ExecStart=/usr/local/bin/start_wg.py",
"",
"[Install]",
"WantedBy=default.target",
]
if not cls.SYSTEMD_USER_FOLDER.exists():
cls.SYSTEMD_USER_FOLDER.mkdir(parents=True, exist_ok=True)
return """[Unit]Description=Automatic Tunnel Start for line in SYSTEMD_FILE:
After=network-online.target cls.AUTOSTART_SERVICE.write_text(line)
[Service] process = subprocess.run(
Type=oneshot ["systemctl", "--user", "enable", "wg_start.service"],
ExecStartPre=/bin/sleep 5 stdout=subprocess.PIPE,
ExecStart=/usr/local/bin/start_wg.py text=True,
[Install] check=True,
WantedBy=default.target""" )
print(process.stdout)
if process.returncode == 0:
print("File for autostart created successfully")
print(process.stdout)
else:
print(f"Error with the following code... {process.returncode}")
# here is inizialize the class for translate strrings # here is inizialize the class for translate strrings