37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
|
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
|
||
|
|