Assignment 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

ASSIGNMENT 2:

Implement a TCP Proxy Server using socket.

A TCP proxy server (say, tcpproxy) is a server that acts as an intermediary


between a client and another server, called the destination server. Clients
establish connections to the TCP proxy server, which then establishes a
connection to the destination server. The proxy server sends data received from
the client to the destination server and forwards data received from the
destination server to the client. Interestingly, the TCP proxy server is actually
both a server and a client. It is a server to its client and a client to its destination
server.

The proxy server will be invoked at the command line as follows:

$ tcpproxy destination-host destination-port listen-port

Version1 : The proxy server will accept a single connection from a client and
forward it using a single connection to the server. During the connection, the
proxy will not accept any other connections.

Version 2: The proxy server will accept connections from multiple clients and
forward them using multiple connections to the server. The proxy should
forward data in both directions: from client to server and from server to client.
No client or server should be able to hang the proxy server by refusing to read
or write data on its connection. For instance, if one client suddenly stops
reading from the socket to the proxy, other clients should not notice
interruptions of service through the proxy.

Example
Here is an example of how to use tcpproxy to redirect all connections to port
4000 on your local machine to google's web server.

$ tcpproxy www.google.com 80 4000


<ConnectPorts.java>

import java.io.*;
import java.net.*;
import java.util.*;

public class ConnectPorts implements Runnable {

String IPAddress1, IPAddress2;


int port1, port2;

Socket socket1, socket2;


Scanner sockIn1, sockIn2;
PrintWriter sockOut1, sockOut2;

ConnectPorts(String port1, String port2)


{
this("localhost", port1, "localhost", port2);
}

ConnectPorts(String IPAddress1, String port1, String IPAddress2,


String port2)
{
this.IPAddress1=IPAddress1;
this.port1=Integer.parseInt(port1);
this.IPAddress2=IPAddress2;
this.port2=Integer.parseInt(port2);

try
{
socket1=new Socket(IPAddress1, this.port1);
socket2=new Socket(IPAddress2, this.port2);

sockIn1=new Scanner(socket1.getInputStream());
sockOut1=new PrintWriter(socket2.getOutputStream(),
true);

sockIn2=new Scanner(socket2.getInputStream());
sockOut2=new PrintWriter(socket1.getOutputStream(),
true);

new Thread(this).start();

while(sockIn1.hasNextLine())
{
String line=sockIn1.nextLine();

System.out.println(IPAddress1+":"+port1+">"+line);
sockOut1.println(line);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}

public static void main(String args[])


{
if(args.length!=2 && args.length!=4)
{
System.out.println("Usage 1: java ConnectPorts Port1
Port2");
System.out.println("Usage 2: java ConnectPorts
IPAddress1 Port1 IPAddress2 Port2");
System.out.println("Note: Usage 1 considers both
IPAddresses to be localhost");
System.exit(1);
}

if(args.length==2)
new ConnectPorts(args[0], args[1]);
else
new ConnectPorts(args[0], args[1], args[2], args[3]);
}

@Override
public void run()
{
while(sockIn2.hasNextLine())
{
String line=sockIn2.nextLine();
System.out.println(IPAddress2+":"+port2+">"+line);
sockOut2.println(line);
}
}

}
<PortRedirect.java>

import java.io.*;
import java.net.*;
import java.util.*;

public class PortRedirect {

PortRedirect(String sourcePort, String destIPAddress, String destPort)


{
this(Integer.parseInt(sourcePort), destIPAddress,
Integer.parseInt(destPort));
}

PortRedirect(int sourcePort, String destIPAddress, int destPort)


{
try
{
ServerSocket serverSocket=new
ServerSocket(sourcePort);
while (true)
{
Socket incoming = serverSocket.accept();
new Handler(incoming, new Socket(destIPAddress,
destPort));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String args[])
{
if(args.length!=3)
{
System.out.println("Usage: java PortRedirect
<sourcePort> <destIPAddress> <destPort>");
System.out.println("<sourcePort> : The port in the
localhost that needs to be redirected.");
System.out.println("<destIPAddress> : The IP Address of
the machine(local/remote) you want this port to be redirected to.");
System.out.println("<destPort> : The port number of the
destination machine(local/remote) you want this port to be redirected
to.");
System.exit(0);
}
new PortRedirect(args[0], args[1], args[2]);
}
}

<Handler.java>

import java.io.*;
import java.net.*;
import java.util.*;

class Handler
{
public Handler(Socket socket1, Socket socket2) throws Exception
{
this(new Scanner(socket1.getInputStream()), new
PrintWriter(socket2.getOutputStream(), true), new
Scanner(socket2.getInputStream()), new
PrintWriter(socket1.getOutputStream(), true));
}

public Handler(Scanner in1, PrintWriter out1, Scanner in2,


PrintWriter out2) throws Exception
{
new Thread(new Runnable(){
public void run()
{
while(in1.hasNextLine())
out1.println(in1.nextLine());
}
}).start();

new Thread(new Runnable(){


public void run()
{
while(in2.hasNextLine())
out2.println(in2.nextLine());
}
}).start();
}
}

You might also like