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 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-tcp-proxy

v0.0.28

Published

A simple TCP proxy built using Node.js

Downloads

4,649

Readme

node-tcp-proxy Build Status Codacy Badge Maintainability

A classical TCP proxy that may be used to access a service on another network. An extensible replacement for socat when used thus

socat TCP-LISTEN:port,fork TCP:host:port

port is where socat listens for incoming requests. host:port are the host and port where the actual service is listening at.

To achieve the same with node-tcp-proxy

tcpproxy  --proxyPort port [--hostname <name or IP>] --serviceHost host1,host2 --servicePort port1,port2 [--q] [--tls [both]] [--pfx file] [--passphrase secret]

Optionally, use --hostname to specify host or IP address to listen at. Node.js listens on unspecified IPv6 address :: by default. If --serviceHost and --servicePort specify a comma separated list, the proxy will perform load balancing on a round-robin basis.

TLS can be enabled at the proxy port using --tls. Use --pfx followed by path to specify server certificate, and --passphrase to provide the password required to access it. Use --tls both, to also enable TLS with the service.

npm

Install node-tcp-proxy using npm

sudo npm install -g node-tcp-proxy

Programming Interface

To create a proxy in your own code

var proxy = require("node-tcp-proxy");
var newProxy = proxy.createProxy(8080, "host", 10080);

To end the proxy

newProxy.end();

hostname can be provided through an optional fourth parameter e.g. {hostname: 0.0.0.0} to createProxy. Console output may be silenced by adding quiet: true e.g. {hostname: 0.0.0.0, quiet: true}.

If you specify more than one service host and port pair, the proxy will perform round-robin load balancing

var hosts = ["host1", "host2"];
var ports = [10080, 10080];
var newProxy = proxy.createProxy(8080, hosts, ports);
// or var newProxy = proxy.createProxy(8080, "host1,host2", "10080,10080");

You can intercept and modify data sent in either direction, and modify the service host selection strategy

var proxy = require("node-tcp-proxy");
var util = require("util");
var serviceHosts = ["www.google.com", "www.bing.com"];
var servicePorts = [80, 80];
var newProxy = proxy.createProxy(8080, serviceHosts, servicePorts, {
    upstream: function(context, data) {
        console.log(util.format("Client %s:%s sent:",
            context.proxySocket.remoteAddress,
            context.proxySocket.remotePort));
        // do something with the data and return modified data
        return data;
    },
    downstream: function(context, data) {
        console.log(util.format("Service %s:%s sent:",
            context.serviceSocket.remoteAddress,
            context.serviceSocket.remotePort));
        // do something with the data and return modified data
        return data;
    },
    serviceHostSelected: function(proxySocket, i) {
        console.log(util.format("Service host %s:%s selected for client %s:%s.",
            serviceHosts[i],
            servicePorts[i],
            proxySocket.remoteAddress,
            proxySocket.remotePort));
        // use your own strategy to calculate i
        return i;
    }
});

Alternatives

You may want to check out these interesting alternatives

  • http-proxy - programmable proxying library that supports websockets. It is suitable for implementing components such as reverse proxies and load balancers.

  • sslh - accepts connections on specified ports, and forwards them further based on tests performed on the first data packet sent by the remote client.

  • socat - socat is a relay for bidirectional data transfer between two independent data channels. Each of these data channels may be a file, pipe, device (serial line etc. or a pseudo terminal), a socket (UNIX, IP4, IP6 - raw, UDP, TCP), an SSL socket, proxy CONNECT connection, a file descriptor (stdin etc.), the GNU line editor (readline), a program, or a combination of two of these.