@riocrypto/common-server 1.0.2278 → 1.0.2279
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -25,8 +25,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.createDynamicRateLimiter = void 0;
|
|
27
27
|
const express_rate_limit_1 = __importDefault(require("express-rate-limit"));
|
|
28
|
-
// Removed path-to-regexp import
|
|
29
|
-
// Adjust the import path according to your project structure
|
|
30
28
|
const custom_rate_limit_1 = require("../models/custom-rate-limit");
|
|
31
29
|
// Cache for rate limiter instances
|
|
32
30
|
const limiterInstanceCache = new Map();
|
|
@@ -86,15 +84,30 @@ function createDynamicRateLimiter(options) {
|
|
|
86
84
|
(customIpRulesCache.size === 0 && lastCacheRefreshTime === 0)) {
|
|
87
85
|
yield refreshCustomIpRulesCache(CustomRateLimit, req.path);
|
|
88
86
|
}
|
|
89
|
-
|
|
87
|
+
// Determine the client's IP address, prioritizing Cloudflare/proxy headers
|
|
88
|
+
let rawClientIp;
|
|
89
|
+
const cfConnectingIp = req.headers["cf-connecting-ip"];
|
|
90
|
+
const xForwardedForHeader = req.headers["x-forwarded-for"];
|
|
91
|
+
if (cfConnectingIp) {
|
|
92
|
+
rawClientIp = cfConnectingIp;
|
|
93
|
+
}
|
|
94
|
+
else if (xForwardedForHeader) {
|
|
95
|
+
// X-Forwarded-For can be a comma-separated list (client, proxy1, proxy2)
|
|
96
|
+
// The first IP is the original client IP
|
|
97
|
+
rawClientIp = xForwardedForHeader.split(",")[0].trim();
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
rawClientIp = req.ip; // Fallback to Express's req.ip
|
|
101
|
+
}
|
|
90
102
|
let normalizedIp;
|
|
91
|
-
if (
|
|
92
|
-
// Normalize IPv4-mapped IPv6 addresses (::ffff:
|
|
93
|
-
|
|
94
|
-
|
|
103
|
+
if (rawClientIp) {
|
|
104
|
+
// Normalize IPv4-mapped IPv6 addresses (e.g., ::ffff:192.0.2.1 -> 192.0.2.1)
|
|
105
|
+
// Also handles direct IPv4 and standard IPv6 addresses
|
|
106
|
+
if (rawClientIp.startsWith("::ffff:")) {
|
|
107
|
+
normalizedIp = rawClientIp.substring(7);
|
|
95
108
|
}
|
|
96
109
|
else {
|
|
97
|
-
normalizedIp =
|
|
110
|
+
normalizedIp = rawClientIp;
|
|
98
111
|
}
|
|
99
112
|
}
|
|
100
113
|
let currentLimit = defaultMax;
|