【问题标题】:How do I use Proximo as a SOCKS proxy in Java?如何在 Java 中使用 Proximo 作为 SOCKS 代理?
【发布时间】:2014-03-20 03:54:39
【问题描述】:

我有一个在 Heroku 上运行的 Java 应用程序,它需要访问 Salesforce,它只允许从您列入白名单的 IP 访问 API。 Proximo 是一个 Heroku 附加组件,它允许您代理您的应用程序通过具有静态 IP 的单个服务器发出的所有请求。不幸的是,这样做的标准方法——running your app within Proximo's wrapper——搞砸了为我的应用程序的网络服务器创建一个监听套接字,as it seems to for a number of folks

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

【问题讨论】:

    标签: java heroku proxy socks


    【解决方案1】:

    我查看了他们作为上述包装器分发的stacklet,并看到它作为 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;
        }
    }
    

    【讨论】:

      【解决方案2】:

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

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

      1. 使用命令行 JVM 参数。
      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 包)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-22
        • 1970-01-01
        • 1970-01-01
        • 2011-01-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多