繁体   English   中英

如何在Java中将Proximo用作SOCKS代理?

[英]How do I use Proximo as a SOCKS proxy in Java?

我有一个在Heroku上运行的Java应用程序,需要访问Salesforce,它只允许从您列入白名单的IP访问API。 Proximo是一个Heroku附加组件,允许您通过具有静态IP的单个服务器代理您的应用程序所做的所有请求。 不幸的是,这样做的标准方法 - 在Proximo的包装器中运行你的应用程序 -为我的应用程序的web服务器创建一个监听套接字, 就像许多人一样

如何在我的Java应用程序中将Proximo用作SOCKS代理?

我查看了他们作为上述包装器分发的堆栈 ,并看到它作为SOCKS代理连接到Proximo服务器。 这在Java中很容易做到,所以我将其添加到我的应用程序的启动中(Groovy语法):

URL proximo = new URL(System.getenv('PROXIMO_URL')
String userInfo = proximo.getUserInfo()
String user = userInfo.substring(0, userInfo.indexOf(':'))
String password = userInfo.substring(userInfo.indexOf(':') + 1)
System.setProperty('socksProxyHost', proximo.getHost())
System.setProperty('socksProxyPort', '1080')
Authenticator.setDefault(new ProxyAuth(user, password))

使用ProxyAuth作为其他地方的内部类:

private class ProxyAuth extends Authenticator {
    private PasswordAuthentication passwordAuthentication;

    private ProxyAuth(String user, String password) {
        passwordAuthentication = new PasswordAuthentication(user, password.toCharArray())
    }

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return passwordAuthentication;
    }
}

这里描述了什么样的基于Java的Web应用程序? 我有一个基于Spring MVC的Web应用程序,遇到了与此处提到的相同的问题。

我尝试了两种方法在我的应用程序中配置SOCKS代理:

  1. 使用命令行JVM args。
  2. 使用这里提到的java代码,包装在我在根应用程序上下文中加载的Bean

我的Java类作为bean加载:

package be.example.backend.configuration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;

public class ProximoSocksConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProximoSocksConfig.class);
    private static final String PROXIMO_ENV_VAR = "PROXIMO_URL";
    private static final String PROXIMO_PORT = "1080";

    public ProximoSocksConfig() throws MalformedURLException {
        setup();
    }

    /**
     * Setup Proximo proxying
     *
     * @return True if Proximo was configured, false if the Proximo environment variable was not found
     */
    public static boolean setup() throws MalformedURLException {

        String proximoUrl = System.getenv(PROXIMO_ENV_VAR);

        if (proximoUrl != null) {
            URL proximo = new URL(proximoUrl);

            String userInfo = proximo.getUserInfo();
            String user = userInfo.substring(0, userInfo.indexOf(':'));
            String password = userInfo.substring(userInfo.indexOf(':') + 1);
            String host = proximo.getHost();

            System.setProperty("socksProxyHost", host);
            System.setProperty("socksProxyPort", PROXIMO_PORT);

            Authenticator.setDefault(new ProxyAuth(user, password));
            LOGGER.info("{} specified; using <{}:{}> {}:{} as SOCKS proxy", PROXIMO_ENV_VAR, user, password, host,
                    PROXIMO_PORT);
            return true;
        }

        LOGGER.info("{} environment variable not set", PROXIMO_ENV_VAR);
        return false;
    }

    private static class ProxyAuth extends Authenticator {

        private final PasswordAuthentication passwordAuthentication;

        private ProxyAuth(String user, String password) {
            passwordAuthentication = new PasswordAuthentication(user, password.toCharArray());
        }

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return passwordAuthentication;
        }
    }
}

我基于XML的根应用程序上下文

<!-- Proximo SOCKS proxy configuration -->
<bean id="proximoSocksConfig" class="be.example.backend.configuration.ProximoSocksConfig"/>

我没有得到绑定错误,因为不再使用proximo二进制包装器,但是我的应用程序无法在Proximo提供的公共IP地址上访问(我使用Proximo Starters包)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM