You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Sachin Survase <ss...@tibco.com> on 2013/01/26 08:27:44 UTC

How to configure basic authentication for SOCKs Proxy

Hi,I am using Apache HttpClient 4.2.3 library for for accessing resources via
HTTP/HTTPS.Requests are sent through SOCKs proxy which requires 'basic
authentication'.I looked at the API doc and found that there is class
/ProxyAuthenticationStrategy/, which looks like serves the purpose.But I am
not able to figure it out how to use it. Specifically I am not able to find
how to provide proxy credentials to /ProxyAuthenticationStrategy/.I looked
at the documentation & searched over net but could not find appropriate help
over this topic.Can someone please guide me on how to configure basic
authentication for SOCKs Proxy?Thanks,Sachin



--
View this message in context: http://httpcomponents.10934.n7.nabble.com/How-to-configure-basic-authentication-for-SOCKs-Proxy-tp19030.html
Sent from the HttpClient-User mailing list archive at Nabble.com.

Re: How to configure basic authentication for SOCKs Proxy

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2013-01-30 at 22:23 -0800, Sachin Survase wrote:
> Hi Oleg,
> 
> Got it working with Authentication as well.
> I used java.net.Authenticator which provides credentials.
> I have placed this Custom Authenticator in my extended
> ClientConnectionOperator.
> 
> Please have a look at my ClientConnectionOperator code below.
> 
> Please let me know if you find any issues with the code.
> 

I see nothing wrong with your code. I would just move this logic to a
custom SchemeSocketFactory#connect implementation. Socket factories are
much easier to plug in than a client connection operator.

Oleg

> /public class SocksProxyClientConnOperator extends
> 		DefaultClientConnectionOperator {
> 	
> 	private static final Object AUTHENTICATOR_LOCK = new Object();
> 	
> 	private SocksProxyAuthenticator socksAuthenticator;
> 	
> 	public SocksProxyClientConnOperator(SchemeRegistry schemes) {
> 		super(schemes);
> 	}
> 	
> 	public void openConnection(final OperatedClientConnection conn, final
> HttpHost target,
>             final InetAddress local, final HttpContext context, final
> HttpParams params) throws IOException {
>         if (conn == null) {
>             throw new IllegalArgumentException("Connection may not be
> null");
>         }
>         if (target == null) {
>             throw new IllegalArgumentException("Target host may not be
> null");
>         }
>         if (params == null) {
>             throw new IllegalArgumentException("Parameters may not be
> null");
>         }
>         if (conn.isOpen()) {
>             throw new IllegalStateException("Connection must not be open");
>         }
> 
>         SchemeSocketFactory socksSocketFactory = new SocksSocketFactory();
> 
>         Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
>         SchemeSocketFactory sf = schm.getSchemeSocketFactory();
>         
>         InetAddress[] addresses = resolveHostname(target.getHostName());
>         int port = schm.resolvePort(target.getPort());
>         
>         Socket sock = socksSocketFactory.createSocket(params);
>         conn.opening(sock, target);
>         
>         for (int i = 0; i < addresses.length; i++) {
>             InetAddress address = addresses[i];
>             boolean last = i == addresses.length - 1;
>             InetSocketAddress remoteAddress = new InetSocketAddress(address,
> port);
>             InetSocketAddress localAddress = null;
>             if (local != null) {
>                 localAddress = new InetSocketAddress(local, 0);
>             }
>             try {
>             	Socket connsock = null;
>             	
>             	if(this.socksAuthenticator != null){
>             		synchronized (AUTHENTICATOR_LOCK) {
>                 		Authenticator.setDefault(this.socksAuthenticator);
>                 		connsock = sf.connectSocket(sock, remoteAddress,
> localAddress, params);
>     				}
>             	}else{
>             		connsock = sf.connectSocket(sock, remoteAddress, localAddress,
> params);
>             	}
>                 
>                 if (sock != connsock) {
>                     sock = connsock;
>                     conn.opening(sock, target);
>                 }
>                 prepareSocket(sock, context, params);
>                 conn.openCompleted(sf.isSecure(sock), params);
>                 break;
>             } catch (ConnectException ex) {
>                 if (last) {
>                     throw new HttpHostConnectException(target, ex);
>                 }
>             } catch (ConnectTimeoutException ex) {
>                 if (last) {
>                     throw ex;
>                 }
>             }
>         }
>     }
> 	
> 	public SocksProxyAuthenticator getSocksAuthenticator() {
> 		return this.socksAuthenticator;
> 	}
> 
> 	public void setSocksAuthenticator(SocksProxyAuthenticator
> socksAuthenticator) {
> 		this.socksAuthenticator = socksAuthenticator;
> 	}
> 
> }/
> 
> Thanks,
> Sachin Survase
> 
> 
> 
> --
> View this message in context: http://httpcomponents.10934.n7.nabble.com/How-to-configure-basic-authentication-for-SOCKs-Proxy-tp19030p19118.html
> Sent from the HttpClient-User mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 



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


Re: How to configure basic authentication for SOCKs Proxy

Posted by Sachin Survase <ss...@tibco.com>.
Hi Oleg,

Got it working with Authentication as well.
I used java.net.Authenticator which provides credentials.
I have placed this Custom Authenticator in my extended
ClientConnectionOperator.

Please have a look at my ClientConnectionOperator code below.

Please let me know if you find any issues with the code.

/public class SocksProxyClientConnOperator extends
		DefaultClientConnectionOperator {
	
	private static final Object AUTHENTICATOR_LOCK = new Object();
	
	private SocksProxyAuthenticator socksAuthenticator;
	
	public SocksProxyClientConnOperator(SchemeRegistry schemes) {
		super(schemes);
	}
	
	public void openConnection(final OperatedClientConnection conn, final
HttpHost target,
            final InetAddress local, final HttpContext context, final
HttpParams params) throws IOException {
        if (conn == null) {
            throw new IllegalArgumentException("Connection may not be
null");
        }
        if (target == null) {
            throw new IllegalArgumentException("Target host may not be
null");
        }
        if (params == null) {
            throw new IllegalArgumentException("Parameters may not be
null");
        }
        if (conn.isOpen()) {
            throw new IllegalStateException("Connection must not be open");
        }

        SchemeSocketFactory socksSocketFactory = new SocksSocketFactory();

        Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
        SchemeSocketFactory sf = schm.getSchemeSocketFactory();
        
        InetAddress[] addresses = resolveHostname(target.getHostName());
        int port = schm.resolvePort(target.getPort());
        
        Socket sock = socksSocketFactory.createSocket(params);
        conn.opening(sock, target);
        
        for (int i = 0; i < addresses.length; i++) {
            InetAddress address = addresses[i];
            boolean last = i == addresses.length - 1;
            InetSocketAddress remoteAddress = new InetSocketAddress(address,
port);
            InetSocketAddress localAddress = null;
            if (local != null) {
                localAddress = new InetSocketAddress(local, 0);
            }
            try {
            	Socket connsock = null;
            	
            	if(this.socksAuthenticator != null){
            		synchronized (AUTHENTICATOR_LOCK) {
                		Authenticator.setDefault(this.socksAuthenticator);
                		connsock = sf.connectSocket(sock, remoteAddress,
localAddress, params);
    				}
            	}else{
            		connsock = sf.connectSocket(sock, remoteAddress, localAddress,
params);
            	}
                
                if (sock != connsock) {
                    sock = connsock;
                    conn.opening(sock, target);
                }
                prepareSocket(sock, context, params);
                conn.openCompleted(sf.isSecure(sock), params);
                break;
            } catch (ConnectException ex) {
                if (last) {
                    throw new HttpHostConnectException(target, ex);
                }
            } catch (ConnectTimeoutException ex) {
                if (last) {
                    throw ex;
                }
            }
        }
    }
	
	public SocksProxyAuthenticator getSocksAuthenticator() {
		return this.socksAuthenticator;
	}

	public void setSocksAuthenticator(SocksProxyAuthenticator
socksAuthenticator) {
		this.socksAuthenticator = socksAuthenticator;
	}

}/

Thanks,
Sachin Survase



--
View this message in context: http://httpcomponents.10934.n7.nabble.com/How-to-configure-basic-authentication-for-SOCKs-Proxy-tp19030p19118.html
Sent from the HttpClient-User mailing list archive at Nabble.com.

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


Re: How to configure basic authentication for SOCKs Proxy

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, 2013-01-28 at 05:27 -0800, Sachin Survase wrote:
> Thanks Oleg.
> 
> Is there any custom way to achieve this functionality ?
> 
> Regards,
> Sachin
> 

Sachin

Honestly I have no idea. I have never worked with the SOCKs protocol.
Supposedly the damn thing should be well supported by modern JRE right
out of the box.

Oleg



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


Re: How to configure basic authentication for SOCKs Proxy

Posted by Sachin Survase <ss...@tibco.com>.
Thanks Oleg.

Is there any custom way to achieve this functionality ?

Regards,
Sachin



--
View this message in context: http://httpcomponents.10934.n7.nabble.com/How-to-configure-basic-authentication-for-SOCKs-Proxy-tp19030p19045.html
Sent from the HttpClient-User mailing list archive at Nabble.com.

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


Re: How to configure basic authentication for SOCKs Proxy

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Fri, Jan 25, 2013 at 11:27:44PM -0800, Sachin Survase wrote:
> Hi,I am using Apache HttpClient 4.2.3 library for for accessing resources via
> HTTP/HTTPS.Requests are sent through SOCKs proxy which requires 'basic
> authentication'.I looked at the API doc and found that there is class
> /ProxyAuthenticationStrategy/, which looks like serves the purpose.But I am
> not able to figure it out how to use it. Specifically I am not able to find
> how to provide proxy credentials to /ProxyAuthenticationStrategy/.I looked
> at the documentation & searched over net but could not find appropriate help
> over this topic.Can someone please guide me on how to configure basic
> authentication for SOCKs Proxy?Thanks,Sachin
> 
> 

Sachin,

SOCKS is a TCP/IP level proxy protocol. It has nothing to do with HTTP and is out of scope as far HttpClient is concerned. HttpClient can be configured to connect all network sockets it creates via a SOCKS proxy but it will make no attempt to provide any user credentials to the SOCKS proxy.

Oleg

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