add new methode of get_username() as fallback and add own file_parser for replace con_to_dict
This commit is contained in:
parent
ca26576eab
commit
481362b2e6
114
common_tools.py
114
common_tools.py
@ -99,6 +99,7 @@ class Create:
|
||||
if file_in_path:
|
||||
if process.returncode == 0:
|
||||
print("File successfully decrypted...")
|
||||
|
||||
else:
|
||||
print(f"Error with the following code... {process.returncode}")
|
||||
else:
|
||||
@ -244,16 +245,26 @@ class LxTools(tk.Tk):
|
||||
return lists_file
|
||||
|
||||
@staticmethod
|
||||
def uos() -> None:
|
||||
def get_username() -> str:
|
||||
"""
|
||||
uos = LOGIN USERNAME
|
||||
Returns the username of the logged-in user,
|
||||
even if the script is running with root privileges.
|
||||
"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["logname"],
|
||||
stdout=subprocess.PIPE,
|
||||
text=True,
|
||||
check=True,
|
||||
)
|
||||
if result.returncode != 0:
|
||||
exit(1)
|
||||
else:
|
||||
print(result.stdout.strip())
|
||||
return result.stdout.strip()
|
||||
|
||||
This method displays the username of the logged-in user,
|
||||
even if you are rooted in a shell
|
||||
"""
|
||||
log_name: str = f"{Path.home()}"[6:]
|
||||
file: Path = Path.home() / "/tmp/.log_user"
|
||||
Path(file).write_text(log_name, encoding="utf-8")
|
||||
except subprocess.CalledProcessError:
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def clean_files(TEMP_DIR: Path = None, file: Path = None) -> None:
|
||||
@ -265,8 +276,12 @@ class LxTools(tk.Tk):
|
||||
"""
|
||||
if AppConfig.TEMP_DIR is not None:
|
||||
shutil.rmtree(AppConfig.TEMP_DIR)
|
||||
if file is not None:
|
||||
Path.unlink(file)
|
||||
try:
|
||||
if file is not None:
|
||||
Path.unlink(file)
|
||||
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def msg_window(
|
||||
@ -389,6 +404,55 @@ class Tunnel:
|
||||
Class of Methods for Wire-Py
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def parse_files_to_dictionary() -> Dict[str, List[str]]:
|
||||
data = {}
|
||||
|
||||
if not AppConfig.TEMP_DIR.exists() or not AppConfig.TEMP_DIR.is_dir():
|
||||
pass
|
||||
|
||||
# Get a list of all files in the directorys
|
||||
files = [file for file in AppConfig.TEMP_DIR.iterdir() if file.is_file()]
|
||||
if not files:
|
||||
pass
|
||||
|
||||
# Search for the string in the files
|
||||
for file in files:
|
||||
try:
|
||||
with open(file, "r") as f:
|
||||
content = f.read()
|
||||
# Hier parsen wir die relevanten Zeilen aus dem Inhalt
|
||||
address_line = next(
|
||||
line
|
||||
for line in content.splitlines()
|
||||
if line.startswith("Address")
|
||||
)
|
||||
dns_line = next(
|
||||
line for line in content.splitlines() if line.startswith("DNS")
|
||||
)
|
||||
endpoint_line = next(
|
||||
line
|
||||
for line in content.splitlines()
|
||||
if line.startswith("Endpoint")
|
||||
)
|
||||
|
||||
# Extrahiere die Werte
|
||||
address = address_line.split("=")[1].strip()
|
||||
dns = dns_line.split("=")[1].strip()
|
||||
endpoint = endpoint_line.split("=")[1].strip()
|
||||
|
||||
# Speichere im Dictionary
|
||||
data[file.stem] = {
|
||||
"Address": address,
|
||||
"DNS": dns,
|
||||
"Endpoint": endpoint,
|
||||
}
|
||||
except Exception:
|
||||
# Ignore errors and continue to the next file
|
||||
continue
|
||||
|
||||
return data
|
||||
|
||||
@classmethod
|
||||
def con_to_dict(cls, file: TextIO) -> Tuple[str, str, str, Optional[str]]:
|
||||
"""
|
||||
@ -591,36 +655,6 @@ class ConfigManager:
|
||||
config = cls.load()
|
||||
return config.get(key, default)
|
||||
|
||||
@property
|
||||
def username(self) -> str:
|
||||
"""
|
||||
Returns the username of the logged-in user,
|
||||
even if the script is running with root privileges.
|
||||
|
||||
Returns:
|
||||
str: The username of the logged-in user
|
||||
"""
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
# Versuche zuerst SUDO_USER zu bekommen
|
||||
sudo_user = os.environ.get("SUDO_USER")
|
||||
if sudo_user:
|
||||
return sudo_user
|
||||
|
||||
# Extrahiere aus Path.home()
|
||||
home_path = str(Path.home())
|
||||
if "/home/" in home_path:
|
||||
return home_path.split("/home/")[1].split("/")[0]
|
||||
|
||||
# Fallbacks
|
||||
try:
|
||||
return os.getlogin()
|
||||
except:
|
||||
import getpass
|
||||
|
||||
return getpass.getuser()
|
||||
|
||||
|
||||
class ThemeManager:
|
||||
@staticmethod
|
||||
|
@ -6,13 +6,18 @@ import shutil
|
||||
from pathlib import Path
|
||||
from subprocess import check_call
|
||||
from wp_app_config import AppConfig
|
||||
from common_tools import ConfigManager
|
||||
import getpass
|
||||
|
||||
log_name: str = getpass.getuser()
|
||||
if log_name == "root":
|
||||
|
||||
from common_tools import LxTools
|
||||
|
||||
log_name: str = LxTools.get_username()
|
||||
print("replacement method applied")
|
||||
|
||||
log_name: str = ConfigManager.username()
|
||||
keyfile: Path = Path(f"/home/{log_name}/.config/wire_py/pbwgk.pem")
|
||||
|
||||
# PKEYFILE: Path = "/usr/local/etc/ssl/pwgk.pem"
|
||||
|
||||
if not keyfile.is_file():
|
||||
|
||||
check_call(
|
||||
|
@ -5,13 +5,10 @@ import os
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from subprocess import check_call
|
||||
from common_tools import LxTools
|
||||
from wp_app_config import AppConfig
|
||||
from common_tools import ConfigManager
|
||||
from common_tools import LxTools
|
||||
|
||||
log_name = ConfigManager.username()
|
||||
|
||||
keyfile: Path = Path(f"/home/{log_name}/.config/wire_py/pbwgk.pem")
|
||||
keyfile: Path = AppConfig.PUBLICKEY
|
||||
|
||||
if not keyfile.is_file():
|
||||
|
||||
|
@ -23,7 +23,6 @@ from common_tools import (
|
||||
)
|
||||
from wp_app_config import AppConfig, Msg
|
||||
|
||||
|
||||
Create.dir_and_files()
|
||||
Create.make_dir()
|
||||
Create.decrypt()
|
||||
|
@ -19,6 +19,7 @@ class AppConfig:
|
||||
CONFIG_DIR: Path = BASE_DIR / ".config/wire_py"
|
||||
TEMP_DIR: Path = Path("/tmp/tlecdcwg")
|
||||
USER_FILE: Path = Path("/tmp/.log_user")
|
||||
PUBLICKEY: Path = CONFIG_DIR / "pbwgk.pem"
|
||||
|
||||
# Configuration files
|
||||
SETTINGS_FILE: Path = CONFIG_DIR / "settings"
|
||||
|
Loading…
x
Reference in New Issue
Block a user