This commit is contained in:
parent 06a5a758a3
commit 58fa7d1ee6
19 changed files with 567 additions and 377 deletions

View file

@ -10,15 +10,14 @@ class NetworkScanner:
def __init__(self, interface=None):
"""Инициализация параметров интерфейсов"""
self.interface = (
interface or netifaces.gateways()["default"][netifaces.AF_INET][1]
)
self.ip_info = netifaces.ifaddresses(self.interface).get(netifaces.AF_INET, [])
interface or netifaces.gateways()["default"][netifaces.AF_INET][1])
self.ip_info = netifaces.ifaddresses(self.interface).get(
netifaces.AF_INET, [])
self.networks = [
IPNetwork(f"{ip['addr']}/{ip['netmask']}") for ip in self.ip_info
]
self.local_mac = netifaces.ifaddresses(self.interface)[netifaces.AF_LINK][0][
"addr"
]
self.local_mac = netifaces.ifaddresses(
self.interface)[netifaces.AF_LINK][0]["addr"]
def _create_arp_packet(self, target_ip):
"""Создание пакета ARP для отправки"""
@ -36,9 +35,10 @@ class NetworkScanner:
clients = []
for _ in range(attempts):
result = srp(packet, timeout=timeout, iface=self.interface, verbose=0)[
0
]
result = srp(packet,
timeout=timeout,
iface=self.interface,
verbose=0)[0]
for _, received in result:
client = {"ip": received.psrc, "mac": received.hwsrc}
if client not in clients: # Избегаем дублирования клиентов

View file

@ -37,7 +37,8 @@ class AuthService:
def create_access_token(self, data: dict) -> str:
"""Создаёт токен"""
to_encode = data.copy()
expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
expire = datetime.utcnow() + timedelta(
minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
@ -75,22 +76,28 @@ class AuthService:
db_user = self.storage.get_user_by_username(user.username)
if not db_user:
logger.error(f"Пользователь {user.username} не найден")
raise HTTPException(status_code=401, detail="Неверный логин или пароль")
if not self.storage.verify_password(
user.password, db_user["password_hash"]
):
logger.error(f"Неверный пароль для пользователя {user.username}")
raise HTTPException(status_code=401, detail="Неверный логин или пароль")
access_token = self.create_access_token(data={"sub": user.username})
raise HTTPException(status_code=401,
detail="Неверный логин или пароль")
if not self.storage.verify_password(user.password,
db_user["password_hash"]):
logger.error(
f"Неверный пароль для пользователя {user.username}")
raise HTTPException(status_code=401,
detail="Неверный логин или пароль")
access_token = self.create_access_token(
data={"sub": user.username})
session_id = self.session_storage.create_session(
user.username, access_token, client_id
)
user.username, access_token, client_id)
logger.info(f"Пользователь {user.username} успешно авторизован")
return {
"access_token": access_token,
"token_type": "bearer",
"session_id": session_id,
"user": UserResponse(
"access_token":
access_token,
"token_type":
"bearer",
"session_id":
session_id,
"user":
UserResponse(
id=db_user["id"],
username=db_user["username"],
is_admin=db_user["is_admin"],
@ -102,7 +109,8 @@ class AuthService:
)
raise HTTPException(status_code=500, detail="Ошибка авторизации")
def get_current_user(self, token: str = Depends(oauth2_scheme)) -> UserResponse:
def get_current_user(
self, token: str = Depends(oauth2_scheme)) -> UserResponse:
"""Получает текущего пользователя по токену"""
credentials = self.verify_token(token)
if not credentials:
@ -112,30 +120,36 @@ class AuthService:
db_user = self.storage.get_user_by_username(username)
if not db_user:
logger.error(f"Пользователь {username} не найден")
raise HTTPException(status_code=401, detail="Пользователь не найден")
return UserResponse(
id=db_user["id"], username=db_user["username"], is_admin=db_user["is_admin"]
)
raise HTTPException(status_code=401,
detail="Пользователь не найден")
return UserResponse(id=db_user["id"],
username=db_user["username"],
is_admin=db_user["is_admin"])
def get_user_by_session(self, client_id: str) -> dict:
"""Получает пользователя и токен по client_id"""
session = self.session_storage.get_latest_session(client_id)
if not session:
logger.error(f"Сессия для client_id {client_id} не найдена или истекла")
raise HTTPException(status_code=401, detail="Сессия недействительна")
logger.error(
f"Сессия для client_id {client_id} не найдена или истекла")
raise HTTPException(status_code=401,
detail="Сессия недействительна")
username = session["username"]
token = session["token"]
db_user = self.storage.get_user_by_username(username)
if not db_user:
logger.error(f"Пользователь {username} не найден")
raise HTTPException(status_code=401, detail="Пользователь не найден")
raise HTTPException(status_code=401,
detail="Пользователь не найден")
return {
"user": UserResponse(
"user":
UserResponse(
id=db_user["id"],
username=db_user["username"],
is_admin=db_user["is_admin"],
),
"access_token": token,
"access_token":
token,
}
def logout(self, client_id: str):
@ -149,5 +163,7 @@ class AuthService:
)
return {"message": "Успешный выход из системы"}
except Exception as e:
logger.error(f"Ошибка при выходе из системы: {type(e).__name__} - {str(e)}")
raise HTTPException(status_code=500, detail="Ошибка при выходе из системы")
logger.error(
f"Ошибка при выходе из системы: {type(e).__name__} - {str(e)}")
raise HTTPException(status_code=500,
detail="Ошибка при выходе из системы")

View file

@ -7,11 +7,11 @@ DEBUG = True
class FirewalldService:
def __init__(self):
self.bus = SystemBus()
self.proxy = self.bus.get(
"org.fedoraproject.FirewallD1", "/org/fedoraproject/FirewallD1"
)
self.proxy = self.bus.get("org.fedoraproject.FirewallD1",
"/org/fedoraproject/FirewallD1")
def get_zones(self) -> List[str]:
"""Get list of all zones."""
@ -34,12 +34,16 @@ class FirewalldService:
except Exception as e:
raise Exception(f"Failed to set default zone to {zone}: {str(e)}")
def add_service(self, zone: str, service: str, timeout: Optional[int] = 0) -> None:
def add_service(self,
zone: str,
service: str,
timeout: Optional[int] = 0) -> None:
"""Add a service to a zone."""
try:
self.proxy.addService(zone, service, timeout)
except Exception as e:
raise Exception(f"Failed to add service {service} to zone {zone}: {str(e)}")
raise Exception(
f"Failed to add service {service} to zone {zone}: {str(e)}")
def remove_service(self, zone: str, service: str) -> None:
"""Remove a service from a zone."""
@ -55,20 +59,22 @@ class FirewalldService:
try:
return self.proxy.getServices(zone)
except Exception as e:
raise Exception(f"Failed to get services for zone {zone}: {str(e)}")
raise Exception(
f"Failed to get services for zone {zone}: {str(e)}")
def add_port(
self, zone: str, port: int, protocol: str, timeout: Optional[int] = 0
) -> None:
def add_port(self,
zone: str,
port: int,
protocol: str,
timeout: Optional[int] = 0) -> None:
"""Add a port to a zone."""
try:
if DEBUG:
print(f"Adding port {port}/{protocol} to zone {zone}")
print(f"Timeout: {timeout}")
# Get the zone interface
zone_proxy = self.bus.get(
"org.fedoraproject.FirewallD1", "/org/fedoraproject/FirewallD1"
)
zone_proxy = self.bus.get("org.fedoraproject.FirewallD1",
"/org/fedoraproject/FirewallD1")
zone_iface = zone_proxy["org.fedoraproject.FirewallD1.zone"]
# Convert port to string
port_str = str(port)
@ -105,7 +111,8 @@ class FirewalldService:
try:
self.proxy.addSource(zone, source)
except Exception as e:
raise Exception(f"Failed to add source {source} to zone {zone}: {str(e)}")
raise Exception(
f"Failed to add source {source} to zone {zone}: {str(e)}")
def remove_source(self, zone: str, source: str) -> None:
"""Remove a source from a zone."""
@ -113,8 +120,7 @@ class FirewalldService:
self.proxy.removeSource(zone, source)
except Exception as e:
raise Exception(
f"Failed to remove source {source} from zone {zone}: {str(e)}"
)
f"Failed to remove source {source} from zone {zone}: {str(e)}")
def get_sources(self, zone: str) -> List[str]:
"""Get list of sources in a zone."""
@ -139,4 +145,5 @@ class FirewalldService:
"sources": self.get_sources(zone),
}
except Exception as e:
raise Exception(f"Failed to get zone settings for {zone}: {str(e)}")
raise Exception(
f"Failed to get zone settings for {zone}: {str(e)}")

View file

@ -35,7 +35,10 @@ class IPService:
clients = self.arp_scanner.scan_network()
new_ip = args["ipv4_updates"]["ipv4.address-data"][0]["address"]
if any(client["ip"] == new_ip for client in clients):
return {"available": False, "message": f"IP address {new_ip} уже занят."}
return {
"available": False,
"message": f"IP address {new_ip} уже занят."
}
return {
"available": True,
"message": "Network profile settings are available for use.",
@ -46,7 +49,8 @@ class IPService:
self._reset_bus()
nm = NetworkManager()
settings = NetworkManagerSettings()
connection_paths = await settings.get_connections_by_id(args["connection_id"])
connection_paths = await settings.get_connections_by_id(
args["connection_id"])
if not connection_paths:
raise ValueError(f"Подключение {args['connection_id']} не найдено")
@ -73,10 +77,10 @@ class IPService:
if await device.interface == interface_name:
await device.disconnect()
print(f"Отключён от {interface_name}")
await nm.activate_connection(
connection=connection_paths[0], device=device_path
)
await nm.activate_connection(connection=connection_paths[0],
device=device_path)
print(f"Переподключён к {args['connection_id']}")
return
raise RuntimeError(f"Устройство для интерфейса {interface_name} не найдено")
raise RuntimeError(
f"Устройство для интерфейса {interface_name} не найдено")

View file

@ -30,7 +30,8 @@ class NetworkService:
self.nm = NetworkManager(self.system_bus)
self.nm_settings = NetworkManagerSettings(self.system_bus)
async def get_ip_method(self, device: NetworkDeviceGeneric, ip_type: str) -> str:
async def get_ip_method(self, device: NetworkDeviceGeneric,
ip_type: str) -> str:
"""Получает метод IP для устройства"""
try:
config = await device.get_applied_connection()
@ -52,14 +53,14 @@ class NetworkService:
# добавить потом условия для отключенных интерфейсов
return "unknown"
async def get_ip_config(
self, device: NetworkDeviceGeneric, ip_version: str
) -> List[IndividualIPConfig]:
async def get_ip_config(self, device: NetworkDeviceGeneric,
ip_version: str) -> List[IndividualIPConfig]:
"""Получает конфигурацию IP для устройства"""
configs = []
device_ip = await getattr(device, f"ip{ip_version}_config")
if device_ip != "/":
ip_config = globals()[f"IPv{ip_version}Config"](device_ip, self.system_bus)
ip_config = globals()[f"IPv{ip_version}Config"](device_ip,
self.system_bus)
address_data = await ip_config.address_data
for address in address_data:
config = IndividualIPConfig(
@ -67,24 +68,27 @@ class NetworkService:
routes=[
globals()[f"IPv{ip_version}Route"](
destination=route.get(
"dest", ("s", "0.0.0.0" if ip_version == "4" else "::")
)[1],
"dest",
("s",
"0.0.0.0" if ip_version == "4" else "::"))[1],
prefix=route.get("prefix", ("u", 0))[1],
next_hop=route.get(
"next-hop",
("s", "0.0.0.0" if ip_version == "4" else "::"),
("s",
"0.0.0.0" if ip_version == "4" else "::"),
)[1],
metric=route.get("metric", ("u", 0))[1],
)
for route in await ip_config.route_data
) for route in await ip_config.route_data
],
method=await self.get_ip_method(device, f"ipv{ip_version}"),
method=await self.get_ip_method(device,
f"ipv{ip_version}"),
gateway=str(await ip_config.gateway),
dns=[
str(ns["address"][1]) if ip_version == "4" else str(ns)
for ns in await getattr(
ip_config,
"nameserver_data" if ip_version == "4" else "nameservers",
"nameserver_data" if ip_version ==
"4" else "nameservers",
)
],
)
@ -106,19 +110,18 @@ class NetworkService:
}
if active_ap != "/":
ap = AccessPoint(active_ap, self.system_bus)
wifi_info.update(
{
"ssid": await ap.ssid,
"bssid": await ap.hw_address,
"strength": await ap.strength,
"frequency": await ap.frequency,
"mode": await ap.mode,
}
)
wifi_info.update({
"ssid": await ap.ssid,
"bssid": await ap.hw_address,
"strength": await ap.strength,
"frequency": await ap.frequency,
"mode": await ap.mode,
})
return wifi_info
async def get_network_interfaces(
self, initial_positions: Dict[str, Tuple[int, int]] = None
self,
initial_positions: Dict[str, Tuple[int, int]] = None
) -> List[Union[NetworkInterface, WiFiDevice]]:
"""Получает список сетевых интерфейсов"""
devices_paths = await self.nm.get_devices()
@ -138,26 +141,36 @@ class NetworkService:
continue
common_data = {
"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],
"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],
}
if device_type == "WIFI":
wifi_device = NetworkDeviceWireless(device_path, self.system_bus)
wifi_device = NetworkDeviceWireless(device_path,
self.system_bus)
wifi_info = await self.get_wifi_info(wifi_device)
network_interfaces.append(WiFiDevice(**common_data, **wifi_info))
network_interfaces.append(
WiFiDevice(**common_data, **wifi_info))
else:
network_interfaces.append(NetworkInterface(**common_data))
@ -166,9 +179,10 @@ class NetworkService:
async def get_hostname(self) -> Dict[str, str]:
"""Возвращает имя хоста из команды hostname в формате JSON"""
try:
result = subprocess.run(
["hostname"], capture_output=True, text=True, check=True
)
result = subprocess.run(["hostname"],
capture_output=True,
text=True,
check=True)
hostname = result.stdout.strip()
return {"hostname": hostname}
except subprocess.CalledProcessError as e:
@ -177,14 +191,17 @@ class NetworkService:
raise RuntimeError(f"Error processing hostname output: {e}")
async def get_interfaces_by_state(
self, state: str, initial_positions: Dict[str, Tuple[int, int]] = None
self,
state: str,
initial_positions: Dict[str, Tuple[int, int]] = None
) -> List[NetworkInterface]:
"""
Получает список интерфейсов по состоянию
"""
all_interfaces = await self.get_network_interfaces(initial_positions)
return [
interface for interface in all_interfaces if interface.device_state == state
interface for interface in all_interfaces
if interface.device_state == state
]
async def get_interfaces_container(
@ -196,12 +213,13 @@ class NetworkService:
) -> NetworkInterfacesContainer:
"""Получает объект, содержащий список интерфейсов и координаты контейнера"""
if state:
interfaces = await self.get_interfaces_by_state(state, initial_positions)
interfaces = await self.get_interfaces_by_state(
state, initial_positions)
else:
interfaces = await self.get_network_interfaces(initial_positions)
return NetworkInterfacesContainer(
interfaces=interfaces, container_x=container_x, container_y=container_y
)
return NetworkInterfacesContainer(interfaces=interfaces,
container_x=container_x,
container_y=container_y)
async def get_network_interfaces_container(
self,
@ -209,23 +227,26 @@ class NetworkService:
) -> NetworkInterfacesContainer:
"""Получает контейнер с сетевыми интерфейсами и координатами контейнера"""
interfaces = await self.get_network_interfaces(initial_positions)
return NetworkInterfacesContainer(
interfaces=interfaces, container_x=0, container_y=0
)
return NetworkInterfacesContainer(interfaces=interfaces,
container_x=0,
container_y=0)
async def get_activated_interfaces(
self, initial_positions: Dict[str, Tuple[int, int]] = None
self,
initial_positions: Dict[str, Tuple[int, int]] = None
) -> List[NetworkInterface]:
"""Получает список активных сетевых интерфейсов"""
return await self.get_interfaces_by_state("100", initial_positions)
async def get_deactivated_interfaces(
self, initial_positions: Dict[str, Tuple[int, int]] = None
self,
initial_positions: Dict[str, Tuple[int, int]] = None
) -> List[NetworkInterface]:
"""Получает список неактивных сетевых интерфейсов"""
all_interfaces = await self.get_network_interfaces(initial_positions)
return [
interface for interface in all_interfaces if interface.device_state != "100"
interface for interface in all_interfaces
if interface.device_state != "100"
]
async def get_activated_interfaces_container(
@ -235,9 +256,8 @@ class NetworkService:
container_y: int = 0,
) -> NetworkInterfacesContainer:
"""Получает объект, содержащий список активных интерфейсов и координаты контейнера"""
return await self.get_interfaces_container(
"100", initial_positions, container_x, container_y
)
return await self.get_interfaces_container("100", initial_positions,
container_x, container_y)
async def get_deactivated_interfaces_container(
self,
@ -247,6 +267,6 @@ class NetworkService:
) -> NetworkInterfacesContainer:
"""Получает объект, содержащий список неактивных интерфейсов и координаты контейнера"""
interfaces = await self.get_deactivated_interfaces(initial_positions)
return NetworkInterfacesContainer(
interfaces=interfaces, container_x=container_x, container_y=container_y
)
return NetworkInterfacesContainer(interfaces=interfaces,
container_x=container_x,
container_y=container_y)

View file

@ -20,12 +20,13 @@ from scapy.layers.l2 import ARP, Ether
from scapy.sendrecv import srp, sr1
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
class NetworkScanner:
def __init__(self, interface: str):
self.interface = interface
conf.iface = interface
@ -35,14 +36,19 @@ class NetworkScanner:
arp = ARP(pdst=subnet)
ether = Ether(dst="ff:ff:ff:ff:ff:ff")
packet = ether / arp
result = srp(packet, timeout=2, verbose=False, iface=self.interface)[0]
return [(received.psrc, received.hwsrc) for sent, received in result]
result = srp(packet,
timeout=2,
verbose=False,
iface=self.interface)[0]
return [(received.psrc, received.hwsrc)
for sent, received in result]
except Exception as e:
logger.error(f"Ошибка ARP-сканирования подсети {subnet}: {str(e)}")
return []
class PortScanner:
def __init__(self, interface: str):
self.interface = interface
@ -61,7 +67,8 @@ class PortScanner:
return port_config.get_port_name(port)
return None
except Exception as e:
logger.debug(f"Ошибка проверки порта {port} для IP {ip}: {str(e)}")
logger.debug(
f"Ошибка проверки порта {port} для IP {ip}: {str(e)}")
return None
try:
@ -72,11 +79,13 @@ class PortScanner:
logger.debug(f"Открытые порты для IP {ip}: {open_ports}")
return open_ports
except Exception as e:
logger.warning(f"Ошибка при сканировании портов для IP {ip}: {str(e)}")
logger.warning(
f"Ошибка при сканировании портов для IP {ip}: {str(e)}")
return []
class DeviceInfoCollector:
def __init__(self):
self.mac_lookup = MacLookup()
@ -86,8 +95,7 @@ class DeviceInfoCollector:
return self.mac_lookup.lookup(mac)
except Exception as e:
logger.warning(
f"Не удалось определить производителя для MAC {mac}: {str(e)}"
)
f"Не удалось определить производителя для MAC {mac}: {str(e)}")
return "Unknown"
def get_hostname(self, ip: str) -> Optional[str]:
@ -98,6 +106,7 @@ class DeviceInfoCollector:
class DeviceStorage:
def __init__(self, storage_path: str = "storage/devices/devices.json"):
self.storage_file = Path(storage_path)
self.storage_file.parent.mkdir(parents=True, exist_ok=True)
@ -119,7 +128,8 @@ class DeviceStorage:
logger.error(f"Ошибка при загрузке устройств: {str(e)}")
return {}
def _clean_old_devices(self, devices: Dict[str, Device]) -> Dict[str, Device]:
def _clean_old_devices(self, devices: Dict[str,
Device]) -> Dict[str, Device]:
now = datetime.now(ZoneInfo("UTC"))
filtered = {}
for mac, device in devices.items():
@ -129,9 +139,11 @@ class DeviceStorage:
if now - last_seen <= self.offline_timeout:
filtered[mac] = device
else:
logger.info(f"Удалено устройство {mac} (offline > 7 дней)")
logger.info(
f"Удалено устройство {mac} (offline > 7 дней)")
except ValueError:
logger.warning(f"Неверный формат last_seen для устройства {mac}")
logger.warning(
f"Неверный формат last_seen для устройства {mac}")
else:
filtered[mac] = device
return filtered
@ -141,12 +153,14 @@ class DeviceStorage:
data = SubnetScanResult(devices=devices).model_dump()
with open(self.storage_file, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
logger.info(f"Сохранено {len(devices)} устройств в {self.storage_file}")
logger.info(
f"Сохранено {len(devices)} устройств в {self.storage_file}")
except Exception as e:
logger.error(f"Ошибка при сохранении устройств: {str(e)}")
class SubnetService:
def __init__(self):
self.interface = self._get_default_interface()
self.network_scanner = NetworkScanner(self.interface)
@ -157,7 +171,8 @@ class SubnetService:
def _get_default_interface(self) -> str:
try:
gateways = netifaces.gateways()
default_gateway = gateways.get("default", {}).get(netifaces.AF_INET)
default_gateway = gateways.get("default",
{}).get(netifaces.AF_INET)
if default_gateway:
return default_gateway[1]
interfaces = netifaces.interfaces()
@ -169,7 +184,9 @@ class SubnetService:
logger.warning(f"Не удалось определить интерфейс: {str(e)}")
return "eth0"
def scan_subnet(self, subnet: str, ports: List[int] = None) -> SubnetScanResult:
def scan_subnet(self,
subnet: str,
ports: List[int] = None) -> SubnetScanResult:
start_time = time.time()
devices = []
now = datetime.now(ZoneInfo("UTC")).isoformat()
@ -220,7 +237,8 @@ class SubnetService:
)
with ThreadPoolExecutor(max_workers=10) as executor:
device_results = executor.map(process_device, discovered_devices)
device_results = executor.map(process_device,
discovered_devices)
devices.extend([d for d in device_results if d is not None])
active_macs = {d.mac for d in devices if d.mac}
@ -275,10 +293,12 @@ class SubnetService:
last_seen=now,
note=None,
)
logger.warning(f"Локальное устройство не найдено в подсети {subnet}")
logger.warning(
f"Локальное устройство не найдено в подсети {subnet}")
return None
except Exception as e:
logger.warning(f"Не удалось получить локальное устройство: {str(e)}")
logger.warning(
f"Не удалось получить локальное устройство: {str(e)}")
return None
def add_note(self, mac: str, note: str) -> Tuple[bool, Optional[str]]:
@ -302,14 +322,17 @@ class SubnetService:
logger.warning(f"Устройство с MAC {mac} не найдено")
return False, f"Устройство с MAC {mac} не найдено"
if saved_devices[mac].note is not None:
logger.error(f"Заметка для устройства с MAC {mac} уже существует")
logger.error(
f"Заметка для устройства с MAC {mac} уже существует")
return False, f"Заметка для устройства с MAC {mac} уже существует"
saved_devices[mac].note = note
self.storage.save_devices(list(saved_devices.values()))
logger.info(f"Добавлена заметка '{note}' для устройства с MAC {mac}")
logger.info(
f"Добавлена заметка '{note}' для устройства с MAC {mac}")
return True, None
except Exception as e:
logger.error(f"Ошибка при добавлении заметки для MAC {mac}: {str(e)}")
logger.error(
f"Ошибка при добавлении заметки для MAC {mac}: {str(e)}")
return False, f"Внутренняя ошибка сервера: {str(e)}"
def update_note(self, mac: str, note: str) -> Tuple[bool, Optional[str]]:
@ -333,14 +356,17 @@ class SubnetService:
logger.warning(f"Устройство с MAC {mac} не найдено")
return False, f"Устройство с MAC {mac} не найдено"
if saved_devices[mac].note is None:
logger.error(f"Заметка для устройства с MAC {mac} не существует")
logger.error(
f"Заметка для устройства с MAC {mac} не существует")
return False, f"Заметка для устройства с MAC {mac} не существует"
saved_devices[mac].note = note
self.storage.save_devices(list(saved_devices.values()))
logger.info(f"Обновлена заметка '{note}' для устройства с MAC {mac}")
logger.info(
f"Обновлена заметка '{note}' для устройства с MAC {mac}")
return True, None
except Exception as e:
logger.error(f"Ошибка при обновлении заметки для MAC {mac}: {str(e)}")
logger.error(
f"Ошибка при обновлении заметки для MAC {mac}: {str(e)}")
return False, f"Внутренняя ошибка сервера: {str(e)}"
def delete_note(self, mac: str) -> Tuple[bool, Optional[str]]:
@ -363,12 +389,14 @@ class SubnetService:
logger.warning(f"Устройство с MAC {mac} не найдено")
return False, f"Устройство с MAC {mac} не найдено"
if saved_devices[mac].note is None:
logger.error(f"Заметка для устройства с MAC {mac} не существует")
logger.error(
f"Заметка для устройства с MAC {mac} не существует")
return False, f"Заметка для устройства с MAC {mac} не существует"
saved_devices[mac].note = None
self.storage.save_devices(list(saved_devices.values()))
logger.info(f"Удалена заметка для устройства с MAC {mac}")
return True, None
except Exception as e:
logger.error(f"Ошибка при удалении заметки для MAC {mac}: {str(e)}")
logger.error(
f"Ошибка при удалении заметки для MAC {mac}: {str(e)}")
return False, f"Внутренняя ошибка сервера: {str(e)}"

View file

@ -41,9 +41,9 @@ class UFWService:
print(f"Command error: {result.stderr}")
if result.returncode != 0:
raise subprocess.CalledProcessError(
result.returncode, command, result.stdout, result.stderr
)
raise subprocess.CalledProcessError(result.returncode, command,
result.stdout,
result.stderr)
return result.stdout
@ -116,9 +116,8 @@ class UFWService:
"""
try:
status_result = self._run_command("ufw status")
first_line = (
status_result.splitlines()[0] if status_result.splitlines() else ""
)
first_line = (status_result.splitlines()[0]
if status_result.splitlines() else "")
is_enabled = "Status: active" in first_line
message = "UFW активен" if is_enabled else "UFW неактивен"
@ -166,7 +165,9 @@ class UFWService:
print(f"Ошибка при выключении UFW: {e}")
return False
def add_rules_base(self, rules: List[UFWRule], direction: str = "") -> bool:
def add_rules_base(self,
rules: List[UFWRule],
direction: str = "") -> bool:
"""
Базовый метод для добавления правил UFW.
@ -296,7 +297,10 @@ class UFWService:
except Exception as e:
error_msg = f"Ошибка выполнения команды: {str(e)}"
print(error_msg)
results.append({"success": False, "message": error_msg})
results.append({
"success": False,
"message": error_msg
})
except Exception as e:
error_msg = f"Ошибка при добавлении правила: {str(e)}"
@ -334,7 +338,7 @@ class UFWService:
try:
# Извлекаем номер правила из строки [N]
rule_num = int(line[1 : line.index("]")])
rule_num = int(line[1:line.index("]")])
rules.append(rule_num)
except (ValueError, IndexError):
continue
@ -349,32 +353,37 @@ class UFWService:
try:
# Проверяем существует ли правило с таким номером
if rule_number not in rules:
results.append(
{
"number": rule_number,
"success": False,
"error": f"Правило с номером {rule_number} не найдено",
}
)
results.append({
"number":
rule_number,
"success":
False,
"error":
f"Правило с номером {rule_number} не найдено",
})
continue
# Удаляем правило
self._run_command(f"ufw --force delete {rule_number}")
results.append(
{
"number": rule_number,
"success": True,
"message": f"Правило {rule_number} успешно удалено",
}
)
results.append({
"number":
rule_number,
"success":
True,
"message":
f"Правило {rule_number} успешно удалено",
})
except Exception as e:
if DEBUG:
print(f"Ошибка при удалении правила {rule_number}: {e}")
results.append(
{"number": rule_number, "success": False, "error": str(e)}
)
print(
f"Ошибка при удалении правила {rule_number}: {e}")
results.append({
"number": rule_number,
"success": False,
"error": str(e)
})
# Подсчитываем статистику
total = len(rule_numbers)

View file

@ -10,8 +10,8 @@ from pywifi import Profile
from models.wifi import WifiAccessPoint, WifiScanResult
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
level=logging.DEBUG,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
@ -38,7 +38,7 @@ class WifiService:
for iface in interfaces:
iface_name = iface.name()
if (
"p2p" in iface_name.lower()
"p2p" in iface_name.lower()
): # Исключаю сетевые интерфейсы типа p2p из выбора, возможно сделать решение получше нежели исключать интерфейсы
logger.debug(f"Пропущен интерфейс {iface_name} (P2P)")
continue
@ -67,14 +67,14 @@ class WifiService:
return self.mac_lookup.lookup(mac)
except Exception as e:
logger.warning(
f"Не удалось определить производителя для MAC {mac}: {str(e)}"
)
f"Не удалось определить производителя для MAC {mac}: {str(e)}")
return "Unknown"
def _get_encryption_from_iw(self, ap_info: Dict) -> str:
"""Определяет шифрование из данных iw"""
security = ap_info.get("security", "")
logger.debug(f"Шифрование для BSSID {ap_info.get('bssid')}: {security}")
logger.debug(
f"Шифрование для BSSID {ap_info.get('bssid')}: {security}")
if "WPA3" in security:
return "WPA3"
elif "WPA2" in security or "RSN" in security:
@ -127,12 +127,16 @@ class WifiService:
return []
cmd = ["sudo", "iw", "dev", self.interface.name(), "scan"]
logger.debug(f"Выполняем команду: {' '.join(cmd)}")
result = subprocess.run(cmd, capture_output=True, text=True, timeout=10)
result = subprocess.run(cmd,
capture_output=True,
text=True,
timeout=10)
if result.returncode != 0:
logger.error(f"Ошибка iw scan: {result.stderr}")
return []
output = result.stdout
logger.debug(f"Вывод iw scan (первые 500 символов): {output[:500]}...")
logger.debug(
f"Вывод iw scan (первые 500 символов): {output[:500]}...")
aps = []
current_ap = None
@ -149,12 +153,15 @@ class WifiService:
if bssid_match := bssid_pattern.match(line):
if current_ap:
aps.append(current_ap)
current_ap = {"bssid": bssid_match.group(1).lower(), "security": ""}
current_ap = {
"bssid": bssid_match.group(1).lower(),
"security": ""
}
elif current_ap:
if ssid_match := ssid_pattern.match(line):
current_ap["ssid"] = (
ssid_match.group(1) if ssid_match.group(1).strip() else ""
)
current_ap["ssid"] = (ssid_match.group(1)
if ssid_match.group(1).strip()
else "")
elif freq_match := freq_pattern.match(line):
current_ap["frequency"] = int(freq_match.group(1))
elif signal_match := signal_pattern.match(line):
@ -163,8 +170,10 @@ class WifiService:
current_ap["security"] += "RSN;"
elif wpa_pattern.match(line):
current_ap["security"] += "WPA;"
elif security_type_match := security_type_pattern.search(line):
current_ap["security"] += security_type_match.group(1) + ";"
elif security_type_match := security_type_pattern.search(
line):
current_ap["security"] += security_type_match.group(
1) + ";"
if current_ap:
aps.append(current_ap)
@ -206,7 +215,10 @@ class WifiService:
logger.debug(f"Найдено {len(py_results)} точек через pywifi")
iw_results = self._get_iw_scan_data()
iw_by_bssid = {ap["bssid"]: ap for ap in iw_results if ap.get("bssid")}
iw_by_bssid = {
ap["bssid"]: ap
for ap in iw_results if ap.get("bssid")
}
best_aps_by_ssid: Dict[str, WifiAccessPoint] = (
{}
@ -243,18 +255,15 @@ class WifiService:
)
if ssid not in best_aps_by_ssid or (
current_ap.signal
and best_aps_by_ssid[ssid].signal
and current_ap.signal > best_aps_by_ssid[ssid].signal
):
current_ap.signal and best_aps_by_ssid[ssid].signal
and current_ap.signal > best_aps_by_ssid[ssid].signal):
best_aps_by_ssid[ssid] = current_ap
aps = list(best_aps_by_ssid.values())
logger.info(
f"Сканирование Wi-Fi завершено за {time.time() - start_time:.2f} секунд, "
f"найдено {len(aps)} уникальных сетей"
)
f"найдено {len(aps)} уникальных сетей")
return WifiScanResult(access_points=aps)
except Exception as e: