CCTV Street View budget: key the client before the camera wait, share the default

- The /frame handler reads the client's network key (IPv6 /64) before it
  awaits the camera, so a client that drops the connection is still
  charged to its own window instead of the shared fallback key.
- The budget is charged only after the Google key and finite catalog
  coordinates are confirmed, and the key is read once.
- Places and the CCTV window share one default (GOOGLE_DEFAULT_PER_MIN).
- Tests: the catalog-only test checks heading/fov/pitch from the query at
  the catalog location; the budget test covers unknown ids, forwarded
  requests and /64 grouping; a real-socket test covers a dropped
  connection. The duplicate pose test is removed.
- .env.example, SECURITY.md, CURRENT-STATE and the changelog fragment
  describe the separate per-IP window, the catalog position and the
  placeholder for seed cameras.
This commit is contained in:
German Khokhlov committed 2026-11-20 11:31:14 +04:00
1 parent 01368b95bf
commit 01d4a3f454
10 files changed
+175 -201

No files matched your search

+26 -30
View File
@@ -21,8 +21,11 @@ import { PROXY_SIGNALS } from '../../src/localRequestGate.mjs';
import { createHlsPuller } from './cctv/stream.js';
import { isMlitRiverAdjustmentImage } from './cctv/worldCameras.js';
import { ottawaAuthorizedFrameUrl } from './cctv/ottawa.js';
import { googleServerApiKey } from './places/google-key.js';
import { clientKey, makeCostRateLimiter } from './common/rate-limit.js';
import {
GOOGLE_DEFAULT_PER_MIN,
googleServerApiKey,
} from './places/google-key.js';
import { clientNetworkKey, makeCostRateLimiter } from './common/rate-limit.js';
export { CCTV_FRAME_FETCH_TIMEOUT_MS, fetchCctvImageFromUpstream };
/**
@@ -200,21 +203,7 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) {
};
};
/**
* Per-IP budget for the Street View fallback, one per proxy, on the same
* setting as the other Google routes (GEV_RATELIMIT_GOOGLE_PER_MIN; 0
* disables it). Built on first use, after the standalone env has loaded.
* undefined = not built yet; null = the explicit 0 opt-out.
*/
let streetViewBudget;
const streetViewRateLimiter = () => {
if (streetViewBudget === undefined)
streetViewBudget = makeCostRateLimiter(
process.env.GEV_RATELIMIT_GOOGLE_PER_MIN,
120,
);
return streetViewBudget;
};
let streetViewBudget; // undefined = not built yet; null = the explicit 0 opt-out
/**
* Fetch a Google Street View static image as a fallback frame. Server-side
@@ -222,11 +211,23 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) {
* (#33: a key scoped to Street View Static/Places, restricted by server IP
* rather than HTTP referrer) and falls back to the browser-exposed
* GOOGLE_MAPS_API_KEY for setups that haven't split the two yet.
* Each IP (or IPv6 /64) has its own GEV_RATELIMIT_GOOGLE_PER_MIN window,
* separate from Places. Setting it to 0 disables the limit.
*/
const streetViewFallback = async ({ lat, lon, heading, fov, pitch }) => {
const streetViewFallback = async (
client,
{ lat, lon, heading, fov, pitch },
) => {
const streetViewKey = googleServerApiKey();
if (!streetViewKey || !Number.isFinite(lat) || !Number.isFinite(lon))
return null;
// Built on first use, after the standalone env has loaded.
if (streetViewBudget === undefined)
streetViewBudget = makeCostRateLimiter(
process.env.GEV_RATELIMIT_GOOGLE_PER_MIN,
GOOGLE_DEFAULT_PER_MIN,
);
if (streetViewBudget && !streetViewBudget(client)) return null;
try {
const sv = new URL('https://maps.googleapis.com/maps/api/streetview');
sv.searchParams.set('size', '960x540');
@@ -282,6 +283,8 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) {
// keyless gate keeps the routes reachable behind a reverse proxy; the
// key-bearing Street View fallback below has its own check.
if (admitSameSiteKeyless(req, res)) return;
// Read before any await: a socket the client has closed has no address.
const client = clientNetworkKey(req);
const sources = await getCctvSources();
const sourceById = new Map(
sources.map((source) => [source.id, source]),
@@ -653,8 +656,7 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) {
const source = sourceById.get(cameraId);
const label = url.searchParams.get('label') || source?.name || cameraId;
const city = url.searchParams.get('city') || source?.city || '';
// The view follows the pose the browser draws (calibration included);
// the position never comes from the query (see the Street View fallback).
// The query sets only the view, the pose the browser draws.
const heading = Number(
url.searchParams.get('heading') || source?.headingDeg,
);
@@ -749,21 +751,15 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) {
// request (whose client this server cannot see) gets the placeholder,
// as if no key were configured. Any forwarding header counts, even an
// empty one.
// It is also only for catalog cameras, at the catalog position (never a
// spot the query names; the query sets only the view), within the per-IP
// Google budget.
// It also answers only for catalog cameras, at the catalog position;
// the query never sets the location.
const forwarded = PROXY_SIGNALS.some(
(name) => req.headers?.[name] !== undefined,
);
const googleBudget = googleServerApiKey()
? streetViewRateLimiter()
: null;
const sv =
forwarded ||
!source ||
(googleBudget && !googleBudget(clientKey(req)))
forwarded || !source
? null
: await streetViewFallback({
: await streetViewFallback(client, {
lat: source.lat,
lon: source.lon,
heading,