Files
goodgo-platform/apps/api/src/modules/shared/infrastructure/guards/throttler-behind-proxy.guard.ts
Ho Ngoc Hai 6ebacbc9bf fix: apply consistent-type-imports across API codebase (728 lint errors)
- Convert `import type { X }` to `import { type X }` (inline-type-imports style)
- Suppress consistent-type-imports for `typeof import()` in instrument.ts
- Includes uncommitted agent work: metrics module, redis caching, audit logs,
  saved searches, circuit breaker, rate limiting, and admin enhancements

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-10 23:22:21 +07:00

18 lines
651 B
TypeScript

import { Injectable } from '@nestjs/common';
import { ThrottlerGuard } from '@nestjs/throttler';
import { type Request } from 'express';
/**
* Extends ThrottlerGuard to extract real client IP behind reverse proxies
* (e.g., nginx, CloudFlare, AWS ALB) using X-Forwarded-For header.
*/
@Injectable()
export class ThrottlerBehindProxyGuard extends ThrottlerGuard {
protected override getTracker(req: Request): Promise<string> {
const forwarded = req.headers['x-forwarded-for'];
const ip =
typeof forwarded === 'string' ? (forwarded.split(',')[0]?.trim() ?? '127.0.0.1') : req.ip;
return Promise.resolve(ip ?? '127.0.0.1');
}
}