Simple Port Forwarder

Written by Sean R. Owens (sean at guild dot net). Share and enjoy. http://darksleep.com/player

This was a very quick hack for a friend, to help them spy on what was going on between a web browser and a web server. There are two fairly simple programs here.

JustListen

JustListen is a simple program that more or less echoes anything sent to it back to the sender.

JustListen.java (source, 52 lines)

The JustListen class opens a server socket and listens on it. When a new connection is accepted, the JustListen class creates a JustListenHandler instance for that connection and then goes back to listening.

JustListenHandler.java (source, 61 lines)

JustListenHandler takes a socket connection and simply reads data from it, then writes that data back to the socket as well as printing it to System.err.

Server

The real portforwarder is contained in two classes, Server and Handler. It takes three command line args, which are; the port it should listen on; and address and port of the server it should forward to.

Server.java (source, 61 lines)

The Server is fairly straightforward. It just opens a server socket and then listens forever on that socket. Every time a new connection is accepted, Server first opens a new socket to the destination. Then Server creates two instances of Handler, which run independently. One instance of Handler takes care of reading data from the incoming socket and writing it to the destination socket, the other instance of Handelr takes care of reading data from the destination socket and writing it to the incoming socket.

Handler.java (source, 66 lines)

Handler simply takes two sockets in the constructor, and reads data from the first socket, and then writes it to the second socket. Thats it. Oh yeah, it also prints out what ever it is reading to System.err.


Last modified: Wed May 27 19:16:34 EDT 2009