summaryrefslogtreecommitdiff
path: root/packages/backend/test-federation/daemon.ts
blob: 46b6963c79ac26a85e105d0b1d7212b218e54612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import IPCIDR from 'ip-cidr';
import { Redis } from 'ioredis';

const TESTER_IP_ADDRESS = '172.20.1.1';

/**
 * This should be same as {@link file://./../src/misc/get-ip-hash.ts}.
 */
function getIpHash(ip: string) {
	const prefix = IPCIDR.createAddress(ip).mask(64);
	return `ip-${BigInt('0b' + prefix).toString(36)}`;
}

/**
 * This prevents hitting rate limit when login.
 */
export async function purgeLimit(host: string, client: Redis) {
	const ipHash = getIpHash(TESTER_IP_ADDRESS);
	const key = `${host}:limit:${ipHash}:signin`;
	const res = await client.zrange(key, 0, -1);
	if (res.length !== 0) {
		console.log(`${key} - ${JSON.stringify(res)}`);
		await client.del(key);
	}
}

console.log('Daemon started running');

{
	const redisClient = new Redis({
		host: 'redis.test',
	});

	setInterval(() => {
		purgeLimit('a.test', redisClient);
		purgeLimit('b.test', redisClient);
	}, 200);
}