-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
과도한 요청 방지 기능 추가
- Loading branch information
Showing
5 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// throttler-behind-proxy.guard.ts | ||
// 고스락 백엔드 서버는 nginx 뒤에 프록시 형태로 연결되어있기 때문에 | ||
// X-Forwarded-For 헤더값을 통해서 | ||
// 요청한 사람의 원래 ip 주소를 가져와야합니다. | ||
import { ThrottlerGuard } from '@nestjs/throttler'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { Request } from 'express'; | ||
import { v4 } from 'uuid'; | ||
|
||
@Injectable() | ||
export class ThrottlerBehindProxyGuard extends ThrottlerGuard { | ||
protected getTracker(req: Request): string { | ||
if (process.env.NODE_ENV === 'prod') { | ||
const clientProxyIps = req.headers['x-forwarded-for']; | ||
if (!clientProxyIps) { | ||
return v4(); | ||
} | ||
if (Array.isArray(clientProxyIps)) { | ||
return clientProxyIps[0]; | ||
} else { | ||
return clientProxyIps; | ||
} | ||
} else { | ||
return req.ips.length ? req.ips[0] : req.ip; // individualize IP extraction to meet your own needs | ||
} | ||
} | ||
} | ||
|
||
// app.controller.ts |