aboutsummaryrefslogtreecommitdiffstats
path: root/lib/registration-limit.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/registration-limit.ts')
-rw-r--r--lib/registration-limit.ts19
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/registration-limit.ts b/lib/registration-limit.ts
new file mode 100644
index 00000000..2550e37f
--- /dev/null
+++ b/lib/registration-limit.ts
@@ -0,0 +1,19 @@
+import { db } from "$lib/db";
+
+// Onion clients share the Tor gateway's private IP. Keep a separate quota:
+// exempting private IPs lets onion clients create unlimited accounts, while
+// applying the public /16 quota would also count unrelated internal traffic.
+export const allowTorRegistration = async (req, res, store = db) => {
+ if (!req.isTor) return true;
+ const [count, ttl] = (await store.eval(
+ `local n = redis.call('INCR', KEYS[1])
+ if n == 1 then redis.call('EXPIRE', KEYS[1], 3600) end
+ return {n, redis.call('TTL', KEYS[1])}`,
+ { keys: ["register:tor:hour"], arguments: [] },
+ )) as number[];
+ if (count <= 5) return true;
+ res.header("Retry-After", Math.max(1, ttl)).code(429).send({
+ message: "Too many accounts created over Tor, try again later",
+ });
+ return false;
+};