$ cat docs/middleware/client-ip.md
Client IPIP do cliente
Decide once where the client IP comes from, so every middleware agrees on the same value.Decida uma vez de onde vem o IP do cliente, para todos os middlewares usarem o mesmo valor.
What it doesO que faz
clientIp(options: ClientIpOptions): MiddlewareHandler. It applies resolveClientIp, the same rule as rateLimit: trust: 'cloudflare' reads only cf-connecting-ip; trust: 'proxy' reads x-forwarded-for from the right, skipping your own trustedHops. getClientIp(ctx) returns the value (or undefined); logger adds it masked, audit and captcha use it, and rateLimit({ keyGenerator: getClientIp }) counts by it.
clientIp(options: ClientIpOptions): MiddlewareHandler. Ele aplica o resolveClientIp, a mesma regra do rateLimit: trust: 'cloudflare' lê só cf-connecting-ip; trust: 'proxy' lê x-forwarded-for pela direita, pulando os seus trustedHops. getClientIp(ctx) devolve o valor (ou undefined); o logger o inclui mascarado, audit e captcha o usam, e rateLimit({ keyGenerator: getClientIp }) conta por ele.
Install and orderInstalar e ordenar
pnpm add @tupik/middleware Register it first, with app.use, so every later middleware sees the IP.
Registre primeiro, com app.use, para todos os middlewares seguintes verem o IP.
ExampleExemplo
import { createApp } from '@tupik/core';
import { clientIp, getClientIp, logger, rateLimit } from '@tupik/middleware';
const app = createApp()
.use(logger()) // logs ip masked (/24, /48)
.use(clientIp({ trust: 'cloudflare' }))
.use(rateLimit({ windowMs: 60_000, limit: 100, keyGenerator: getClientIp }));import { createApp } from '@tupik/core';
import { clientIp, getClientIp, logger, rateLimit } from '@tupik/middleware';
const app = createApp()
.use(logger()) // loga o ip mascarado (/24, /48)
.use(clientIp({ trust: 'cloudflare' }))
.use(rateLimit({ windowMs: 60_000, limit: 100, keyGenerator: getClientIp })); APIAPI
| OptionOpção | TypeTipo | Default / behaviorPadrão / comportamento |
|---|---|---|
trust | 'cloudflare' | 'proxy' | Required. Which layer sets the IP header.Obrigatório. Qual camada define o header de IP. |
trustedHops | number | 1, only with trust: 'proxy'. Proxies you control in front of the app.1, só com trust: 'proxy'. Proxies seus na frente do app. |
interface ClientIpOptions { trust: 'cloudflare' | 'proxy'; trustedHops? }
interface ClientIpOptions { trust: 'cloudflare' | 'proxy'; trustedHops? }
Errors and limitsErros e limites
- A missing or unknown
trust, ortrustedHopsthat is not an integer ≥ 1 withproxy, throws at creation. - No trusted header:
getClientIp(ctx)isundefined. It never falls back to another header.
trustausente ou desconhecido, outrustedHopsque não seja inteiro ≥ 1 comproxy, lança na criação.- Sem header confiável:
getClientIp(ctx)éundefined. Nunca cai para outro header.
When not to use itQuando não usar
If the app is reachable without your proxy or Cloudflare, any client can forge these headers: lock the origin first. The IP is personal data; it never identifies a user alone.
Se o app for alcançável sem o seu proxy ou a Cloudflare, qualquer cliente forja esses headers: tranque a origem antes. O IP é dado pessoal; nunca identifica um usuário sozinho.