Menu

#1960 Authenticated SOCKS5 with username/password does not work

2.30
closed
RBRi
5
2021-10-30
2018-04-03
No

I am using this to set up my proxy connection:

client.getOptions().setProxyConfig(new ProxyConfig(host, port, true));
DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) client.getCredentialsProvider();
credentialsProvider.addCredentials(username, password);

When I try to connect I get this:

[main] INFO org.apache.http.impl.execchain.RetryExec - I/O exception (java.net.SocketException) caught when processing request to {}->http://checkip.amazonaws.com:80: SOCKS : authentication failed
[main] INFO org.apache.http.impl.execchain.RetryExec - Retrying request to {}->http://checkip.amazonaws.com:80
[main] INFO org.apache.http.impl.execchain.RetryExec - I/O exception (java.net.SocketException) caught when processing request to {}->http://checkip.amazonaws.com:80: SOCKS : authentication failed
[main] INFO org.apache.http.impl.execchain.RetryExec - Retrying request to {}->http://checkip.amazonaws.com:80
[main] INFO org.apache.http.impl.execchain.RetryExec - I/O exception (java.net.SocketException) caught when processing request to {}->http://checkip.amazonaws.com:80: SOCKS : authentication failed
[main] INFO org.apache.http.impl.execchain.RetryExec - Retrying request to {}->http://checkip.amazonaws.com:80
Error getting web client IP address
java.net.SocketException: SOCKS : authentication failed
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:473)
at java.net.Socket.connect(Socket.java:589)
at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:74)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:192)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1401)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1319)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:394)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:315)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:463)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:448)

Discussion

  • Rahul Vaidya

    Rahul Vaidya - 2018-04-03

    I was able to get authenticated SOCKS5 to work by creating my own Authenticator subclass that checks the incoming SOCKS5 authentication request against a list of registered proxies.

    In the below code snippet, VProxy.getProxies() is getting a list of all proxies that I have defined - you can of course create your own way of having users specify proxies to use.

    public enum VProxyType_e {
        HTTP,
        SOCKS5
    }
    public abstract class VProxy {
        protected VProxyType_e proxyType;
        protected String host, username, password;
        Integer port;
    
        protected VProxy(VProxyType_e proxyType, String host, Integer port, String username, String password) {
            this.proxyType = proxyType;
            this.host = host;
            this.port = port;
            this.username = username;
            this.password = password;
        }
    }
    public class VProxyAuthenticator extends Authenticator {
    
        private final Map<String, Map<String, VProxy>> _proxyMap = new HashMap<>();
        private PasswordAuthentication _defaultPasswordAuthentication = null;
    
        private VProxyAuthenticator() {
            Authenticator.setDefault(this);
    
            for (VProxy proxy : VProxy.getProxies()) {
    
                String outerKey = proxy.proxyType.toString();
    
                if (!_proxyMap.containsKey(outerKey))
                    _proxyMap.put(outerKey, new HashMap<>());
    
                Map<String, VProxy> innerMap = _proxyMap.get(outerKey);
                if (!innerMap.containsKey(proxy.host))
                    innerMap.put(proxy.host, proxy);
            }
        }
    
        private static class SingletonHolder {
            public static final VProxyAuthenticator INSTANCE = new VProxyAuthenticator();
        }
    
        public static VProxyAuthenticator getInstance() {
            return SingletonHolder.INSTANCE;
        }
    
        public synchronized void setDefaultPasswordAuthentication(PasswordAuthentication credentials) {
            _defaultPasswordAuthentication = credentials;
        }
    
            private synchronized PasswordAuthentication getDefaultPasswordAuthentication() {
            return _defaultPasswordAuthentication;
        }
    
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            String protocol = getRequestingProtocol();
            if (protocol != null && _proxyMap.containsKey(protocol)) {
                Map<String, VProxy> proxies = _proxyMap.get(protocol);
                String host = getRequestingHost();
                if (host != null && proxies.containsKey(host)) {
                    VProxy proxyToUse = proxies.get(host);
                    return new PasswordAuthentication(proxyToUse.username, proxyToUse.password.toCharArray());
                }
            }
    
            PasswordAuthentication defaultPasswordAuthentication = getDefaultPasswordAuthentication();
            if (defaultPasswordAuthentication != null)
                return defaultPasswordAuthentication;
    
            return super.getPasswordAuthentication();
        }
    }
    
     
  • RBRi

    RBRi - 2021-10-30
    • status: open --> closed
    • assigned_to: RBRi
     
  • RBRi

    RBRi - 2021-10-30
     

Log in to post a comment.