UNPKG

@xmobitea/gn-server

Version:

GearN Server by XmobiTea (Pro)

296 lines (295 loc) 14.3 kB
import express from "express"; import { Socket } from "socket.io"; import * as mongodb from "mongodb"; /** * Transport-agnostic throttling middleware for HTTP, socket, uploads, downloads, and blacklist/whitelist rules. */ export declare class AntiDdosMiddleware { private static readonly FIVE_MINUTE_TS; private static readonly K_MULTIPLE_ADMIN; private static readonly K_MULTIPLE_WHITE_LIST; private static readonly K_MULTIPLE_BLACK_LIST; private socketRequestPerIPDic; private socketRequestPerPeerDic; private httpRequestPerIPDic; private httpRequestPerPeerDic; private currentHttpRequestCount; private currentSocketRequestCount; private currentHttpUploadRequestCount; private currentHttpDownloadRequestCount; private reportDdosCollection; private banDdosCollection; private socketMaxRequestPerSecondPerIp; private socketMaxRequestPerSecondPerPeer; private socketMaxPendingRequest; private socketMaxRequestSize; private httpMaxRequestPerSecondPerIp; private httpMaxRequestPerSecondPerPeer; private httpMaxPendingRequest; private httpMaxRequestSize; private maxUploadPendingRequest; private maxDownloadPendingRequest; private defaultIPWhiteLst; private ipWhiteLst; private ipBlackLst; /** * Sets the report DDoS collection. * @param reportDdosCollection Provides the report DDoS collection used by this operation. */ setReportDdosCollection(reportDdosCollection: mongodb.Collection<mongodb.Document>): void; /** * Sets the ban DDoS collection. * @param banDdosCollection Provides the ban DDoS collection used by this operation. */ setBanDdosCollection(banDdosCollection: mongodb.Collection<mongodb.Document>): void; /** * Sets the socket max request per second per IP. * @param socketMaxRequestPerSecondPerIp Provides the socket max request per second per IP value used by this operation. */ setSocketMaxRequestPerSecondPerIp(socketMaxRequestPerSecondPerIp: number): void; /** * Sets the socket max request per second per peer. * @param socketMaxRequestPerSecondPerPeer Provides the socket max request per second per peer value used by this operation. */ setSocketMaxRequestPerSecondPerPeer(socketMaxRequestPerSecondPerPeer: number): void; /** * Sets the socket max pending request. * @param socketMaxPendingRequest Provides the socket max pending request value used by this operation. */ setSocketMaxPendingRequest(socketMaxPendingRequest: number): void; /** * Sets the socket max request size. * @param socketMaxRequestSize Provides the socket max request size value used by this operation. */ setSocketMaxRequestSize(socketMaxRequestSize: number): void; /** * Sets the HTTP max request per second per IP. * @param httpMaxRequestPerSecondPerIp Provides the HTTP max request per second per IP value used by this operation. */ setHttpMaxRequestPerSecondPerIp(httpMaxRequestPerSecondPerIp: number): void; /** * Sets the HTTP max request per second per peer. * @param httpMaxRequestPerSecondPerPeer Provides the HTTP max request per second per peer value used by this operation. */ setHttpMaxRequestPerSecondPerPeer(httpMaxRequestPerSecondPerPeer: number): void; /** * Sets the HTTP max pending request. * @param httpMaxPendingRequest Provides the HTTP max pending request value used by this operation. */ setHttpMaxPendingRequest(httpMaxPendingRequest: number): void; /** * Sets the HTTP max request size. * @param httpMaxRequestSize Provides the HTTP max request size value used by this operation. */ setHttpMaxRequestSize(httpMaxRequestSize: number): void; /** * Sets the max upload pending request. * @param maxUploadPendingRequest Provides the max upload pending request value used by this operation. */ setMaxUploadPendingRequest(maxUploadPendingRequest: number): void; /** * Sets the max download pending request. * @param maxDownloadPendingRequest Provides the max download pending request value used by this operation. */ setMaxDownloadPendingRequest(maxDownloadPendingRequest: number): void; /** * Seeds the in-memory allow-list with statically configured IPs. * @param ipWhiteList Provides the IP white list value used by this operation. */ setIPWhiteList(ipWhiteList: string[]): void; /** * Rejects oversized JSON HTTP payloads before they reach the request parser. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. * @param next Provides the next value used by this operation. */ checkRequestSizeBodyViaJson(req: express.Request, res: express.Response, next: Function): Promise<any>; /** * Rejects oversized MsgPack HTTP payloads before they reach the request parser. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. * @param next Provides the next value used by this operation. */ checkRequestSizeBodyViaMsgPack(req: express.Request, res: express.Response, next: Function): Promise<any>; /** * Calculates the HTTP payload-size result code, with overrides for a few intentionally large admin routes. * @param req Provides the req value used by this operation. * @returns Returns the return code for request size HTTP. */ private getReturnCodeForRequestSizeHttp; /** * Executes the check request size body via socket msg pack workflow asynchronously. * @param socket Provides the socket value used by this operation. * @param data Provides the data value used by this operation. * @param next Provides the next value used by this operation. */ checkRequestSizeBodyViaSocketMsgPack(socket: Socket, data: any, next: Function): Promise<any>; /** * Executes the check request size body via socket JSON workflow asynchronously. * @param socket Provides the socket value used by this operation. * @param data Provides the data value used by this operation. * @param next Provides the next value used by this operation. */ checkRequestSizeBodyViaSocketJson(socket: Socket, data: any, next: Function): Promise<any>; /** * Executes the check max request per second per IP via socket workflow asynchronously. * @param socket Provides the socket value used by this operation. * @param data Provides the data value used by this operation. * @param next Provides the next value used by this operation. */ checkMaxRequestPerSecondPerIpViaSocket(socket: Socket, data: any, next: Function): Promise<any>; /** * Executes the check max request per second per peer via socket workflow asynchronously. * @param socket Provides the socket value used by this operation. * @param data Provides the data value used by this operation. * @param next Provides the next value used by this operation. */ checkMaxRequestPerSecondPerPeerViaSocket(socket: Socket, data: any, next: Function): Promise<any>; /** * Executes the check max request per second per IP via JSON workflow asynchronously. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. * @param next Provides the next value used by this operation. */ checkMaxRequestPerSecondPerIpViaJson(req: express.Request, res: express.Response, next: Function): Promise<any>; /** * Executes the check max upload request per second per IP workflow asynchronously. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. * @param next Provides the next value used by this operation. */ checkMaxUploadRequestPerSecondPerIp(req: express.Request, res: express.Response, next: Function): Promise<any>; /** * Executes the check max request per second per IP via msg pack workflow asynchronously. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. * @param next Provides the next value used by this operation. */ checkMaxRequestPerSecondPerIpViaMsgPack(req: express.Request, res: express.Response, next: Function): Promise<any>; /** * Returns the return code for max request per second per IP. * @param req Provides the req value used by this operation. * @returns Returns the return code for max request per second per IP. */ private getReturnCodeForMaxRequestPerSecondPerIp; /** * Executes the check max request per second per peer via JSON workflow asynchronously. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. * @param next Provides the next value used by this operation. */ checkMaxRequestPerSecondPerPeerViaJson(req: express.Request, res: express.Response, next: Function): Promise<any>; /** * Executes the check max upload request per second per peer workflow asynchronously. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. * @param next Provides the next value used by this operation. */ checkMaxUploadRequestPerSecondPerPeer(req: express.Request, res: express.Response, next: Function): Promise<any>; /** * Executes the check max request per second per peer via msg pack workflow asynchronously. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. * @param next Provides the next value used by this operation. */ checkMaxRequestPerSecondPerPeerViaMsgPack(req: express.Request, res: express.Response, next: Function): Promise<any>; /** * Returns the return code for max request per second per peer. * @param req Provides the req value used by this operation. * @returns Returns the return code for max request per second per peer. */ private getReturnCodeForMaxRequestPerSecondPerPeer; /** * Adds the pending request via JSON. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. * @param next Provides the next value used by this operation. */ addPendingRequestViaJson(req: express.Request, res: express.Response, next: Function): Promise<any>; /** * Adds the pending request via msg pack. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. * @param next Provides the next value used by this operation. */ addPendingRequestViaMsgPack(req: express.Request, res: express.Response, next: Function): Promise<any>; /** * Returns the return code for pending request. * @param req Provides the req value used by this operation. * @returns Returns the return code for pending request. */ private getReturnCodeForPendingRequest; /** * Removes the pending request. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. */ removePendingRequest(req: express.Request, res: express.Response): void; /** * Adds the pending upload request. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. * @param next Provides the next value used by this operation. */ addPendingUploadRequest(req: express.Request, res: express.Response, next: Function): Promise<any>; /** * Removes the pending upload request. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. */ removePendingUploadRequest(req: express.Request, res: express.Response): void; /** * Adds the pending request via socket. * @param socket Provides the socket value used by this operation. * @param data Provides the data value used by this operation. * @param next Provides the next value used by this operation. */ addPendingRequestViaSocket(socket: Socket, data: any, next: Function): any; /** * Removes the pending request via socket. * @param socket Provides the socket value used by this operation. * @param data Provides the data value used by this operation. */ removePendingRequestViaSocket(socket: Socket, data: any): void; /** * Adds the pending download request. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. * @param next Provides the next value used by this operation. */ addPendingDownloadRequest(req: express.Request, res: express.Response, next: Function): Promise<any>; /** * Removes the pending download request. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. */ removePendingDownloadRequest(req: express.Request, res: express.Response): void; /** * Runs the current workflow. */ run(): void; /** * Reloads blacklist/whitelist rules from MongoDB and merges them with static allow-list entries. */ private getBanDdos; /** * Drops expired HTTP-per-IP counters once they are older than one rolling window. */ private clearHttpRequestPerIPDic; /** * Drops expired HTTP-per-peer counters once they are older than one rolling window. */ private clearHttpRequestPerPeerDic; /** * Drops expired socket-per-IP counters once they are older than one rolling window. */ private clearSocketRequestPerIPDic; /** * Drops expired socket-per-peer counters once they are older than one rolling window. */ private clearSocketRequestPerPeerDic; /** * Initializes a new anti DDoS middleware instance. */ constructor(); }