Proxy settings are ignored when custom SocketFactory is provided

Issue #68 closed
Dmytro Diachenko created an issue

Proxy settings are ignored if SocketFactory is provided.

I'm using an SSL connection over socks proxy. It can be created like this:

http://stackoverflow.com/questions/5783832/socks5-proxy-using-sslsocket

Note that this method is defined in SSLSocketFactory (not in SocketFactory):

public abstract Socket createSocket(Socket s,
                  String host,
                  int port,
                  boolean autoClose)
                             throws IOException

As a workaround for I'm creating own instance of SocketFactory:

new SocketFactory() {

                        final SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();

                        @Override
                        public Socket createSocket() throws IOException {
                            Socket underlying = new Socket(proxy);
                            underlying.connect(new InetSocketAddress(hostname, port), connectionTimeout);
                            Socket socket = factory.createSocket(underlying, proxyHost, proxyPort, true);
                            return socket;
                        }

...
}

The underlying socket MUST be connected for this to work and the socket returned by the factory is also in connected state. I had to add a check here:

https://bitbucket.org/sco0ter/babbler/src/ca1b38c22070f8b12908e7130d0fc6c5254bb14f/xmpp-core-client/src/main/java/rocks/xmpp/core/session/TcpConnection.java?at=master&fileviewer=file-view-default#TcpConnection.java-225

        if (!socket.isConnected()) {
            socket.connect(new InetSocketAddress(unresolvedAddress.getHostName(), unresolvedAddress.getPort()), tcpConnectionConfiguration.getConnectTimeout());
        }

Comments (4)

  1. Christian Schudt repo owner

    The assumption was, if you use a custom socket factory, it's the factory's responsibility to create the Socket with the proxy. Because there's no way to pass a Proxy to SocketFactory#createSocket(...);

    You could probably also extend SSLSocketFactory instead of wrapping it, because it extends SocketFactory.

    I will apply the last check! Thanks for finding it.

  2. Christian Schudt repo owner

    Check connected state of socket before connecting.

    This prevents SocketException, if the socket is already connected, e.g. if provided by SocketFactory.

    Fixes issue #68

    → <<cset 23054bd6d843>>

  3. Christian Schudt repo owner

    Check connected state of socket before connecting.

    This prevents SocketException, if the socket is already connected, e.g. if provided by SocketFactory.

    Fixes issue #68

    → <<cset f48fab3db681>>

  4. Log in to comment