First of all i would like to thank you for reading my article,and i wish to tell you that this is my FIRST post. I have done this proxy server implementation in one of my projects titled 'ANTIPHISHING SYSTEM' and this will be use full for all of you .If you Like the post please comment it and let me know valuable suggestions , all appreciations and criticisms are invited . Thank you .
1)Before running this application first you have to Configure your Browser In the way shown below .(The picture below is only for Firefox user's)

2)The Application contains 4 Files.
*ClientProxy.java
*RequestHandler.java
*ResponseHandler.java
*SETTINGS.java
/*
* @(#)ClientProxy.java
*
* Copyright (c) 2012, . All rights reserved.
*
*/
import java.net.*;
/*
* This is the main Class .This class responsible for reading user equest from browser,
* after reading request it will send request to RequestThraed.java
* pls do Browser setiings before running this application
*/
public class ClientProxy implements Runnable {
ServerSocket browserSocket = null;
Socket outputSocket = null;
Socket inputSocket = null;
String serverIP = SETTINGS.SERVER_IP;
int serverPort = SETTINGS.SQUID_PORT;
public ClientProxy(int port) {
try {
browserSocket = new ServerSocket(port);
run();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void run() {
try {
System.out.println("Proxy Server Started...");
while (true) {
inputSocket = browserSocket.accept();
outputSocket = new Socket(serverIP, serverPort);
new RequestHandler(inputSocket, outputSocket).start();
new ResponseHandler(outputSocket, inputSocket).start();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new ClientProxy(SETTINGS.BROWSER_REQUEST_PORT); //browser request will come here ... :)
}
}
/*
*@(#)RequestHandler.java
* author:riyo
*Copyright (c) 2012, . All rights reserved.
*
*/
import java.io.*;
import java.net.*;
/*
* This class responsible for reading user equest from clientProxy .
* after reading request it will send request to squid port in server system.
*/
public class RequestHandler extends Thread {
Socket inputSocket = null;
Socket outputSocket = null;
InputStream in = null;
OutputStream out = null;
String request = "";
public RequestHandler(Socket in, Socket out) {
try {
inputSocket = in;
outputSocket = out;
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public void run() {
try {
in = inputSocket.getInputStream();
out = outputSocket.getOutputStream();
while (true) {
byte[] b = new byte[10000];
int len = in.read(b);
if (len == -1) {
in.close();
out.close();
break;
}
request = new String(b, 0, len);
System.out.println("***************************REQUEST***************************");
System.out.println(request);
out.write(b, 0, len);
System.out.println("request send to server.");
}
} catch (Exception e) {
System.out.println(e);
}
}
}
/*
*@(#)ResponseHandler.java
*Copyright (c) 2012, . All rights reserved.
*author:riyo
*/
import java.io.*;
import java.net.*;
/*
* This class responsible for reading response from Server .
* after reading response it will write response in browser.
*/
public class ResponseHandler extends Thread {
Socket inputSocket = null;
Socket outputSocket = null;
InputStream in = null;
OutputStream out = null;
public ResponseHandler(Socket in, Socket out) {
try {
inputSocket = in;
outputSocket = out;
} catch (Exception ex) {
System.out.println("Error: " + ex);
}
}
@Override
public void run() {
try {
in = inputSocket.getInputStream();
out = outputSocket.getOutputStream();
while (true) {
byte[] b = new byte[10000];
int len = in.read(b);
if (len == -1) {
in.close();
out.close();
break;
}
out.write(b, 0, len);
System.out.println("response written to firefox.");
System.out.println("***************************END***************************\n\n\n");
}
} catch (Exception e) {
System.out.println(e);
}
}
}
/*
*@(#)SETTINGS.java
*Copyright (c) 2012, . All rights reserved.
*author:riyo
*/
public class SETTINGS {
public static final String SERVER_IP = "192.168.1.100"; //This is the Server Ip in My Network
public static final int SQUID_PORT = 3128; //This is the SQUID Application Running Port in Server System.
public static final int BROWSER_REQUEST_PORT = 9999; //In This port all browser request are coming ...
}
3)Run the main Class that is ClientProxy.java .
4)Connect to a URL using your Browser That's it. :)
5)Thank you,visit Again.
*Download full source code Here
thanx dear
ReplyDeleteWelcome Da hw wz it ... ?
ReplyDeleteAd a Dell employee I think your post about Proxy Server Implementation using JAVA in a Client-Server Architecture is really impressive. The code you shown is surely useful and valuable. Thanks for sharing with us, keep posting such important information.
ReplyDelete