Skip to content

Commit f9b6d19

Browse files
committed
Refactor getIpAddress function to prioritize original client IP. Update logic to always return the first IP from X-Forwarded-For header instead of the last, ensuring more accurate client identification.
1 parent fc8f29b commit f9b6d19

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

server/src/tracker/utils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,22 @@ export function createBasePayload(
126126

127127
// Helper function to get IP address
128128
const getIpAddress = (request: FastifyRequest): string => {
129+
// Priority 1: Cloudflare header (already validated by CF)
129130
const cfConnectingIp = request.headers["cf-connecting-ip"];
130131
if (cfConnectingIp && typeof cfConnectingIp === "string") {
131132
return cfConnectingIp.trim();
132133
}
133134

135+
// Priority 2: X-Forwarded-For - just use the first IP
134136
const forwardedFor = request.headers["x-forwarded-for"];
135137
if (forwardedFor && typeof forwardedFor === "string") {
136138
const ips = forwardedFor
137139
.split(",")
138140
.map((ip) => ip.trim())
139141
.filter(Boolean);
140142
if (ips.length > 0) {
141-
// Return rightmost IP - the last proxy before reaching our server
142-
// This is the most trustworthy IP in the chain
143-
return ips[ips.length - 1];
143+
// Always use the first IP - the original client
144+
return ips[0];
144145
}
145146
}
146147

0 commit comments

Comments
 (0)