Repository navigation
Expand file tree
/
Copy pathproxy.ts
More file actions
57 lines (49 loc) · 1.71 KB
/
Copy pathproxy.ts
File metadata and controls
57 lines (49 loc) · 1.71 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
45
46
47
48
49
50
51
52
53
54
55
56
57
import { NextRequest, NextResponse } from "next/server";
export const config = {
matcher:
"/((?!api|_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp|ico|css|js)).*)",
};
export async function proxy(req: NextRequest) {
const { searchParams, pathname } = req.nextUrl;
const ref = searchParams.get("ref");
if (ref) {
const webhookUrl = process.env.REF_TRACKING_WEBHOOK_URL;
const webhookSecret = process.env.WEBHOOK_SECRET || "";
if (!webhookUrl) {
console.warn(
"[MUNSoC Proxy] REF_TRACKING_WEBHOOK_URL is not set, skipping tracking.",
);
return NextResponse.next();
}
const ip =
req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ||
req.headers.get("x-real-ip") ||
"unknown";
const payload = {
action: "track",
ref,
secret: webhookSecret,
url: pathname + req.nextUrl.search,
ip,
timestamp: new Date().toISOString(),
user_agent: req.headers.get("user-agent") || "",
referer: req.headers.get("referer") || "",
accept_language: req.headers.get("accept-language") || "",
country: req.headers.get("x-vercel-ip-country") || "",
city: req.headers.get("x-vercel-ip-city") || "",
region: req.headers.get("x-vercel-ip-country-region") || "",
};
try {
await fetch(webhookUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
signal: AbortSignal.timeout(4000),
});
console.log(`[MUNSoC Proxy] Referral track dispatched for ref: ${ref}`);
} catch (err) {
console.error("[MUNSoC Proxy] Tracking fetch failed:", err);
}
}
return NextResponse.next();
}