29 lines
677 B
TypeScript
29 lines
677 B
TypeScript
import { NextResponse } from 'next/server';
|
|
|
|
export async function GET(request: Request) {
|
|
const headers = request.headers;
|
|
// 1. x-forwarded-for 또는 x-real-ip에서 IP 추출
|
|
let ip = headers.get('x-forwarded-for') || headers.get('x-real-ip') || '';
|
|
|
|
// IP가 있으면 첫 번째 IP 주소만 추출
|
|
if (ip) {
|
|
if (ip.includes(',')) {
|
|
ip = ip.split(',')[0]?.trim()||'';
|
|
} else {
|
|
ip = ip.trim();
|
|
}
|
|
}
|
|
|
|
// 3. IPv6 주소 정규화
|
|
if (ip.startsWith('::ffff:')) {
|
|
ip = ip.replace('::ffff:', '');
|
|
}
|
|
|
|
// 4. localhost IPv6를 IPv4로 변환
|
|
if (ip === '::1') {
|
|
ip = '127.0.0.1';
|
|
}
|
|
|
|
return NextResponse.json({ ip });
|
|
}
|