使用SSLSocket的SOCKS5代理 [英] SOCKS5 Proxy using SSLSocket

查看:1886
本文介绍了使用SSLSocket的SOCKS5代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端/服务器应用程序通过Java的SSLSocket远程连接到服务器。

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

我正在尝试实现一个可选模式,通过经过身份验证的连接启用SOCKS v5代理。

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

我尝试使用相关教程,但它没有具体提及有关SSL的任何内容。

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

我尝试过设置系统 - 宽属性(socksProxyHost和socksProxyPort),但它似乎没有做任何事情。

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

我的下一个方法是在SSLSocketFactory中使用工厂方法:

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);

但这种方法的问题是createSocket方法要求底层套接字已经连接,并且我的服务器不接受非SSL连接。

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.

使用SOCKS连接到我的远程服务器的最佳方法是什么?此外,我接下来不知道如何在此系统中为经过身份验证的SOCKS提供用户名/密码。

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.

谢谢!

推荐答案

        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);

这篇关于使用SSLSocket的SOCKS5代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆