Is it possible to change plain socket to SSLSocket?

后端 未结 4 1770
一整个雨季
一整个雨季 2020-11-27 19:48

There is a plain socket server listening on port 12345;

ServerSocket s = new ServerSocket(12345);

What I want to know is that

4条回答
  •  無奈伤痛
    2020-11-27 20:20

    The Socket/SSLSocket interface does not allow convenient content-recognition here - when you start reading from a socket and see that it is only garbage (not plain HTTP), you can't supply this data to a new SSLSocket wrapped around a normal one.

    You could use a Socket (or SocketChannel) and look at the data, and then (if it is not the start of a plain HTTP request) pass the same data to an SSLEngine object for decryption/encryption. This means that you have to handle all the encryption/decryption calls yourself, which is not totally trivial (at least, it is a lot more complicated than simply using an SSLSocket with it's two streams - I did it once).

    Of course, if you do this, you would likely better implement the RFC 2817 interface, instead of trying automated content sniffing.

提交回复
热议问题