Keep It Simple Stupid. Started using the CherryPy server and made classes from the parts.
This commit is contained in:
29
kiss/toml-manager/services/toml_service.py
Normal file
29
kiss/toml-manager/services/toml_service.py
Normal file
@ -0,0 +1,29 @@
|
||||
import tomllib
|
||||
from pathlib import Path
|
||||
import logging
|
||||
|
||||
def read_toml_file(file_path: str) -> dict:
|
||||
try:
|
||||
with open(file_path, "rb") as file:
|
||||
return tomllib.load(file)
|
||||
except FileNotFoundError:
|
||||
logging.error(f"File {file_path} not found.")
|
||||
return {}
|
||||
except tomllib.TOMLDecodeError as e:
|
||||
logging.error(f"Error parsing {file_path}: {e}")
|
||||
return {}
|
||||
|
||||
def write_toml_file(file_path: str, data: dict) -> None:
|
||||
with open(file_path, "w") as file:
|
||||
toml.dump(data, file)
|
||||
|
||||
def create_backup(file_path: str) -> str:
|
||||
config_path = Path(file_path)
|
||||
backup_dir = config_path.parent / "backups"
|
||||
backup_dir.mkdir(exist_ok=True)
|
||||
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
backup_file = backup_dir / f"{config_path.stem}_{timestamp}.toml"
|
||||
shutil.copy2(file_path, backup_file)
|
||||
logging.info(f"Backup created: {backup_file}")
|
||||
return backup_file
|
36
kiss/toml-manager/services/wireguard_service.py
Normal file
36
kiss/toml-manager/services/wireguard_service.py
Normal file
@ -0,0 +1,36 @@
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import logging
|
||||
import subprocess
|
||||
from datetime import datetime
|
||||
|
||||
def read_config(config_file: str) -> str:
|
||||
with open(config_file, 'r') as file:
|
||||
return file.read()
|
||||
|
||||
def write_config(config_file: str, content: str) -> None:
|
||||
with open(config_file, 'w') as file:
|
||||
file.write(content)
|
||||
|
||||
def create_backup(config_file: str) -> str:
|
||||
config_path = Path(config_file)
|
||||
backup_dir = config_path.parent / "backups"
|
||||
backup_dir.mkdir(exist_ok=True)
|
||||
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
backup_file = backup_dir / f"{config_path.stem}_{timestamp}.conf"
|
||||
shutil.copy2(config_file, backup_file)
|
||||
logging.info(f"Backup created: {backup_file}")
|
||||
return backup_file
|
||||
|
||||
def reload_wireguard_service(config_file: str):
|
||||
interface = Path(config_file).stem
|
||||
try:
|
||||
subprocess.run(["wg-quick", "down", interface], check=True)
|
||||
subprocess.run(["wg-quick", "up", interface], check=True)
|
||||
logging.info(f"WireGuard service for interface {interface} restarted successfully")
|
||||
return True
|
||||
except subprocess.CalledProcessError as e:
|
||||
logging.error(f"Failed to reload WireGuard service: {str(e)}")
|
||||
return False
|
||||
|
Reference in New Issue
Block a user