Global HTTP & HTTPS Tunneling

2.7.1 · maintenance · verified Tue Apr 21

global-tunnel-ng is a utility that globally configures Node.js's built-in `http` and `https` agents to route all outgoing network traffic through an upstream HTTP proxy. It works transparently for modules that utilize Node's default `http.request()` method and popular libraries like `request`. The current stable version is 2.7.1, published in November 2018. The package focuses on transparent global proxying, allowing developers to set a proxy once for an entire application without modifying individual HTTP client calls. It supports both `CONNECT` tunneling for HTTPS (and optionally HTTP) and absolute-URI request methods, accommodating various proxy configurations. Its primary differentiator is its low-level global interception of Node.js's native networking stack. For modern Node.js versions (v10+), `global-agent` is often recommended as a more actively maintained alternative.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes a global HTTP/HTTPS proxy using `global-tunnel-ng` and demonstrates how a standard Node.js `http.get` request automatically routes through it. It also shows how to tear down the proxy.

const globalTunnel = require('global-tunnel-ng');

// Configure a global HTTP/HTTPS proxy
globalTunnel.initialize({
  host: '10.0.0.10',
  port: 8080,
  proxyAuth: process.env.PROXY_USER_PASS ?? '', // optional authentication as 'userId:password'
  sockets: 50, // optional pool size for each http and https agent
  // For a proxy requiring absolute URIs for HTTP/HTTPS:
  // connect: 'neither',
  // For a proxy requiring CONNECT for both HTTP/HTTPS:
  // connect: 'both',
  // If the proxy itself speaks HTTPS:
  // protocol: 'https:'
});

// Example of an HTTP request that will go through the configured proxy
const http = require('http');

http.get('http://example.com/data', (res) => {
  console.log(`Status: ${res.statusCode}`);
  res.setEncoding('utf8');
  let rawData = '';
  res.on('data', (chunk) => { rawData += chunk; });
  res.on('end', () => {
    try {
      console.log('Received data:', rawData.substring(0, 100) + '...');
    } catch (e) {
      console.error(e.message);
    }
    globalTunnel.end(); // Clean up the proxy configuration
  });
}).on('error', (e) => {
  console.error(`Request error: ${e.message}`);
  globalTunnel.end();
});

view raw JSON →