hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Arni Sumarlidason <sumarlida...@gmail.com>
Subject Re: How To: SSLSocket w/ SocksProxy
Date Thu, 23 Jan 2014 21:04:26 GMT
Hi again... found the answer, thought i'd share..

Just need to override createSocket() for the extension of
SSLConnectionSocketFactory...

@Override
public Socket createSocket(final HttpContext context) throws IOException
{

if (context.getAttribute("socksPort") != null &&
context.getAttribute("socksAddress") != null)
{
InetSocketAddress socksaddr = new InetSocketAddress((String)
context.getAttribute("socksAddress"), (int)
context.getAttribute("socksPort"));
Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
return new Socket(proxy);

}
return new Socket();
}

Thanks to http://hsiaosiyuan.com/wp/%E5%9C%A8-phonegap-%E4%B8%AD%E4%BD%BF%E7%94%A8-gcm/

Arni


On Thu, Jan 23, 2014 at 3:41 PM, Arni Sumarlidason
<sumarlidason@gmail.com> wrote:
> Hello & Thank you for reading...
>
> I am attempting to enable a socks proxy via HttpContext for HTTPS (I
> want to be able to turn the proxy on and off @ the request level). The
> problem looks to be inside connectSocket().
>
> I have extended SSLConnectionSocketFactory, and create the
> SocksSSLConnectionSocketFactory:
> ---------------------------------------------------------------
> public class SocksSSLConnectionSocketFactory extends SSLConnectionSocketFactory
> {
>
>     public SocksSSLConnectionSocketFactory(SSLContext sslContext,
> X509HostnameVerifier hostnameVerifier)
>     {
>         super(sslContext, hostnameVerifier);
>     }
>
>     @Override
>     public Socket connectSocket(int connectTimeout, Socket socket,
> HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress
> localAddress, HttpContext context) throws IOException
>     {
>         if (host == null)
>         {
>             throw new IllegalArgumentException("Host may not be null");
>         }
>
>         if (context.getAttribute("socksPort") != null &&
> context.getAttribute("socksAddress") != null)
>         {
>             InetSocketAddress socksaddr = new
> InetSocketAddress((String) context.getAttribute("socksAddress"), (int)
> context.getAttribute("socksPort"));
>             Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
>
>             Socket proxySocket = new Socket(proxy);
>             if (localAddress != null)
>             {
>                 proxySocket.bind(localAddress);
>             }
>             proxySocket.connect(remoteAddress, connectTimeout);
>
>             return proxySocket;
>         }
>
>         if (localAddress != null)
>         {
>             socket.bind(localAddress);
>         }
>         socket.connect(remoteAddress, connectTimeout);
>
>         return socket;
>     }
> }
> ---------------------------------------------------------------
> Then to create my client:
> ---------------------------------------------------------------
> SSLContextBuilder builder = SSLContexts.custom();
> builder.loadTrustMaterial(null, new TrustStrategy()
> {
> @Override public boolean isTrusted(X509Certificate[] chain, String
> authType) throws CertificateException { return true; }
> });
>
> SSLContext sslContext = builder.build();
> SSLConnectionSocketFactory sslsf = new
> SocksSSLConnectionSocketFactory(sslContext, new X509HostnameVerifier()
> {
> @Override public boolean verify(String s, SSLSession sslSession) {
> return true; }
> @Override public void verify(String host, SSLSocket ssl) throws IOException { }
> @Override public void verify(String host, String[] cns, String[]
> subjectAlts) throws SSLException { }
> @Override public void verify(String host, X509Certificate cert) throws
> SSLException { }
> });
>
> SocksConnectionSocketFactory socksFactory = new SocksConnectionSocketFactory();
> Registry<ConnectionSocketFactory> socketFactoryRegistry =
> RegistryBuilder.<ConnectionSocketFactory> create().register("https",
> sslsf).register("http", socksFactory).build();
> PoolingHttpClientConnectionManager cm = new
> PoolingHttpClientConnectionManager(socketFactoryRegistry);
> ---------------------------------------------------------------
> I have been in the debugger for a while, however nothing is sticking
> out at me, this is what TRACE looks like:
> ---------------------------------------------------------------
> [DEBUG] [2014-01-23 15:23:30,762] [main]
> [org.apache.http.client.protocol.RequestAuthCache]: Auth cache not set
> in the context
> [DEBUG] [2014-01-23 15:23:30,764] [main]
> [org.apache.http.impl.conn.PoolingHttpClientConnectionManager]:
> Connection request: [route: {s}->https://google.com:443][total kept
> alive: 0; route allocated: 0 of 20; total allocated: 0 of 300]
> [DEBUG] [2014-01-23 15:23:30,784] [main]
> [org.apache.http.impl.conn.PoolingHttpClientConnectionManager]:
> Connection leased: [id: 0][route: {s}->https://google.com:443][total
> kept alive: 0; route allocated: 1 of 20; total allocated: 1 of 300]
> [DEBUG] [2014-01-23 15:23:30,804] [main]
> [org.apache.http.impl.execchain.MainClientExec]: Opening connection
> {s}->https://google.com:443
> [DEBUG] [2014-01-23 15:23:31,057] [main]
> [org.apache.http.conn.HttpClientConnectionManager]: Connecting to
> google.com/74.125.228.103:443
> [DEBUG] [2014-01-23 15:23:31,567] [main]
> [org.apache.http.impl.execchain.MainClientExec]: Executing request GET
> / HTTP/1.1
> [DEBUG] [2014-01-23 15:23:31,567] [main]
> [org.apache.http.impl.execchain.MainClientExec]: Target auth state:
> UNCHALLENGED
> [DEBUG] [2014-01-23 15:23:31,568] [main]
> [org.apache.http.impl.execchain.MainClientExec]: Proxy auth state:
> UNCHALLENGED
> [DEBUG] [2014-01-23 15:23:31,570] [main] [org.apache.http.headers]:
> http-outgoing-0 >> GET / HTTP/1.1
> [DEBUG] [2014-01-23 15:23:31,570] [main] [org.apache.http.headers]:
> http-outgoing-0 >> Host: google.com
> [DEBUG] [2014-01-23 15:23:31,570] [main] [org.apache.http.headers]:
> http-outgoing-0 >> Connection: Keep-Alive
> [DEBUG] [2014-01-23 15:23:31,571] [main] [org.apache.http.headers]:
> http-outgoing-0 >> User-Agent: Apache-HttpClient/4.3.1 (java 1.5)
> [DEBUG] [2014-01-23 15:23:31,571] [main] [org.apache.http.headers]:
> http-outgoing-0 >> Accept-Encoding: gzip,deflate
> [DEBUG] [2014-01-23 15:23:31,571] [main] [org.apache.http.wire]:
> http-outgoing-0 >> "GET / HTTP/1.1[\r][\n]"
> [DEBUG] [2014-01-23 15:23:31,571] [main] [org.apache.http.wire]:
> http-outgoing-0 >> "Host: google.com[\r][\n]"
> [DEBUG] [2014-01-23 15:23:31,571] [main] [org.apache.http.wire]:
> http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
> [DEBUG] [2014-01-23 15:23:31,571] [main] [org.apache.http.wire]:
> http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.3.1 (java
> 1.5)[\r][\n]"
> [DEBUG] [2014-01-23 15:23:31,571] [main] [org.apache.http.wire]:
> http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
> [DEBUG] [2014-01-23 15:23:31,571] [main] [org.apache.http.wire]:
> http-outgoing-0 >> "[\r][\n]"
> [DEBUG] [2014-01-23 15:23:31,951] [main]
> [org.apache.http.impl.conn.DefaultManagedHttpClientConnection]:
> http-outgoing-0: Close connection
> [DEBUG] [2014-01-23 15:23:31,952] [main]
> [org.apache.http.impl.conn.DefaultManagedHttpClientConnection]:
> http-outgoing-0: Shutdown connection
> [DEBUG] [2014-01-23 15:23:31,952] [main]
> [org.apache.http.impl.execchain.MainClientExec]: Connection discarded
> [DEBUG] [2014-01-23 15:23:31,952] [main]
> [org.apache.http.impl.conn.DefaultManagedHttpClientConnection]:
> http-outgoing-0: Close connection
> [DEBUG] [2014-01-23 15:23:31,952] [main]
> [org.apache.http.impl.conn.PoolingHttpClientConnectionManager]:
> Connection released: [id: 0][route: {s}->https://google.com:443][total
> kept alive: 0; route allocated: 0 of 20; total allocated: 0 of 300]
> [INFO ] [2014-01-23 15:23:31,953] [main]
> [org.apache.http.impl.execchain.RetryExec]: I/O exception
> (org.apache.http.NoHttpResponseException) caught when processing
> request: The target server failed to respond
> [DEBUG] [2014-01-23 15:23:31,954] [main]
> [org.apache.http.impl.execchain.RetryExec]: The target server failed
> to respond
> org.apache.http.NoHttpResponseException: The target server failed to respond
> at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:143)
> at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
> at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260)
> at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:161)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at java.lang.reflect.Method.invoke(Method.java:606)
> at org.apache.http.impl.conn.CPoolProxy.invoke(CPoolProxy.java:138)
> at com.sun.proxy.$Proxy0.receiveResponseHeader(Unknown Source)
> at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)
> at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
> at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:253)
> at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:194)
> at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:85)
> at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
> at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
> at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message