@liberstudio/cloudflare-list 2.0.18 → 2.2.0

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.
@@ -13,7 +13,6 @@ export declare class AttackLoggerMiddleware implements NestMiddleware, OnModuleD
13
13
  use(req: Request, res: Response, next: NextFunction): void;
14
14
  private handleSuspicious;
15
15
  private isThrottled;
16
- private getClientIp;
17
16
  private sanitize;
18
17
  private ensureDirectoryExists;
19
18
  onModuleDestroy(): void;
@@ -49,7 +49,7 @@ let AttackLoggerMiddleware = AttackLoggerMiddleware_1 = class AttackLoggerMiddle
49
49
  next();
50
50
  }
51
51
  handleSuspicious(req) {
52
- const ip = this.getClientIp(req);
52
+ const ip = (0, utils_1.getClientIp)(req);
53
53
  if (!ip || this.isThrottled(ip))
54
54
  return;
55
55
  const entry = {
@@ -65,9 +65,13 @@ let AttackLoggerMiddleware = AttackLoggerMiddleware_1 = class AttackLoggerMiddle
65
65
  this.logger.debug("Stream del log degli attacchi svuotato");
66
66
  });
67
67
  }
68
- this.attSrv.updateIpList(ip).catch((err) => {
68
+ const cidr = (0, utils_1.normalizeIp)(ip);
69
+ if (!cidr) {
70
+ this.logger.warn(`IP non valido: ${ip}`);
71
+ return;
72
+ }
73
+ this.attSrv.updateIpList(cidr).catch((err) => {
69
74
  const msg = err instanceof Error ? err.message : "Errore sconosciuto";
70
- ;
71
75
  this.logger.error(`Aggiornamento Cloudflare fallito: ${msg}`);
72
76
  });
73
77
  }
@@ -80,19 +84,6 @@ let AttackLoggerMiddleware = AttackLoggerMiddleware_1 = class AttackLoggerMiddle
80
84
  this.recentIps.set(ip, timeout);
81
85
  return false;
82
86
  }
83
- getClientIp(req) {
84
- const cfIp = req.headers["cf-connecting-ip"];
85
- if (typeof cfIp === "string")
86
- return cfIp;
87
- const xRealIp = req.headers["x-real-ip"];
88
- if (typeof xRealIp === "string")
89
- return xRealIp;
90
- const xForwardedFor = req.headers["x-forwarded-for"];
91
- if (typeof xForwardedFor === "string") {
92
- return xForwardedFor.split(",")[0].trim();
93
- }
94
- return req.socket.remoteAddress ?? null;
95
- }
96
87
  sanitize(value) {
97
88
  return value.replace(/[\r\n]/g, "_");
98
89
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const node_fs_1 = require("node:fs");
4
+ const node_path_1 = require("node:path");
5
+ const pkgPath = (0, node_path_1.resolve)(__dirname, '../../package.json');
6
+ const pkg = JSON.parse((0, node_fs_1.readFileSync)(pkgPath, 'utf8'));
7
+ let [major, minor, patch] = pkg.version.split('.').map(Number);
8
+ // Incremento patch
9
+ patch += 1;
10
+ // Gestione rollover
11
+ if (patch >= 10) {
12
+ minor += Math.floor(patch / 10);
13
+ patch = patch % 10;
14
+ }
15
+ if (minor >= 10) {
16
+ major += Math.floor(minor / 10);
17
+ minor = minor % 10;
18
+ }
19
+ // Ricostruzione versione
20
+ pkg.version = `${major}.${minor}.${patch}`;
21
+ // Scrittura package.json
22
+ (0, node_fs_1.writeFileSync)(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
23
+ console.log(`Version updated to ${pkg.version}`);
@@ -1,2 +1,3 @@
1
- import { Request } from "express";
1
+ import type { Request } from "express";
2
2
  export declare function getClientIp(req: Request): string;
3
+ export declare function normalizeIp(ip: string): string | null;
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getClientIp = getClientIp;
4
+ exports.normalizeIp = normalizeIp;
5
+ const net_1 = require("net");
4
6
  function getClientIp(req) {
5
7
  const cfIp = req.headers["cf-connecting-ip"];
6
8
  if (typeof cfIp === "string")
@@ -14,3 +16,39 @@ function getClientIp(req) {
14
16
  }
15
17
  return req.socket.remoteAddress ?? "unknown";
16
18
  }
19
+ function normalizeIp(ip) {
20
+ if (!ip)
21
+ return null;
22
+ ip = ip.trim();
23
+ // IPv4-mapped IPv6 → converti in IPv4 puro
24
+ if (ip.startsWith("::ffff:")) {
25
+ ip = ip.replace("::ffff:", "");
26
+ }
27
+ const version = (0, net_1.isIP)(ip);
28
+ if (!version)
29
+ return null;
30
+ // Blocca loopback
31
+ if (ip === "127.0.0.1" || ip === "::1")
32
+ return null;
33
+ // Blocca IPv4 privati
34
+ if (version === 4) {
35
+ if (ip.startsWith("10.") || ip.startsWith("192.168.") || (ip.startsWith("172.") && isPrivate172(ip))) {
36
+ return null;
37
+ }
38
+ return `${ip}/32`;
39
+ }
40
+ // Blocca IPv6 link-local e unique local
41
+ if (version === 6) {
42
+ if (ip.startsWith("fe80:") || // link-local
43
+ ip.startsWith("fc") || // unique local
44
+ ip.startsWith("fd")) {
45
+ return null;
46
+ }
47
+ return `${ip}/128`;
48
+ }
49
+ return null;
50
+ }
51
+ function isPrivate172(ip) {
52
+ const secondOctet = Number(ip.split('.')[1]);
53
+ return secondOctet >= 16 && secondOctet <= 31;
54
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liberstudio/cloudflare-list",
3
- "version": "2.0.18",
3
+ "version": "2.2.0",
4
4
  "description": "Modulo NestJS per gestione IP List Cloudflare",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -15,14 +15,14 @@
15
15
  "scripts": {
16
16
  "build": "npx tsc",
17
17
  "prepublishOnly": "npm run build",
18
- "version:bump": "npm version patch --no-git-tag-version",
18
+ "version:bump": "npx tsx src/scripts/version-bump.ts",
19
19
  "pub": "npm publish --access public"
20
20
  },
21
21
  "publishConfig": {
22
22
  "access": "public"
23
23
  },
24
24
  "simple-git-hooks": {
25
- "pre-commit": "npm run version:bump && git add package.json package-lock.json"
25
+ "pre-commit": "source ~/.bashrc && npm run version:bump && git add package.json package-lock.json"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "@nestjs/axios": ">=3.0.0",