Skip to content

Commit

Permalink
trust proxy를 통한 real ip 추출 (12.05)
Browse files Browse the repository at this point in the history
`trust proxy`를 통한 real ip 추출 (12.05)
  • Loading branch information
seoko97 authored Dec 5, 2023
2 parents 6f40e22 + a8d58dd commit aeccc69
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
10 changes: 0 additions & 10 deletions src/common/decorators/RealIp.decorator.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/common/decorators/real-ip.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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";

return ip;
});

export { RealIp };
5 changes: 4 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { ValidationPipe } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { NestFactory } from "@nestjs/core";
import { NestExpressApplication } from "@nestjs/platform-express";

import * as cookieParser from "cookie-parser";

import { AppModule } from "@/app.module";

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const app = await NestFactory.create<NestExpressApplication>(AppModule);

app.setGlobalPrefix("api");

Expand All @@ -16,6 +17,8 @@ async function bootstrap() {
const PORT = configService.get("PORT");
const HOST = configService.get("HOST");

app.set("trust proxy", 1);

app.use(cookieParser());
app.enableCors({
origin: HOST,
Expand Down
2 changes: 1 addition & 1 deletion src/routes/post/post.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Body, Controller, Delete, Get, Param, Patch, Post, Put, Query } from "@nestjs/common";

import { Public } from "@/common/decorators";
import { RealIp } from "@/common/decorators/RealIp.decorator";
import { RealIp } from "@/common/decorators/real-ip.decorator";
import { CreatePostDto } from "@/routes/post/dto/create-post.dto";
import { GetPostsDto } from "@/routes/post/dto/get-posts.dto";
import { UpdatePostDto } from "@/routes/post/dto/update-post.dto";
Expand Down

0 comments on commit aeccc69

Please sign in to comment.