Search code examples
javaherokuproxysocks

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


I've got a Java app that runs on Heroku that needs to access Salesforce, which only allows API access from IPs that you whitelist. Proximo is a Heroku add-on that allows you to proxy all of the requests your app makes through a single server with a static IP. Unfortunately, the standard way of doing this—running your app within Proximo's wrapper—mucks up the creating of a listening socket for my app's webserver, as it seems to for a number of folks.

How can I use Proximo as a SOCKS proxy in my Java application?


Solution

  • I looked that stacklet that they distribute as the aforementioned wrapper and saw that it connects to the Proximo server as a SOCKS proxy. That's easy to do in Java, so I added this to the startup of my app (Groovy syntax):

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

    With that ProxyAuth being an inner class elsewhere:

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