npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

socks-ssh

v0.0.1

Published

Auto-establishing SSH SOCKS5 tunnel with selective fetch proxy routing for Node.js

Readme

socks-ssh

A reusable Node.js utility package that auto-establishes a SOCKS5 tunnel over SSH and selectively routes specific outbound fetch requests through it.

This is particularly useful when developing locally on a dynamic IP but interacting with third-party APIs (e.g., TerraPay) that require IP whitelisting.


Features

  • Auto-Establishing SSH Tunnel: Automatically spawns and maintains the ssh -D <port> -N -C ... process when initialized.
  • Selective Proxy Routing: Dynamically intercepts globalThis.fetch to route specific target hosts (e.g. *.terrapay.com) through the SOCKS tunnel, while sending all other requests directly.
  • Redacted Request Headers: Logs headers for routed requests with sensitive headers (like API keys, passwords, authorizations) automatically redacted.
  • Response Body Logs: Logs target server responses and truncates/prints body content for dev/debugging clarity.
  • Self-Cleaning: Automatically kills the spawned SSH child process when the parent Node.js application process terminates.

Installation

npm install socks-ssh

All required dependencies (fetch-socks, undici, socks, dotenv) are installed automatically — no extra setup needed.


Configuration

Set up these variables in your target project's .env file (see .env.example):

# Option 1: Full SSH connect string
[email protected]

# Option 2: Split parameters
# SSH_USER=root
# SSH_HOST=128.33.453.3
# SSH_PORT=22
# SSH_KEY=~/.ssh/id_rsa

# Port SOCKS5 listens on (maps to ssh -D <port>)
SOCKS_PORT=1080

# Comma-separated list of host suffixes to route through the tunnel
SOCKS_PROXY_HOSTS=terrapay.com,routefusion.com

# Production hardening (recommended):
# Cap/disable response body logging (default: 2000 chars; 0 = unlimited, not recommended)
SOCKS_PROXY_BODY_MAX=2000
# Pin the remote host key instead of trust-on-first-connect (accept-new)
SSH_KNOWN_HOSTS_FILE=/path/to/known_hosts
SSH_STRICT_HOST_KEY_CHECKING=yes

Usage

Option A: Preload at Startup (Recommended)

Launch your Node.js application (like Next.js or a backend server) using the --import flag. This automatically configures the proxy without needing any code changes inside your project:

{
  "scripts": {
    "dev:proxy": "NODE_OPTIONS='--import socks-ssh/register' next dev"
  }
}

Option B: Programmatic Initialization

If you want to initialize it conditionally inside your code (e.g., in server.ts or index.js):

import { init } from 'socks-ssh';

if (process.env.NODE_ENV === 'development') {
  // init() returns a Promise that resolves when the tunnel and local proxies are fully ready.
  await init();
}

Runtime Compatibility

socks-ssh supports both Node.js and Bun environments transparently:

  • Node.js (18+): Configures a custom undici Agent dispatcher to selectively route requests through the SOCKS5 proxy.
  • Bun: Since Bun's native HTTP client does not support SOCKS5 natively and intercepts Undici, socks-ssh automatically spins up a local HTTP-to-SOCKS5 forwarding CONNECT proxy and redirects matching fetch calls through it using Bun's native proxy option. No manual proxy setup or background commands are required.