-
-
Notifications
You must be signed in to change notification settings - Fork 340
Expand file tree
/
Copy pathrequest.server.ts
More file actions
44 lines (39 loc) · 1.09 KB
/
request.server.ts
File metadata and controls
44 lines (39 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* Request utilities for extracting client information.
*/
/**
* Extract client IP address from request headers.
* Checks common proxy headers (x-forwarded-for, x-real-ip, cf-connecting-ip).
*
* @param request - The incoming request
* @param fallback - Value to return if no IP found (default: undefined)
*/
export function getClientIp(request: Request): string | undefined
export function getClientIp<T extends string>(
request: Request,
fallback: T,
): string
export function getClientIp(
request: Request,
fallback?: string,
): string | undefined {
const forwardedFor = request.headers.get('x-forwarded-for')
if (forwardedFor) {
return forwardedFor.split(',')[0].trim()
}
const realIp = request.headers.get('x-real-ip')
if (realIp) {
return realIp
}
const cfConnectingIp = request.headers.get('cf-connecting-ip')
if (cfConnectingIp) {
return cfConnectingIp
}
return fallback
}
/**
* Extract user agent from request headers.
*/
export function getUserAgent(request: Request): string | undefined {
return request.headers.get('user-agent') || undefined
}