Skip to content

Commit 08a2c61

Browse files
author
Tom Softreck
committed
update
1 parent af1c6e5 commit 08a2c61

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ dependencies = [
3737
"jinja2>=3.1.0,<4.0.0",
3838
"opencv-python>=4.7.0,<5.0.0",
3939
"numpy>=1.21.0,<2.0.0",
40-
"python-nmap>=0.7.1,<1.0.0",
41-
"scapy>=2.5.0,<3.0.0",
42-
"python-rtsp-client>=0.2.7,<0.3.0",
43-
"imap-tools>=1.0.0,<2.0.0"
40+
"python-nmap>=0.7.1,<1.0.0"
4441
]
4542

4643
[project.urls]

src/dialogchain/scanner.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import ipaddress
77
import socket
88
import nmap
9-
from typing import List, Dict, Any, Optional, Tuple
9+
import cv2
10+
from typing import List, Dict, Any, Optional, Tuple, Callable
1011
from dataclasses import dataclass
12+
from concurrent.futures import ThreadPoolExecutor
1113

1214
@dataclass
1315
class NetworkService:
@@ -52,6 +54,7 @@ def __init__(self, timeout: float = 2.0, max_workers: int = 50):
5254
self.timeout = timeout
5355
self.max_workers = max_workers
5456
self.nm = nmap.PortScanner()
57+
self.executor = ThreadPoolExecutor(max_workers=max_workers)
5558

5659
async def scan_network(self, network: str = '192.168.1.0/24',
5760
ports: Optional[List[int]] = None,
@@ -104,6 +107,34 @@ async def scan_rtsp_servers(self, network: str = '192.168.1.0/24') -> List[Netwo
104107
async def scan_email_servers(self, network: str = '192.168.1.0/24') -> List[NetworkService]:
105108
"""Scan for email servers (SMTP, IMAP) on the network."""
106109
return await self.scan_network(network, service_types=['smtp', 'smtps', 'imap', 'imaps'])
110+
111+
async def _run_in_executor(self, func: Callable, *args) -> Any:
112+
"""Run a function in the thread pool."""
113+
loop = asyncio.get_event_loop()
114+
return await loop.run_in_executor(self.executor, func, *args)
115+
116+
async def check_rtsp_stream(self, ip: str, port: int = 554, timeout: float = 2.0) -> bool:
117+
"""Check if an RTSP stream is accessible using OpenCV."""
118+
rtsp_url = f"rtsp://{ip}:{port}"
119+
120+
def _check() -> bool:
121+
cap = None
122+
try:
123+
cap = cv2.VideoCapture(rtsp_url, cv2.CAP_FFMPEG)
124+
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('H', '2', '6', '4'))
125+
cap.set(cv2.CAP_PROP_OPEN_TIMEOUT_MSEC, int(timeout * 1000))
126+
cap.set(cv2.CAP_PROP_READ_TIMEOUT_MSEC, int(timeout * 1000))
127+
return cap.isOpened() and cap.grab()
128+
except Exception:
129+
return False
130+
finally:
131+
if cap is not None:
132+
cap.release()
133+
134+
try:
135+
return await self._run_in_executor(_check)
136+
except Exception:
137+
return False
107138

108139
async def check_rtsp_stream(self, ip: str, port: int = 554, timeout: float = 2.0) -> bool:
109140
"""Check if an RTSP stream is accessible."""

0 commit comments

Comments
 (0)