title: Connect to FTPS through proxy
body: <p>The initial problem we encountered was that a regular FTPs download started failing due to an untrusted server certificate. This prompted us to wonder whether the certificate had been updated without the counterparty notifying us so we wanted to download the current certificate and compare it to the one we have in our keystore.</p>
  
  <p>This seems to be a trickier problem than we had anticipated. The usual suspects (firefox, filezilla, ...) did not seem up to the task of connecting to an FTPs server through an FTP proxy so out of curiosity I started playing around with a more low level java approach. I can not for the life of me get it to work though.</p>
  
  <p>First (overly simplistic) java attempt:</p>
  
  <pre><code>    // create proxy connection
      SocketFactory plainFactory = SocketFactory.getDefault();
      Socket proxy = plainFactory.createSocket(proxyServer, proxyPort);
  
      // create ssl connection on top of it?
      SSLSocketFactory sslFactory = getSocketFactory();
      SSLSocket socket = (SSLSocket) sslFactory.createSocket(proxy, server, port, true);
  </code></pre>
  
  <p>This approach obviously does not work.</p>
  
  <p>Next I started playing around with ftp4j (http://www.sauronsoftware.it/projects/ftp4j/) it seems to have a clean and accessible codebase:</p>
  
  <pre><code>    FTPClient client = new FTPClient();
      client.setConnector(new FTPProxyConnector(proxyHost, proxyPort));
      client.getConnector().setConnectionTimeout(0);
      client.getConnector().setReadTimeout(0);
      client.setSSLSocketFactory(getSocketFactory());
      // also tried SECURITY_FTPS
      client.setSecurity(FTPClient.SECURITY_FTPES);
      client.connect(server, port);
  </code></pre>
  
  <p>This outputs:</p>
  
  <pre><code>REPLY: 220 Blue Coat FTP Service
  SEND: USER anonymous
  REPLY: 530-User Access denied.
  REPLY: 530-
  REPLY: 530-Usage: USER username@hostname
  REPLY: 331        PASS userpassword
  Exception in thread "main" java.io.IOException: Invalid proxy response
  </code></pre>
  
  <p>The proxy server has optional authentication and on our development servers we generally use "user@host" without proxy authentication. As such I assume the username, hostname and password are those of the remote server?</p>
  
  <p>So I tried adding the remote parameters, this does not work:</p>
  
  <pre><code>REPLY: 220 Blue Coat FTP Service
  SEND: USER test@ftps.example.com
  Exception in thread "main" java.io.IOException: FTPConnection closed
  </code></pre>
  
  <p>Adding the proxy user to match the bluecoat format does not seem to work either:</p>
  
  <pre><code>USER %u@%h %s
  PASS %p
  ACCT %w
  </code></pre>
  
  <p>Any help with either of these two problems would be most welcome:</p>
  
  <ul>
  <li>how to retrieve the server certificate from an ftps server through an ftp proxy</li>
  <li>how to connect to an ftps server through an ftp proxy in java</li>
  </ul>