创建一个接受HTTPS的Java代理服务器

问题描述:

我已经有一个可以处理多个HTTP请求的工作HTTP代理服务器。现在我的问题是如何处理https请求?

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) { }
        }
    }

}

和ClientHandler

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 
            ...........
       }
}

}

我确实尝试过如何搜索,我遇到过SSL隧道,证书,握手,SSLSocket,SSLFactory,trustStore等类似的东西,但仍然无法使其工作..我只需要知道我需要什么,以及建立连接到启用SSL的步骤网络服务器。

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.

我终于明白了。

我只需要使用普通套接字并向客户端发送建立连接的消息。然后继续进行隧道掘进。

I only need to use normal socket and send a message to client that a connection is established. then proceed to tunneling.

这是一个有效的代码:

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()
       {

            remoteSocket = new Socket(request.url,request.port);

            if(request.isSecure() )
            {
                 // send ok message to client
                 String ConnectResponse = "HTTP/1.0 200 Connection established\n" +
                                          "Proxy-agent: ProxyServer/1.0\n" +
                                          "\r\n";
                try
                {
           DataOutputStream out =  new DataOutputStream(socket.getOutputStream());
                   out.writeByte(ConnectResponse);
                    out.flush();
                } catch(Exception e) {} 

            }

            // start connecting remoteSocket and clientSocket 
            ...........
       }

这里有一个很好的解释代理服务器如何处理CONNECT。
http://curl.haxx.se /rfc/draft-luotonen-web-proxy-tunneling-01.txt

here's a good explanation on how proxy server handles CONNECT. http://curl.haxx.se/rfc/draft-luotonen-web-proxy-tunneling-01.txt