Форматирование всего серверного кода по стандартам (black)
This commit is contained in:
parent
e61ffd793c
commit
06a5a758a3
28 changed files with 611 additions and 305 deletions
|
|
@ -19,9 +19,12 @@ from scapy.layers.inet import IP, TCP
|
|||
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")
|
||||
logging.basicConfig(
|
||||
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
|
||||
|
|
@ -38,6 +41,7 @@ class NetworkScanner:
|
|||
logger.error(f"Ошибка ARP-сканирования подсети {subnet}: {str(e)}")
|
||||
return []
|
||||
|
||||
|
||||
class PortScanner:
|
||||
def __init__(self, interface: str):
|
||||
self.interface = interface
|
||||
|
|
@ -51,7 +55,7 @@ class PortScanner:
|
|||
IP(dst=ip) / TCP(dport=port, flags="S"),
|
||||
timeout=2,
|
||||
verbose=False,
|
||||
iface=self.interface
|
||||
iface=self.interface,
|
||||
)
|
||||
if pkt and pkt.haslayer(TCP) and pkt[TCP].flags == 0x12:
|
||||
return port_config.get_port_name(port)
|
||||
|
|
@ -71,6 +75,7 @@ class PortScanner:
|
|||
logger.warning(f"Ошибка при сканировании портов для IP {ip}: {str(e)}")
|
||||
return []
|
||||
|
||||
|
||||
class DeviceInfoCollector:
|
||||
def __init__(self):
|
||||
self.mac_lookup = MacLookup()
|
||||
|
|
@ -80,7 +85,9 @@ class DeviceInfoCollector:
|
|||
try:
|
||||
return self.mac_lookup.lookup(mac)
|
||||
except Exception as e:
|
||||
logger.warning(f"Не удалось определить производителя для MAC {mac}: {str(e)}")
|
||||
logger.warning(
|
||||
f"Не удалось определить производителя для MAC {mac}: {str(e)}"
|
||||
)
|
||||
return "Unknown"
|
||||
|
||||
def get_hostname(self, ip: str) -> Optional[str]:
|
||||
|
|
@ -89,6 +96,7 @@ class DeviceInfoCollector:
|
|||
except:
|
||||
return None
|
||||
|
||||
|
||||
class DeviceStorage:
|
||||
def __init__(self, storage_path: str = "storage/devices/devices.json"):
|
||||
self.storage_file = Path(storage_path)
|
||||
|
|
@ -137,6 +145,7 @@ class DeviceStorage:
|
|||
except Exception as e:
|
||||
logger.error(f"Ошибка при сохранении устройств: {str(e)}")
|
||||
|
||||
|
||||
class SubnetService:
|
||||
def __init__(self):
|
||||
self.interface = self._get_default_interface()
|
||||
|
|
@ -187,12 +196,14 @@ class SubnetService:
|
|||
return Device(
|
||||
ip=ip,
|
||||
mac=mac,
|
||||
hostname=self.device_info.get_hostname(ip) or existing_device.hostname,
|
||||
hostname=self.device_info.get_hostname(ip)
|
||||
or existing_device.hostname,
|
||||
vendor=self.device_info.get_vendor(mac),
|
||||
open_ports=self.port_scanner.scan(ip) or existing_device.open_ports,
|
||||
open_ports=self.port_scanner.scan(ip)
|
||||
or existing_device.open_ports,
|
||||
status="online",
|
||||
last_seen=now,
|
||||
note=existing_device.note
|
||||
note=existing_device.note,
|
||||
)
|
||||
vendor = self.device_info.get_vendor(mac)
|
||||
hostname = self.device_info.get_hostname(ip)
|
||||
|
|
@ -205,7 +216,7 @@ class SubnetService:
|
|||
open_ports=open_ports,
|
||||
status="online",
|
||||
last_seen=now,
|
||||
note=None
|
||||
note=None,
|
||||
)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=10) as executor:
|
||||
|
|
@ -251,7 +262,9 @@ class SubnetService:
|
|||
vendor = self.device_info.get_vendor(mac)
|
||||
hostname = self.device_info.get_hostname(ip)
|
||||
open_ports = self.port_scanner.scan(ip)
|
||||
logger.debug(f"Локальное устройство: IP={ip}, MAC={mac}, Ports={open_ports}")
|
||||
logger.debug(
|
||||
f"Локальное устройство: IP={ip}, MAC={mac}, Ports={open_ports}"
|
||||
)
|
||||
return Device(
|
||||
ip=ip,
|
||||
mac=mac,
|
||||
|
|
@ -260,7 +273,7 @@ class SubnetService:
|
|||
open_ports=open_ports,
|
||||
status="online",
|
||||
last_seen=now,
|
||||
note=None
|
||||
note=None,
|
||||
)
|
||||
logger.warning(f"Локальное устройство не найдено в подсети {subnet}")
|
||||
return None
|
||||
|
|
@ -358,4 +371,4 @@ class SubnetService:
|
|||
return True, None
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка при удалении заметки для MAC {mac}: {str(e)}")
|
||||
return False, f"Внутренняя ошибка сервера: {str(e)}"
|
||||
return False, f"Внутренняя ошибка сервера: {str(e)}"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue