Files
youtube-generator/worker/env.ts
T
poli0981andClaude Opus 5.5 bdaf15d835 feat(worker): rate-limit every client before the gate
Two Workers Rate Limiting bindings in wrangler.jsonc, checked first in
the router so a flood costs no siteverify call, HMAC check or asset
fetch:

- RATE_LIMIT: 120 requests a minute for everything the Worker handles
  (gate page, app shell, legal pages, /__gate/*, any method).
- VERIFY_RATE_LIMIT: 10 a minute for POST /__gate/verify on top.

Clients are keyed by IPv4 address or IPv6 /64. Over the limit: 429 with
Retry-After: 60 (plain text, or JSON for the verify call). Like the
gate, it fails open when a binding is missing or limit() throws, and
logs that once per isolate. A test keeps wrangler.jsonc's bindings and
period in step with the code.

HOSTING documents both layers and the zone's WAF rate limiting rule
(Free plan: one rule, per IP, 10 s) as the first one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 06:28:19 +07:00

70 lines
2.7 KiB
TypeScript

/**
* Bindings and constants for the Cloudflare Worker that fronts
* https://ytgenerator.stream.
*
* The Worker only uses standard Web APIs (fetch, Request, Response, Web
* Crypto), so it type-checks against the DOM lib and runs unchanged under
* Vitest in Node. The static-assets binding is described structurally rather
* than through `@cloudflare/workers-types`.
*/
/** The `ASSETS` binding from `wrangler.jsonc` — serves files out of `dist/`. */
export interface AssetsBinding {
fetch(request: Request): Promise<Response>;
}
/** A `ratelimits` binding from `wrangler.jsonc` (Workers Rate Limiting API). */
export interface RateLimitBinding {
limit(options: { key: string }): Promise<{ success: boolean }>;
}
export interface Env {
ASSETS: AssetsBinding;
/** Every request the Worker handles, per client. Optional: a missing
* binding lets traffic through (see ./rate-limit.ts). */
RATE_LIMIT?: RateLimitBinding;
/** `POST /__gate/verify` only — each one costs a siteverify call. */
VERIFY_RATE_LIMIT?: RateLimitBinding;
/** Public Turnstile sitekey (`vars` in wrangler.jsonc). */
TURNSTILE_SITE_KEY: string;
/** Turnstile secret — a dashboard Secret, never committed. */
TURNSTILE_SECRET: string;
/** How long a passed check lasts, in seconds (string, from `vars`). */
GATE_TTL_SECONDS?: string;
}
/** Everything the router needs from the outside world, injectable in tests. */
export interface Deps {
/** Current time in milliseconds since the epoch. */
now(): number;
fetch: typeof fetch;
/** A fresh random string: CSP nonces and siteverify idempotency keys. */
randomId(): string;
}
export const GATE_COOKIE = "__Host-ytg_gate";
export const VERIFY_PATH = "/__gate/verify";
/** 204 when the request carries a valid gate cookie — lets the gate page
* detect a browser that silently refused to store it. */
export const STATUS_PATH = "/__gate/status";
export const TURNSTILE_ACTION = "enter";
export const SITEVERIFY_URL = "https://challenges.cloudflare.com/turnstile/v0/siteverify";
/** Turnstile tokens are valid for 300 s; anything older is refused. */
export const MAX_TOKEN_AGE_SECONDS = 300;
/** `Retry-After` on a 429: the `period` of both rate limiters in
* wrangler.jsonc (a test keeps them equal). */
export const RATE_LIMIT_PERIOD_SECONDS = 60;
const DEFAULT_GATE_TTL_SECONDS = 86_400;
const MIN_GATE_TTL_SECONDS = 300;
const MAX_GATE_TTL_SECONDS = 30 * 86_400;
/** The configured gate lifetime, clamped to [5 min, 30 days]. */
export function gateTtlSeconds(env: Env): number {
const parsed = Number.parseInt(env.GATE_TTL_SECONDS ?? "", 10);
if (!Number.isFinite(parsed)) return DEFAULT_GATE_TTL_SECONDS;
return Math.min(MAX_GATE_TTL_SECONDS, Math.max(MIN_GATE_TTL_SECONDS, parsed));
}