question on handshake with ssl connection

J

Jakekeke

I am going to write a proxy server with Jave.
It is mainly for testing purpose and logging all the connection data
of the users.
However, i become confuse while implement the part on HTTPS request
with JSSE.
Let me tell what i have done briefly

When the thread start, a ServerSocket was create at port 8080,
which is the proxy port of all user's browser.
my program should become a middle man between the browsers and
destinate host
after receive the resquest from browser,
and on the other site, the program should make another handshake to
destinate host before forward the modified request.
I found that the broswer haven't get any response after i forward the
response header from remote host, which status code is 200.

the program should handshake with the browser, right?
however, there is an exception if i just use starthandshake() at the
beginning
and i know that a simple Socket should be used instead of SSLSocket
before handshaking.

So, the question is how can i act handshaking with the broswer?
is there any method or package can help me to do so?
or i should write a set of coding myself for HelloClient, etc???

Thanks for your help.

Here is my coding

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io_OutputStreamWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;

import javax.net.ssl.SSLSocketFactory;

public class Test {

public static final String TARGET_HTTPS_SERVER = "localhost";
//this model is just simulate to https://localhost basically
public static final int TARGET_HTTPS_PORT = 443;

public static void main(String[] args) throws Exception {

ServerSocket serverSocket = new ServerSocket(8080);
while (true){

Socket soc = serverSocket.accept();

BufferedReader br = new BufferedReader(
new InputStreamReader(soc.getInputStream()));
//reading inputstream was skipped

//should i start the handshake here? how?

Writer ou = new OutputStreamWriter(
soc.getOutputStream(), "ISO-8859-1");

Socket socket = SSLSocketFactory.getDefault().createSocket(
TARGET_HTTPS_SERVER, TARGET_HTTPS_PORT);

try {
//System.out.println("here");
Writer out = new OutputStreamWriter(
socket.getOutputStream(), "ISO-8859-1");
out.write("GET / HTTP/1.1\r\n");
out.write("Host: " + TARGET_HTTPS_SERVER + ":" +
TARGET_HTTPS_PORT + "\r\n");
out.write("Agent: SSL-TEST\r\n");
out.write("\r\n");
out.flush();
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream(),
"ISO-8859-1"));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
ou.write(line+"\r\n");
}
ou.flush();
//after flushing the stream, the browser hasn't any
response since handshaking was not done
} finally {
socket.close();
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top