mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +08:00
TRUST_PROXY=1 trusted CF-Connecting-IP and the first X-Forwarded-For entry, both of which a client can send itself when the proxy in front isn't Cloudflare (nginx's $proxy_add_x_forwarded_for only appends), so per-IP limits could be sidestepped. Now TRUST_PROXY=1 takes the last X-Forwarded-For entry (the one the proxy appended), and the new TRUST_PROXY=cloudflare takes only CF-Connecting-IP. The bundled compose file (cloudflared) switches to cloudflare. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
41 lines
1.6 KiB
TypeScript
41 lines
1.6 KiB
TypeScript
import type { IncomingMessage } from "node:http";
|
|
|
|
// Behind a reverse proxy / tunnel every request arrives from the proxy's own
|
|
// address, so per-IP limits would lump all users into one bucket. Only set
|
|
// TRUST_PROXY when the gateway is reachable *solely* through that proxy:
|
|
// - "cloudflare": CF-Connecting-IP, which Cloudflare always sets itself.
|
|
// - "1": the last X-Forwarded-For entry, the one our own proxy appended;
|
|
// anything before it came from the client and can be made up.
|
|
const TRUST_PROXY = process.env.TRUST_PROXY;
|
|
|
|
export function clientIp(req: IncomingMessage): string {
|
|
if (TRUST_PROXY === "cloudflare") {
|
|
const cf = req.headers["cf-connecting-ip"];
|
|
if (typeof cf === "string" && cf.trim()) return cf.trim();
|
|
} else if (TRUST_PROXY === "1") {
|
|
const last = String(req.headers["x-forwarded-for"] ?? "").split(",").at(-1)?.trim();
|
|
if (last) return last;
|
|
}
|
|
return (req.socket.remoteAddress ?? "unknown").replace(/^::ffff:/, "");
|
|
}
|
|
|
|
/** Sliding-window "N per window per key" limiter. Expired keys are swept
|
|
* periodically so the map can't grow without bound under a stream of
|
|
* one-shot IPs. */
|
|
export function createRateLimiter(limit: number, windowMs: number): (key: string) => boolean {
|
|
const hits = new Map<string, number[]>();
|
|
setInterval(() => {
|
|
const now = Date.now();
|
|
for (const [key, times] of hits) {
|
|
if (times.every((t) => now - t >= windowMs)) hits.delete(key);
|
|
}
|
|
}, windowMs).unref();
|
|
return (key) => {
|
|
const now = Date.now();
|
|
const recent = (hits.get(key) ?? []).filter((t) => now - t < windowMs);
|
|
recent.push(now);
|
|
hits.set(key, recent);
|
|
return recent.length > limit;
|
|
};
|
|
}
|