A simple TCP proxy in Node.js


A simple TCP proxy that may be used to access a service on another network. It is an extensible replacement for socat when used thus

socat TCP-LISTEN:port1,fork TCP:host:port2 

To achieve the same with node-tcp-proxy

tcpproxy  --proxyPort [port1] --serviceHost [host] --servicePort [port2]

Install node-tcp-proxy from npm

sudo npm install -g node-tcp-proxy

To create a proxy in your own code

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

To end the proxy

newProxy.end();

The source code is available, and can be forked, at GitHub.

9 thoughts on “A simple TCP proxy in Node.js

  1. Hi,
    When using a code like this, I’m experiencing occasional exceptions:
    var writeReq = this._handle.write(data); TypeError: Cannot call method ‘write’ of null
    My guess is that this is because proxySocket.on(‘data’, … sometimes happens before the serviceSocket gets connected.
    I tried to solve this by wrapping all the “proxying code” into a
    serviceSocket.on(‘connect’, …
    but then sometimes I get data lost from the client, probably because data happens before the connection.
    Do you have any solution for this? Should I buffer client data until the serviceSocket gets connected?

  2. You are right with your guess. Buffering the data is certainly a more robust solution. You can push the buffers into a javascript array. A simple boolean value can be used to determine if you’ll buffer or call write. Send the buffers when the connect event is emitted, and flip the boolean value.

  3. Hello! Thank you for posting this. I have adapted this approach with the buffering suggestion and also a “middleware” scheme similar to Express: https://gist.github.com/2020839

    An interesting issue I can see arising from this approach, however, is the possibility that the server-facing socket can send packets out of order due to the asynchronous nature of the implementation… :D


    Proxy.to("nodejs.org", 80)
    .use(function(data, src, dst, next) {
    setTimeout(function() {
    next();
    }, Math.random() * 5000);
    })
    .use(Proxy.consoleLog)
    .use(Proxy.send)
    .listen(8081);

    1. Hi! Thank you for sharing this. I’ve taken the opportunity to update the code in my post. It now works with the latest revision of Node.js. I hope to try your implementation when the need crops up.

      1. Thank you. Your guidance (along with some logging and reading above comments) helped. If you provide consulting services in node and other topics, email me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s