🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Refactor IP address retrieval in trackingUtils for improved accuracy
Bill Yang7c398e848319
diff --git a/server/src/tracker/trackingUtils.ts b/server/src/tracker/trackingUtils.tsindex 5489fe1dd..ddff0fed8 100644--- a/server/src/tracker/trackingUtils.ts+++ b/server/src/tracker/trackingUtils.ts@@ -218,18 +218,23 @@ function getUserId(// Helper function to get IP addressconst getIpAddress = (request: FastifyRequest): string => {- // Check for proxied IP addresses- const forwardedFor = request.headers["x-forwarded-for"];- if (forwardedFor && typeof forwardedFor === "string") {- return forwardedFor.split(",")[0].trim();- }-- // Check for Cloudflareconst cfConnectingIp = request.headers["cf-connecting-ip"];if (cfConnectingIp && typeof cfConnectingIp === "string") {- return cfConnectingIp;+ return cfConnectingIp.trim();+ }++ const forwardedFor = request.headers["x-forwarded-for"];+ if (forwardedFor && typeof forwardedFor === "string") {+ const ips = forwardedFor+ .split(",")+ .map((ip) => ip.trim())+ .filter(Boolean);+ if (ips.length > 0) {+ // Return rightmost IP - the last proxy before reaching our server+ // This is the most trustworthy IP in the chain+ return ips[ips.length - 1];+ }}- // Fallback to direct IPreturn request.ip;};