简体   繁体   中英

Create unconnected SSLSocket over an existing Socket in Java

I am implementing a custom TLS socket factory over a SOCKS proxy. Among other methods I should override is Socket createSocket() which just creates an unconnected Socket which will be connected somewhere later in the code.

So in the TLS socket factory without a SOCKS proxy, the way I implement it is the following:

@Override
public Socket createSocket() throws IOException, UnknownHostException {
  // Initialize my SSL Context
  SSLContext sslCtx = initializeSSLContext();
  SSLSocketFactory socketFactory = sslCtx.getSocketFactory();
  // Return an unconnected socket
  return socketFactory.createSocket();
}

In my TLS socket factory over SOCKS the equivalent is roughly like this:

@Override
  public Socket createSocket() throws IOException, UnknownHostException {
    // proxy is an instance of java.net.Proxy
    Socket socks = new Socket(proxy);

    // Initialize my SSL context
    SSLContext sslCtx = initializeSSLContext();
    SSLSocketFactory socketFactory = sslCtx.getSocketFactory();
    return socketFactory.createSocket(socks, ????, ????, true);
  }

As I mentioned earlier I need to be able to create a socket without connecting it. SSLSocketFactory provides two methods to create sockets over an existing one but both assume that you know the host:port you are connecting to.

Is there any way to create a TLS socket over an existing socket (proxy) but without providing the target host?

Thanks in advance

Use SSLSocketFactory.createSocket() to create an unconnected socket. Check the docs here for more details SSLSocketFactory

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