🚧 rate limit

This commit is contained in:
Flam3rboy
2021-07-01 09:33:54 +02:00
parent 14a31ad143
commit efc522a0c6
7 changed files with 98 additions and 326 deletions
+4 -11
View File
@@ -1,13 +1,7 @@
import { Config } from "@fosscord/server-util";
import { Request } from "express";
// use ipdata package instead of simple fetch because of integrated caching
import IPData, { LookupResponse } from "ipdata";
var ipdata: IPData;
const cacheConfig = {
max: 1000, // max size
maxAge: 1000 * 60 * 60 * 24 // max age in ms (i.e. one day)
};
import fetch from "node-fetch";
const exampleData = {
ip: "",
@@ -66,15 +60,14 @@ const exampleData = {
status: 200
};
export async function IPAnalysis(ip: string): Promise<LookupResponse> {
export async function IPAnalysis(ip: string): Promise<typeof exampleData> {
const { ipdataApiKey } = Config.get().security;
if (!ipdataApiKey) return { ...exampleData, ip };
if (!ipdata) ipdata = new IPData(ipdataApiKey, cacheConfig);
return await ipdata.lookup(ip);
return (await fetch(`https://api.ipdata.co/${ip}?api-key=${ipdataApiKey}`)).json();
}
export function isProxy(data: LookupResponse) {
export function isProxy(data: typeof exampleData) {
if (!data || !data.asn || !data.threat) return false;
if (data.asn.type !== "isp") return true;
if (Object.values(data.threat).some((x) => x)) return true;