This commit is contained in:
oiov
2025-05-26 20:43:56 +08:00
parent ba086b602f
commit 7bf2aa8b3c
3 changed files with 18 additions and 8 deletions
+2 -1
View File
@@ -4,9 +4,10 @@
NEXT_PUBLIC_APP_URL=http://localhost:3000
# -----------------------------------------------------------------------------
# Authentication (NextAuth.js)
# Authentication (NextAuth.js 5.0.x)
# -----------------------------------------------------------------------------
AUTH_SECRET=
AUTH_URL=http://localhost:3000/api/auth
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
+14 -6
View File
@@ -11,13 +11,14 @@ const isVercel = process.env.VERCEL;
export async function getGeolocation(
req: NextAuthRequest,
ip: string,
): Promise<GeoLocation | null> {
console.log("[Runtime Env]", isVercel ? "Vercel" : "Other");
if (isVercel) {
return geolocation(req);
} else {
return await getClientGeolocation();
return await getClientGeolocation(req, ip);
}
}
@@ -39,9 +40,16 @@ export function getUserAgent(req: NextAuthRequest) {
}
}
export async function getClientGeolocation(): Promise<GeoLocation | null> {
const response = await fetch("https://ip.wr.do/api", {
signal: AbortSignal.timeout(3000),
export async function getClientGeolocation(
req,
ip,
): Promise<GeoLocation | null> {
const new_headers = new Headers();
new_headers.set("X-Forwarded-For", ip);
new_headers.set("User-Agent", req.headers.get("user-agent") || "");
const response = await fetch(`https://ip.wr.do/api?ip=${ip}`, {
// signal: AbortSignal.timeout(3000),
headers: new_headers,
});
if (!response.ok) return null;
return await response.json();
@@ -81,9 +89,9 @@ function isValidIP(ip: string): boolean {
export async function getIpInfo(req) {
const headers = req.headers;
const geo = await getGeolocation(req);
const ip = isVercel ? ipAddress(req) : geo?.ip;
const ip = isVercel ? ipAddress(req) : extractRealIP(headers);
const ua = getUserAgent(req);
const geo = await getGeolocation(req, ip || "::1");
const userLanguage =
req.headers.get("accept-language")?.split(",")[0] || "en-US";
+2 -1
View File
@@ -29,10 +29,11 @@ async function handleShortUrl(req: NextAuthRequest) {
return NextResponse.redirect(`${siteConfig.url}/docs/short-urls`, 302);
const headers = req.headers;
const geo = await getGeolocation(req);
const ip = isVercel ? ipAddress(req) : extractRealIP(headers);
const ua = getUserAgent(req);
const geo = await getGeolocation(req, ip || "::1");
const url = new URL(req.url);
const password = url.searchParams.get("password") || "";