@imqueue/http-protect
Advanced tools
Comparing version
{ | ||
"name": "@imqueue/http-protect", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "HTTP DDoS Protection Middleware", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -102,2 +102,10 @@ /*! | ||
banLimit?: number; | ||
/** | ||
* List of CIDR networks which should be considered as safe and should not | ||
* be limited or banned. By default, it is empty. You may specify your own | ||
* list of CIDR records which should be considered as safe and should not | ||
* be limited or banned. This may be useful for your own services which | ||
* should not be limited or banned by this module. | ||
*/ | ||
safeNetworks?: string[]; | ||
} | ||
@@ -138,2 +146,3 @@ export interface Response { | ||
readonly blockListKey: string; | ||
readonly safeNetworks: Networks; | ||
constructor(options?: HttpProtectOptions | undefined); | ||
@@ -149,2 +158,3 @@ connect(options?: RedisOptions): Redis; | ||
destroy(): void; | ||
private isSafeIp; | ||
} |
@@ -47,2 +47,3 @@ "use strict"; | ||
blockListKey; | ||
safeNetworks; | ||
constructor(options) { | ||
@@ -58,2 +59,3 @@ this.options = options; | ||
this.blockListKey = `${this.prefix}:block-list`; | ||
this.safeNetworks = new net_1.Networks(this.options?.safeNetworks || []); | ||
} | ||
@@ -68,6 +70,12 @@ connect(options) { | ||
async verify(req) { | ||
const ip = (0, request_ip_1.getClientIp)(req) || ''; | ||
if (this.isSafeIp(ip)) { | ||
return { | ||
status: VerificationStatus.SAFE, | ||
httpCode: 200, | ||
}; | ||
} | ||
if (!this.redis) { | ||
throw new Error('Redis connection is not established!'); | ||
} | ||
const ip = (0, request_ip_1.getClientIp)(req) || ''; | ||
const key = `${this.prefix}:${ip}`; | ||
@@ -174,4 +182,7 @@ if (await this.redis?.sismember(this.blockListKey, ip)) { | ||
} | ||
isSafeIp(ip) { | ||
return this.safeNetworks.includes(ip); | ||
} | ||
} | ||
exports.default = HttpProtect; | ||
//# sourceMappingURL=index.js.map |
19887
4.2%340
6.58%