SOCKS5 Proxy using SSLSocket

2019-04-10 15:36发布

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!

1条回答
The star\"
2楼-- · 2019-04-10 15:58
        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);
查看更多
登录 后发表回答