cameras: RTSP preview via ffmpeg, stop opening VLC, drop masscan to 200pps
All checks were successful
build-and-deploy / build (push) Successful in 2m23s
All checks were successful
build-and-deploy / build (push) Successful in 2m23s
Masscan finds are rtsp:// with no snapshot_url, so the popup skipped the
<img> and the leftover source link handed the OS an rtsp:// URL (VLC).
- Popup always hits /api/cameras/{id}/snapshot (HTTP stills, then one
ffmpeg frame grab). No credentials. 10s hard timeout.
- rtsp:// is rendered as text, never as an href.
- ffmpeg added to the app image for the RTSP still/MJPEG path.
- Default MASSCAN_RATE 200 (1k/10k saturated the home uplink).
This commit is contained in:
parent
3af9511a3a
commit
12262d1562
6 changed files with 268 additions and 27 deletions
42
app/main.py
42
app/main.py
|
|
@ -11,6 +11,7 @@ Real-time geospatial OSINT dashboard API:
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
|
@ -752,20 +753,28 @@ async def list_cameras(
|
|||
|
||||
@app.get("/api/cameras/{camera_id}/snapshot")
|
||||
async def camera_snapshot(camera_id: UUID):
|
||||
"""Snapshot image for one camera, served through the local TTL cache."""
|
||||
"""Still image for one camera.
|
||||
|
||||
HTTP cameras go through the TTL cache. masscan/RTSP finds have no HTTP
|
||||
snapshot_url — we probe common still-image paths and, failing that, grab
|
||||
one JPEG frame from RTSP via ffmpeg. No credentials are tried.
|
||||
"""
|
||||
from camera_models import cameras as cam_table
|
||||
from camera_scraper import fetch_snapshot
|
||||
from camera_preview import resolve_preview
|
||||
from fastapi.responses import Response
|
||||
|
||||
async with async_session() as session:
|
||||
row = (await session.execute(
|
||||
select(cam_table).where(cam_table.c.id == camera_id)
|
||||
)).mappings().one_or_none()
|
||||
if not row or not row["snapshot_url"]:
|
||||
raise HTTPException(404, "Camera or snapshot not found")
|
||||
data = await fetch_snapshot(row["snapshot_url"])
|
||||
if not row:
|
||||
raise HTTPException(404, "Camera not found")
|
||||
try:
|
||||
data, _url = await asyncio.wait_for(resolve_preview(row), timeout=10)
|
||||
except asyncio.TimeoutError:
|
||||
raise HTTPException(502, "Snapshot unavailable")
|
||||
if not data:
|
||||
raise HTTPException(502, "Snapshot unavailable")
|
||||
from fastapi.responses import Response
|
||||
return Response(content=data, media_type="image/jpeg")
|
||||
|
||||
|
||||
|
|
@ -783,13 +792,22 @@ async def camera_stream(camera_id: UUID):
|
|||
import httpx
|
||||
|
||||
async with async_session() as session:
|
||||
url = (await session.execute(
|
||||
select(cam_table.c.snapshot_url).where(cam_table.c.id == camera_id)
|
||||
)).scalar_one_or_none()
|
||||
if not url:
|
||||
row = (await session.execute(
|
||||
select(cam_table).where(cam_table.c.id == camera_id)
|
||||
)).mappings().one_or_none()
|
||||
if not row:
|
||||
raise HTTPException(404, "Camera not found")
|
||||
url = row["snapshot_url"] or row["source_url"]
|
||||
if str(url or "").lower().startswith("rtsp://"):
|
||||
from camera_preview import ffmpeg_mjpeg_stream, _FFMPEG
|
||||
if not _FFMPEG:
|
||||
raise HTTPException(502, "RTSP preview requires ffmpeg")
|
||||
return StreamingResponse(
|
||||
ffmpeg_mjpeg_stream(url),
|
||||
media_type="multipart/x-mixed-replace; boundary=ffmpeg",
|
||||
)
|
||||
if not str(url or "").lower().startswith(("http://", "https://")):
|
||||
raise HTTPException(404, "Camera or snapshot not found")
|
||||
if not str(url).lower().startswith(("http://", "https://")):
|
||||
raise HTTPException(400, "URL is not streamable over HTTP")
|
||||
|
||||
# connect timeout short so dead cams fail fast; read timeout None because
|
||||
# an MJPEG stream legitimately idles between frames.
|
||||
|
|
|
|||
Loading…
Reference in a new issue