2025-06-09 14:43:25 +05:00
|
|
|
|
import asyncio
|
|
|
|
|
|
from typing import Dict, Any
|
2025-06-20 11:22:54 +05:00
|
|
|
|
from sdbus_async.networkmanager import (
|
|
|
|
|
|
NetworkManager,
|
|
|
|
|
|
NetworkManagerSettings,
|
|
|
|
|
|
NetworkConnectionSettings,
|
|
|
|
|
|
NetworkDeviceGeneric,
|
|
|
|
|
|
)
|
2025-06-09 14:43:25 +05:00
|
|
|
|
import sdbus
|
2025-06-20 11:22:54 +05:00
|
|
|
|
from sdbus_async.networkmanager.settings import ConnectionProfile
|
2025-06-09 14:43:25 +05:00
|
|
|
|
from services.arp_service import NetworkScanner
|
|
|
|
|
|
|
2025-06-20 11:22:54 +05:00
|
|
|
|
|
2025-06-09 14:43:25 +05:00
|
|
|
|
class IPService:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Сервис проверки доступности IP-адреса и применения настроек сети
|
|
|
|
|
|
"""
|
2025-06-20 11:22:54 +05:00
|
|
|
|
|
2025-06-09 14:43:25 +05:00
|
|
|
|
def __init__(self):
|
|
|
|
|
|
self.arp_scanner = NetworkScanner()
|
|
|
|
|
|
self.bus = sdbus.sd_bus_open_system()
|
|
|
|
|
|
sdbus.set_default_bus(self.bus)
|
|
|
|
|
|
|
|
|
|
|
|
def _reset_bus(self):
|
2025-06-20 11:22:54 +05:00
|
|
|
|
"""Перезапускает соединение с системной шиной D-Bus"""
|
2025-06-09 14:43:25 +05:00
|
|
|
|
new_bus = sdbus.sd_bus_open_system()
|
|
|
|
|
|
sdbus.set_default_bus(new_bus)
|
2025-06-20 11:22:54 +05:00
|
|
|
|
if hasattr(self, "bus") and self.bus is not None:
|
2025-06-09 14:43:25 +05:00
|
|
|
|
self.bus = None
|
|
|
|
|
|
self.bus = new_bus
|
|
|
|
|
|
|
|
|
|
|
|
async def check_ip_config(self, args: Dict[str, Any]) -> Dict[str, Any]:
|
2025-06-20 11:22:54 +05:00
|
|
|
|
"""Проверяет доступность IP-адреса перед его применением"""
|
2025-06-09 14:43:25 +05:00
|
|
|
|
self._reset_bus()
|
|
|
|
|
|
clients = self.arp_scanner.scan_network()
|
|
|
|
|
|
new_ip = args["ipv4_updates"]["ipv4.address-data"][0]["address"]
|
2025-06-20 11:22:54 +05:00
|
|
|
|
if any(client["ip"] == new_ip for client in clients):
|
|
|
|
|
|
return {
|
|
|
|
|
|
"available": False,
|
|
|
|
|
|
"message": f"IP address {new_ip} уже занят."
|
|
|
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
|
|
|
"available": True,
|
|
|
|
|
|
"message": "Network profile settings are available for use.",
|
|
|
|
|
|
}
|
2025-06-09 14:43:25 +05:00
|
|
|
|
|
|
|
|
|
|
async def apply_ip_config(self, args: Dict[str, Any]) -> None:
|
2025-06-20 11:22:54 +05:00
|
|
|
|
"""Применяет проверенные настройки IP"""
|
2025-06-09 14:43:25 +05:00
|
|
|
|
self._reset_bus()
|
2025-06-20 11:22:54 +05:00
|
|
|
|
nm = NetworkManager()
|
|
|
|
|
|
settings = NetworkManagerSettings()
|
|
|
|
|
|
connection_paths = await settings.get_connections_by_id(
|
|
|
|
|
|
args["connection_id"])
|
2025-06-09 14:43:25 +05:00
|
|
|
|
|
|
|
|
|
|
if not connection_paths:
|
|
|
|
|
|
raise ValueError(f"Подключение {args['connection_id']} не найдено")
|
|
|
|
|
|
|
2025-06-20 11:22:54 +05:00
|
|
|
|
connection = NetworkConnectionSettings(connection_paths[0])
|
2025-06-09 14:43:25 +05:00
|
|
|
|
profile = await connection.get_profile()
|
|
|
|
|
|
settings_dict = profile.to_settings_dict()
|
|
|
|
|
|
|
|
|
|
|
|
for key_path, value in args["ipv4_updates"].items():
|
2025-06-20 11:22:54 +05:00
|
|
|
|
section, *props = key_path.split(".")
|
2025-06-09 14:43:25 +05:00
|
|
|
|
current = settings_dict[section]
|
|
|
|
|
|
for prop in props[:-1]:
|
|
|
|
|
|
current = current.setdefault(prop, {})
|
|
|
|
|
|
current[props[-1]] = value
|
|
|
|
|
|
|
|
|
|
|
|
new_profile = ConnectionProfile.from_settings_dict(settings_dict)
|
|
|
|
|
|
await connection.update(new_profile.to_dbus())
|
|
|
|
|
|
print(f"Обновлены настройки для {args['connection_id']}:")
|
|
|
|
|
|
|
2025-06-20 11:22:54 +05:00
|
|
|
|
interface_name = settings_dict["connection"]["interface-name"]
|
2025-06-09 14:43:25 +05:00
|
|
|
|
devices = await nm.get_devices()
|
|
|
|
|
|
for device_path in devices:
|
2025-06-20 11:22:54 +05:00
|
|
|
|
device = NetworkDeviceGeneric(device_path)
|
2025-06-09 14:43:25 +05:00
|
|
|
|
if await device.interface == interface_name:
|
|
|
|
|
|
await device.disconnect()
|
|
|
|
|
|
print(f"Отключён от {interface_name}")
|
2025-06-20 11:22:54 +05:00
|
|
|
|
await nm.activate_connection(connection=connection_paths[0],
|
|
|
|
|
|
device=device_path)
|
2025-06-09 14:43:25 +05:00
|
|
|
|
print(f"Переподключён к {args['connection_id']}")
|
|
|
|
|
|
return
|
|
|
|
|
|
|
2025-06-20 11:22:54 +05:00
|
|
|
|
raise RuntimeError(
|
|
|
|
|
|
f"Устройство для интерфейса {interface_name} не найдено")
|