Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions apps/web/__tests__/unit/analytics-track-route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => ({
Expand Down Expand Up @@ -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");
Expand Down
7 changes: 5 additions & 2 deletions apps/web/app/api/analytics/track/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion apps/web/app/api/mobile/[...route]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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: "",
Expand Down
Loading