creating a Java Proxy Server that accepts HTTPS

前端 未结 3 1771
离开以前
离开以前 2020-12-13 16:43

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 us

3条回答
  •  时光说笑
    2020-12-13 17:08

    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:

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

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

提交回复
热议问题