Introduction

Learn about Network module for secure client IP extraction with proxy support.

What is Network?

The Network module provides secure utilities for extracting the real client IP address from HTTP requests, with built-in support for popular reverse proxies and CDNs like Cloudflare, AWS, Vercel, and more.

Why is this important?

When your application is behind a proxy or CDN, the direct connection IP (req.socket.remoteAddress) will be the proxy's IP, not the client's. Headers like X-Forwarded-For can be spoofed by malicious clients, making IP extraction a security concern.

The Network module solves this by:

  • Prioritizing proprietary headers (like CF-Connecting-IP for Cloudflare) that cannot be spoofed
  • Safely parsing X-Forwarded-For from right to left, trusting only configured proxies
  • Providing utilities to validate and normalize IP addresses

Quick Example

import { Network } from "toolkitify/network";

// Create a network instance for your proxy.
const network = new Network({ proxy: "cloudflare" });

// In your Express middleware.
app.use((req, res, next) => {
    const { ip, trusted } = network.getClientIP(req);
    console.log(`Client IP: ${ip}, Trusted: ${trusted}`);
    next();
});

Supported Proxies

ProxyHeaders Used
cloudflarecf-connecting-ip, cf-pseudo-ipv4
awsx-amzn-source-ip, x-amz-cf-id
vercelx-vercel-forwarded-for, x-real-ip
fastlyfastly-client-ip
akamaitrue-client-ip, akamai-origin-hop
nginxx-real-ip
gcpx-cloud-trace-context, x-appengine-user-ip
azurex-azure-clientip, x-client-ip
flyfly-client-ip
renderx-render-origin-ip
railwayx-railway-client-ip
herokux-forwarded-for