diff --git a/src/plugins/ftp/src/android/com/foxdebug/ftp/Ftp.java b/src/plugins/ftp/src/android/com/foxdebug/ftp/Ftp.java index b2de25446a..18cf783036 100644 --- a/src/plugins/ftp/src/android/com/foxdebug/ftp/Ftp.java +++ b/src/plugins/ftp/src/android/com/foxdebug/ftp/Ftp.java @@ -78,6 +78,11 @@ public void connect(JSONArray args, CallbackContext callback) { connect(args, callback, false); } + private FTPClient createClient(String securityType) { + // explicit FTPS: AUTH TLS on the normal port + return "ftps".equals(securityType) ? new FTPSClient(false) : new FTPClient(); + } + public void connect( JSONArray args, CallbackContext callback, @@ -93,11 +98,10 @@ public void run() { String host = args.optString(0); String username = args.optString(2); String password = args.optString(3); - String defaultPath = args.optString(4); + // order must match www/ftp.js connect() + String connectionMode = args.optString(4); String securityType = args.optString(5); - String connectionMode = args.optString(6); - String encryption = args.optString(7); - String encoding = args.optString(8); + String encoding = args.optString(6); String ftpId = getFtpId(host, port, username); FTPClient ftp = null; @@ -105,7 +109,14 @@ public void run() { if (ftpProfiles.containsKey(ftpId)) { ftp = ftpProfiles.get(ftpId); reply = ftp.getReplyCode(); - if (ftp.isConnected() && FTPReply.isPositiveCompletion(reply)) { + // ftpId ignores security, so don't reuse a client of the other type + boolean sameSecurity = + (ftp instanceof FTPSClient) == "ftps".equals(securityType); + if ( + sameSecurity && + ftp.isConnected() && + FTPReply.isPositiveCompletion(reply) + ) { ftp.setControlEncoding("UTF-8"); ftp.setAutodetectUTF8(true); System.setProperty("ftp.client.encoding", "UTF-8"); @@ -118,9 +129,11 @@ public void run() { Log.d("FTP", "FTPClient (" + ftpId + ") is not connected"); ftp.disconnect(); Log.d("FTP", "FTPClient (" + ftpId + ") disconnecting..."); + ftp = createClient(securityType); + ftpProfiles.put(ftpId, ftp); } else { Log.d("FTP", "Creating new FTPClient (" + ftpId + ")"); - ftp = new FTPClient(); + ftp = createClient(securityType); ftpProfiles.put(ftpId, ftp); } @@ -149,6 +162,12 @@ public void run() { return; } + if (ftp instanceof FTPSClient) { + // RFC 4217: protect the data channel too + ((FTPSClient) ftp).execPBSZ(0); + ((FTPSClient) ftp).execPROT("P"); + } + ftp.setListHiddenFiles(true); ftpProfiles.put(ftpId, ftp); Log.d("FTP", "FTPClient (" + ftpId + ") connected");