Skip to content

Commit

Permalink
Fix #5087.
Browse files Browse the repository at this point in the history
Former-commit-id: 93f027afeca3930dcedd797a3f63bd7c97711fb4
  • Loading branch information
dkocher committed Mar 16, 2013
1 parent 86d55e9 commit a9a6621
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions source/ch/cyberduck/core/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ protected void setDefaults() {
2) Private (requested by 'PROT P')
*/
defaults.put("ftp.tls.datachannel", "P"); //C
defaults.put("ftp.tls.session.requirereuse", String.valueOf(true));

/**
* Try to determine the timezone automatically using timestamp comparison from MLST and LIST
Expand Down
41 changes: 37 additions & 4 deletions source/ch/cyberduck/core/ftp/FTPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,23 @@

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSessionContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -49,7 +54,7 @@
* @version $Id$
*/
public class FTPClient extends FTPSClient {
private static Logger log = Logger.getLogger(FTPSession.class);
private static final Logger log = Logger.getLogger(FTPClient.class);

private SSLSocketFactory sslSocketFactory;

Expand Down Expand Up @@ -123,22 +128,50 @@ public int sendCommand(final int command, final String args) throws IOException
}

protected String getCommand(final int command) {
String value = commands.get(command);
final String value = commands.get(command);
if(null == value) {
return FTPCommand.getCommand(command);
}
return value;
}

@Override
protected Socket _openDataConnection_(final int command, final String arg) throws IOException {
Socket socket = super._openDataConnection_(command, arg);
protected Socket _openDataConnection_(final String command, final String arg) throws IOException {
final Socket socket = super._openDataConnection_(command, arg);
if(null == socket) {
throw new FTPException(this.getReplyString());
}
return socket;
}

@Override
protected void _prepareDataSocket_(final Socket socket) throws IOException {
if(Preferences.instance().getBoolean("ftp.tls.session.requirereuse")) {
if(socket instanceof SSLSocket) {
// Control socket is SSL
final SSLSessionContext sessions = (((SSLSocket) _socket_).getSession()).getSessionContext();
try {
final Field sessionHostPortCache = sessions.getClass().getDeclaredField("sessionHostPortCache");
sessionHostPortCache.setAccessible(true);
final Object cachedSession = sessionHostPortCache.get(sessions);
final String key = String.format("%s:%s", socket.getInetAddress().getHostAddress(),
String.valueOf(socket.getPort())).toLowerCase(Locale.ROOT);
final Method method = cachedSession.getClass().getDeclaredMethod("put", Object.class, Object.class);
method.setAccessible(true);
method.invoke(cachedSession, key, (((SSLSocket) _socket_).getSession()));
}
catch(NoSuchFieldException e) {
// Not running in expected JRE
log.warn("No field sessionHostPortCache in SSLSessionContext", e);
}
catch(Exception e) {
// Not running in expected JRE
log.warn(e.getMessage());
}
}
}
}

/**
* SSL versions enabled.
*/
Expand Down

0 comments on commit a9a6621

Please sign in to comment.