i already have a working HTTP proxy server that can handle multiple HTTP request. now my problem is how do I handle https request?
here's a simplified code i am using:
class Daemon
{
public static void main(String[] args)
{
ServerSocket cDaemonSocket = new ServerSocket(3128);
while(true)
{
try
{
Socket ClientSocket = cDaemonSocket.accept();
(new ClientHandler(ClientSocket )).start();
}catch(Exception e) { }
}
}
}
and the ClientHandler
class ClientHandler extends Thread
{
private Socket socket = null;
private Socket remoteSocket = null;
private HTTPReqHeader request = null;
ClientHandler(Socket socket)
{
this.socket = socket;
request = new HTTPReqHeader();
request.parse(socket); // I read and parse the HTTP request here
}
public void run()
{
if(!request.isSecure() )
{
remoteSocket = new Socket(request.url,request.port);
}
else
{
// now what should I do to established a secured socket?
}
// start connecting remoteSocket and clientSocket
...........
}
}
}
I really did try searching how, I have encounter SSL tunneling, certificate,handshaking, SSLSocket, SSLFactory, trustStore and etc. something like that but still could not make it work.. I just need to know what are the things I need and the steps to established a connection to a SSL-enabled web server.
Google "https server in java" and you may find a relevant tutorial, a related RFC and standard documentation. I hope this will help :).
Please find below java code to create HTTPS proxy. It doesn't modify the response. To integrate it with HTTP write HTTP code in else clause. You can find HTTP code for proxy at a number of places.
Basically what is happening is when the client sends an HTTPS request to proxy it comes with CONNECT keyword. You have to send HTTP/1.1 200 OK to client after establishing connection with upstream server. After that you have to supply client's incoming input stream without headers/host etc to upstream server and incoming stream from upstream server to client.
You don't need to think about SSL at all.
I finally got it.
I only need to use normal socket and send a message to client that a connection is established. then proceed to tunneling.
here is a working code:
here's a good explanation on how proxy server handles CONNECT. http://curl.haxx.se/rfc/draft-luotonen-web-proxy-tunneling-01.txt