추가
This commit is contained in:
33
backend/utils/ip.ts
Normal file
33
backend/utils/ip.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import type { H3Event } from 'h3'
|
||||
|
||||
/**
|
||||
* 클라이언트 IP 주소 가져오기
|
||||
*/
|
||||
export function getClientIp(event: H3Event): string {
|
||||
// 프록시/로드밸런서 뒤에 있을 경우
|
||||
const xForwardedFor = getHeader(event, 'x-forwarded-for')
|
||||
if (xForwardedFor) {
|
||||
return xForwardedFor.split(',')[0].trim()
|
||||
}
|
||||
|
||||
const xRealIp = getHeader(event, 'x-real-ip')
|
||||
if (xRealIp) {
|
||||
return xRealIp
|
||||
}
|
||||
|
||||
// 직접 연결
|
||||
const remoteAddress = event.node.req.socket?.remoteAddress
|
||||
if (remoteAddress) {
|
||||
// IPv6 localhost를 IPv4로 변환
|
||||
if (remoteAddress === '::1' || remoteAddress === '::ffff:127.0.0.1') {
|
||||
return '127.0.0.1'
|
||||
}
|
||||
// IPv6 매핑된 IPv4 주소 처리
|
||||
if (remoteAddress.startsWith('::ffff:')) {
|
||||
return remoteAddress.substring(7)
|
||||
}
|
||||
return remoteAddress
|
||||
}
|
||||
|
||||
return 'unknown'
|
||||
}
|
||||
Reference in New Issue
Block a user