fix middlewares
This commit is contained in:
parent
410a786497
commit
25941e0437
4 changed files with 11 additions and 11 deletions
|
@ -1,7 +1,7 @@
|
||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, NextFunction } from "express";
|
||||||
import { app } from "..";
|
import { app } from "..";
|
||||||
|
|
||||||
app.use('*', (req: Request, res: Response, next: NextFunction) => {
|
app.use('*', (_req: Request, res: Response, next: NextFunction) => {
|
||||||
res.header('Access-Control-Allow-Origin', '*');
|
res.header('Access-Control-Allow-Origin', '*');
|
||||||
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, x-auth-user, x-auth-token');
|
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, x-auth-user, x-auth-token');
|
||||||
res.header('Access-Control-Allow-Methods', '*');
|
res.header('Access-Control-Allow-Methods', '*');
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { Request, Response } from "express";
|
import { Request, Response, NextFunction } from "express";
|
||||||
import { app, logger } from "..";
|
import { app, logger } from "..";
|
||||||
|
|
||||||
app.use('*', (req: Request, _res: Response, next: () => void) => {
|
app.use('*', (req: Request, _res: Response, next: NextFunction) => {
|
||||||
logger.debug(`${req.method} ${req.url}`);
|
logger.debug(`${req.method} ${req.url}`);
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
|
@ -20,7 +20,7 @@ class RateLimiter {
|
||||||
const ip = req.ip;
|
const ip = req.ip;
|
||||||
const reqId = ulid();
|
const reqId = ulid();
|
||||||
// ratelimit:ip_address_base64:route_base64
|
// ratelimit:ip_address_base64:route_base64
|
||||||
const redisKey = `ratelimit:${Buffer.from(ip).toString('base64')}:${Buffer.from(this.route).toString('base64')}`;
|
const redisKey = `ratelimit:${Buffer.from(ip!).toString('base64')}:${Buffer.from(this.route).toString('base64')}`;
|
||||||
const reqs = await redis.SCARD(redisKey);
|
const reqs = await redis.SCARD(redisKey);
|
||||||
if (reqs >= this.limit) {
|
if (reqs >= this.limit) {
|
||||||
logger.debug(`Ratelimiter: IP address exceeded ratelimit for ${this.route} [${this.limit}/${this.timeframe}]`);
|
logger.debug(`Ratelimiter: IP address exceeded ratelimit for ${this.route} [${this.limit}/${this.timeframe}]`);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Request } from "express";
|
import { Request, Response, NextFunction } from "express";
|
||||||
import { Collection, Db } from "mongodb";
|
import { Collection, Db } from "mongodb";
|
||||||
import { app, SESSION_LIFETIME } from "..";
|
import { app, SESSION_LIFETIME } from "..";
|
||||||
|
|
||||||
|
@ -8,17 +8,17 @@ export function initializeSessionsMiddleware(db: Db) {
|
||||||
sessionsCollection = db.collection('sessions');
|
sessionsCollection = db.collection('sessions');
|
||||||
}
|
}
|
||||||
|
|
||||||
app.use('*', async (req: Request, next: () => void) => {
|
app.use('*', async (req: Request, _res: Response, next: NextFunction) => {
|
||||||
next();
|
next();
|
||||||
const user = req.header('x-auth-user');
|
const user = req.header('x-auth-user');
|
||||||
const token = req.header('x-auth-token');
|
const token = req.header('x-auth-token');
|
||||||
if (!user || !token) return;
|
if (!user || !token) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const session = await sessionsCollection.findOne({
|
const session = await sessionsCollection.findOne({
|
||||||
user,
|
user,
|
||||||
token,
|
token,
|
||||||
expires: { $gt: new Date() }
|
expires: { $gt: new Date() }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (session) {
|
if (session) {
|
||||||
|
@ -27,7 +27,7 @@ app.use('*', async (req: Request, next: () => void) => {
|
||||||
{ $set: { expires: new Date(Date.now() + SESSION_LIFETIME) } }
|
{ $set: { expires: new Date(Date.now() + SESSION_LIFETIME) } }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue