Files
poli0981andClaude Opus 5.5 7920011c70 feat(worker): rate limits for the whole site (4.5.0) (#167)
Every request that runs the Worker is counted per client (IPv4, or IPv6
/64): RL_SITE 120/min (app pages, public /api, the old apps' /data JSON,
misses), RL_IMG 600/min (covers), RL_ADMIN 300/min (/admin + /api/admin);
RL_SUGGEST 5 and RL_GATE 20 stay on top for their POSTs. Verified bots are
not counted. Over a limit: 429 + Retry-After 60 (JSON for the API, covers,
files and /data with CORS; a script-less EN/VI page for app pages). Fails
open.

Every rate-limit namespace moves to 8801-8806 (staging 8901-8906):
namespace ids are account-wide, and other Workers on the account use
1001-1006, so RL_SUGGEST / RL_GATE shared their counters.

docs/operations.md section 1 step 15: the zone's WAF rate-limiting rule as
the first layer (Free plan: one rule, IP, 10 s; 300 requests / 10 s).

[skip-discuss]

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

128 lines
5.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Worker for freeitchgames.win. Existing static assets (the app bundles,
* /data/pack/*) are served by the assets layer without invoking this script.
* It runs for the paths in wrangler.jsonc `assets.run_worker_first` ("/" and
* the readable /data JSON among them), and for every request that matches no
* asset (`not_found_handling: "none"`, see spa.ts).
*
* app pages the shell, once the visitor has passed the gate (spa.ts, gate.ts)
* /img/* resized covers, gated too (img.ts)
* /api/verify verification gate status + Turnstile check (gate.ts)
* /api/suggest public suggestion form (suggest.ts)
* /api/admin/* maintainer API, Access + JWT check (admin.ts)
* /admin, /admin/* admin app shell, maintainer only (admin.ts)
* /data/*.json readable catalog JSON, for apps 4.0–4.1.1 only (legacy-data.ts)
*
* Every request is first counted against its per-client rate limit
* (ratelimit.ts; over it → 429).
*
* Cron: one RSS feed per run into the review queue, queue bookkeeping (with
* the pipeline's ingest outcomes), the admin's automatic rules (auto-approve,
* watched itch.io collections — automation.ts), then retention.
*/
import { handleAdminApi, serveAdminApp } from './admin'
import { runAutomation } from './automation'
import { catalogUrls } from './data'
import { discover } from './discover'
import type { WorkerEnv } from './env'
import { checkPass, gateConfig, gateNavigation, gateRequired, handleVerify } from './gate'
import { json } from './http'
import { handleImg } from './img'
import { handleLegacyData } from './legacy-data'
import { QueueStore } from './queue'
import { rateLimit } from './ratelimit'
import { repoOps } from './repo'
import { handleMiss } from './spa'
import { handleSuggest } from './suggest'
function queueStore(env: WorkerEnv): QueueStore | null {
return env.DB ? new QueueStore(env.DB) : null
}
export default {
async fetch(request, env: WorkerEnv, ctx): Promise<Response> {
const url = new URL(request.url)
const limited = await rateLimit(request, url, env)
if (limited) return limited
const path = url.pathname
const data = { assets: env.ASSETS, origin: url.origin }
const gate = gateConfig(env, url)
if (path.startsWith('/img/')) {
// Before the edge cache and R2: a visitor without a pass costs nothing.
if (gate && !(await checkPass(request, gate)).via) return gateRequired()
return handleImg(request, url, {
assets: env.ASSETS,
bucket: env.THUMBS,
cache: caches.default,
fetch: (input, init) => fetch(input, init),
waitUntil: (p) => ctx.waitUntil(p),
})
}
if (path === '/api/health') return json({ ok: true })
if (path === '/api/verify') {
return handleVerify(request, url, env, { fetch: (input, init) => fetch(input, init), limiter: env.RL_GATE })
}
if (path === '/api/suggest') {
return handleSuggest(request, env, {
store: queueStore(env),
data,
fetch: (input, init) => fetch(input, init),
limiter: env.RL_SUGGEST,
})
}
if (path.startsWith('/api/admin/')) return handleAdminApi(request, url, env, queueStore(env))
if (path.startsWith('/api/')) return json({ error: 'not_found' }, 404)
if (path === '/admin' || path.startsWith('/admin/')) return serveAdminApp(request, url, env)
// Not gated. Packs (/data/pack/*) are plain assets; a missing one ends up in handleMiss.
if (path.startsWith('/data/') && !path.startsWith('/data/pack/')) return handleLegacyData(request, env.ASSETS)
return handleMiss(request, url, env.ASSETS, gate ? (r) => gateNavigation(r, gate) : undefined)
},
async scheduled(_controller, env: WorkerEnv): Promise<void> {
const store = queueStore(env)
if (!store) return
const data = { assets: env.ASSETS, origin: env.SITE_ORIGIN }
const now = new Date()
const repo = repoOps(env)
const itch = {
fetch: (input: RequestInfo | URL, init?: RequestInit) => fetch(input, init),
userAgent: env.BOT_UA,
sleep: (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms)),
}
// Each step on its own: one failing must not skip the others.
try {
console.log('discover', await discover({ store, data, fetch: itch.fetch, userAgent: env.BOT_UA, now }))
} catch (e) {
console.error('discover failed', e)
}
try {
const catalog = new Set((await catalogUrls(data)).keys())
let outcomes = new Map<string, { outcome: string; at: string }>()
if (repo) {
try {
outcomes = (await repo.ingestView()).outcomes
} catch (e) {
console.error('reading ingest outcomes failed', e)
}
}
console.log('reconcile', await store.reconcile(catalog, outcomes, now))
} catch (e) {
console.error('reconcile failed', e)
}
if (repo) {
try {
console.log('automation', await runAutomation({ store, data, repo, itch, now }))
} catch (e) {
console.error('automation failed', e)
}
}
try {
await store.purge(now)
} catch (e) {
console.error('purge failed', e)
}
},
} satisfies ExportedHandler<WorkerEnv>