diff options
author | poeti8 <ezzati.upt@gmail.com> | 2020-07-30 17:48:15 +0300 |
---|---|---|
committer | poeti8 <ezzati.upt@gmail.com> | 2020-07-30 17:48:15 +0300 |
commit | c5ba1d67aed50a8887542699c72fb695aad8baed (patch) | |
tree | 5b115cd82b1bcbc7829dada8831ba2ca65df92ef /server | |
parent | 21bd3f1132254e5ecbd98673d4c6c9cc7ca6d78f (diff) |
fix: apply non-logged-in cooldown
Diffstat (limited to 'server')
-rw-r--r-- | server/handlers/auth.ts | 16 | ||||
-rw-r--r-- | server/queries/ip.ts | 12 | ||||
-rw-r--r-- | server/routes/links.ts | 1 |
3 files changed, 19 insertions, 10 deletions
diff --git a/server/handlers/auth.ts b/server/handlers/auth.ts index bf6ba3e..4b55519 100644 --- a/server/handlers/auth.ts +++ b/server/handlers/auth.ts @@ -8,10 +8,10 @@ import axios from "axios"; import { CustomError } from "../utils"; import * as utils from "../utils"; +import * as redis from "../redis"; +import queries from "../queries"; import * as mail from "../mail"; import query from "../queries"; -import knex from "../knex"; -import * as redis from "../redis"; import env from "../env"; const authenticate = ( @@ -65,14 +65,10 @@ export const cooldown: Handler = async (req, res, next) => { const cooldownConfig = env.NON_USER_COOLDOWN; if (req.user || !cooldownConfig) return next(); - const ip = await knex<IP>("ips") - .where({ ip: req.realIP.toLowerCase() }) - .andWhere( - "created_at", - ">", - subMinutes(new Date(), cooldownConfig).toISOString() - ) - .first(); + const ip = await queries.ip.find({ + ip: req.realIP.toLowerCase(), + created_at: [">", subMinutes(new Date(), cooldownConfig).toISOString()] + }); if (ip) { const timeToWait = diff --git a/server/queries/ip.ts b/server/queries/ip.ts index 1461ab8..b5ecd5a 100644 --- a/server/queries/ip.ts +++ b/server/queries/ip.ts @@ -25,6 +25,18 @@ export const add = async (ipToAdd: string) => { return ip; }; +export const find = async (match: Match<IP>) => { + const query = knex<IP>("ips"); + + Object.entries(match).forEach(([key, value]) => { + query.andWhere(key, ...(Array.isArray(value) ? value : [value])); + }); + + const ip = await query.first(); + + return ip; +}; + export const clear = async () => knex<IP>("ips") .where( diff --git a/server/routes/links.ts b/server/routes/links.ts index 992f014..36e2614 100644 --- a/server/routes/links.ts +++ b/server/routes/links.ts @@ -23,6 +23,7 @@ router.post( asyncHandler(auth.apikey), asyncHandler(auth.jwtLoose), asyncHandler(auth.recaptcha), + asyncHandler(auth.cooldown), validators.createLink, asyncHandler(helpers.verify), asyncHandler(link.create) |