Skip to content

Instantly share code, notes, and snippets.

@fabrizioc1
Created September 7, 2012 06:42
Show Gist options
  • Save fabrizioc1/3663905 to your computer and use it in GitHub Desktop.
Save fabrizioc1/3663905 to your computer and use it in GitHub Desktop.
Simple Network Proxy
import java.io.*;
import java.net.*;
public class ProxyServer
{
/**
* Thread to handle a single client connection
* This way the server can handle other incoming requests
*/
public static class ClientHandler extends Thread
{
Socket client;
Socket server;
public ClientHandler(Socket client,Socket server)
{
super("ClientHandler: "+client.getInetAddress()+"=>"+server.getInetAddress());
this.client = client;
this.server = server;
}
public void run()
{
try {
String address = client.getInetAddress().getHostAddress();
System.out.println("ClientHandler: Handling client from "+address);
InputStream clientInput = client.getInputStream();
OutputStream clientOutput = client.getOutputStream();
InputStream serverInput = server.getInputStream();
OutputStream serverOutput = server.getOutputStream();
IOPipe clientToServer = new IOPipe(clientInput,serverOutput);
IOPipe serverToClient = new IOPipe(serverInput,clientOutput);
clientToServer.start();
serverToClient.start();
clientToServer.join();
serverToClient.join();
System.out.println("ClientHandler: Connection to "+address+" is closed");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try { client.close(); } catch (IOException e1) {}
try { server.close(); } catch (IOException e2) {}
}
}
}
/*
* Thread to pipe one socket's input another's output so that the IO is non-blocking
*/
public static class IOPipe extends Thread
{
InputStream input;
OutputStream output;
public IOPipe(InputStream input, OutputStream output) {
super("IOPipe");
this.input = input;
this.output = output;
}
// Pipe the input to the output
public void run()
{
int bytesRead = 0;
byte buffer[] = new byte[64000];
try {
while ((bytesRead = input.read(buffer)) > 0) {
output.write(buffer,0,bytesRead);
}
}
catch (Exception e) {
e.printStackTrace();
}
/*
finally {
try { output.close(); } catch (IOException e1) {}
try { input.close(); } catch (IOException e2) {}
}
*/
}
}
public static void main(String args[])
throws Exception
{
ServerSocket serverSocket = null;
if (args.length < 4) {
System.out.println("Usage: java ProxyServer [localhost] [localport] [remotehost] [remoteport]");
System.exit(-1);
}
String localhost = args[0];
int localport = Integer.parseInt(args[1]);
String remotehost = args[2];
int remoteport = Integer.parseInt(args[3]);
try {
serverSocket = new ServerSocket(localport);
} catch (IOException e) {
System.out.println("Could not listen on port: "+localport+": "+e.toString());
System.exit(-1);
}
System.out.println("ProxyServer: Waiting for connections ...");
Socket clientSocket = null;
try {
while (true) {
clientSocket = serverSocket.accept();
String address = clientSocket.getInetAddress().getHostAddress();
System.out.println("ProxyServer: Got a client from "+address);
Socket remoteSocket = new Socket(remotehost,remoteport);
ClientHandler handler = new ClientHandler(clientSocket,remoteSocket);
handler.start();
}
} catch (IOException e) {
System.out.println(e.toString());
System.exit(-1);
}
serverSocket.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment