Skip to content

Commit

Permalink
x-forwarded-for 헤더를 통해 클라이언트 IP를 가져오는 RealIP 데코레이터 추가 (12.06)
Browse files Browse the repository at this point in the history
x-forwarded-for 헤더를 통해 클라이언트 IP를 가져오는 RealIP 데코레이터 추가 (12.06)
  • Loading branch information
seoko97 committed Dec 5, 2023
2 parents aeccc69 + 7aa0715 commit 2200c8c
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/common/decorators/real-ip.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { createParamDecorator } from "@nestjs/common";

const RealIp = createParamDecorator((_, ctx) => {
const req = ctx.switchToHttp().getRequest();
const ip =
(req.ips.length ? req.ips[0] : req.ip) ||
req.headers["x-forwarded-for"] ||
req.headers["x-real-ip"] ||
"0.0.0.0";

const forwarded = req.headers["x-forwarded-for"]?.split(", ");

const ip = forwarded?.[0] || req.ip || req.header("x-real-ip") || "0.0.0.0";

return ip;
});
Expand Down

0 comments on commit 2200c8c

Please sign in to comment.