简体   繁体   中英

Problem in establishing connection in HTTPS in proxy server.(CONNECT Method)

I am developing a proxy server based on java. For simple http request, proxy server is working. But for HTTPS Connection, connection gets timed out. Here are the steps I did. I first read one line from input stream and created a socket connecting Server. After that I gave 200 Status to client. After that I asynchronously read and write between Client Socket and Server socket. But currently this isn't working and connection gets timedout and I couldn't debug the problem.

public class ProxyServer extends Thread {
private String host;
private int port;
private ServerSocket serverSocket;
private InputStream proxyToClientIP;
private OutputStream proxyToClientOP;
private InputStream proxyToServerIP;
private OutputStream proxyToServerOP;
private Socket socket;
private Socket socketFromProxyServer;


ProxyServer(ServerSocket serverSocket, Socket socket) {
    this.serverSocket = serverSocket;
    this.socket = socket;
    this.start();
}

public void run() {
    processInputRequest();
}

public void processInputRequest() {
    try {
        proxyToClientIP = socket.getInputStream();
        proxyToClientOP = socket.getOutputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(proxyToClientIP));
        String hostDetails = reader.readLine();
        System.out.println(hostDetails);
        boolean isConnect = false;
        //Need to parse request and find req type as GET or CONNECT
        //As of now we assume it to be Connect request
        if (!isConnect) {
            processGetRequest();
        } else {
            processConnectRequest();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

public void processConnectRequest() {
    //Need to get host name from request. Currently Hardcoded for developing purpose
    host = "harish-4072";
    port = 8383;
    try {
        socketFromProxyServer = new Socket(host, port);
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(proxyToClientOP));
        writer.write("HTTP/1.1 200 Connection established\r\n" + "\r\n");
        writer.flush();
        proxyToServerOP = socketFromProxyServer.getOutputStream();
        proxyToServerIP = socketFromProxyServer.getInputStream();
        proxyRequest();
    } catch (IOException ex) {
        System.out.println(ex);
    }
}




public void proxyRequest() {
    try {
        new Thread() {
            @Override
            public void run() {
                try {
                    byte[] read = new byte[1024];
                    int in;
                    System.out.println("Reading");
                    while ((in = proxyToClientIP.read(read)) != -1) {
                        proxyToServerOP.write(read, 0, in);
                        proxyToServerOP.flush();
                    }
                } catch (SocketException e) {
                    System.out.println(e);
                } catch (IOException ex) {

                }
            }
        }.start();
        byte[] reply = new byte[1024];
        int out;
        System.out.println("Writing");
        while ((out = proxyToServerIP.read(reply)) != -1) {
            proxyToClientOP.write(reply, 0, out);

            proxyToClientOP.flush();
        }
    } catch (IOException ex) {

    }

    public void processGetRequest() {
   //
   }
}

I first read one line from input stream and created a socket connecting Server. ... After that I asynchronously read and write between Client Socket and Server socket.

The problem is that you are reading only a single line while you would need to read the full HTTP request header from the client, ie everything up to the end of the request header ( \r\n\r\n ).

Because you fail to do so the unread parts of the HTTP request are forwarded to the server. But the server is expecting the start of the TLS handshake and these data confuse the server. This might result in hanging or aborting, depending on the content of the data and one the kind of server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM