The ConnInfo helper provides type definitions for accessing HTTP connection information, including remote address, port, and transport protocol details. This is useful for logging, security checks, and network debugging.
type NetAddrInfo = { /** * Transport protocol type */ transport?: 'tcp' | 'udp' /** * Transport port number */ port?: number /** * IP address or hostname */ address?: string /** * Address type (IPv4 or IPv6) */ addressType?: AddressType} & ( | { address: string addressType: AddressType } | {})
import { getConnInfo } from 'hono/cloudflare-workers'app.get('/localized', async (c) => { const info = getConnInfo(c) const ip = info.remote.address // Use IP geolocation service const region = await getRegionFromIP(ip) return c.json({ ip, region, message: getLocalizedMessage(region) })})
When your application is behind a proxy or load balancer, the remote address will be the proxy’s IP. Use headers like X-Forwarded-For for the actual client IP:
import { getConnInfo } from 'hono/cloudflare-workers'app.get('/real-ip', (c) => { // Try X-Forwarded-For first (from proxy) const forwardedFor = c.req.header('X-Forwarded-For') if (forwardedFor) { const clientIP = forwardedFor.split(',')[0].trim() return c.json({ ip: clientIP, source: 'X-Forwarded-For' }) } // Fall back to direct connection info const info = getConnInfo(c) return c.json({ ip: info.remote.address, source: 'direct' })})
Security: Only trust X-Forwarded-For and similar headers when you control the proxy. These headers can be spoofed by clients if not properly configured.