mktcms 0.3.12 → 0.3.13
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.
package/dist/module.json
CHANGED
|
@@ -1,14 +1,25 @@
|
|
|
1
1
|
import { defineEventHandler, getRequestIP } from "h3";
|
|
2
|
+
import { isIP } from "node:net";
|
|
2
3
|
import { incrementTrafficRequests, trackUniqueIp } from "../../utils/trafficMetrics.js";
|
|
3
|
-
function normalizeIp(ip) {
|
|
4
|
+
export function normalizeIp(ip) {
|
|
4
5
|
if (!ip) {
|
|
5
6
|
return void 0;
|
|
6
7
|
}
|
|
7
|
-
const
|
|
8
|
+
const candidate = ip.split(",")[0]?.trim();
|
|
9
|
+
if (!candidate) {
|
|
10
|
+
return void 0;
|
|
11
|
+
}
|
|
12
|
+
let normalizedIp = candidate;
|
|
8
13
|
if (normalizedIp.startsWith("::ffff:")) {
|
|
9
|
-
|
|
14
|
+
normalizedIp = normalizedIp.slice(7);
|
|
15
|
+
}
|
|
16
|
+
if (normalizedIp.length > 64) {
|
|
17
|
+
return void 0;
|
|
18
|
+
}
|
|
19
|
+
if (isIP(normalizedIp) === 0) {
|
|
20
|
+
return void 0;
|
|
10
21
|
}
|
|
11
|
-
return normalizedIp;
|
|
22
|
+
return normalizedIp.toLowerCase();
|
|
12
23
|
}
|
|
13
24
|
export default defineEventHandler((event) => {
|
|
14
25
|
const userAgent = event.node.req.headers["user-agent"];
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export declare function incrementTrafficRequests(): void;
|
|
2
2
|
export declare function trackUniqueIp(ip?: string): void;
|
|
3
|
+
export declare function resetTrafficMetrics(): void;
|
|
3
4
|
export declare function getTrafficMetrics(): {
|
|
4
5
|
requestsTotal: number;
|
|
5
6
|
uniqueIpsTotal: number;
|
|
6
7
|
};
|
|
8
|
+
export declare function getTrackedIpCount(): number;
|
|
@@ -1,23 +1,40 @@
|
|
|
1
1
|
const MAX_STORED_IPS = 1e4;
|
|
2
|
+
const MAX_IP_LENGTH = 64;
|
|
2
3
|
let requestsTotal = 0;
|
|
3
4
|
let uniqueIpsTotal = 0;
|
|
4
|
-
const seenIps = /* @__PURE__ */ new
|
|
5
|
+
const seenIps = /* @__PURE__ */ new Map();
|
|
5
6
|
export function incrementTrafficRequests() {
|
|
6
7
|
requestsTotal += 1;
|
|
7
8
|
}
|
|
8
9
|
export function trackUniqueIp(ip) {
|
|
9
|
-
if (!ip ||
|
|
10
|
+
if (!ip || ip.length > MAX_IP_LENGTH) {
|
|
10
11
|
return;
|
|
11
12
|
}
|
|
12
|
-
if (seenIps.
|
|
13
|
+
if (seenIps.has(ip)) {
|
|
14
|
+
seenIps.delete(ip);
|
|
15
|
+
seenIps.set(ip, true);
|
|
13
16
|
return;
|
|
14
17
|
}
|
|
15
|
-
seenIps.
|
|
18
|
+
if (seenIps.size >= MAX_STORED_IPS) {
|
|
19
|
+
const oldestIp = seenIps.keys().next().value;
|
|
20
|
+
if (oldestIp) {
|
|
21
|
+
seenIps.delete(oldestIp);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
seenIps.set(ip, true);
|
|
16
25
|
uniqueIpsTotal += 1;
|
|
17
26
|
}
|
|
27
|
+
export function resetTrafficMetrics() {
|
|
28
|
+
requestsTotal = 0;
|
|
29
|
+
uniqueIpsTotal = 0;
|
|
30
|
+
seenIps.clear();
|
|
31
|
+
}
|
|
18
32
|
export function getTrafficMetrics() {
|
|
19
33
|
return {
|
|
20
34
|
requestsTotal,
|
|
21
35
|
uniqueIpsTotal
|
|
22
36
|
};
|
|
23
37
|
}
|
|
38
|
+
export function getTrackedIpCount() {
|
|
39
|
+
return seenIps.size;
|
|
40
|
+
}
|