🔒 register proxy ip check
This commit is contained in:
@@ -1,49 +0,0 @@
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { Config } from "@fosscord/server-util";
|
||||
|
||||
// TODO: use mongodb ttl index
|
||||
// TODO: increment count on serverside
|
||||
|
||||
export async function GlobalRateLimit(req: Request, res: Response, next: NextFunction) {
|
||||
return next();
|
||||
// TODO: use new db mongoose models
|
||||
/*
|
||||
if (!Config.get().limits.rate.ip.enabled) return next();
|
||||
|
||||
const ip = getIpAdress(req);
|
||||
let limit = (await db.data.ratelimit.global[ip].get()) || { start: Date.now(), count: 0 };
|
||||
if (limit.start < Date.now() - Config.get().limits.rate.ip.timespan) {
|
||||
limit.start = Date.now();
|
||||
limit.count = 0;
|
||||
}
|
||||
|
||||
if (limit.count > Config.get().limits.rate.ip.count) {
|
||||
const timespan = Date.now() - limit.start;
|
||||
|
||||
return res
|
||||
.set("Retry-After", `${timespan.toFixed(0)}`)
|
||||
.set("X-RateLimit-Global", "true")
|
||||
.status(429)
|
||||
.json({
|
||||
message: "You are being rate limited.",
|
||||
retry_after: timespan,
|
||||
global: true,
|
||||
});
|
||||
}
|
||||
|
||||
res.once("close", async () => {
|
||||
if (res.statusCode >= 400) {
|
||||
limit.count++;
|
||||
await db.data.ratelimit.global[ip].set(limit);
|
||||
}
|
||||
});
|
||||
|
||||
return next();
|
||||
*/
|
||||
}
|
||||
|
||||
export function getIpAdress(req: Request): string {
|
||||
const { forwadedFor } = Config.get().security;
|
||||
const ip = forwadedFor ? <string>req.headers[forwadedFor] : req.ip;
|
||||
return ip.replaceAll(".", "_").replaceAll(":", "_");
|
||||
}
|
||||
@@ -1,45 +1,8 @@
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { db, MongooseCache } from "@fosscord/server-util";
|
||||
import { NextFunction } from "express";
|
||||
|
||||
import { getIpAdress } from "./GlobalRateLimit";
|
||||
const Cache = new MongooseCache(db.collection("ratelimit"), [], { onlyEvents: false });
|
||||
|
||||
export function RateLimit({ count = 10, timespan = 1000 * 5, name = "/" }) {
|
||||
return async (req: Request, res: Response, next: NextFunction) => {
|
||||
return next();
|
||||
// TODO: use new db mongoose models
|
||||
/*
|
||||
|
||||
let id = req.user_id || getIpAdress(req);
|
||||
|
||||
const limit: { count: number; start: number } = (await db.data.ratelimit.routes[name][id].get()) || {
|
||||
count: 0,
|
||||
start: Date.now(),
|
||||
};
|
||||
|
||||
if (limit.start < Date.now() - timespan) {
|
||||
limit.start = Date.now();
|
||||
limit.count = 0;
|
||||
}
|
||||
|
||||
if (limit.count > count) {
|
||||
const wait = Date.now() - limit.start;
|
||||
|
||||
return res
|
||||
.set("Retry-After", `${wait.toFixed(0)}`)
|
||||
.set("X-RateLimit-Limit", `${count}`)
|
||||
.set("X-RateLimit-Remaining", "0")
|
||||
.set("X-RateLimit-Reset", `${limit.start + wait}`)
|
||||
.set("X-RateLimit-Reset-After", `${wait}`)
|
||||
.set("X-RateLimit-Bucket", name)
|
||||
.set("X-RateLimit-Global", "false")
|
||||
.status(429)
|
||||
.json({
|
||||
message: "You are being rate limited.",
|
||||
retry_after: wait,
|
||||
global: false,
|
||||
});
|
||||
}
|
||||
|
||||
return next();
|
||||
*/
|
||||
};
|
||||
export default function RateLimit({}) {
|
||||
return async (req: Request, res: Response, next: NextFunction) => {};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user