@senzops/apm-node 1.1.17 → 1.1.18
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/CHANGELOG.md +4 -0
- package/dist/index.global.js +1 -1
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/middleware/express.ts +2 -1
- package/src/utils/getClientIp.ts +175 -0
- package/src/wrappers/fastify.ts +2 -1
- package/src/wrappers/h3.ts +2 -1
- package/src/wrappers/next.ts +3 -2
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* getClientIp.ts
|
|
3
|
+
*
|
|
4
|
+
* Robust, proxy-aware client IP extraction.
|
|
5
|
+
*
|
|
6
|
+
* Priority order (mirrors Umami + industry best practice):
|
|
7
|
+
* 1. ENV-configured custom header (CLIENT_IP_HEADER)
|
|
8
|
+
* 2. CF-Connecting-IP (Cloudflare — single trusted IP)
|
|
9
|
+
* 3. True-Client-IP (Cloudflare Enterprise / Akamai)
|
|
10
|
+
* 4. X-Real-IP (Nginx realip module)
|
|
11
|
+
* 5. Forwarded (RFC 7239 — "for=" field)
|
|
12
|
+
* 6. X-Forwarded-For (De-facto standard — leftmost public IP)
|
|
13
|
+
* 7. req.socket.remoteAddress (Direct connection fallback)
|
|
14
|
+
*
|
|
15
|
+
* Security note: headers 2-6 can be spoofed by clients when your server is
|
|
16
|
+
* directly internet-facing. If that is a concern, restrict extraction to the
|
|
17
|
+
* header your trusted reverse-proxy injects (CLIENT_IP_HEADER or X-Real-IP).
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { isIP } from "net";
|
|
21
|
+
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
// Helpers
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
/** Strip IPv4-mapped IPv6 prefix (::ffff:1.2.3.4 → 1.2.3.4) */
|
|
27
|
+
const stripIPv6Mapped = (ip: string): string =>
|
|
28
|
+
ip.startsWith("::ffff:") ? ip.slice(7) : ip;
|
|
29
|
+
|
|
30
|
+
/** Strip optional port from an IPv4 address (1.2.3.4:5678 → 1.2.3.4). */
|
|
31
|
+
const stripIPv4Port = (ip: string): string => {
|
|
32
|
+
const lastColon = ip.lastIndexOf(":");
|
|
33
|
+
if (lastColon === -1) return ip;
|
|
34
|
+
const maybeIP = ip.slice(0, lastColon);
|
|
35
|
+
return isIP(maybeIP) === 4 ? maybeIP : ip;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** Strip brackets + optional port from an IPv6 address ([::1]:5678 → ::1). */
|
|
39
|
+
const stripIPv6Brackets = (ip: string): string => {
|
|
40
|
+
const match = ip.match(/^\[([^\]]+)\](?::\d+)?$/);
|
|
41
|
+
return match ? match[1] : ip;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** Normalise raw IP string into a clean, routable address (or null). */
|
|
45
|
+
export const normaliseIP = (raw: string | undefined | null): string | null => {
|
|
46
|
+
if (!raw) return null;
|
|
47
|
+
let ip = raw.trim();
|
|
48
|
+
if (!ip) return null;
|
|
49
|
+
|
|
50
|
+
ip = stripIPv6Brackets(ip);
|
|
51
|
+
ip = stripIPv4Port(ip);
|
|
52
|
+
ip = stripIPv6Mapped(ip);
|
|
53
|
+
|
|
54
|
+
return isIP(ip) !== 0 ? ip : null;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Returns true for IPs that will never produce a geo result:
|
|
59
|
+
* loopback, link-local, private ranges, and unspecified addresses.
|
|
60
|
+
*/
|
|
61
|
+
export const isPrivateOrLoopback = (ip: string): boolean => {
|
|
62
|
+
// IPv4 private / loopback / link-local
|
|
63
|
+
if (
|
|
64
|
+
ip === "127.0.0.1" ||
|
|
65
|
+
ip.startsWith("10.") ||
|
|
66
|
+
ip.startsWith("192.168.") ||
|
|
67
|
+
ip.startsWith("169.254.") || // link-local
|
|
68
|
+
/^172\.(1[6-9]|2\d|3[01])\./.test(ip) // 172.16–31
|
|
69
|
+
)
|
|
70
|
+
return true;
|
|
71
|
+
|
|
72
|
+
// IPv6 loopback / unspecified / link-local / unique-local
|
|
73
|
+
if (
|
|
74
|
+
ip === "::1" ||
|
|
75
|
+
ip === "::" ||
|
|
76
|
+
ip.toLowerCase().startsWith("fe80:") || // link-local
|
|
77
|
+
ip.toLowerCase().startsWith("fc") || // unique-local
|
|
78
|
+
ip.toLowerCase().startsWith("fd") // unique-local
|
|
79
|
+
)
|
|
80
|
+
return true;
|
|
81
|
+
|
|
82
|
+
return false;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// ---------------------------------------------------------------------------
|
|
86
|
+
// RFC 7239 "Forwarded" header parser
|
|
87
|
+
// e.g. Forwarded: for=192.0.2.60;proto=http, for="[2001:db8::cafe]"
|
|
88
|
+
// ---------------------------------------------------------------------------
|
|
89
|
+
const parseForwardedHeader = (header: string): string | null => {
|
|
90
|
+
const parts = header.split(",");
|
|
91
|
+
for (const part of parts) {
|
|
92
|
+
const forMatch = part.match(/for=["[]?([^\]",;>\s]+)/i);
|
|
93
|
+
if (forMatch) {
|
|
94
|
+
const ip = normaliseIP(forMatch[1]);
|
|
95
|
+
if (ip && !isPrivateOrLoopback(ip)) return ip;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// ---------------------------------------------------------------------------
|
|
102
|
+
// X-Forwarded-For parser — pick the leftmost *public* IP
|
|
103
|
+
// e.g. X-Forwarded-For: client, proxy1, proxy2
|
|
104
|
+
// ---------------------------------------------------------------------------
|
|
105
|
+
const parseXForwardedFor = (header: string): string | null => {
|
|
106
|
+
const ips = header.split(",").map((s) => s.trim());
|
|
107
|
+
for (const raw of ips) {
|
|
108
|
+
const ip = normaliseIP(raw);
|
|
109
|
+
if (ip && !isPrivateOrLoopback(ip)) return ip;
|
|
110
|
+
}
|
|
111
|
+
// If every hop is private (intranet-only setup) fall back to first valid IP
|
|
112
|
+
for (const raw of ips) {
|
|
113
|
+
const ip = normaliseIP(raw);
|
|
114
|
+
if (ip) return ip;
|
|
115
|
+
}
|
|
116
|
+
return null;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// ---------------------------------------------------------------------------
|
|
120
|
+
// Main export
|
|
121
|
+
// ---------------------------------------------------------------------------
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Extract the best-available client IP from a request.
|
|
125
|
+
*
|
|
126
|
+
* Returns `null` if no valid IP can be determined.
|
|
127
|
+
*/
|
|
128
|
+
export const getClientIp = (req: any): string | null => {
|
|
129
|
+
const h = req.headers;
|
|
130
|
+
|
|
131
|
+
// 2. Cloudflare single-IP header (most reliable when behind CF)
|
|
132
|
+
{
|
|
133
|
+
const ip = normaliseIP(h["cf-connecting-ip"] as string);
|
|
134
|
+
if (ip) return ip;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// 3. Cloudflare Enterprise / Akamai
|
|
138
|
+
{
|
|
139
|
+
const ip = normaliseIP(h["true-client-ip"] as string);
|
|
140
|
+
if (ip) return ip;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// 4. Nginx realip module (single, already-trusted IP)
|
|
144
|
+
{
|
|
145
|
+
const ip = normaliseIP(h["x-real-ip"] as string);
|
|
146
|
+
if (ip) return ip;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// 5. RFC 7239 Forwarded header
|
|
150
|
+
{
|
|
151
|
+
const fwd = h["forwarded"] as string;
|
|
152
|
+
if (fwd) {
|
|
153
|
+
const ip = parseForwardedHeader(fwd);
|
|
154
|
+
if (ip) return ip;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 6. De-facto standard XFF
|
|
159
|
+
{
|
|
160
|
+
const xff = h["x-forwarded-for"] as string;
|
|
161
|
+
if (xff) {
|
|
162
|
+
const ip = parseXForwardedFor(xff);
|
|
163
|
+
if (ip) return ip;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// 7. Direct TCP connection (local dev / no proxy)
|
|
168
|
+
{
|
|
169
|
+
const raw = req.socket?.remoteAddress;
|
|
170
|
+
const ip = normaliseIP(raw);
|
|
171
|
+
if (ip) return ip;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return null;
|
|
175
|
+
};
|
package/src/wrappers/fastify.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { client } from '../core/client';
|
|
2
2
|
import { SenzorOptions } from '../core/types';
|
|
3
|
+
import { getClientIp } from '../utils/getClientIp';
|
|
3
4
|
|
|
4
5
|
export const senzorPlugin = (fastify: any, options: SenzorOptions, done: Function) => {
|
|
5
6
|
if (options && options.apiKey) {
|
|
@@ -10,7 +11,7 @@ export const senzorPlugin = (fastify: any, options: SenzorOptions, done: Functio
|
|
|
10
11
|
client.startTrace({
|
|
11
12
|
method: request.method,
|
|
12
13
|
path: request.raw.url || request.url,
|
|
13
|
-
ip: request
|
|
14
|
+
ip: getClientIp(request),
|
|
14
15
|
userAgent: request.headers['user-agent'],
|
|
15
16
|
headers: request.headers // Pass headers
|
|
16
17
|
}, () => next());
|
package/src/wrappers/h3.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { client } from '../core/client';
|
|
2
2
|
import { getRoute } from '../core/normalizer';
|
|
3
|
+
import { getClientIp } from '../utils/getClientIp';
|
|
3
4
|
|
|
4
5
|
type EventHandler = (event: any) => any;
|
|
5
6
|
|
|
@@ -11,7 +12,7 @@ export const wrapH3 = (handler: EventHandler) => {
|
|
|
11
12
|
return client.startTrace({
|
|
12
13
|
method: req.method || 'GET',
|
|
13
14
|
path: path,
|
|
14
|
-
ip: req
|
|
15
|
+
ip: getClientIp(req),
|
|
15
16
|
userAgent: req.headers['user-agent'],
|
|
16
17
|
headers: req.headers // Pass headers
|
|
17
18
|
}, async () => {
|
package/src/wrappers/next.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { client } from '../core/client';
|
|
2
2
|
import { normalizePath } from '../core/normalizer';
|
|
3
|
+
import { getClientIp } from '../utils/getClientIp';
|
|
3
4
|
|
|
4
5
|
// --- App Router Wrapper ---
|
|
5
6
|
export const wrapNextRoute = (handler: Function) => {
|
|
@@ -34,7 +35,7 @@ export const wrapNextRoute = (handler: Function) => {
|
|
|
34
35
|
method,
|
|
35
36
|
path: url.pathname,
|
|
36
37
|
userAgent: ua,
|
|
37
|
-
ip: ip,
|
|
38
|
+
ip: ip || getClientIp(req),
|
|
38
39
|
headers: headers // Pass extracted headers
|
|
39
40
|
}, async () => {
|
|
40
41
|
try {
|
|
@@ -61,7 +62,7 @@ export const wrapNextPages = (handler: Function) => {
|
|
|
61
62
|
method: req.method || 'GET',
|
|
62
63
|
path: path,
|
|
63
64
|
userAgent: req.headers['user-agent'],
|
|
64
|
-
ip: req
|
|
65
|
+
ip: getClientIp(req),
|
|
65
66
|
headers: req.headers // Standard Node headers work fine
|
|
66
67
|
}, async () => {
|
|
67
68
|
|