One of the things I coded in Perl a while ago that I was planning to port to Ruby at some point was a little port forwarding utility called portforw (GPL license). I provided a link to download it here because perhaps you may find it useful, too, if you also need such a utility. I consider it moderately well written—I can be somewhat proud of it and somewhat embarrassed by it at the same time. I was going to clean it up when I made the Ruby port (which at one point I wrote quite a bit of code for, but now it languishes).

Portforw does what it sounds like: it forwards connections from one place to another. But simply forwarding connections is not the limit of portforw’s abilities; if connection forwarding was all I needed I would have used the port redirect feature with some host-based firewall software. Portforw also filters the data as it passes through, allowing it to pass connections through different kinds of proxies.

Using portforw is rather simple. If you want to forward port 5901 to another system, call it my-win-box, you invoke it:

portforw 5901//my-win-box

You can change the destination port that connections are forwarded:

portforw 5901//my-win-box:5900

You can make portforw listen on just a single interface, possibly loopback:


You can setup multiple ports to forward, all handled by one process:

portforw 5901//my-win-box 5902//your-win-box 5903//his-win-box

Or make it a poor-man’s netcat by having standard input and output bridged to the remote port:

portforw -f /fd:0,1//my-telnet-box:23

If you want to make your forwarding connection through an HTTP proxy, a telnet proxy, or tunneled through an SSL connection, it is possible using portforw (though not necessarily trivial). To forward the connection through an HTTP proxy, for instance, you would need a separate program to prepare the HTTP header containing the CONNECT statement ahead of time and then pass that header to portforw as part of the data. Add a flag to tell portforw to filter out the HTTP response header from the active data stream, and your connection is complete. It is quite a kludge, but it admirably handled the task at hand when I needed these features.

If you end up using portforw for anything interesting or have questions about some of the filter flags, I would appreciate it if you let me know (my email address is in the footer). If ever sufficient interest in portforw surfaces, I might get back to that Ruby port! 😉

  • You can skip to the end and leave a comments. Trackback is currently closed.
  • Trackback URI: http://cosine.org/2007/07/29/portforw/trackback/
  • Comments RSS 2.0

Leave a Reply

You must be logged in to post a comment.