Proxy settings are ignored when custom SocketFactory is provided
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:
if (!socket.isConnected()) {
socket.connect(new InetSocketAddress(unresolvedAddress.getHostName(), unresolvedAddress.getPort()), tcpConnectionConfiguration.getConnectTimeout());
}
Comments (4)
-
repo owner -
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>>
-
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>>
-
repo owner - changed status to closed
- Log in to comment
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.