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.iutputStreamWriter;
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();
}
}
}
}
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.iutputStreamWriter;
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();
}
}
}
}