aboutsummaryrefslogtreecommitdiffstats
path: root/lib/registration-limit.ts
blob: 2550e37f16c9dc545286e30c2450fe576fcab0d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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;
};