2025-06-09 10:17:12 +05:00
|
|
|
|
from typing import List, Dict, Tuple, Union, Optional
|
|
|
|
|
|
from sdbus_async.networkmanager import (
|
2025-06-12 16:32:05 +05:00
|
|
|
|
NetworkManager,
|
|
|
|
|
|
NetworkDeviceGeneric,
|
|
|
|
|
|
IPv4Config,
|
|
|
|
|
|
IPv6Config,
|
|
|
|
|
|
NetworkManagerSettings,
|
|
|
|
|
|
NetworkDeviceWireless,
|
|
|
|
|
|
AccessPoint,
|
|
|
|
|
|
DeviceType,
|
2025-06-09 10:17:12 +05:00
|
|
|
|
)
|
|
|
|
|
|
from sdbus import sd_bus_open_system
|
|
|
|
|
|
from models.network import (
|
2025-06-12 16:32:05 +05:00
|
|
|
|
NetworkInterface,
|
|
|
|
|
|
IPv4Route,
|
|
|
|
|
|
IPv6Route,
|
|
|
|
|
|
IPConfig,
|
|
|
|
|
|
IndividualIPConfig,
|
|
|
|
|
|
NetworkInterfacesContainer,
|
|
|
|
|
|
WiFiDevice,
|
2025-06-09 10:17:12 +05:00
|
|
|
|
)
|
|
|
|
|
|
import subprocess
|
|
|
|
|
|
|
2025-06-12 16:32:05 +05:00
|
|
|
|
|
2025-06-09 10:17:12 +05:00
|
|
|
|
class NetworkService:
|
|
|
|
|
|
"""Сервис для взаимодействия с NetworkManager через D-Bus"""
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
self.system_bus = sd_bus_open_system()
|
|
|
|
|
|
self.nm = NetworkManager(self.system_bus)
|
|
|
|
|
|
self.nm_settings = NetworkManagerSettings(self.system_bus)
|
|
|
|
|
|
|
2025-06-12 16:47:27 +05:00
|
|
|
|
async def get_ip_method(self, device: NetworkDeviceGeneric,
|
|
|
|
|
|
ip_type: str) -> str:
|
2025-06-09 10:17:12 +05:00
|
|
|
|
"""Получает метод IP для устройства"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
config = await device.get_applied_connection()
|
|
|
|
|
|
settings = config[0].get(ip_type, {})
|
2025-06-12 16:32:05 +05:00
|
|
|
|
return str(settings.get("method", ("s", "unknown"))[1])
|
2025-06-09 10:17:12 +05:00
|
|
|
|
except Exception:
|
2025-06-12 16:32:05 +05:00
|
|
|
|
return "unknown"
|
2025-06-09 10:17:12 +05:00
|
|
|
|
|
|
|
|
|
|
async def get_connection_name(self, device: NetworkDeviceGeneric) -> str:
|
|
|
|
|
|
"""Извлекает имя соединения (GENERAL.CONNECTION) для устройства"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
applied_connection = await device.get_applied_connection()
|
|
|
|
|
|
connection_settings = applied_connection[0]
|
2025-06-12 16:32:05 +05:00
|
|
|
|
connection_info = connection_settings.get("connection", {})
|
|
|
|
|
|
return str(connection_info.get("id", ("s", "unknown"))[1])
|
2025-06-09 10:17:12 +05:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
# print(f"Ошибка при получении соединения для устройства {await device.interface}: {e}")
|
|
|
|
|
|
# срёт ошибки дл отключенных интерфейсов
|
|
|
|
|
|
# добавить потом условия для отключенных интерфейсов
|
2025-06-12 16:32:05 +05:00
|
|
|
|
return "unknown"
|
2025-06-09 10:17:12 +05:00
|
|
|
|
|
2025-06-12 16:47:27 +05:00
|
|
|
|
async def get_ip_config(self, device: NetworkDeviceGeneric,
|
|
|
|
|
|
ip_version: str) -> List[IndividualIPConfig]:
|
2025-06-09 10:17:12 +05:00
|
|
|
|
"""Получает конфигурацию IP для устройства"""
|
|
|
|
|
|
configs = []
|
2025-06-12 16:32:05 +05:00
|
|
|
|
device_ip = await getattr(device, f"ip{ip_version}_config")
|
|
|
|
|
|
if device_ip != "/":
|
2025-06-12 16:47:27 +05:00
|
|
|
|
ip_config = globals()[f"IPv{ip_version}Config"](device_ip,
|
|
|
|
|
|
self.system_bus)
|
2025-06-09 10:17:12 +05:00
|
|
|
|
address_data = await ip_config.address_data
|
|
|
|
|
|
for address in address_data:
|
|
|
|
|
|
config = IndividualIPConfig(
|
2025-06-12 16:32:05 +05:00
|
|
|
|
address=str(address["address"][1]),
|
2025-06-09 10:17:12 +05:00
|
|
|
|
routes=[
|
2025-06-12 16:32:05 +05:00
|
|
|
|
globals()[f"IPv{ip_version}Route"](
|
|
|
|
|
|
destination=route.get(
|
2025-06-12 16:47:27 +05:00
|
|
|
|
"dest",
|
|
|
|
|
|
("s",
|
|
|
|
|
|
"0.0.0.0" if ip_version == "4" else "::"))[1],
|
2025-06-12 16:32:05 +05:00
|
|
|
|
prefix=route.get("prefix", ("u", 0))[1],
|
|
|
|
|
|
next_hop=route.get(
|
|
|
|
|
|
"next-hop",
|
2025-06-12 16:47:27 +05:00
|
|
|
|
("s",
|
|
|
|
|
|
"0.0.0.0" if ip_version == "4" else "::"),
|
2025-06-12 16:32:05 +05:00
|
|
|
|
)[1],
|
|
|
|
|
|
metric=route.get("metric", ("u", 0))[1],
|
2025-06-12 16:47:27 +05:00
|
|
|
|
) for route in await ip_config.route_data
|
2025-06-09 10:17:12 +05:00
|
|
|
|
],
|
2025-06-12 16:47:27 +05:00
|
|
|
|
method=await self.get_ip_method(device,
|
|
|
|
|
|
f"ipv{ip_version}"),
|
2025-06-09 10:17:12 +05:00
|
|
|
|
gateway=str(await ip_config.gateway),
|
2025-06-12 16:32:05 +05:00
|
|
|
|
dns=[
|
|
|
|
|
|
str(ns["address"][1]) if ip_version == "4" else str(ns)
|
|
|
|
|
|
for ns in await getattr(
|
|
|
|
|
|
ip_config,
|
2025-06-12 16:47:27 +05:00
|
|
|
|
"nameserver_data" if ip_version ==
|
|
|
|
|
|
"4" else "nameservers",
|
2025-06-12 16:32:05 +05:00
|
|
|
|
)
|
|
|
|
|
|
],
|
2025-06-09 10:17:12 +05:00
|
|
|
|
)
|
|
|
|
|
|
configs.append(config)
|
|
|
|
|
|
return configs
|
|
|
|
|
|
|
2025-06-12 16:32:05 +05:00
|
|
|
|
async def get_wifi_info(
|
|
|
|
|
|
self, wifi_device: NetworkDeviceWireless
|
|
|
|
|
|
) -> Dict[str, Optional[Union[str, int]]]:
|
2025-06-09 10:17:12 +05:00
|
|
|
|
"""Получает информацию о Wi-Fi устройстве"""
|
|
|
|
|
|
active_ap = await wifi_device.active_access_point
|
|
|
|
|
|
wifi_info = {
|
2025-06-12 16:32:05 +05:00
|
|
|
|
"ssid": None,
|
|
|
|
|
|
"bssid": None,
|
|
|
|
|
|
"bitrate": await wifi_device.bitrate,
|
|
|
|
|
|
"strength": None,
|
|
|
|
|
|
"frequency": None,
|
|
|
|
|
|
"mode": None,
|
2025-06-09 10:17:12 +05:00
|
|
|
|
}
|
2025-06-12 16:32:05 +05:00
|
|
|
|
if active_ap != "/":
|
2025-06-09 10:17:12 +05:00
|
|
|
|
ap = AccessPoint(active_ap, self.system_bus)
|
2025-06-12 16:47:27 +05:00
|
|
|
|
wifi_info.update({
|
|
|
|
|
|
"ssid": await ap.ssid,
|
|
|
|
|
|
"bssid": await ap.hw_address,
|
|
|
|
|
|
"strength": await ap.strength,
|
|
|
|
|
|
"frequency": await ap.frequency,
|
|
|
|
|
|
"mode": await ap.mode,
|
|
|
|
|
|
})
|
2025-06-09 10:17:12 +05:00
|
|
|
|
return wifi_info
|
|
|
|
|
|
|
2025-06-12 16:32:05 +05:00
|
|
|
|
async def get_network_interfaces(
|
2025-06-12 16:47:27 +05:00
|
|
|
|
self,
|
|
|
|
|
|
initial_positions: Dict[str, Tuple[int, int]] = None
|
2025-06-12 16:32:05 +05:00
|
|
|
|
) -> List[Union[NetworkInterface, WiFiDevice]]:
|
2025-06-09 10:17:12 +05:00
|
|
|
|
"""Получает список сетевых интерфейсов"""
|
|
|
|
|
|
devices_paths = await self.nm.get_devices()
|
|
|
|
|
|
network_interfaces = []
|
|
|
|
|
|
initial_positions = initial_positions or {}
|
|
|
|
|
|
|
|
|
|
|
|
for device_path in devices_paths:
|
|
|
|
|
|
generic_device = NetworkDeviceGeneric(device_path, self.system_bus)
|
|
|
|
|
|
interface_name = str(await generic_device.interface)
|
|
|
|
|
|
device_type = DeviceType(await generic_device.device_type).name
|
|
|
|
|
|
|
2025-06-12 16:32:05 +05:00
|
|
|
|
if device_type == "LOOPBACK":
|
2025-06-09 10:17:12 +05:00
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
device_state = str(await generic_device.state)
|
2025-06-12 16:32:05 +05:00
|
|
|
|
if device_state == "10":
|
2025-06-09 10:17:12 +05:00
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
common_data = {
|
2025-06-12 16:47:27 +05:00
|
|
|
|
"name":
|
|
|
|
|
|
interface_name,
|
|
|
|
|
|
"type":
|
|
|
|
|
|
device_type,
|
|
|
|
|
|
"device_state":
|
|
|
|
|
|
device_state,
|
|
|
|
|
|
"active_connection":
|
|
|
|
|
|
await self.get_connection_name(generic_device),
|
|
|
|
|
|
"mac_address":
|
|
|
|
|
|
str(await generic_device.hw_address),
|
|
|
|
|
|
"device_mtu":
|
|
|
|
|
|
str(await generic_device.mtu),
|
|
|
|
|
|
"ipv4":
|
|
|
|
|
|
IPConfig(configurations=await self.get_ip_config(
|
|
|
|
|
|
generic_device, "4")),
|
|
|
|
|
|
"ipv6":
|
|
|
|
|
|
IPConfig(configurations=await self.get_ip_config(
|
|
|
|
|
|
generic_device, "6")),
|
|
|
|
|
|
"x":
|
|
|
|
|
|
initial_positions.get(interface_name, (100, 100))[0],
|
|
|
|
|
|
"y":
|
|
|
|
|
|
initial_positions.get(interface_name, (100, 100))[1],
|
2025-06-09 10:17:12 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if device_type == "WIFI":
|
2025-06-12 16:47:27 +05:00
|
|
|
|
wifi_device = NetworkDeviceWireless(device_path,
|
|
|
|
|
|
self.system_bus)
|
2025-06-09 10:17:12 +05:00
|
|
|
|
wifi_info = await self.get_wifi_info(wifi_device)
|
2025-06-12 16:47:27 +05:00
|
|
|
|
network_interfaces.append(
|
|
|
|
|
|
WiFiDevice(**common_data, **wifi_info))
|
2025-06-09 10:17:12 +05:00
|
|
|
|
else:
|
|
|
|
|
|
network_interfaces.append(NetworkInterface(**common_data))
|
|
|
|
|
|
|
|
|
|
|
|
return network_interfaces
|
|
|
|
|
|
|
|
|
|
|
|
async def get_hostname(self) -> Dict[str, str]:
|
|
|
|
|
|
"""Возвращает имя хоста из команды hostname в формате JSON"""
|
|
|
|
|
|
try:
|
2025-06-12 16:47:27 +05:00
|
|
|
|
result = subprocess.run(["hostname"],
|
|
|
|
|
|
capture_output=True,
|
|
|
|
|
|
text=True,
|
|
|
|
|
|
check=True)
|
2025-06-09 10:17:12 +05:00
|
|
|
|
hostname = result.stdout.strip()
|
|
|
|
|
|
return {"hostname": hostname}
|
|
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
|
|
raise RuntimeError(f"Failed to execute hostname: {e}")
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
raise RuntimeError(f"Error processing hostname output: {e}")
|
|
|
|
|
|
|
2025-06-12 16:32:05 +05:00
|
|
|
|
async def get_interfaces_by_state(
|
2025-06-12 16:47:27 +05:00
|
|
|
|
self,
|
|
|
|
|
|
state: str,
|
|
|
|
|
|
initial_positions: Dict[str, Tuple[int, int]] = None
|
2025-06-12 16:32:05 +05:00
|
|
|
|
) -> List[NetworkInterface]:
|
2025-06-09 10:17:12 +05:00
|
|
|
|
"""
|
|
|
|
|
|
Получает список интерфейсов по состоянию
|
|
|
|
|
|
"""
|
|
|
|
|
|
all_interfaces = await self.get_network_interfaces(initial_positions)
|
2025-06-12 16:32:05 +05:00
|
|
|
|
return [
|
2025-06-12 16:47:27 +05:00
|
|
|
|
interface for interface in all_interfaces
|
|
|
|
|
|
if interface.device_state == state
|
2025-06-12 16:32:05 +05:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
async def get_interfaces_container(
|
|
|
|
|
|
self,
|
|
|
|
|
|
state: Optional[str] = None,
|
|
|
|
|
|
initial_positions: Dict[str, Tuple[int, int]] = None,
|
|
|
|
|
|
container_x: int = 0,
|
|
|
|
|
|
container_y: int = 0,
|
|
|
|
|
|
) -> NetworkInterfacesContainer:
|
|
|
|
|
|
"""Получает объект, содержащий список интерфейсов и координаты контейнера"""
|
2025-06-09 10:17:12 +05:00
|
|
|
|
if state:
|
2025-06-12 16:47:27 +05:00
|
|
|
|
interfaces = await self.get_interfaces_by_state(
|
|
|
|
|
|
state, initial_positions)
|
2025-06-09 10:17:12 +05:00
|
|
|
|
else:
|
|
|
|
|
|
interfaces = await self.get_network_interfaces(initial_positions)
|
2025-06-12 16:47:27 +05:00
|
|
|
|
return NetworkInterfacesContainer(interfaces=interfaces,
|
|
|
|
|
|
container_x=container_x,
|
|
|
|
|
|
container_y=container_y)
|
2025-06-09 10:17:12 +05:00
|
|
|
|
|
2025-06-12 16:32:05 +05:00
|
|
|
|
async def get_network_interfaces_container(
|
|
|
|
|
|
self,
|
|
|
|
|
|
initial_positions: Optional[Dict[str, Tuple[int, int]]] = None,
|
|
|
|
|
|
) -> NetworkInterfacesContainer:
|
|
|
|
|
|
"""Получает контейнер с сетевыми интерфейсами и координатами контейнера"""
|
2025-06-09 10:17:12 +05:00
|
|
|
|
interfaces = await self.get_network_interfaces(initial_positions)
|
2025-06-12 16:47:27 +05:00
|
|
|
|
return NetworkInterfacesContainer(interfaces=interfaces,
|
|
|
|
|
|
container_x=0,
|
|
|
|
|
|
container_y=0)
|
2025-06-09 10:17:12 +05:00
|
|
|
|
|
2025-06-12 16:32:05 +05:00
|
|
|
|
async def get_activated_interfaces(
|
2025-06-12 16:47:27 +05:00
|
|
|
|
self,
|
|
|
|
|
|
initial_positions: Dict[str, Tuple[int, int]] = None
|
2025-06-12 16:32:05 +05:00
|
|
|
|
) -> List[NetworkInterface]:
|
|
|
|
|
|
"""Получает список активных сетевых интерфейсов"""
|
|
|
|
|
|
return await self.get_interfaces_by_state("100", initial_positions)
|
2025-06-09 10:17:12 +05:00
|
|
|
|
|
2025-06-12 16:32:05 +05:00
|
|
|
|
async def get_deactivated_interfaces(
|
2025-06-12 16:47:27 +05:00
|
|
|
|
self,
|
|
|
|
|
|
initial_positions: Dict[str, Tuple[int, int]] = None
|
2025-06-12 16:32:05 +05:00
|
|
|
|
) -> List[NetworkInterface]:
|
|
|
|
|
|
"""Получает список неактивных сетевых интерфейсов"""
|
2025-06-09 10:17:12 +05:00
|
|
|
|
all_interfaces = await self.get_network_interfaces(initial_positions)
|
2025-06-12 16:32:05 +05:00
|
|
|
|
return [
|
2025-06-12 16:47:27 +05:00
|
|
|
|
interface for interface in all_interfaces
|
|
|
|
|
|
if interface.device_state != "100"
|
2025-06-12 16:32:05 +05:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
async def get_activated_interfaces_container(
|
|
|
|
|
|
self,
|
|
|
|
|
|
initial_positions: Dict[str, Tuple[int, int]] = None,
|
|
|
|
|
|
container_x: int = 0,
|
|
|
|
|
|
container_y: int = 0,
|
|
|
|
|
|
) -> NetworkInterfacesContainer:
|
|
|
|
|
|
"""Получает объект, содержащий список активных интерфейсов и координаты контейнера"""
|
2025-06-12 16:47:27 +05:00
|
|
|
|
return await self.get_interfaces_container("100", initial_positions,
|
|
|
|
|
|
container_x, container_y)
|
2025-06-09 10:17:12 +05:00
|
|
|
|
|
2025-06-12 16:32:05 +05:00
|
|
|
|
async def get_deactivated_interfaces_container(
|
|
|
|
|
|
self,
|
|
|
|
|
|
initial_positions: Dict[str, Tuple[int, int]] = None,
|
|
|
|
|
|
container_x: int = 0,
|
|
|
|
|
|
container_y: int = 0,
|
|
|
|
|
|
) -> NetworkInterfacesContainer:
|
|
|
|
|
|
"""Получает объект, содержащий список неактивных интерфейсов и координаты контейнера"""
|
2025-06-09 10:17:12 +05:00
|
|
|
|
interfaces = await self.get_deactivated_interfaces(initial_positions)
|
2025-06-12 16:47:27 +05:00
|
|
|
|
return NetworkInterfacesContainer(interfaces=interfaces,
|
|
|
|
|
|
container_x=container_x,
|
|
|
|
|
|
container_y=container_y)
|