Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.net.*;
- import java.io.*;
- class aproxy extends Thread {
- Socket socket;
- aproxy(Socket s) {socket = s;}
- public static void main(String args[]) {
- try {
- ServerSocket ss = new ServerSocket(8080);
- while(true) {
- System.err.print("-Waiting for Clients \r\n");
- Socket s = ss.accept();
- System.err.print("-Client Accepted \r\n");
- System.err.print(" Number of Active Threads: "+activeCount()+"\r\n");
- Thread t = new aproxy(s);
- t.start();
- }
- } catch (IOException e) {}
- }
- public void run() {
- try {
- Scanner in = new Scanner(socket.getInputStream()); // CLIENT-->PROXY
- PrintWriter out = new PrintWriter(socket.getOutputStream(),true); // PROXY-->CLIENT
- if (isGET(in,out))
- readRequestHead(in,out);
- System.err.print("-Client Gone \r\n");
- } catch (Exception e) {}
- }
- public boolean isGET (Scanner in,PrintWriter out) { // Get the input from the client:
- String head = in.next(); //The word 'GET'.
- if ( !head.toUpperCase().equals("GET") ) {
- sendEResponse(out,1,null);
- return false;
- } else
- return true;
- }
- public void sendEResponse (PrintWriter out, int exceptionCase, String targetAddr) { // Send the errors and exceptions:
- try {
- switch (exceptionCase){
- case 1:
- //System.err.println("Not an HTTP 'GET' petition!");
- out.print("HTTP/1.1 501 Not Implemented\r\n");
- out.print("Content-Length: xxxx\r\n");
- out.print("Content-Type: xxxx\r\n");
- out.print("Connection: close\r\n");
- out.print("\r\n");
- out.print("<HTML>\n"+"<HEAD><TITLE>Error - Proxy Java</TITLE></HEAD>\n"+"<BODY bgColor=#FF9900><H1>Error HTTP 501</H1>\n"+"<H2> The method you're using is not valid for the HTTP protocol! </H2>\n"+"</BODY>\n"+"</HTML>\r\n\r\n");
- out.flush();
- socket.close();
- break;
- case 2:
- //System.err.println("Unknown host!");
- out.print("HTTP/1.1 302 Found\r\n"+"Location: http://www.google.com/search?q="+targetAddr+"\r\n"+"Connection: close\r\n\r\n");
- out.flush();
- socket.close();
- break;
- case 3:
- //System.err.println("Connection rejected!");
- out.print("HTTP/1.1 500 Internal Server Error\r\n"+"ContentLength: xxxx\r\n"+"ContentType: text/html; charset=utf8\r\n"+"Connection: close\r\n");
- out.print("\r\n");
- out.print("<HTML>\n"+"<HEAD><TITLE>Error - Proxy Java</TITLE></HEAD>\n"+"<BODY bgColor=#FF9900><H1>Error HTTP 500</H1>\n"+"<H2> The server has rejected the request! </H2>\n"+"</BODY>\n"+"<HTML>\r\n\r\n");
- out.flush();
- socket.close();
- break;
- }
- } catch (IOException e) { System.err.println("IO error!"); }
- }
- public void readRequestHead (Scanner in,PrintWriter out) { // Get part of the header as the target address to make the socket:
- String path = in.next(); //Full path inside the target host.
- String lineOne = ("GET "+path+" "+in.next()+"\r\n"); //The first line with the 'GET' + the path of the host and the http version.
- String wordHost = (in.next()+" "); //The word 'Host:' of the header and the space.
- String targetAddr = in.next(); //The target host itself.
- String lineTwoEnd = (targetAddr+in.nextLine()+"\r\n"); //The host + the end of the line.
- String lineTwo = (wordHost+lineTwoEnd);
- readNsendRequest(in,out,lineOne,lineTwo,targetAddr);
- }
- public void readNsendRequest (Scanner in,PrintWriter out,String lineOne,String lineTwo,String targetAddr) { // Get the remaining part of the header and send it to the target server:
- try {
- Socket tsocket = new Socket(targetAddr, 80); //Socket to the target server the client wants to access.
- PrintWriter tout = new PrintWriter(tsocket.getOutputStream(),true); // PROXY-->SERVER
- System.err.println("> Header from the client: \r\n");
- String line;
- System.err.print(lineOne+lineTwo);
- tout.print(lineOne+lineTwo);
- do {
- line = in.nextLine();
- if ( ( !line.equals("Connection: keep-alive") ) && ( !line.equals("Proxy-Connection: keep-alive") ) ) {
- System.err.print(line+"\r\n");
- tout.print(line+"\r\n");
- } else {
- System.err.print("Connection: close\r\n");
- tout.print("Connection: close\r\n");
- }
- } while( !line.equals("") );
- tout.flush();
- getNsendResponse(tsocket);
- } catch (UnknownHostException e) { sendEResponse(out,2,targetAddr); }
- catch (ConnectException e) { sendEResponse(out,3,null); }
- catch (IOException e) { System.err.println("IO error!"); }
- }
- public void getNsendResponse (Socket tsocket) { // Send the server output to the client:
- try {
- InputStream streamIn = tsocket.getInputStream(); // SERVER-->PROXY
- OutputStream streamOut = socket.getOutputStream(); // PROXY-->CLIENT
- System.err.println(">> Response from the server: \r\n");
- // Buffer:
- int i;
- byte[] b = new byte[1024];
- i = streamIn.read(b);
- while ( (i != 0) && (i != -1) ) { //Loop until the end of the stream.
- System.err.print("--Reading buffer\r\n");
- System.err.print("--Read "+i+" bytes\r\n");
- streamOut.write(b, 0, i);
- System.err.print("--Writing buffer\r\n");
- i = streamIn.read(b);
- }
- streamOut.flush();
- streamIn.close();
- streamOut.close();
- tsocket.close();
- socket.close();
- } catch (IOException e) { System.err.println("IO error!"); }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment