makes max IP limit configurable per-user

This commit is contained in:
nai-degen
2023-09-18 23:16:06 -05:00
parent 40e71435f0
commit c6453638e9
8 changed files with 74 additions and 54 deletions
+2
View File
@@ -50,6 +50,8 @@ export const UserSchema = z
disabledReason: z.string().optional(),
/** Time at which the user will expire and be disabled (for temp users). */
expiresAt: z.number().optional(),
/** The user's maximum number of IP addresses; supercedes global max. */
maxIps: z.coerce.number().int().min(0).optional(),
})
.strict();
+3 -5
View File
@@ -17,8 +17,6 @@ import { User, UserUpdate } from "./schema";
const log = logger.child({ module: "users" });
const MAX_IPS_PER_USER = config.maxIpsPerUser;
const users: Map<string, User> = new Map();
const usersToFlush = new Set<string>();
let quotaRefreshJob: schedule.Job | null = null;
@@ -173,10 +171,10 @@ export function authenticate(token: string, ip: string) {
const user = users.get(token);
if (!user || user.disabledAt) return;
if (!user.ip.includes(ip)) user.ip.push(ip);
// If too many IPs are associated with the user, disable the account.
const configIpLimit = user.maxIps ?? config.maxIpsPerUser;
const ipLimit =
user.type === "special" || !MAX_IPS_PER_USER ? Infinity : MAX_IPS_PER_USER;
user.type === "special" || !configIpLimit ? Infinity : configIpLimit;
if (user.ip.length > ipLimit) {
disableUser(token, "IP address limit exceeded.");
return;