from __future__ import annotations import logging from pathlib import Path from app.config import Config from app.models import Mirror from app.services.host import host_path, run_on_host logger = logging.getLogger(__name__) DROPIN_FILENAME = "mirror-manager.conf" def read_current_dns() -> dict: dropin = host_path("etc/systemd/resolved.conf.d", DROPIN_FILENAME) main_conf = host_path("etc/systemd/resolved.conf") return { "dropin_exists": dropin.exists(), "dropin_path": str(dropin), "main_conf_exists": main_conf.exists(), "content": dropin.read_text(encoding="utf-8") if dropin.exists() else None, } def apply_dns(mirror: Mirror) -> dict: ips = mirror.get_ips() if not ips: return {"success": False, "error": "آدرس DNS تعریف نشده"} dropin_dir = host_path("etc/systemd/resolved.conf.d") dropin_dir.mkdir(parents=True, exist_ok=True) dropin_path = dropin_dir / DROPIN_FILENAME dns_line = " ".join(ips) content = f"""# Managed by Mirror Manager - do not edit manually [Resolve] DNS={dns_line} FallbackDNS=1.1.1.1 8.8.8.8 DNSStubListener=yes """ dropin_path.write_text(content, encoding="utf-8") result = run_on_host(["systemctl", "restart", "systemd-resolved"]) if result.returncode != 0: return { "success": False, "error": result.stderr or "خطا در restart systemd-resolved", "applied_content": content, } return { "success": True, "message": f"DNS اعمال شد: {mirror.name}", "ips": ips, "path": str(dropin_path), } def remove_dns_dropin() -> None: dropin = host_path("etc/systemd/resolved.conf.d", DROPIN_FILENAME) if dropin.exists(): dropin.unlink() run_on_host(["systemctl", "restart", "systemd-resolved"])