This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Redirecting Proxy Server request...  RSS feed

 
Bala Chandra
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends
the question may be big, But Please take time to go through it.
Thanks in Advance...
The Problem....
In my company we have a proxy server to connect to the internet. It just happens that during daytime my Project manager is authorised to access the net but not me. I thought why not utilise my Managers connection by writing a daemon which will act for me within my Managers machine and send request to the proxy server and retreive the proxy response and redirect it to me.... Its not illegal as i have the permission of my Manager for this.
I have written Four classes.
1) ServerApp this is the class which acts as an agent for me in my managers machine.(This is the proxy for me).
2) ServerThread. This is the class which processes any request to the above mentioned App.
3) RequestForwarder: This is a thread which forwards the requst from my machine to the proxy server.
4) RequestRetreiver: This is a thread which retrives the data from the proxy server and sends it backto me.
All these classes would reside in my managers machine.
The code of the classes are as follows.
===============================================
ServerApp
==========
package in.co.motech.proxy;
import java.net.*;
import java.io.*;
/**
* Insert the type's description here.
* Creation date: (08-Nov-01 2:25:01 PM)
* @author: Administrator
*/
public class ServerApp {
private int listenPort;
/**
* ServerApp constructor comment.
*/
public ServerApp() {
super();
}
/**
* Insert the method's description here.
* Creation date: (08-Nov-01 2:26:14 PM)
* @param listenPort int
*/
public ServerApp(int listenPort)
{
this.listenPort = listenPort;
}
/**
* Starts the application.
* @param args an array of command-line arguments
*/
public static void main(java.lang.String[] args) {
// Insert code to start the application here.
ServerApp serverApp = new ServerApp(6400);
serverApp.run();
}
/**
* Insert the method's description here.
* Creation date: (08-Nov-01 2:27:25 PM)
*/
public void run()
{
System.out.println("Listening on port "+listenPort);
try{
ServerSocket serverSocket = new ServerSocket(listenPort);
Socket client = null;
do{
client = serverSocket.accept();
System.out.println("Got a connection to process ");
(new ServerThread(client)).start();

}while(true);

}
catch(IOException ioe)
{
System.out.println("�xception in Run method of ServerApp");
ioe.printStackTrace();
}
}
}

=======================================================
ServerThread
===========
package in.co.motech.proxy;
import java.io.*;
import java.net.*;
/**
* Insert the type's description here.
* Creation date: (08-Nov-01 2:33:41 PM)
* @author: Administrator
*/
public class ServerThread extends Thread {
private Socket client = null;
/**
* ServerThread constructor comment.
*/
public ServerThread() {
super();
}
/**
* Insert the method's description here.
* Creation date: (08-Nov-01 2:35:56 PM)
* @param client java.net.Socket
*/
public ServerThread(Socket client)
{
this.client = client;
}
/**
* Insert the method's description here.
* Creation date: (08-Nov-01 2:37:05 PM)
*/
public void run()
{
String data = null;
try{
Socket motechProxy = new Socket("10.125.0.3",808);
(new RequestForwarder(client,motechProxy)).start();
(new RequestRetreiver(client,motechProxy)).start();

}
catch(IOException ioe)
{
System.out.println("Exception in run method of ServerThread");
ioe.printStackTrace();
}
}
}
===================================
RequestForwarder
===============
package in.co.motech.proxy;
import java.net.*;
import java.io.*;
/**
* Insert the type's description here.
* Creation date: (08-Nov-01 6:36:14 PM)
* @author: Administrator
*/
public class RequestForwarder extends Thread {
private Socket client;
private Socket proxy;
/**
* Insert the method's description here.
* Creation date: (08-Nov-01 6:38:04 PM)
* @param fromClient java.net.Socket
* @param toProxy java.net.Socket
*/
public RequestForwarder(Socket client, Socket proxy)
{
this.client = client;
this.proxy = proxy;
}
/**
* Insert the method's description here.
* Creation date: (08-Nov-01 6:38:59 PM)
*/
public void run()
{
System.out.println("In request Forwarder");
try{
BufferedReader fromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedWriter toMotechProxy = new BufferedWriter(new OutputStreamWriter(proxy.getOutputStream()));
String data = null;
while((data = fromClient.readLine()) != null)
{
toMotechProxy.write(data);
data = null;
}
}
catch(IOException ioe)
{
System.out.println("Error while forwarding details to proxy server");
ioe.printStackTrace();
}
}
}

====================================
RequestRetreiver
==============
package in.co.motech.proxy;
import java.net.*;
import java.io.*;
/**
* Insert the type's description here.
* Creation date: (08-Nov-01 6:36:14 PM)
* @author: Administrator
*/
public class RequestRetreiver extends Thread {
private Socket client;
private Socket proxy;
/**
* Insert the method's description here.
* Creation date: (08-Nov-01 6:38:04 PM)
* @param fromClient java.net.Socket
* @param toProxy java.net.Socket
*/
public RequestRetreiver(Socket client, Socket proxy)
{
this.client = client;
this.proxy = proxy;
}
/**
* Insert the method's description here.
* Creation date: (08-Nov-01 6:38:59 PM)
*/
public void run()
{
System.out.println("In request Retreiver");
try{
BufferedWriter toClient = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
BufferedReader fromMotechProxy = new BufferedReader(new InputStreamReader(proxy.getInputStream()));
String data = null;
while((data = fromMotechProxy.readLine()) != null)
{
toClient.write(data);
System.out.println("Wrote Data**** "+data);
data = null;
}
client.close();
proxy.close();
}
catch(IOException ioe)
{
System.out.println("Error while retreiving details from proxy server");
ioe.printStackTrace();
}
}
}
===========================================================
Things that are working....
The RequestForwarder is working and is properly forwarding the requests to the proxy server.
The RequestRetreiver is working and is getting some information from the proxy server(actual one)
Things that are not working
I have configured my IE to contact my daemon(ServerApp) as the proxy server. The data retreived from the actual proxy server is not shown on my IE and it goes into an infinite wait....
I can see just a blank page... nothing else..
I would you to go through my code if possible copy and test it help me in debugging the issue.
Looking forward to great response from you all
C Balajee

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!