Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SOCKS5 Proxy using SSLSocket

I have a client/server application that remotely connects to a server via Java's SSLSocket.

I'm trying to implement an optional mode that enables connections via an authenticated SOCKS v5 proxy.

I tried using the relevant tutorial but it doesn't mention anything about SSL specifically.

I've tried setting the system-wide properties ("socksProxyHost" and "socksProxyPort") but it doesn't seem to do anything.

My next approach was to use factory method in SSLSocketFactory:

            String proxyHost = Config.prefs.get("PROXY_NAME", "localhost");
            int proxyPort = Config.prefs.getInt("PROXY_PORT", 1080);
            InetSocketAddress proxyAddr = new InetSocketAddress(proxyHost, proxyPort);
            Socket underlying = new SSLSocket(new Proxy(Proxy.Type.SOCKS, proxyAddr));
            underlying.connect(new InetSocketAddress(getHost(), getPort()));
            socket = (SSLSocket) factory.createSocket(
                    underlying,
                    getHost(),
                    getPort(),
                    true);

But the problem with this approach is that the createSocket method requires the underlying socket to be already connected, and my server won't accept non SSL connections.

What's the best way to connect to my remote server using SOCKS? Also, I have next to no idea how to supply a username/password for authenticated SOCKS in this system.

Thanks!

like image 530
Ryan N Avatar asked Feb 25 '23 10:02

Ryan N


1 Answers

        String proxyHost = Config.prefs.get("PROXY_NAME", "localhost");
        int proxyPort = Config.prefs.getInt("PROXY_PORT", 1080);
        InetSocketAddress proxyAddr = new InetSocketAddress(proxyHost, proxyPort);
        Socket underlying = new Socket(new Proxy(Proxy.Type.SOCKS, proxyAddr));
        underlying.connect(new InetSocketAddress(getHost(), getPort()));
        socket = (SSLSocket) factory.createSocket(
                underlying,
                proxyHost,
                proxyPort,
                true);
like image 118
simone Avatar answered Mar 05 '23 16:03

simone