diff --git a/apps/web/__tests__/unit/analytics-track-route.test.ts b/apps/web/__tests__/unit/analytics-track-route.test.ts index 4b3011a5060..56909042a75 100644 --- a/apps/web/__tests__/unit/analytics-track-route.test.ts +++ b/apps/web/__tests__/unit/analytics-track-route.test.ts @@ -31,7 +31,6 @@ vi.mock("@/lib/rate-limit", () => ({ return count > REQUESTS_PER_IP_LIMIT; }, ), - clientAddress: (headers: Headers) => headers.get("x-real-ip") ?? undefined, })); vi.mock("@/lib/visitor-location", () => ({ @@ -221,7 +220,11 @@ describe("POST /api/analytics/track", () => { }); it("falls back to the GeoIP database without Vercel's headers", async () => { - await track({ videoId: VIDEO_ID }, "198.51.100.7"); + // Railway's edge appends its own address to X-Forwarded-For; X-Real-IP + // is the client. + await track({ videoId: VIDEO_ID }, "198.51.100.7", { + "x-forwarded-for": "198.51.100.7, 203.0.113.250", + }); await track({ videoId: VIDEO_ID }, "203.0.113.50"); expect(geoIpLocation).toHaveBeenCalledWith("198.51.100.7"); diff --git a/apps/web/app/api/analytics/track/route.ts b/apps/web/app/api/analytics/track/route.ts index aa455e1479e..b44130158db 100644 --- a/apps/web/app/api/analytics/track/route.ts +++ b/apps/web/app/api/analytics/track/route.ts @@ -10,11 +10,12 @@ import UAParser from "ua-parser-js"; import { z } from "zod"; import { getAnonymousName } from "@/lib/anonymous-names"; +import { getClientIp } from "@/lib/client-ip"; import { createAnonymousViewNotification, sendFirstViewEmail, } from "@/lib/Notification"; -import { clientAddress, isRateLimited, RATE_LIMIT_IDS } from "@/lib/rate-limit"; +import { isRateLimited, RATE_LIMIT_IDS } from "@/lib/rate-limit"; import { runPromise } from "@/lib/server"; import { geoIpLocation } from "@/lib/visitor-location"; @@ -96,7 +97,9 @@ export async function POST(request: NextRequest) { decodeUrlEncodedHeaderValue(request.headers.get("x-vercel-ip-city")), ) || ""; if (!country) { - const location = geoIpLocation(clientAddress(request.headers)); + const location = geoIpLocation( + getClientIp((name) => request.headers.get(name)), + ); if (location) ({ country, region, city } = location); } diff --git a/apps/web/app/api/mobile/[...route]/route.ts b/apps/web/app/api/mobile/[...route]/route.ts index d5017b1e89f..1c617832847 100644 --- a/apps/web/app/api/mobile/[...route]/route.ts +++ b/apps/web/app/api/mobile/[...route]/route.ts @@ -69,6 +69,7 @@ import { createMobileContentReport, hasPendingAccountDeletion, } from "@/lib/account-deletion-request"; +import { getClientIp } from "@/lib/client-ip"; import { isCommentRateLimited } from "@/lib/comment-rate-limit"; import { queueDesktopSegmentsFinalization } from "@/lib/desktop-segments-finalization"; import { @@ -2973,7 +2974,7 @@ const trackMobileCapView = Effect.fn("Mobile.trackCapView")(function* ( region: readHeader(request, "x-vercel-ip-country-region"), city: readHeader(request, "x-vercel-ip-city"), } - : (geoIpLocation(clientAddress(toHeaders(request.headers))) ?? { + : (geoIpLocation(getClientIp((name) => request.headers[name])) ?? { country: "", region: "", city: "",