Re: writing a proxy ..



ebby83@xxxxxxxxx wrote:
shakah wrote:
ebby83@xxxxxxxxx wrote:
Hi ppl ...
I am writing a simple proxy server ...

I redirect my browser to the local server and re-route the HTTP
request to the host.
I get a reply back from the HOST but unfortunately this is not shown in
my browser ( html appears in my console ) .

I am putting the code here .. ( Its a thread ) pls run with the Server
Class specified below it ..

package com.webproxy.fthread;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class fthread extends Thread {

Socket orig;

Socket dest;

public fthread(Socket s) {
this.orig = s;

}

public void run() {
String line;
try {

BufferedReader r = new BufferedReader(new InputStreamReader(orig
.getInputStream()));

PrintWriter w = new PrintWriter(new OutputStreamWriter(orig
.getOutputStream()));

BufferedReader r2 = null;

PrintWriter w2 = null;

while(true){
line = r.readLine();
if(line.equals(null) || line.equals(""))
break;
System.out.println( line );
if (line.indexOf("GET") > -1) {
String deststr = line.split(" ")[1].split("/")[2]; //
System.out.println(deststr);
dest = new Socket(deststr, 80);

w2 = new PrintWriter(new OutputStreamWriter(dest
.getOutputStream()));

r2 = new BufferedReader(new InputStreamReader(dest
.getInputStream()));
}

w2.write(line + "\n");
w2.flush();
}

w2.write("\n\n");
w2.flush();

System.out.println("\nNow Reading...\n\n" );

while ((line = r2.readLine()) != null) {
System.out.println(line);
w.write(line);
w.flush();
}

w.close();
r2.close();
r.close();
w2.close();
} catch (IOException e) { // TODO Auto-generated catch
e.printStackTrace();
}

}
}


package webproxy.com.webproxy.main;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import webproxy.com.webproxy.fthread.fthread;

public class ServerClass {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
ServerSocket ss = new ServerSocket(8080);
Socket s = new Socket("localhost", 8080);
Socket s2 = null;
(new PrintWriter(new OutputStreamWriter(s.getOutputStream())))
.write("GET http://www.google.com/ig HTTP/1.0");

s2 = ss.accept();
fthread t = new fthread(s2);
t.start();
t.join();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

I didn't try executing your code, but maybe you need to add CR/LF pairs
to the socket output so the browser interprets the HTTP response
header, e.g.:

while ((line = r2.readLine()) != null) {
System.out.println(line);
w.write(line);
w.write("\015\012") ;
w.flush();
}

?


thanks .. that helped me get this thing started but I still am having
problems loading the images ( which I think shud be the problem ) which
get garbled or do not get loaded at all .. also complex URLs such as

https://www.google.com/accounts/ServiceLogin?service=ig&passive=true&continue=http://www.google.com/ig&followup=http://www.google.com/ig&cd=US&hl=en&nui=1&ltmpl=default

dont really work coz of my logic ... but im not sure how to pass the
connection info to the socket class for eg. would the socket connect to
www.google.com/accounts/ServiceLogin?service=ig&passive=true&continue=http://www.google.com/ig&followup=http://www.google.com/ig&cd=US&hl=en&nui=1&ltmpl=default

(doesnt work in my case ...

also y does this work much slower than a normal proxy ... ??

You really shouldn't be using readLine() to read the HTTP response,
you'd be better off just reading blocks of bytes. That should be
faster, and it should avoid the problems you've seen with binary
content (which the "add CR/LF" hack most likely screwed up). Something
along the lines of (you might have to work out the actual stream
classes to use):

static final int BLOCK_SIZE = 2048 ;

byte [] ab = new byte[BLOCK_SIZE] ;
java.io.InputStream is = dest.getInputStream() ;
java.io.OutputStream os = orig.getOutputStream() ;
while(true) {
// ...read the next chunk of the HTTP response
int nBytesRead = is.read(ab, 0, BLOCK_SIZE) ;
if(nBytesRead < 0) {
break ;
}
else {
// ...write it to the client
os.write(ab, 0, nBytesRead) ;
}

.



Relevant Pages

  • Re: How to write something to a html textfield and send it?
    ... > No need for controlling any particular browser. ... I'm not familiar with HTTP user ... and building the request in your program. ... The server doesn't know anything about a textfield; ...
    (comp.programming)
  • Re: LSP HTTP redirecting
    ... In WSPSend you have access to the data that is being passed over the net, ... that means all the HTTP traffic is vizible to you. ... If you want to redirect the browser write proxy server. ...
    (microsoft.public.win32.programmer.networks)
  • Intercept and DECRYPT HTTP/S requests
    ... for dumping http and https connections. ... by viewing exactly what is passing between the browser and the ... the browser and the server, you can compare conversations that work vs. ... mail me at nntp AT dawes DOT za DOt net ...
    (comp.lang.java.programmer)
  • Re: Wrapping TCP communications in HTTP
    ... HTTP is a half-duplex protocol. ... The send socket is used to send all requests. ... so the server can return any data it has. ...
    (microsoft.public.win32.programmer.networks)
  • Re: IE6 bug: doesnt resend form-data when server resets connectio
    ... >> When the server closes the connection on a socket where IE is currently ... >> sending a HTTP POST, IE will resend the request on a new socket, but will ... >> only send the HTTP headers and not the form-data. ... IE holds on to keepalive'd sockets for 60 secs and due to this bug I get the ...
    (microsoft.public.windows.inetexplorer.ie6.browser)