rename @fosscord/server-util to -> @fosscord/util

This commit is contained in:
Flam3rboy
2021-08-13 12:59:59 +02:00
parent 0802d5e6a8
commit 90d5307a56
66 changed files with 254 additions and 165 deletions
+11 -10
View File
@@ -1,15 +1,15 @@
import { NextFunction, Request, Response } from "express";
import { HTTPError } from "lambert-server";
import { checkToken, Config } from "@fosscord/server-util";
import { checkToken, Config } from "@fosscord/util";
export const NO_AUTHORIZATION_ROUTES = [
/^\/api(\/v\d+)?\/auth\/login/,
/^\/api(\/v\d+)?\/auth\/register/,
/^\/api(\/v\d+)?\/webhooks\//,
/^\/api(\/v\d+)?\/ping/,
/^\/api(\/v\d+)?\/gateway/,
/^\/api(\/v\d+)?\/experiments/,
/^\/api(\/v\d+)?\/guilds\/\d+\/widget\.(json|png)/
"/auth/login",
"/auth/register",
"/webhooks/",
"/ping",
"/gateway",
"/experiments"
// /^\/api(\/v\d+)?\/guilds\/\d+\/widget\.(json|png)/
];
export const API_PREFIX = /^\/api(\/v\d+)?/;
@@ -24,13 +24,14 @@ declare global {
}
}
}
// TODO wenn client offen ist, wird http://localhost:8080/api/v9/users/@me/guild-events blockiert?
export async function Authentication(req: Request, res: Response, next: NextFunction) {
if (req.method === "OPTIONS") return res.sendStatus(204);
if (!req.url.startsWith("/api")) return next();
const apiPath = req.url.replace(API_PREFIX, "");
if (apiPath.startsWith("/invites") && req.method === "GET") return next();
if (NO_AUTHORIZATION_ROUTES.some((x) => x.test(req.url))) return next();
if (apiPath.startsWith("/invites") && req.method === "GET") return next(); // @ts-ignore
if (NO_AUTHORIZATION_ROUTES.some((x) => apiPath.startsWith(x) || x.test?.(req.url))) return next();
if (!req.headers.authorization) return next(new HTTPError("Missing Authorization Header", 401));
try {
-2
View File
@@ -25,8 +25,6 @@ export function ErrorHandler(error: Error, req: Request, res: Response, next: Ne
if (httpcode > 511) httpcode = 400;
res.status(httpcode).json({ code: code, message, errors });
return;
} catch (error) {
console.error(error);
return res.status(500).json({ code: 500, message: "Internal Server Error" });
+12 -9
View File
@@ -1,16 +1,17 @@
import { db, MongooseCache, Bucket, Config } from "@fosscord/server-util";
// @ts-nocheck
import { db, Bucket, Config } from "@fosscord/util";
import { NextFunction, Request, Response, Router } from "express";
import { getIpAdress } from "../util/ipAddress";
import { API_PREFIX_TRAILING_SLASH } from "./Authentication";
const Cache = new MongooseCache(
db.collection("ratelimits"),
[
// TODO: uncomment $match and fix error: not receiving change events
// { $match: { blocked: true } }
],
{ onlyEvents: false, array: true }
);
// const Cache = new MongooseCache(
// db.collection("ratelimits"),
// [
// // TODO: uncomment $match and fix error: not receiving change events
// // { $match: { blocked: true } }
// ],
// { onlyEvents: false, array: true }
// );
// Docs: https://discord.com/developers/docs/topics/rate-limits
@@ -31,6 +32,7 @@ TODO: use config values
*/
// TODO: FIX with new event handling
export default function RateLimit(opts: {
bucket?: string;
window: number;
@@ -44,6 +46,7 @@ export default function RateLimit(opts: {
success?: boolean;
onlyIp?: boolean;
}): any {
return (req, res, next) => next();
Cache.init(); // will only initalize it once
return async (req: Request, res: Response, next: NextFunction): Promise<any> => {